Merge "Adds unit tests for AppWidgetsRestoredReceiver" into main
This commit is contained in:
committed by
Android (Google) Code Review
commit
c8ac1b41f7
@@ -1,17 +1,20 @@
|
||||
package com.android.launcher3;
|
||||
|
||||
import static com.android.launcher3.LauncherPrefs.APP_WIDGET_IDS;
|
||||
import static com.android.launcher3.LauncherPrefs.OLD_APP_WIDGET_IDS;
|
||||
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.launcher3.provider.RestoreDbTask;
|
||||
import com.android.launcher3.util.IntArray;
|
||||
import com.android.launcher3.widget.LauncherWidgetHolder;
|
||||
|
||||
public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
|
||||
|
||||
private static final String TAG = "AWRestoredReceiver";
|
||||
private static final String TAG = "AppWidgetsRestoredReceiver";
|
||||
|
||||
@Override
|
||||
public void onReceive(final Context context, Intent intent) {
|
||||
@@ -25,7 +28,9 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
|
||||
final int[] oldIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_OLD_IDS);
|
||||
final int[] newIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
|
||||
if (oldIds != null && newIds != null && oldIds.length == newIds.length) {
|
||||
RestoreDbTask.setRestoredAppWidgetIds(context, oldIds, newIds);
|
||||
LauncherPrefs.get(context).putSync(
|
||||
OLD_APP_WIDGET_IDS.to(IntArray.wrap(oldIds).toConcatString()),
|
||||
APP_WIDGET_IDS.to(IntArray.wrap(newIds).toConcatString()));
|
||||
} else {
|
||||
Log.e(TAG, "Invalid host restored received");
|
||||
}
|
||||
|
||||
@@ -529,13 +529,6 @@ public class RestoreDbTask {
|
||||
}
|
||||
}
|
||||
|
||||
public static void setRestoredAppWidgetIds(Context context, @NonNull int[] oldIds,
|
||||
@NonNull int[] newIds) {
|
||||
LauncherPrefs.get(context).putSync(
|
||||
OLD_APP_WIDGET_IDS.to(IntArray.wrap(oldIds).toConcatString()),
|
||||
APP_WIDGET_IDS.to(IntArray.wrap(newIds).toConcatString()));
|
||||
}
|
||||
|
||||
protected static void maybeOverrideShortcuts(Context context, ModelDbController controller,
|
||||
SQLiteDatabase db, long currentUser) {
|
||||
Map<String, LauncherActivityInfo> activityOverrides = ApiWrapper.getActivityOverrides(
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
package com.android.launcher3
|
||||
|
||||
import android.appwidget.AppWidgetManager.ACTION_APPWIDGET_DELETED
|
||||
import android.appwidget.AppWidgetManager.ACTION_APPWIDGET_HOST_RESTORED
|
||||
import android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_IDS
|
||||
import android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_OLD_IDS
|
||||
import android.appwidget.AppWidgetManager.EXTRA_HOST_ID
|
||||
import android.content.Intent
|
||||
import android.platform.uiautomator_helpers.DeviceHelpers
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import androidx.test.filters.SmallTest
|
||||
import com.android.launcher3.LauncherPrefs.Companion.APP_WIDGET_IDS
|
||||
import com.android.launcher3.LauncherPrefs.Companion.OLD_APP_WIDGET_IDS
|
||||
import com.android.launcher3.util.IntArray
|
||||
import com.android.launcher3.util.LauncherModelHelper.TEST_PACKAGE
|
||||
import com.android.launcher3.widget.LauncherWidgetHolder.APPWIDGET_HOST_ID
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
/** Tests for [AppWidgetsRestoredReceiver] */
|
||||
@SmallTest
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class AppWidgetsRestoredReceiverTest {
|
||||
private lateinit var launcherPrefs: LauncherPrefs
|
||||
private lateinit var receiverUnderTest: AppWidgetsRestoredReceiver
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
launcherPrefs = LauncherPrefs(DeviceHelpers.context)
|
||||
receiverUnderTest = AppWidgetsRestoredReceiver()
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
launcherPrefs.remove(OLD_APP_WIDGET_IDS, APP_WIDGET_IDS)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `When AppWidgetsRestoredReceiver gets valid broadcast it sets old and new app widget ids`() {
|
||||
// Given
|
||||
val oldIds = intArrayOf(1, 2, 10)
|
||||
val newIds = intArrayOf(10, 11, 12)
|
||||
val expectedOldIds = IntArray.wrap(*oldIds).toConcatString()
|
||||
val expectedNewIds = IntArray.wrap(*newIds).toConcatString()
|
||||
val intent =
|
||||
Intent().apply {
|
||||
component = null
|
||||
`package` = TEST_PACKAGE
|
||||
action = ACTION_APPWIDGET_HOST_RESTORED
|
||||
putExtra(EXTRA_APPWIDGET_OLD_IDS, oldIds)
|
||||
putExtra(EXTRA_APPWIDGET_IDS, newIds)
|
||||
putExtra(EXTRA_HOST_ID, APPWIDGET_HOST_ID)
|
||||
}
|
||||
|
||||
// When
|
||||
receiverUnderTest.onReceive(DeviceHelpers.context, intent)
|
||||
|
||||
// Then
|
||||
assertThat(launcherPrefs.get(OLD_APP_WIDGET_IDS)).isEqualTo(expectedOldIds)
|
||||
assertThat(launcherPrefs.get(APP_WIDGET_IDS)).isEqualTo(expectedNewIds)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `AppWidgetsRestoredReceiver does not set widget ids when Intent action is invalid`() {
|
||||
// Given
|
||||
val oldIds = intArrayOf(1, 2, 10)
|
||||
val newIds = intArrayOf(10, 11, 12)
|
||||
val intent =
|
||||
Intent().apply {
|
||||
component = null
|
||||
`package` = TEST_PACKAGE
|
||||
action = ACTION_APPWIDGET_DELETED
|
||||
putExtra(EXTRA_APPWIDGET_OLD_IDS, oldIds)
|
||||
putExtra(EXTRA_APPWIDGET_IDS, newIds)
|
||||
putExtra(EXTRA_HOST_ID, APPWIDGET_HOST_ID)
|
||||
}
|
||||
|
||||
// When
|
||||
receiverUnderTest.onReceive(DeviceHelpers.context, intent)
|
||||
|
||||
// Then
|
||||
assertThat(launcherPrefs.has(OLD_APP_WIDGET_IDS, APP_WIDGET_IDS)).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `AppWidgetsRestoredReceiver does not set widget ids when Intent host id is not Launcher`() {
|
||||
// Given
|
||||
val oldIds = intArrayOf(1, 2, 10)
|
||||
val newIds = intArrayOf(10, 11, 12)
|
||||
val intent =
|
||||
Intent().apply {
|
||||
component = null
|
||||
`package` = TEST_PACKAGE
|
||||
action = ACTION_APPWIDGET_HOST_RESTORED
|
||||
putExtra(EXTRA_APPWIDGET_OLD_IDS, oldIds)
|
||||
putExtra(EXTRA_APPWIDGET_IDS, newIds)
|
||||
putExtra(EXTRA_HOST_ID, 999999999)
|
||||
}
|
||||
|
||||
// When
|
||||
receiverUnderTest.onReceive(DeviceHelpers.context, intent)
|
||||
|
||||
// Then
|
||||
assertThat(launcherPrefs.has(OLD_APP_WIDGET_IDS, APP_WIDGET_IDS)).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `AppWidgetsRestoredReceiver does not set ids when new and old ids differ in length`() {
|
||||
// Given
|
||||
val oldIds = intArrayOf(10)
|
||||
val newIds = intArrayOf(10, 11, 12)
|
||||
val intent =
|
||||
Intent().apply {
|
||||
component = null
|
||||
`package` = TEST_PACKAGE
|
||||
action = ACTION_APPWIDGET_HOST_RESTORED
|
||||
putExtra(EXTRA_APPWIDGET_OLD_IDS, oldIds)
|
||||
putExtra(EXTRA_APPWIDGET_IDS, newIds)
|
||||
putExtra(EXTRA_HOST_ID, APPWIDGET_HOST_ID)
|
||||
}
|
||||
|
||||
// When
|
||||
receiverUnderTest.onReceive(DeviceHelpers.context, intent)
|
||||
|
||||
// Then
|
||||
assertThat(launcherPrefs.has(OLD_APP_WIDGET_IDS, APP_WIDGET_IDS)).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `AppWidgetsRestoredReceiver does not set widget ids when old ids not set`() {
|
||||
// Given
|
||||
val newIds = intArrayOf(10, 11, 12)
|
||||
val intent =
|
||||
Intent().apply {
|
||||
component = null
|
||||
`package` = TEST_PACKAGE
|
||||
action = ACTION_APPWIDGET_HOST_RESTORED
|
||||
putExtra(EXTRA_APPWIDGET_IDS, newIds)
|
||||
putExtra(EXTRA_HOST_ID, APPWIDGET_HOST_ID)
|
||||
}
|
||||
|
||||
// When
|
||||
receiverUnderTest.onReceive(DeviceHelpers.context, intent)
|
||||
|
||||
// Then
|
||||
assertThat(launcherPrefs.has(OLD_APP_WIDGET_IDS, APP_WIDGET_IDS)).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `AppWidgetsRestoredReceiver does not set widget ids when new ids not set`() {
|
||||
// Given
|
||||
val oldIds = intArrayOf(10, 11, 12)
|
||||
val intent =
|
||||
Intent().apply {
|
||||
component = null
|
||||
`package` = TEST_PACKAGE
|
||||
action = ACTION_APPWIDGET_HOST_RESTORED
|
||||
putExtra(EXTRA_APPWIDGET_OLD_IDS, oldIds)
|
||||
putExtra(EXTRA_HOST_ID, APPWIDGET_HOST_ID)
|
||||
}
|
||||
|
||||
// When
|
||||
receiverUnderTest.onReceive(DeviceHelpers.context, intent)
|
||||
|
||||
// Then
|
||||
assertThat(launcherPrefs.has(OLD_APP_WIDGET_IDS, APP_WIDGET_IDS)).isFalse()
|
||||
}
|
||||
}
|
||||
@@ -60,6 +60,7 @@ import com.android.launcher3.LauncherPrefs;
|
||||
import com.android.launcher3.LauncherSettings;
|
||||
import com.android.launcher3.LauncherSettings.Favorites;
|
||||
import com.android.launcher3.model.ModelDbController;
|
||||
import com.android.launcher3.util.IntArray;
|
||||
import com.android.launcher3.util.LauncherModelHelper;
|
||||
|
||||
import org.junit.After;
|
||||
@@ -233,7 +234,7 @@ public class RestoreDbTaskTest {
|
||||
mPrefs.remove(RESTORE_DEVICE);
|
||||
|
||||
// When
|
||||
RestoreDbTask.setRestoredAppWidgetIds(mContext, expectedOldIds, expectedNewIds);
|
||||
setRestoredAppWidgetIds(mContext, expectedOldIds, expectedNewIds);
|
||||
mTask.restoreAppWidgetIdsIfExists(mContext, mMockController);
|
||||
|
||||
// Then
|
||||
@@ -255,7 +256,7 @@ public class RestoreDbTaskTest {
|
||||
RestoreDbTask.setPending(mContext);
|
||||
|
||||
// When
|
||||
RestoreDbTask.setRestoredAppWidgetIds(mContext, expectedOldIds, expectedNewIds);
|
||||
setRestoredAppWidgetIds(mContext, expectedOldIds, expectedNewIds);
|
||||
mTask.restoreAppWidgetIdsIfExists(mContext, mMockController);
|
||||
|
||||
// Then
|
||||
@@ -283,7 +284,7 @@ public class RestoreDbTaskTest {
|
||||
RestoreDbTask.setPending(mContext);
|
||||
|
||||
// When
|
||||
RestoreDbTask.setRestoredAppWidgetIds(mContext, expectedOldIds, expectedNewIds);
|
||||
setRestoredAppWidgetIds(mContext, expectedOldIds, expectedNewIds);
|
||||
mTask.restoreAppWidgetIdsIfExists(mContext, mMockController);
|
||||
|
||||
// Then
|
||||
@@ -402,4 +403,10 @@ public class RestoreDbTaskTest {
|
||||
return index >= 0 ? users.keyAt(index) : -1;
|
||||
}
|
||||
}
|
||||
|
||||
private void setRestoredAppWidgetIds(Context context, int[] oldIds, int[] newIds) {
|
||||
LauncherPrefs.get(context).putSync(
|
||||
OLD_APP_WIDGET_IDS.to(IntArray.wrap(oldIds).toConcatString()),
|
||||
APP_WIDGET_IDS.to(IntArray.wrap(newIds).toConcatString()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user