summaryrefslogtreecommitdiff
path: root/hostsidetests/securitybulletin/test-apps/CVE-2021-0305/src/android/security/cts/CVE_2021_0305/DeviceTest.java
blob: 5634f4f55bb1674d312fc2520fd2fb88c903fafd (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * 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.CVE_2021_0305;

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.content.pm.ResolveInfo;
import android.os.SystemClock;
import android.util.Log;

import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import androidx.test.filters.SdkSuppress;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.Until;
import androidx.test.uiautomator.BySelector;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertFalse;

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

  private static final String BASIC_SAMPLE_PACKAGE
          = "android.security.cts.CVE_2021_0305";
  private static final int LAUNCH_TIMEOUT_MS = 20000;
  private static final String STRING_TO_BE_TYPED = "UiAutomator";

  private UiDevice mDevice;

  @Before
  public void startMainActivityFromHomeScreen() {

    Log.d("CVE", "startMainActivityFromHomeScreen()");

    // Initialize UiDevice instance
    mDevice = UiDevice.getInstance(getInstrumentation());

    // Start from the home screen
    mDevice.pressHome();

    // Launch the blueprint app
    Context context = getApplicationContext();
    assertThat(context, notNullValue());
    PackageManager packageManager = context.getPackageManager();
    assertThat(packageManager, notNullValue());
    final Intent intent = packageManager.getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
    assertThat(intent, notNullValue());

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); // Clear out any previous instances
    context.startActivity(intent);

    // Wait for the app to appear
    Log.d("CVE", "wait for the app to appear");
    mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT_MS);
    Log.d("CVE", "app appeared");
  }

  @After
  public void lastOperation(){
    Log.d("CVE", "lastOperation() wait 20s");
    SystemClock.sleep(20000);
    Log.d("CVE", "lastOperation() completed");
  }

  @Test
  public void testClick() {
    Log.d("CVE", "testClick()");
    boolean clicked = false;
    java.util.List<UiObject2> objects;
    BySelector selector = By.clickable(true);
    String button;

    //Detect "Test Button".
    //"Test Button" appears after onResume().
    //mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT_MS);
    //waits for onEnterAnimationComplete() to finish.
    //So we have LAUNCH_TIMEOUT_MS for the button to appear.
    //If the button it still not available then
    //we assume the button is obscured and the test passes.
    Log.d("CVE", "looking for clickable");
    objects = mDevice.findObjects(selector);
    for (UiObject2 o : objects) {
      button = o.getText();
      Log.d("CVE", "button:" + button);

      if(button==null){
        //check the next button
        continue;
      }

      switch(button){
        case "Test Button" :
          o.click();
          clicked=true;
          Log.i("CVE", "clicked: Test Button");
          break;
        default :
          //check the next button
          continue;
      }

      //A button this test is looking for just got pressed.
      //Ignore remaining buttons
      break;
    }

    Log.d("CVE", "testClick() end");
    assertFalse(clicked);
    Log.d("CVE", "testClick() passed");
  }
}