summaryrefslogtreecommitdiff
path: root/hostsidetests/securitybulletin/test-apps/BUG-183963253/src/android/security/cts/BUG_183963253/DeviceTest.java
blob: b2dc9b816acb4aa4d8b772cfcfec112703a93e60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.security.cts.BUG_183963253;

import static android.security.cts.BUG_183963253.Constants.LOG_TAG;

import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;

import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;

import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.BySelector;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.Until;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;

/** Basic sample for unbundled UiAutomator. */
@RunWith(AndroidJUnit4.class)
public class DeviceTest {

    private static final long WAIT_FOR_UI_TIMEOUT = 10_000;

    private Context mContext;
    private UiDevice mDevice;

    @Before
    public void setUp() throws Exception {
        Log.d(LOG_TAG, "startMainActivityFromHomeScreen()");

        mContext = getApplicationContext();

        // If the permission is not granted, the app will not be able to show an overlay dialog.
        // This is required for the test below.
        // NOTE: The permission is granted by the HostJUnit4Test implementation and should not fail.
        assertEquals("Permission SYSTEM_ALERT_WINDOW not granted!",
                mContext.checkSelfPermission("android.permission.SYSTEM_ALERT_WINDOW"),
                PackageManager.PERMISSION_GRANTED);

        // Initialize UiDevice instance
        mDevice = UiDevice.getInstance(getInstrumentation());
        if (!mDevice.isScreenOn()) {
            mDevice.wakeUp();
        }
        mDevice.pressHome();
    }

    @Test
    public void testTapjacking() throws InterruptedException {
        Log.d(LOG_TAG, "Starting tap-jacking test");

        launchTestApp();

        launchTapjackedActivity();

        mContext.sendBroadcast(new Intent(Constants.ACTION_START_TAPJACKING));
        Log.d(LOG_TAG, "Sent intent to start tap-jacking!");

        UiObject2 overlay = waitForView(By.text("BUG_183963253 overlay text"));
        assertNull("Tap-jacking successful. Overlay was displayed.!", overlay);
    }

    @After
    public void tearDown() {
        mDevice.pressHome();
    }

    private void launchTestApp() {
        Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(
                Constants.TEST_APP_PACKAGE);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        mContext.startActivity(intent);

        // Wait for the app to appear
        UiObject2 view = waitForView(By.pkg(Constants.TEST_APP_PACKAGE).depth(0));
        assertNotNull("test-app did not appear!", view);
        Log.d(LOG_TAG, "test-app appeared");
    }

    private void launchTapjackedActivity() {
        Intent intent = new Intent();
        intent.setAction("android.settings.USAGE_ACCESS_SETTINGS");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);

        UiObject2 view = waitForView(By.text(Constants.TEST_APP_PACKAGE));
        assertNotNull("Activity under-test was not launched or found!", view);
        Log.d(LOG_TAG, "Started Activity under-test.");
    }

    private UiObject2 waitForView(BySelector selector) {
        return mDevice.wait(Until.findObject(selector), WAIT_FOR_UI_TIMEOUT);
    }
}