Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a35c6bc1 authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Fix more SettingsUITests

Also add SettingsUITests to postsubmit.

Bug: 290381395
Test: ui test
Change-Id: Ib1dd35bf7823290bf77deb31366e9cb6ae9f65f7
parent f1a1439d
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -27,6 +27,14 @@
        }
      ]
    },
    {
      "name": "SettingsUITests",
      "options": [
        {
          "exclude-annotation": "org.junit.Ignore"
        }
      ]
    },
    {
      "name": "SettingsPerfTests"
    }
+1 −1
Original line number Diff line number Diff line
@@ -22,10 +22,10 @@ import static java.lang.String.format;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.SystemClock;
import android.support.test.uiautomator.UiDevice;
import android.util.Log;

import androidx.test.InstrumentationRegistry;
import androidx.test.uiautomator.UiDevice;

import org.junit.Assert;

+4 −53
Original line number Diff line number Diff line
@@ -19,13 +19,9 @@ package com.android.settings.ui
import android.provider.Settings
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import androidx.test.uiautomator.By
import androidx.test.uiautomator.Direction
import androidx.test.uiautomator.UiDevice
import com.android.settings.ui.testutils.SettingsTestUtils.SETTINGS_PACKAGE
import com.android.settings.ui.testutils.SettingsTestUtils.assertHasTexts
import com.android.settings.ui.testutils.SettingsTestUtils.startMainActivityFromHomeScreen
import com.android.settings.ui.testutils.SettingsTestUtils.waitObject
import com.google.common.truth.Truth.assertWithMessage
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@@ -43,56 +39,11 @@ class AboutPhoneSettingsTests {

    @Test
    fun testAllMenuEntriesExist() {
        searchForItemsAndTakeAction(device)
        device.assertHasTexts(ON_SCREEN_TEXTS)
    }

    /**
     * Removes items found in the view and optionally takes some action.
     */
    private fun removeItemsAndTakeAction(device: UiDevice, itemsLeftToFind: MutableList<String>) {
        val iterator = itemsLeftToFind.iterator()
        while (iterator.hasNext()) {
            val itemText = iterator.next()
            val item = device.waitObject(By.text(itemText))
            if (item != null) {
                iterator.remove()
            }
        }
    }

    /**
     * Searches for UI elements in the current view and optionally takes some action.
     *
     *
     * Will scroll down the screen until it has found all elements or reached the bottom.
     * This allows elements to be found and acted on even if they change order.
     */
    private fun searchForItemsAndTakeAction(device: UiDevice) {
        val itemsLeftToFind = resourceTexts.toMutableList()
        assertWithMessage("There must be at least one item to search for on the screen!")
            .that(itemsLeftToFind)
            .isNotEmpty()
        var canScrollDown = true
        while (canScrollDown && itemsLeftToFind.isNotEmpty()) {
            removeItemsAndTakeAction(device, itemsLeftToFind)

            // when we've finished searching the current view, scroll down
            val view = device.waitObject(By.res("$SETTINGS_PACKAGE:id/main_content"))
            canScrollDown = view?.scroll(Direction.DOWN, 1.0f) ?: false
        }
        // check the last items once we have reached the bottom of the view
        removeItemsAndTakeAction(device, itemsLeftToFind)
        assertWithMessage(
            "The following items were not found on the screen: "
                + itemsLeftToFind.joinToString(", ")
        )
            .that(itemsLeftToFind)
            .isEmpty()
    }

    companion object {
        // TODO: retrieve using name/ids from com.android.settings package
        private val resourceTexts = listOf(
    private companion object {
        val ON_SCREEN_TEXTS = listOf(
            "Device name",
            "Legal information",
            "Regulatory labels"
+4 −11
Original line number Diff line number Diff line
@@ -18,9 +18,8 @@ package com.android.settings.ui

import android.provider.Settings
import androidx.test.uiautomator.By
import androidx.test.uiautomator.Direction
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.Until
import com.android.settings.ui.testutils.SettingsTestUtils.assertHasTexts
import com.android.settings.ui.testutils.SettingsTestUtils.clickObject
import com.android.settings.ui.testutils.SettingsTestUtils.startMainActivityFromHomeScreen
import com.android.settings.ui.testutils.SettingsTestUtils.waitObject
@@ -42,13 +41,7 @@ class AppsSettingsTests {
    @Test
    fun testAppSettingsListForCalculator() {
        device.clickObject(By.text("Calculator"))
        val scrollableObj = device.findObject(By.scrollable(true))
        for (setting in resourceTexts) {
            scrollableObj.scrollUntil(Direction.DOWN, Until.findObject(By.text(setting)))
            val appSetting = device.waitObject(By.text(setting))
            assertWithMessage("Missing setting for Calculator: $setting")
                .that(appSetting).isNotNull()
        }
        device.assertHasTexts(ON_SCREEN_TEXTS)
    }

    @Test
@@ -61,8 +54,8 @@ class AppsSettingsTests {
        assertWithMessage("App not enabled successfully").that(disableButton).isNotNull()
    }

    companion object {
        private val resourceTexts = arrayOf(
    private companion object {
        val ON_SCREEN_TEXTS = listOf(
            "Notifications",
            "Permissions",
            "Storage & cache",
+51 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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 com.android.settings.ui

import android.content.Intent
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.uiautomator.UiDevice
import com.android.settings.ui.testutils.SettingsTestUtils.assertHasTexts
import com.android.settings.ui.testutils.SettingsTestUtils.startMainActivityFromHomeScreen
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class BatterySettingsTest {
    private lateinit var device: UiDevice

    @Before
    fun setUp() {
        device = startMainActivityFromHomeScreen(Intent.ACTION_POWER_USAGE_SUMMARY)
    }

    @Test
    fun hasTexts() {
        device.assertHasTexts(ON_SCREEN_TEXTS)
    }

    private companion object {
        // Items we really want to always show
        val ON_SCREEN_TEXTS = listOf(
            "Battery usage",
            "Battery Saver",
            "Battery percentage",
            "Remaining battery life is approximate and can change based on usage"
        )
    }
}
Loading