[go: nahoru, domu]

Add tests for `pressRecentApps()`, `openNotification()`,
`openQuickSettings()`, `getDisplayWidth()` and `getDisplayHeight()`.

Bug: 235841020
Test: ./gradlew :test:uiautomator:integration-tests:testapp:cAT
Change-Id: I460dcfdeb00a9545021101d750283609bf056a48
diff --git a/test/uiautomator/integration-tests/testapp/build.gradle b/test/uiautomator/integration-tests/testapp/build.gradle
index 27d8d22..c4c98ec 100644
--- a/test/uiautomator/integration-tests/testapp/build.gradle
+++ b/test/uiautomator/integration-tests/testapp/build.gradle
@@ -21,6 +21,7 @@
 
 dependencies {
     implementation("androidx.annotation:annotation:1.4.0")
+    implementation("androidx.core:core:1.6.0")
 
     androidTestImplementation(project(":test:uiautomator:uiautomator"))
     androidTestImplementation(libs.testCore)
diff --git a/test/uiautomator/integration-tests/testapp/src/androidTest/java/androidx/test/uiautomator/testapp/BaseTest.java b/test/uiautomator/integration-tests/testapp/src/androidTest/java/androidx/test/uiautomator/testapp/BaseTest.java
index 07d6094..0f21e88 100644
--- a/test/uiautomator/integration-tests/testapp/src/androidTest/java/androidx/test/uiautomator/testapp/BaseTest.java
+++ b/test/uiautomator/integration-tests/testapp/src/androidTest/java/androidx/test/uiautomator/testapp/BaseTest.java
@@ -47,6 +47,7 @@
     public void setUp() throws Exception {
         mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
         mDevice.wakeUp();
+        mDevice.pressHome();
     }
 
     protected void launchTestActivity(@NonNull Class<? extends Activity> activity) {
diff --git a/test/uiautomator/integration-tests/testapp/src/androidTest/java/androidx/test/uiautomator/testapp/UiDeviceTest.java b/test/uiautomator/integration-tests/testapp/src/androidTest/java/androidx/test/uiautomator/testapp/UiDeviceTest.java
index 033b43d..32b4037 100644
--- a/test/uiautomator/integration-tests/testapp/src/androidTest/java/androidx/test/uiautomator/testapp/UiDeviceTest.java
+++ b/test/uiautomator/integration-tests/testapp/src/androidTest/java/androidx/test/uiautomator/testapp/UiDeviceTest.java
@@ -43,6 +43,7 @@
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.regex.Pattern;
 
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.xpath.XPathConstants;
@@ -52,6 +53,11 @@
 @LargeTest
 public class UiDeviceTest extends BaseTest {
 
+    private static final long TIMEOUT_MS = 5_000;
+
+    // Defined in 'AndroidManifest.xml'.
+    private static final String APP_NAME = "UiAutomator Test App";
+
     @Rule
     public TemporaryFolder mTmpDir = new TemporaryFolder();
 
@@ -99,7 +105,7 @@
         Point buttonCenter = button.getVisibleCenter();
 
         assertTrue(mDevice.performActionAndWait(() -> mDevice.click(buttonCenter.x, buttonCenter.y),
-                Until.newWindow(), 10_000));
+                Until.newWindow(), TIMEOUT_MS));
     }
 
     @Test
@@ -232,18 +238,45 @@
         assertEquals("keycode Z pressed with meta shift left on", textView.getText());
     }
 
