From c57a79721d43dd52dec0a2cab945f8527299fd8d Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Wed, 8 Apr 2020 16:27:28 -0700 Subject: [PATCH] Using config for robo test configuration so that it can easily be customized Change-Id: Ibd0957ac5c28fae1f88d524cda29bbc84a7ff7bc --- robolectric_tests/Android.mk | 4 +- .../config/robolectric.properties | 16 +++ .../folder/FolderNameProviderTest.java | 10 +- .../launcher3/logging/FileLogTest.java | 5 +- .../model/AddWorkspaceItemsTaskTest.java | 4 +- .../launcher3/model/BackupRestoreTest.java | 4 +- .../model/CacheDataUpdatedTaskTest.java | 4 +- .../model/DbDowngradeHelperTest.java | 4 +- .../model/DefaultLayoutProviderTest.java | 4 +- .../launcher3/model/GridBackupTableTest.java | 4 +- .../model/GridSizeMigrationTaskTest.java | 4 +- .../model/GridSizeMigrationTaskV2Test.java | 4 +- .../launcher3/model/LoaderCursorTest.java | 4 +- .../model/ModelMultiCallbacksTest.java | 4 +- .../PackageInstallStateChangedTaskTest.java | 4 +- .../launcher3/popup/PopupPopulatorTest.java | 5 +- .../launcher3/provider/RestoreDbTaskTest.java | 4 +- .../shadows/LShadowAppPredictionManager.java | 38 +++++++ .../launcher3/shadows/LShadowUserManager.java | 9 -- ...SyncRtSurfaceTransactionApplierCompat.java | 42 ++++++++ .../launcher3/ui/LauncherUIScrollTest.java | 32 +----- .../launcher3/util/GridOccupancyTest.java | 9 +- .../android/launcher3/util/IntArrayTest.java | 3 +- .../android/launcher3/util/IntSetTest.java | 9 +- .../util/LauncherRoboTestRunner.java | 101 ----------------- .../util/LauncherTestApplication.java | 54 ++++++++++ .../launcher3/util/LauncherUIHelper.java | 102 ++++++++++++++++++ .../widget/WidgetsListAdapterTest.java | 4 +- 28 files changed, 306 insertions(+), 185 deletions(-) create mode 100644 robolectric_tests/src/com/android/launcher3/shadows/LShadowAppPredictionManager.java create mode 100644 robolectric_tests/src/com/android/launcher3/shadows/ShadowSyncRtSurfaceTransactionApplierCompat.java delete mode 100644 robolectric_tests/src/com/android/launcher3/util/LauncherRoboTestRunner.java create mode 100644 robolectric_tests/src/com/android/launcher3/util/LauncherTestApplication.java create mode 100644 robolectric_tests/src/com/android/launcher3/util/LauncherUIHelper.java diff --git a/robolectric_tests/Android.mk b/robolectric_tests/Android.mk index 7c7e73c374..bbc62e90a8 100644 --- a/robolectric_tests/Android.mk +++ b/robolectric_tests/Android.mk @@ -21,7 +21,7 @@ include $(CLEAR_VARS) LOCAL_MODULE := LauncherRoboTests LOCAL_MODULE_CLASS := JAVA_LIBRARIES -LOCAL_SDK_VERSION := current +LOCAL_SDK_VERSION := system_current LOCAL_SRC_FILES := $(call all-java-files-under, src) LOCAL_STATIC_JAVA_LIBRARIES := \ androidx.test.runner \ @@ -47,7 +47,7 @@ include $(BUILD_STATIC_JAVA_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := RunLauncherRoboTests -LOCAL_SDK_VERSION := current +LOCAL_SDK_VERSION := system_current LOCAL_JAVA_LIBRARIES := LauncherRoboTests LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res diff --git a/robolectric_tests/config/robolectric.properties b/robolectric_tests/config/robolectric.properties index 3d78689f29..0579400655 100644 --- a/robolectric_tests/config/robolectric.properties +++ b/robolectric_tests/config/robolectric.properties @@ -1 +1,17 @@ sdk=29 +shadows= \ + com.android.launcher3.shadows.LShadowAppPredictionManager \ + com.android.launcher3.shadows.LShadowAppWidgetManager \ + com.android.launcher3.shadows.LShadowBackupManager \ + com.android.launcher3.shadows.LShadowBitmap \ + com.android.launcher3.shadows.LShadowLauncherApps \ + com.android.launcher3.shadows.LShadowTypeface \ + com.android.launcher3.shadows.LShadowUserManager \ + com.android.launcher3.shadows.LShadowWallpaperManager \ + com.android.launcher3.shadows.ShadowDeviceFlag \ + com.android.launcher3.shadows.ShadowLooperExecutor \ + com.android.launcher3.shadows.ShadowMainThreadInitializedObject \ + com.android.launcher3.shadows.ShadowOverrides \ + com.android.launcher3.shadows.ShadowSyncRtSurfaceTransactionApplierCompat \ + +application=com.android.launcher3.util.LauncherTestApplication \ No newline at end of file diff --git a/robolectric_tests/src/com/android/launcher3/folder/FolderNameProviderTest.java b/robolectric_tests/src/com/android/launcher3/folder/FolderNameProviderTest.java index 74ef55eb68..32d4f272b2 100644 --- a/robolectric_tests/src/com/android/launcher3/folder/FolderNameProviderTest.java +++ b/robolectric_tests/src/com/android/launcher3/folder/FolderNameProviderTest.java @@ -20,20 +20,20 @@ import static org.junit.Assert.assertEquals; import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.os.UserHandle; import com.android.launcher3.AppInfo; import com.android.launcher3.WorkspaceItemInfo; -import com.android.launcher3.shadows.LShadowUserManager; -import com.android.launcher3.util.LauncherRoboTestRunner; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import java.util.ArrayList; -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) public final class FolderNameProviderTest { private Context mContext; private WorkspaceItemInfo mItem1; @@ -45,13 +45,13 @@ public final class FolderNameProviderTest { mItem1 = new WorkspaceItemInfo(new AppInfo( new ComponentName("a.b.c", "a.b.c/a.b.c.d"), "title1", - LShadowUserManager.newUserHandle(10), + UserHandle.of(10), new Intent().setComponent(new ComponentName("a.b.c", "a.b.c/a.b.c.d")) )); mItem2 = new WorkspaceItemInfo(new AppInfo( new ComponentName("a.b.c", "a.b.c/a.b.c.d"), "title2", - LShadowUserManager.newUserHandle(10), + UserHandle.of(10), new Intent().setComponent(new ComponentName("a.b.c", "a.b.c/a.b.c.d")) )); } diff --git a/robolectric_tests/src/com/android/launcher3/logging/FileLogTest.java b/robolectric_tests/src/com/android/launcher3/logging/FileLogTest.java index 95a41465eb..c892618a9d 100644 --- a/robolectric_tests/src/com/android/launcher3/logging/FileLogTest.java +++ b/robolectric_tests/src/com/android/launcher3/logging/FileLogTest.java @@ -3,12 +3,11 @@ package com.android.launcher3.logging; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import com.android.launcher3.util.LauncherRoboTestRunner; - import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.Shadows; import org.robolectric.util.Scheduler; @@ -21,7 +20,7 @@ import java.util.Calendar; /** * Tests for {@link FileLog} */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) public class FileLogTest { private File mTempDir; diff --git a/robolectric_tests/src/com/android/launcher3/model/AddWorkspaceItemsTaskTest.java b/robolectric_tests/src/com/android/launcher3/model/AddWorkspaceItemsTaskTest.java index b7f224310f..83b217521b 100644 --- a/robolectric_tests/src/com/android/launcher3/model/AddWorkspaceItemsTaskTest.java +++ b/robolectric_tests/src/com/android/launcher3/model/AddWorkspaceItemsTaskTest.java @@ -25,12 +25,12 @@ import com.android.launcher3.util.GridOccupancy; import com.android.launcher3.util.IntArray; import com.android.launcher3.util.IntSparseArrayMap; import com.android.launcher3.util.LauncherModelHelper; -import com.android.launcher3.util.LauncherRoboTestRunner; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.LooperMode; import org.robolectric.annotation.LooperMode.Mode; @@ -41,7 +41,7 @@ import java.util.List; /** * Tests for {@link AddWorkspaceItemsTask} */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) @LooperMode(Mode.PAUSED) public class AddWorkspaceItemsTaskTest { diff --git a/robolectric_tests/src/com/android/launcher3/model/BackupRestoreTest.java b/robolectric_tests/src/com/android/launcher3/model/BackupRestoreTest.java index 7072adf160..90313abe1d 100644 --- a/robolectric_tests/src/com/android/launcher3/model/BackupRestoreTest.java +++ b/robolectric_tests/src/com/android/launcher3/model/BackupRestoreTest.java @@ -43,11 +43,11 @@ import com.android.launcher3.provider.RestoreDbTask; import com.android.launcher3.shadows.LShadowBackupManager; import com.android.launcher3.shadows.LShadowUserManager; import com.android.launcher3.util.LauncherModelHelper; -import com.android.launcher3.util.LauncherRoboTestRunner; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.LooperMode; import org.robolectric.shadow.api.Shadow; @@ -55,7 +55,7 @@ import org.robolectric.shadow.api.Shadow; /** * Tests to verify backup and restore flow. */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) @LooperMode(LooperMode.Mode.PAUSED) public class BackupRestoreTest { diff --git a/robolectric_tests/src/com/android/launcher3/model/CacheDataUpdatedTaskTest.java b/robolectric_tests/src/com/android/launcher3/model/CacheDataUpdatedTaskTest.java index f128e24912..09d611d0ff 100644 --- a/robolectric_tests/src/com/android/launcher3/model/CacheDataUpdatedTaskTest.java +++ b/robolectric_tests/src/com/android/launcher3/model/CacheDataUpdatedTaskTest.java @@ -25,11 +25,11 @@ import com.android.launcher3.icons.BitmapInfo; import com.android.launcher3.icons.IconCache; import com.android.launcher3.icons.cache.CachingLogic; import com.android.launcher3.util.LauncherModelHelper; -import com.android.launcher3.util.LauncherRoboTestRunner; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.LooperMode; import org.robolectric.annotation.LooperMode.Mode; @@ -40,7 +40,7 @@ import java.util.HashSet; /** * Tests for {@link CacheDataUpdatedTask} */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) @LooperMode(Mode.PAUSED) public class CacheDataUpdatedTaskTest { diff --git a/robolectric_tests/src/com/android/launcher3/model/DbDowngradeHelperTest.java b/robolectric_tests/src/com/android/launcher3/model/DbDowngradeHelperTest.java index 1442c55c2e..b7340cf7d0 100644 --- a/robolectric_tests/src/com/android/launcher3/model/DbDowngradeHelperTest.java +++ b/robolectric_tests/src/com/android/launcher3/model/DbDowngradeHelperTest.java @@ -36,11 +36,11 @@ import com.android.launcher3.LauncherProvider; import com.android.launcher3.LauncherProvider.DatabaseHelper; import com.android.launcher3.LauncherSettings.Favorites; import com.android.launcher3.R; -import com.android.launcher3.util.LauncherRoboTestRunner; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import java.io.File; @@ -48,7 +48,7 @@ import java.io.File; /** * Tests for {@link DbDowngradeHelper} */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) public class DbDowngradeHelperTest { private static final String SCHEMA_FILE = "test_schema.json"; diff --git a/robolectric_tests/src/com/android/launcher3/model/DefaultLayoutProviderTest.java b/robolectric_tests/src/com/android/launcher3/model/DefaultLayoutProviderTest.java index 7bc34cf91f..cdabf79e39 100644 --- a/robolectric_tests/src/com/android/launcher3/model/DefaultLayoutProviderTest.java +++ b/robolectric_tests/src/com/android/launcher3/model/DefaultLayoutProviderTest.java @@ -34,11 +34,11 @@ import com.android.launcher3.LauncherSettings; import com.android.launcher3.icons.BitmapInfo; import com.android.launcher3.util.LauncherLayoutBuilder; import com.android.launcher3.util.LauncherModelHelper; -import com.android.launcher3.util.LauncherRoboTestRunner; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.LooperMode; import org.robolectric.annotation.LooperMode.Mode; @@ -46,7 +46,7 @@ import org.robolectric.annotation.LooperMode.Mode; /** * Tests for layout parser for remote layout */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) @LooperMode(Mode.PAUSED) public class DefaultLayoutProviderTest { diff --git a/robolectric_tests/src/com/android/launcher3/model/GridBackupTableTest.java b/robolectric_tests/src/com/android/launcher3/model/GridBackupTableTest.java index f46b849a84..56ce215d96 100644 --- a/robolectric_tests/src/com/android/launcher3/model/GridBackupTableTest.java +++ b/robolectric_tests/src/com/android/launcher3/model/GridBackupTableTest.java @@ -23,17 +23,17 @@ import android.graphics.Point; import com.android.launcher3.LauncherSettings.Favorites; import com.android.launcher3.LauncherSettings.Settings; import com.android.launcher3.util.LauncherModelHelper; -import com.android.launcher3.util.LauncherRoboTestRunner; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; /** * Unit tests for {@link GridBackupTable} */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) public class GridBackupTableTest { private static final int BACKUP_ITEM_COUNT = 12; diff --git a/robolectric_tests/src/com/android/launcher3/model/GridSizeMigrationTaskTest.java b/robolectric_tests/src/com/android/launcher3/model/GridSizeMigrationTaskTest.java index c426dc593f..8e00dcb854 100644 --- a/robolectric_tests/src/com/android/launcher3/model/GridSizeMigrationTaskTest.java +++ b/robolectric_tests/src/com/android/launcher3/model/GridSizeMigrationTaskTest.java @@ -20,11 +20,11 @@ import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.model.GridSizeMigrationTask.MultiStepMigrationTask; import com.android.launcher3.util.IntArray; import com.android.launcher3.util.LauncherModelHelper; -import com.android.launcher3.util.LauncherRoboTestRunner; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import java.util.HashSet; @@ -33,7 +33,7 @@ import java.util.LinkedList; /** * Unit tests for {@link GridSizeMigrationTask} */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) public class GridSizeMigrationTaskTest { private LauncherModelHelper mModelHelper; diff --git a/robolectric_tests/src/com/android/launcher3/model/GridSizeMigrationTaskV2Test.java b/robolectric_tests/src/com/android/launcher3/model/GridSizeMigrationTaskV2Test.java index 8f58d8b507..59c94806b7 100644 --- a/robolectric_tests/src/com/android/launcher3/model/GridSizeMigrationTaskV2Test.java +++ b/robolectric_tests/src/com/android/launcher3/model/GridSizeMigrationTaskV2Test.java @@ -39,17 +39,17 @@ import com.android.launcher3.InvariantDeviceProfile; import com.android.launcher3.LauncherSettings; import com.android.launcher3.pm.UserCache; import com.android.launcher3.util.LauncherModelHelper; -import com.android.launcher3.util.LauncherRoboTestRunner; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import java.util.HashSet; /** Unit tests for {@link GridSizeMigrationTaskV2} */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) public class GridSizeMigrationTaskV2Test { private LauncherModelHelper mModelHelper; diff --git a/robolectric_tests/src/com/android/launcher3/model/LoaderCursorTest.java b/robolectric_tests/src/com/android/launcher3/model/LoaderCursorTest.java index 6e41a4f3e8..8531014bea 100644 --- a/robolectric_tests/src/com/android/launcher3/model/LoaderCursorTest.java +++ b/robolectric_tests/src/com/android/launcher3/model/LoaderCursorTest.java @@ -55,12 +55,12 @@ import com.android.launcher3.LauncherAppState; import com.android.launcher3.LauncherSettings; import com.android.launcher3.WorkspaceItemInfo; import com.android.launcher3.util.Executors; -import com.android.launcher3.util.LauncherRoboTestRunner; import com.android.launcher3.util.PackageManagerHelper; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.LooperMode; import org.robolectric.annotation.LooperMode.Mode; @@ -68,7 +68,7 @@ import org.robolectric.annotation.LooperMode.Mode; /** * Tests for {@link LoaderCursor} */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) @LooperMode(Mode.PAUSED) public class LoaderCursorTest { diff --git a/robolectric_tests/src/com/android/launcher3/model/ModelMultiCallbacksTest.java b/robolectric_tests/src/com/android/launcher3/model/ModelMultiCallbacksTest.java index c7979b21d2..d81ac69f69 100644 --- a/robolectric_tests/src/com/android/launcher3/model/ModelMultiCallbacksTest.java +++ b/robolectric_tests/src/com/android/launcher3/model/ModelMultiCallbacksTest.java @@ -33,13 +33,13 @@ import com.android.launcher3.model.BgDataModel.Callbacks; import com.android.launcher3.util.Executors; import com.android.launcher3.util.LauncherLayoutBuilder; import com.android.launcher3.util.LauncherModelHelper; -import com.android.launcher3.util.LauncherRoboTestRunner; import com.android.launcher3.util.LooperExecutor; import com.android.launcher3.util.ViewOnDrawExecutor; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.LooperMode; import org.robolectric.annotation.LooperMode.Mode; @@ -55,7 +55,7 @@ import java.util.stream.Collectors; /** * Tests to verify multiple callbacks in Loader */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) @LooperMode(Mode.PAUSED) public class ModelMultiCallbacksTest { diff --git a/robolectric_tests/src/com/android/launcher3/model/PackageInstallStateChangedTaskTest.java b/robolectric_tests/src/com/android/launcher3/model/PackageInstallStateChangedTaskTest.java index bd71f01b6b..f017c82200 100644 --- a/robolectric_tests/src/com/android/launcher3/model/PackageInstallStateChangedTaskTest.java +++ b/robolectric_tests/src/com/android/launcher3/model/PackageInstallStateChangedTaskTest.java @@ -7,11 +7,11 @@ import com.android.launcher3.LauncherAppWidgetInfo; import com.android.launcher3.WorkspaceItemInfo; import com.android.launcher3.pm.PackageInstallInfo; import com.android.launcher3.util.LauncherModelHelper; -import com.android.launcher3.util.LauncherRoboTestRunner; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.LooperMode; import org.robolectric.annotation.LooperMode.Mode; @@ -21,7 +21,7 @@ import java.util.HashSet; /** * Tests for {@link PackageInstallStateChangedTask} */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) @LooperMode(Mode.PAUSED) public class PackageInstallStateChangedTaskTest { diff --git a/robolectric_tests/src/com/android/launcher3/popup/PopupPopulatorTest.java b/robolectric_tests/src/com/android/launcher3/popup/PopupPopulatorTest.java index 7612ae105a..83bf7dac89 100644 --- a/robolectric_tests/src/com/android/launcher3/popup/PopupPopulatorTest.java +++ b/robolectric_tests/src/com/android/launcher3/popup/PopupPopulatorTest.java @@ -27,10 +27,9 @@ import static org.mockito.Mockito.spy; import android.content.pm.ShortcutInfo; -import com.android.launcher3.util.LauncherRoboTestRunner; - import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import java.util.ArrayList; @@ -40,7 +39,7 @@ import java.util.List; /** * Tests the sorting and filtering of shortcuts in {@link PopupPopulator}. */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) public class PopupPopulatorTest { @Test diff --git a/robolectric_tests/src/com/android/launcher3/provider/RestoreDbTaskTest.java b/robolectric_tests/src/com/android/launcher3/provider/RestoreDbTaskTest.java index 7ef670c270..58174c793c 100644 --- a/robolectric_tests/src/com/android/launcher3/provider/RestoreDbTaskTest.java +++ b/robolectric_tests/src/com/android/launcher3/provider/RestoreDbTaskTest.java @@ -23,16 +23,16 @@ import android.database.sqlite.SQLiteDatabase; import com.android.launcher3.LauncherProvider.DatabaseHelper; import com.android.launcher3.LauncherSettings.Favorites; -import com.android.launcher3.util.LauncherRoboTestRunner; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; /** * Tests for {@link RestoreDbTask} */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) public class RestoreDbTaskTest { @Test diff --git a/robolectric_tests/src/com/android/launcher3/shadows/LShadowAppPredictionManager.java b/robolectric_tests/src/com/android/launcher3/shadows/LShadowAppPredictionManager.java new file mode 100644 index 0000000000..ae051f7d7c --- /dev/null +++ b/robolectric_tests/src/com/android/launcher3/shadows/LShadowAppPredictionManager.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2020 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.shadows; + +import static org.mockito.Mockito.mock; + +import android.app.prediction.AppPredictionContext; +import android.app.prediction.AppPredictionManager; +import android.app.prediction.AppPredictor; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; + +/** + * Shadow for {@link AppPredictionManager} which create mock predictors + */ +@Implements(value = AppPredictionManager.class) +public class LShadowAppPredictionManager { + + @Implementation + public AppPredictor createAppPredictionSession(AppPredictionContext predictionContext) { + return mock(AppPredictor.class); + } +} diff --git a/robolectric_tests/src/com/android/launcher3/shadows/LShadowUserManager.java b/robolectric_tests/src/com/android/launcher3/shadows/LShadowUserManager.java index 576ddbd407..edf8edbb43 100644 --- a/robolectric_tests/src/com/android/launcher3/shadows/LShadowUserManager.java +++ b/robolectric_tests/src/com/android/launcher3/shadows/LShadowUserManager.java @@ -16,7 +16,6 @@ package com.android.launcher3.shadows; -import android.os.Parcel; import android.os.UserHandle; import android.os.UserManager; import android.util.SparseBooleanArray; @@ -51,12 +50,4 @@ public class LShadowUserManager extends ShadowUserManager { public void setUserLocked(UserHandle userHandle, boolean enabled) { mLockedUsers.put(userHandle.hashCode(), enabled); } - - // Create user handle from parcel since UserHandle.of() was only added in later APIs. - public static UserHandle newUserHandle(int uid) { - Parcel userParcel = Parcel.obtain(); - userParcel.writeInt(uid); - userParcel.setDataPosition(0); - return new UserHandle(userParcel); - } } diff --git a/robolectric_tests/src/com/android/launcher3/shadows/ShadowSyncRtSurfaceTransactionApplierCompat.java b/robolectric_tests/src/com/android/launcher3/shadows/ShadowSyncRtSurfaceTransactionApplierCompat.java new file mode 100644 index 0000000000..238926c65f --- /dev/null +++ b/robolectric_tests/src/com/android/launcher3/shadows/ShadowSyncRtSurfaceTransactionApplierCompat.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2020 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.shadows; + +import static org.robolectric.shadow.api.Shadow.invokeConstructor; +import static org.robolectric.util.ReflectionHelpers.ClassParameter.from; + +import android.view.View; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; +import org.robolectric.annotation.RealObject; + +/** + * Shadow for SyncRtSurfaceTransactionApplierCompat to override default functionality + */ +@Implements(className = "com.android.systemui.shared.system.SyncRtSurfaceTransactionApplierCompat", + isInAndroidSdk = false) +public class ShadowSyncRtSurfaceTransactionApplierCompat { + + @RealObject + private Object mRealObject; + + @Implementation + protected void __constructor__(View view) { + invokeConstructor(mRealObject.getClass(), mRealObject, from(View.class, null)); + } +} diff --git a/robolectric_tests/src/com/android/launcher3/ui/LauncherUIScrollTest.java b/robolectric_tests/src/com/android/launcher3/ui/LauncherUIScrollTest.java index 209bae06a8..d330d10133 100644 --- a/robolectric_tests/src/com/android/launcher3/ui/LauncherUIScrollTest.java +++ b/robolectric_tests/src/com/android/launcher3/ui/LauncherUIScrollTest.java @@ -14,23 +14,20 @@ * limitations under the License. */package com.android.launcher3.ui; -import static android.view.View.MeasureSpec.EXACTLY; -import static android.view.View.MeasureSpec.makeMeasureSpec; - import static com.android.launcher3.util.LauncherModelHelper.TEST_PACKAGE; +import static com.android.launcher3.util.LauncherUIHelper.buildAndBindLauncher; +import static com.android.launcher3.util.LauncherUIHelper.doLayout; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.mockito.Mockito.mock; -import android.app.Activity; import android.content.Context; import android.os.SystemClock; import android.provider.Settings; import android.view.InputDevice; import android.view.MotionEvent; import android.view.MotionEvent.PointerProperties; -import android.view.View; import com.android.launcher3.DeviceProfile; import com.android.launcher3.InvariantDeviceProfile; @@ -44,24 +41,21 @@ import com.android.launcher3.shadows.ShadowOverrides; import com.android.launcher3.util.LauncherLayoutBuilder; import com.android.launcher3.util.LauncherLayoutBuilder.FolderBuilder; import com.android.launcher3.util.LauncherModelHelper; -import com.android.launcher3.util.LauncherRoboTestRunner; -import com.android.launcher3.util.ViewOnDrawExecutor; import com.android.launcher3.widget.WidgetsFullSheet; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.LooperMode; import org.robolectric.annotation.LooperMode.Mode; import org.robolectric.shadows.ShadowLooper; -import org.robolectric.util.ReflectionHelpers; /** * Tests scroll behavior at various Launcher UI components */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) @LooperMode(Mode.PAUSED) public class LauncherUIScrollTest { @@ -166,23 +160,7 @@ public class LauncherUIScrollTest { private Launcher loadLauncher() throws Exception { mModelHelper.setupDefaultLayoutProvider(mLayoutBuilder).loadModelSync(); - - Launcher launcher = Robolectric.buildActivity(Launcher.class).setup().get(); - doLayout(launcher); - ViewOnDrawExecutor executor = ReflectionHelpers.getField(launcher, "mPendingExecutor"); - if (executor != null) { - executor.runAllTasks(); - } - return launcher; - } - - private static void doLayout(Activity activity) { - DeviceProfile dp = InvariantDeviceProfile.INSTANCE - .get(RuntimeEnvironment.application).portraitProfile; - View view = activity.getWindow().getDecorView(); - view.measure(makeMeasureSpec(dp.widthPx, EXACTLY), makeMeasureSpec(dp.heightPx, EXACTLY)); - view.layout(0, 0, dp.widthPx, dp.heightPx); - ShadowLooper.idleMainLooper(); + return buildAndBindLauncher(); } private static MotionEvent createScrollEvent(int scroll) { diff --git a/robolectric_tests/src/com/android/launcher3/util/GridOccupancyTest.java b/robolectric_tests/src/com/android/launcher3/util/GridOccupancyTest.java index e453e31a41..2f3fc3764d 100644 --- a/robolectric_tests/src/com/android/launcher3/util/GridOccupancyTest.java +++ b/robolectric_tests/src/com/android/launcher3/util/GridOccupancyTest.java @@ -1,16 +1,17 @@ package com.android.launcher3.util; -import org.junit.Test; -import org.junit.runner.RunWith; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + /** * Unit tests for {@link GridOccupancy} */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) public class GridOccupancyTest { @Test diff --git a/robolectric_tests/src/com/android/launcher3/util/IntArrayTest.java b/robolectric_tests/src/com/android/launcher3/util/IntArrayTest.java index 5974ea5db9..c08e198a8c 100644 --- a/robolectric_tests/src/com/android/launcher3/util/IntArrayTest.java +++ b/robolectric_tests/src/com/android/launcher3/util/IntArrayTest.java @@ -19,11 +19,12 @@ import static com.google.common.truth.Truth.assertThat; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; /** * Robolectric unit tests for {@link IntArray} */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) public class IntArrayTest { @Test diff --git a/robolectric_tests/src/com/android/launcher3/util/IntSetTest.java b/robolectric_tests/src/com/android/launcher3/util/IntSetTest.java index aedf71ecbc..7a8c00b3c7 100644 --- a/robolectric_tests/src/com/android/launcher3/util/IntSetTest.java +++ b/robolectric_tests/src/com/android/launcher3/util/IntSetTest.java @@ -17,17 +17,18 @@ package com.android.launcher3.util; import static com.google.common.truth.Truth.assertThat; -import org.junit.Test; -import org.junit.runner.RunWith; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + /** * Robolectric unit tests for {@link IntSet} */ -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) public class IntSetTest { @Test diff --git a/robolectric_tests/src/com/android/launcher3/util/LauncherRoboTestRunner.java b/robolectric_tests/src/com/android/launcher3/util/LauncherRoboTestRunner.java deleted file mode 100644 index 744b478b2a..0000000000 --- a/robolectric_tests/src/com/android/launcher3/util/LauncherRoboTestRunner.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (C) 2019 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.util; - -import static org.mockito.Mockito.mock; - -import com.android.launcher3.shadows.LShadowAppWidgetManager; -import com.android.launcher3.shadows.LShadowBackupManager; -import com.android.launcher3.shadows.LShadowBitmap; -import com.android.launcher3.shadows.LShadowLauncherApps; -import com.android.launcher3.shadows.LShadowTypeface; -import com.android.launcher3.shadows.LShadowUserManager; -import com.android.launcher3.shadows.LShadowWallpaperManager; -import com.android.launcher3.shadows.ShadowDeviceFlag; -import com.android.launcher3.shadows.ShadowLooperExecutor; -import com.android.launcher3.shadows.ShadowMainThreadInitializedObject; -import com.android.launcher3.shadows.ShadowOverrides; -import com.android.launcher3.uioverrides.plugins.PluginManagerWrapper; - -import org.junit.runners.model.InitializationError; -import org.robolectric.DefaultTestLifecycle; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.TestLifecycle; -import org.robolectric.annotation.Config; -import org.robolectric.shadows.ShadowLog; - -import java.lang.reflect.Method; - -import javax.annotation.Nonnull; - -/** - * Test runner with Launcher specific configurations - */ -public class LauncherRoboTestRunner extends RobolectricTestRunner { - - private static final Class[] SHADOWS = new Class[] { - LShadowAppWidgetManager.class, - LShadowUserManager.class, - LShadowLauncherApps.class, - LShadowBitmap.class, - LShadowBackupManager.class, - LShadowTypeface.class, - LShadowWallpaperManager.class, - ShadowLooperExecutor.class, - ShadowMainThreadInitializedObject.class, - ShadowDeviceFlag.class, - ShadowOverrides.class - }; - - public LauncherRoboTestRunner(Class testClass) throws InitializationError { - super(testClass); - } - - @Override - protected Config buildGlobalConfig() { - return new Config.Builder().setShadows(SHADOWS).build(); - } - - @Nonnull - @Override - protected Class getTestLifecycleClass() { - return LauncherTestLifecycle.class; - } - - public static class LauncherTestLifecycle extends DefaultTestLifecycle { - - @Override - public void beforeTest(Method method) { - super.beforeTest(method); - ShadowLog.stream = System.out; - - // Disable plugins - PluginManagerWrapper.INSTANCE.initializeForTesting(mock(PluginManagerWrapper.class)); - - // Initialize mock wallpaper manager - LShadowWallpaperManager.initializeMock(); - } - - @Override - public void afterTest(Method method) { - super.afterTest(method); - - ShadowLog.stream = null; - ShadowMainThreadInitializedObject.resetInitializedObjects(); - ShadowOverrides.clearProvider(); - } - } -} diff --git a/robolectric_tests/src/com/android/launcher3/util/LauncherTestApplication.java b/robolectric_tests/src/com/android/launcher3/util/LauncherTestApplication.java new file mode 100644 index 0000000000..6dd4df8377 --- /dev/null +++ b/robolectric_tests/src/com/android/launcher3/util/LauncherTestApplication.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2020 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.util; + +import static org.mockito.Mockito.mock; + +import android.app.Application; + +import com.android.launcher3.shadows.LShadowWallpaperManager; +import com.android.launcher3.shadows.ShadowMainThreadInitializedObject; +import com.android.launcher3.shadows.ShadowOverrides; +import com.android.launcher3.uioverrides.plugins.PluginManagerWrapper; + +import org.robolectric.TestLifecycleApplication; +import org.robolectric.shadows.ShadowLog; + +import java.lang.reflect.Method; + +public class LauncherTestApplication extends Application implements TestLifecycleApplication { + + @Override + public void beforeTest(Method method) { + ShadowLog.stream = System.out; + + // Disable plugins + PluginManagerWrapper.INSTANCE.initializeForTesting(mock(PluginManagerWrapper.class)); + + // Initialize mock wallpaper manager + LShadowWallpaperManager.initializeMock(); + } + + @Override + public void prepareTest(Object test) { } + + @Override + public void afterTest(Method method) { + ShadowLog.stream = null; + ShadowMainThreadInitializedObject.resetInitializedObjects(); + ShadowOverrides.clearProvider(); + } +} diff --git a/robolectric_tests/src/com/android/launcher3/util/LauncherUIHelper.java b/robolectric_tests/src/com/android/launcher3/util/LauncherUIHelper.java new file mode 100644 index 0000000000..f019a2019d --- /dev/null +++ b/robolectric_tests/src/com/android/launcher3/util/LauncherUIHelper.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2020 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.util; + +import static android.view.View.MeasureSpec.EXACTLY; +import static android.view.View.MeasureSpec.makeMeasureSpec; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ResolveInfo; +import android.graphics.Point; +import android.view.View; +import android.view.WindowManager; + +import com.android.launcher3.Launcher; + +import org.robolectric.Robolectric; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.android.controller.ActivityController; +import org.robolectric.shadows.ShadowLooper; +import org.robolectric.util.ReflectionHelpers; + +import java.util.List; + +/** + * Utility class to help manage Launcher UI and related objects for test. + */ +public class LauncherUIHelper { + + /** + * Returns the class name for the Launcher activity as defined in the manifest + */ + public static String getLauncherClassName() { + Context context = RuntimeEnvironment.application; + Intent homeIntent = new Intent(Intent.ACTION_MAIN) + .addCategory(Intent.CATEGORY_HOME) + .setPackage(context.getPackageName()) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + List launchers = context.getPackageManager() + .queryIntentActivities(homeIntent, 0); + if (launchers.size() != 1) { + return null; + } + return launchers.get(0).activityInfo.name; + } + + /** + * Returns an activity controller for Launcher activity defined in the manifest + */ + public static ActivityController buildLauncher() { + try { + Class tClass = (Class) Class.forName(getLauncherClassName()); + return Robolectric.buildActivity(tClass); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * Creates and binds a Launcher activity defined in the manifest. + * Note that the model must be bound before calling this + */ + public static T buildAndBindLauncher() { + ActivityController controller = buildLauncher(); + + T launcher = controller.setup().get(); + doLayout(launcher); + ViewOnDrawExecutor executor = ReflectionHelpers.getField(launcher, "mPendingExecutor"); + if (executor != null) { + executor.runAllTasks(); + } + return launcher; + } + + /** + * Performs a measure and layout pass for the given activity + */ + public static void doLayout(Activity activity) { + Point size = new Point(); + RuntimeEnvironment.application.getSystemService(WindowManager.class) + .getDefaultDisplay().getSize(size); + View view = activity.getWindow().getDecorView(); + view.measure(makeMeasureSpec(size.x, EXACTLY), makeMeasureSpec(size.y, EXACTLY)); + view.layout(0, 0, size.x, size.y); + ShadowLooper.idleMainLooper(); + } +} diff --git a/robolectric_tests/src/com/android/launcher3/widget/WidgetsListAdapterTest.java b/robolectric_tests/src/com/android/launcher3/widget/WidgetsListAdapterTest.java index daae8186a6..76c395245d 100644 --- a/robolectric_tests/src/com/android/launcher3/widget/WidgetsListAdapterTest.java +++ b/robolectric_tests/src/com/android/launcher3/widget/WidgetsListAdapterTest.java @@ -36,13 +36,13 @@ import com.android.launcher3.icons.BitmapInfo; import com.android.launcher3.icons.IconCache; import com.android.launcher3.model.PackageItemInfo; import com.android.launcher3.model.WidgetItem; -import com.android.launcher3.util.LauncherRoboTestRunner; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.shadows.ShadowPackageManager; import org.robolectric.util.ReflectionHelpers; @@ -50,7 +50,7 @@ import org.robolectric.util.ReflectionHelpers; import java.util.ArrayList; import java.util.Collections; -@RunWith(LauncherRoboTestRunner.class) +@RunWith(RobolectricTestRunner.class) public class WidgetsListAdapterTest { @Mock private LayoutInflater mMockLayoutInflater;