diff --git a/Android.bp b/Android.bp index 250744ae8a..f2ba4e8d43 100644 --- a/Android.bp +++ b/Android.bp @@ -61,6 +61,27 @@ filegroup { ], } +filegroup { + name: "launcher-compose-test-helpers-src", + srcs: [ + "compose/tests/com/android/launcher3/helper/TestHelper.kt", + ], +} + +filegroup { + name: "launcher-compose-ui-tests-src", + srcs: [], // Placeholder for compose/tests/com/android/launcher3/ui/.. tests. +} + +// Tests for features that are enabled only when compose dependency is enabled & run as part of +// Launcher3Tests +filegroup { + name: "launcher-compose-unit-tests-src", + srcs: [ + "compose/tests/com/android/launcher3/widget/AddWidgetConfigTest.kt", + ], +} + filegroup { name: "launcher-compose-tests-src", srcs: [ @@ -260,7 +281,8 @@ launcher_compose_tests_java_defaults { soong_config_variables: { release_enable_compose_in_launcher: { srcs: [ - ":launcher-compose-tests-src", + ":launcher-compose-test-helpers-src", + ":launcher-compose-unit-tests-src", ], // Compose dependencies static_libs: [ diff --git a/compose/tests/com/android/launcher3/widget/AddWidgetConfigTest.kt b/compose/tests/com/android/launcher3/widget/AddWidgetConfigTest.kt new file mode 100644 index 0000000000..73daacd1af --- /dev/null +++ b/compose/tests/com/android/launcher3/widget/AddWidgetConfigTest.kt @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2017 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.launcher3.widget + +import android.appwidget.AppWidgetManager +import android.view.View +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.LargeTest +import androidx.test.platform.app.InstrumentationRegistry +import com.android.launcher3.AbstractFloatingView +import com.android.launcher3.Launcher +import com.android.launcher3.model.data.ItemInfo +import com.android.launcher3.model.data.LauncherAppWidgetInfo +import com.android.launcher3.testcomponent.WidgetConfigActivity +import com.android.launcher3.util.BaseLauncherActivityTest +import com.android.launcher3.util.BlockingBroadcastReceiver +import com.android.launcher3.util.LauncherBindableItemsContainer.ItemOperator +import com.android.launcher3.util.Wait +import com.android.launcher3.util.rule.ShellCommandRule +import com.android.launcher3.util.ui.PortraitLandscapeRunner.PortraitLandscape +import com.android.launcher3.util.ui.TestViewHelpers +import com.android.launcher3.util.workspace.FavoriteItemsTransaction +import com.android.launcher3.widgetpicker.listeners.WidgetPickerAddItemListener +import com.google.common.truth.Truth.assertThat +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +/** + * Test verifies that when adding a widget to homescreen using [WidgetPickerAddItemListener], if the + * widget has a configuration activity, it is shown correctly. + */ +@LargeTest +@RunWith(AndroidJUnit4::class) +class AddWidgetConfigTest : BaseLauncherActivityTest() { + @get:Rule val grantWidgetRule: ShellCommandRule = ShellCommandRule.grantWidgetBind() + + private lateinit var widgetInfo: LauncherAppWidgetProviderInfo + private lateinit var appWidgetManager: AppWidgetManager + + private var widgetId = 0 + + @Before + @Throws(Exception::class) + fun setUp() { + widgetInfo = TestViewHelpers.findWidgetProvider(/* hasConfigureScreen= */ true) + appWidgetManager = AppWidgetManager.getInstance(targetContext()) + } + + @Test + @PortraitLandscape + @Throws(Throwable::class) + fun testWidgetConfig() { + runTest(acceptConfig = true) + } + + @Test + @PortraitLandscape + @Throws(Throwable::class) + fun testConfigCancelled() { + runTest(acceptConfig = false) + } + + /** @param acceptConfig accept the config activity */ + @Throws(Throwable::class) + private fun runTest(acceptConfig: Boolean) { + FavoriteItemsTransaction(targetContext()).commit() + loadLauncherSync() + + // Add widget to home screen + val monitor = WidgetConfigStartupMonitor() + launcherActivity.executeOnLauncher({ l: Launcher -> + val addItemListener = WidgetPickerAddItemListener(widgetInfo) + addItemListener.init(l, /* isHomeStarted= */ true) + }) + + uiDevice.waitForIdle() + + // Widget id for which the config activity was opened + widgetId = monitor.widgetId + + // Verify that the widget id is valid and bound + assertThat(appWidgetManager.getAppWidgetInfo(widgetId)).isNotNull() + setResult(acceptConfig) + + if (acceptConfig) { + launcherActivity.getOnceNotNull("Widget was not added") { l: Launcher -> + // Close the resize frame before searching for widget + AbstractFloatingView.closeAllOpenViews(l) + l.workspace.mapOverItems(WidgetSearchCondition()) + } + assertThat(appWidgetManager.getAppWidgetInfo(widgetId)).isNotNull() + } else { + // Verify that the widget id is deleted. + Wait.atMost( + "no widget with id", + { appWidgetManager.getAppWidgetInfo(widgetId) == null }, + ) + } + } + + private fun setResult(success: Boolean) { + InstrumentationRegistry.getInstrumentation() + .targetContext + .sendBroadcast( + WidgetConfigActivity.getCommandIntent( + WidgetConfigActivity::class.java, + if (success) "clickOK" else "clickCancel", + ) + ) + uiDevice.waitForIdle() + } + + /** Condition for searching widget id */ + private inner class WidgetSearchCondition : ItemOperator { + override fun evaluate(info: ItemInfo?, view: View): Boolean { + return info is LauncherAppWidgetInfo && + info.providerName == widgetInfo.provider && + info.appWidgetId == widgetId + } + } + + /** Broadcast receiver for receiving widget config activity status. */ + private class WidgetConfigStartupMonitor : + BlockingBroadcastReceiver(WidgetConfigActivity::class.java.name) { + @get:Throws(InterruptedException::class) + val widgetId: Int + get() { + val intent = checkNotNull(blockingGetExtraIntent()) + assertThat(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE).isEqualTo(intent.action) + val widgetId = + intent.getIntExtra( + AppWidgetManager.EXTRA_APPWIDGET_ID, + LauncherAppWidgetInfo.NO_ID, + ) + assertThat(widgetId).isNotEqualTo(LauncherAppWidgetInfo.NO_ID) + return widgetId + } + } +} diff --git a/modules/widgetpicker/tests/multivalentTests/src/com/android/launcher3/widgetpicker/ui/fullcatalog/screens/landing/LandingScreenSinglePaneTest.kt b/modules/widgetpicker/tests/multivalentTests/src/com/android/launcher3/widgetpicker/ui/fullcatalog/screens/landing/LandingScreenSinglePaneTest.kt index 7316aaa15b..4c288f56c3 100644 --- a/modules/widgetpicker/tests/multivalentTests/src/com/android/launcher3/widgetpicker/ui/fullcatalog/screens/landing/LandingScreenSinglePaneTest.kt +++ b/modules/widgetpicker/tests/multivalentTests/src/com/android/launcher3/widgetpicker/ui/fullcatalog/screens/landing/LandingScreenSinglePaneTest.kt @@ -212,4 +212,34 @@ class LandingScreenSinglePaneTest { composeTestRule.onNode(hasText(PERSONAL_TEST_APPS[0].title!!.toString())) .assertIsNotDisplayed() } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun clickingOnAppHeaderShowsWidgetsForThatApp() = testScope.runTest { + composeTestRule.setContent { SinglePaneTestContent() } + + runCurrent() + composeTestRule.waitForIdle() + + composeTestRule.onNode(hasTextExactly(PERSONAL_LABEL)).assertExists().performClick() + + runCurrent() + composeTestRule.waitForIdle() + + // Click on the an app header + val appTitle = PERSONAL_TEST_APPS[0].title!!.toString() + composeTestRule.onNode(hasText(appTitle)).assertExists().performClick() + + runCurrent() + composeTestRule.waitForIdle() + + // Verify the widget for that selected app is displayed + composeTestRule.onNode( + hasContentDescription( + PERSONAL_TEST_APPS[0].widgets[0].label, + substring = true + ) + ) + .assertIsDisplayed() + } } diff --git a/tests/Android.bp b/tests/Android.bp index 2570f59f4f..d627f8f624 100644 --- a/tests/Android.bp +++ b/tests/Android.bp @@ -64,9 +64,6 @@ filegroup { // Library with all the dependencies for building quickstep android_library { name: "Launcher3TestLib", - defaults: [ - "launcher_compose_tests_defaults", - ], srcs: [], asset_dirs: ["assets"], resource_dirs: ["res"], diff --git a/tests/src/com/android/launcher3/widget/AddConfigWidgetTest.java b/tests/src/com/android/launcher3/widget/AddConfigWidgetTest.java index e88a4fb19a..0acc58dd78 100644 --- a/tests/src/com/android/launcher3/widget/AddConfigWidgetTest.java +++ b/tests/src/com/android/launcher3/widget/AddConfigWidgetTest.java @@ -27,12 +27,15 @@ import android.appwidget.AppWidgetManager; import android.content.Context; import android.content.Intent; import android.os.Process; +import android.platform.test.annotations.DisableFlags; +import android.platform.test.flag.junit.SetFlagsRule; import android.view.View; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.LargeTest; import com.android.launcher3.AbstractFloatingView; +import com.android.launcher3.Flags; import com.android.launcher3.Launcher; import com.android.launcher3.model.data.ItemInfo; import com.android.launcher3.model.data.LauncherAppWidgetInfo; @@ -60,6 +63,9 @@ import org.junit.runner.RunWith; @LargeTest @RunWith(AndroidJUnit4.class) public class AddConfigWidgetTest extends BaseLauncherActivityTest { + @Rule + public final SetFlagsRule mSetFlagsRule = + new SetFlagsRule(SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT); @Rule public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind(); @@ -77,12 +83,14 @@ public class AddConfigWidgetTest extends BaseLauncherActivityTest { @Test @PortraitLandscape + @DisableFlags(Flags.FLAG_ENABLE_WIDGET_PICKER_REFACTOR) public void testWidgetConfig() throws Throwable { runTest(true); } @Test @PortraitLandscape + @DisableFlags(Flags.FLAG_ENABLE_WIDGET_PICKER_REFACTOR) public void testConfigCancelled() throws Throwable { runTest(false); } diff --git a/tests/src/com/android/launcher3/widget/TaplAddWidgetTest.java b/tests/src/com/android/launcher3/widget/TaplAddWidgetTest.java index b18b6497e3..f14052612f 100644 --- a/tests/src/com/android/launcher3/widget/TaplAddWidgetTest.java +++ b/tests/src/com/android/launcher3/widget/TaplAddWidgetTest.java @@ -17,12 +17,15 @@ package com.android.launcher3.widget; import static org.junit.Assert.assertNotNull; +import android.platform.test.annotations.DisableFlags; import android.platform.test.annotations.PlatinumTest; +import android.platform.test.flag.junit.SetFlagsRule; import android.view.View; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.LargeTest; +import com.android.launcher3.Flags; import com.android.launcher3.Launcher; import com.android.launcher3.tapl.Widget; import com.android.launcher3.tapl.WidgetResizeFrame; @@ -44,12 +47,16 @@ import org.junit.runner.RunWith; @LargeTest @RunWith(AndroidJUnit4.class) public class TaplAddWidgetTest extends AbstractLauncherUiTest { + @Rule + public final SetFlagsRule mSetFlagsRule = + new SetFlagsRule(SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT); @Rule public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind(); @Test @PortraitLandscape + @DisableFlags(Flags.FLAG_ENABLE_WIDGET_PICKER_REFACTOR) public void testDragIcon() throws Throwable { commitTransactionAndLoadHome(new FavoriteItemsTransaction(mTargetContext)); @@ -81,6 +88,7 @@ public class TaplAddWidgetTest extends AbstractLauncherUiTest { */ @Test @PortraitLandscape + @DisableFlags(Flags.FLAG_ENABLE_WIDGET_PICKER_REFACTOR) public void testDragCustomShortcut() throws Throwable { // TODO(b/322820039): Enable test for tablets - the picker UI has changed and test needs to // be updated to look for appropriate UI elements. @@ -99,6 +107,7 @@ public class TaplAddWidgetTest extends AbstractLauncherUiTest { */ @PlatinumTest(focusArea = "launcher") @Test + @DisableFlags(Flags.FLAG_ENABLE_WIDGET_PICKER_REFACTOR) public void testResizeWidget() throws Throwable { commitTransactionAndLoadHome(new FavoriteItemsTransaction(mTargetContext)); diff --git a/tests/src/com/android/launcher3/widget/picker/WidgetPickerTest.java b/tests/src/com/android/launcher3/widget/picker/WidgetPickerTest.java index e2f2c6b810..2bc41efe4e 100644 --- a/tests/src/com/android/launcher3/widget/picker/WidgetPickerTest.java +++ b/tests/src/com/android/launcher3/widget/picker/WidgetPickerTest.java @@ -19,6 +19,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import android.platform.test.annotations.DisableFlags; +import android.platform.test.flag.junit.SetFlagsRule; + import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.MediumTest; @@ -34,12 +37,17 @@ import org.junit.Test; import org.junit.rules.TestRule; import org.junit.runner.RunWith; +import com.android.launcher3.Flags; + /** * Make sure the basic interactions with the WidgetPicker works. */ @MediumTest @RunWith(AndroidJUnit4.class) public class WidgetPickerTest extends BaseLauncherActivityTest { + @Rule + public final SetFlagsRule mSetFlagsRule = + new SetFlagsRule(SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT); @Rule public TestRule screenRecordRule = new ScreenRecordRule(); @@ -58,6 +66,7 @@ public class WidgetPickerTest extends BaseLauncherActivityTest { @Test @ScreenRecord @PortraitLandscape + @DisableFlags(Flags.FLAG_ENABLE_WIDGET_PICKER_REFACTOR) public void testWidgets() { loadLauncherSync(); // Test opening widgets.