-    /* TODO(b/235841020): Implement these tests, and the tests for exceptions of each tested method.
+    @Test
+    public void testPressRecentApps() throws Exception {
+        launchTestActivity(KeycodeTestActivity.class);
 
-    public void testPressRecentApps() {}
+        // No app name when the app is running.
+        assertFalse(mDevice.wait(Until.hasObject(By.text(APP_NAME)), TIMEOUT_MS));
 
-    public void testOpenNotification() {}
+        mDevice.pressRecentApps();
 
-    public void testOpenQuickSettings() {}
+        Pattern iconResIdPattern = Pattern.compile(".*launcher.*icon");
+        // For API 28 and above, click on the app icon to make the name visible.
+        if (mDevice.wait(Until.hasObject(By.res(iconResIdPattern)), TIMEOUT_MS)) {
+            UiObject2 icon = mDevice.findObject(By.res(iconResIdPattern));
+            icon.click();
+        }
 
-    public void testGetDisplayWidth() {}
+        // App name appears when on Recent screen.
+        assertTrue(mDevice.wait(Until.hasObject(By.text(APP_NAME)), TIMEOUT_MS));
+    }
 
-    public void testGetDisplayHeight() {}
-     */
+    @Test
+    public void testOpenNotification() {
+        launchTestActivity(NotificationTestActivity.class);
+
+        UiObject2 button = mDevice.findObject(By.res(TEST_APP, "notification_button"));
+        button.click();
+
+        mDevice.openNotification();
+
+        assertTrue(mDevice.wait(Until.hasObject(By.text("Test Notification")), TIMEOUT_MS));
+    }
+
+    @Test
+    public void testOpenQuickSettings() {
+        mDevice.openQuickSettings();
+
+        assertTrue(mDevice.wait(Until.hasObject(By.res(Pattern.compile(".*quick_settings_panel"))),
+                TIMEOUT_MS));
+    }
 
     @Test
     public void testClick() {
diff --git a/test/uiautomator/integration-tests/testapp/src/main/AndroidManifest.xml b/test/uiautomator/integration-tests/testapp/src/main/AndroidManifest.xml
index 35efc01..b5eca98 100644
--- a/test/uiautomator/integration-tests/testapp/src/main/AndroidManifest.xml
+++ b/test/uiautomator/integration-tests/testapp/src/main/AndroidManifest.xml
@@ -18,7 +18,9 @@
 
     <uses-sdk android:minSdkVersion="18" />
 
-    <application android:label="UiAutomator UnitTest App">
+    <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
+
+    <application android:label="UiAutomator Test App">
         <activity android:name=".MainActivity"
             android:exported="true"
             android:theme="@android:style/Theme.Holo.NoActionBar">
@@ -125,6 +127,13 @@
                 <action android:name="android.intent.action.MAIN" />
             </intent-filter>
         </activity>
+        <activity android:name=".NotificationTestActivity"
+            android:exported="true"
+            android:theme="@android:style/Theme.Holo.NoActionBar">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+            </intent-filter>
+        </activity>
         <activity android:name=".ParentChildTestActivity"
             android:exported="true"
             android:theme="@android:style/Theme.Holo.NoActionBar">
diff --git a/test/uiautomator/integration-tests/testapp/src/main/java/androidx/test/uiautomator/testapp/NotificationTestActivity.java b/test/uiautomator/integration-tests/testapp/src/main/java/androidx/test/uiautomator/testapp/NotificationTestActivity.java
new file mode 100644
index 0000000..45b179c
--- /dev/null
+++ b/test/uiautomator/integration-tests/testapp/src/main/java/androidx/test/uiautomator/testapp/NotificationTestActivity.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2022 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 androidx.test.uiautomator.testapp;
+
+import android.app.Activity;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.View;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.core.app.NotificationChannelCompat;
+import androidx.core.app.NotificationCompat;
+import androidx.core.app.NotificationManagerCompat;
+
+public class NotificationTestActivity extends Activity {
+
+    @Override
+    public void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        createNotificationChannel();
+
+        setContentView(R.layout.notification_test_activity);
+    }
+
+    public void pushNotification(@NonNull View v) {
+        NotificationManagerCompat notificationManager =
+                NotificationManagerCompat.from(this.getApplicationContext());
+
+        PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(), 0,
+                new Intent(),
+                PendingIntent.FLAG_IMMUTABLE);
+
+        NotificationCompat.Builder builder = new NotificationCompat.Builder(
+                this.getApplicationContext(), "CHANNEL_ID")
+                .setSmallIcon(android.R.drawable.sym_def_app_icon)
+                .setContentTitle("Test App")
+                .setContentText("Test Notification")
+                .setContentIntent(pendingIntent)
+                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
+                .setAutoCancel(true);
+
+        int uniqueNotificationId = 0;
+        notificationManager.notify(uniqueNotificationId, builder.build());
+    }
+
+    private void createNotificationChannel() {
+        NotificationChannelCompat channel = new NotificationChannelCompat.Builder("CHANNEL_ID",
+                NotificationManager.IMPORTANCE_DEFAULT).setName("CHANNEL_NAME").build();
+        NotificationManagerCompat notificationManager =
+                NotificationManagerCompat.from(this.getApplicationContext());
+        notificationManager.createNotificationChannel(channel);
+    }
+}
diff --git a/test/uiautomator/integration-tests/testapp/src/main/res/layout/notification_test_activity.xml b/test/uiautomator/integration-tests/testapp/src/main/res/layout/notification_test_activity.xml
new file mode 100644
index 0000000..38fa483
--- /dev/null
+++ b/test/uiautomator/integration-tests/testapp/src/main/res/layout/notification_test_activity.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+  Copyright 2022 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.
+  -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical"
+    tools:context=".NotificationTestActivity">
+
+    <Button
+        android:id="@+id/notification_button"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:>
+        android:text="notification button" />
+
+</LinearLayout>
\ No newline at end of file
diff --git a/test/uiautomator/uiautomator/src/androidTest/java/androidx/test/uiautomator/UiDeviceTest.java b/test/uiautomator/uiautomator/src/androidTest/java/androidx/test/uiautomator/UiDeviceTest.java
index 5d46ca9..03248eb 100644
--- a/test/uiautomator/uiautomator/src/androidTest/java/androidx/test/uiautomator/UiDeviceTest.java
+++ b/test/uiautomator/uiautomator/src/androidTest/java/androidx/test/uiautomator/UiDeviceTest.java
@@ -89,6 +89,24 @@
     }
 
     @Test
+    public void testGetDisplayWidth() {
+        DisplayMetrics dm = new DisplayMetrics();
+        WindowManager wm = (WindowManager) mDevice.getUiContext().getSystemService(
+                Context.WINDOW_SERVICE);
+        wm.getDefaultDisplay().getRealMetrics(dm);
+        assertEquals(dm.widthPixels, mDevice.getDisplayWidth());
+    }
+
+    @Test
+    public void testGetDisplayHeight() {
+        DisplayMetrics dm = new DisplayMetrics();
+        WindowManager wm = (WindowManager) mDevice.getUiContext().getSystemService(
+                Context.WINDOW_SERVICE);
+        wm.getDefaultDisplay().getRealMetrics(dm);
+        assertEquals(dm.heightPixels, mDevice.getDisplayHeight());
+    }
+
+    @Test
     @SdkSuppress(maxSdkVersion = Build.VERSION_CODES.M)
     public void testGetUiAutomation_withoutFlags() {
         mDevice.getUiAutomation();