diff options
16 files changed, 297 insertions, 22 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/drawer/CategoryManager.java b/packages/SettingsLib/src/com/android/settingslib/drawer/CategoryManager.java index 03130899452b..ed411be9c3e4 100644 --- a/packages/SettingsLib/src/com/android/settingslib/drawer/CategoryManager.java +++ b/packages/SettingsLib/src/com/android/settingslib/drawer/CategoryManager.java @@ -17,6 +17,7 @@ package com.android.settingslib.drawer; import android.content.ComponentName; import android.content.Context; +import android.support.annotation.VisibleForTesting; import android.util.ArrayMap; import android.util.Log; import android.util.Pair; @@ -109,18 +110,21 @@ public class CategoryManager { for (DashboardCategory category : mCategories) { mCategoryByKeyMap.put(category.key, category); } - backwardCompatCleanupForCategory(); + backwardCompatCleanupForCategory(mTileByComponentCache, mCategoryByKeyMap); } } - private synchronized void backwardCompatCleanupForCategory() { + @VisibleForTesting + synchronized void backwardCompatCleanupForCategory( + Map<Pair<String, String>, Tile> tileByComponentCache, + Map<String, DashboardCategory> categoryByKeyMap) { // A package can use a) CategoryKey, b) old category keys, c) both. // Check if a package uses old category key only. // If yes, map them to new category key. // Build a package name -> tile map first. final Map<String, List<Tile>> packageToTileMap = new HashMap<>(); - for (Entry<Pair<String, String>, Tile> tileEntry : mTileByComponentCache.entrySet()) { + for (Entry<Pair<String, String>, Tile> tileEntry : tileByComponentCache.entrySet()) { final String packageName = tileEntry.getKey().first; List<Tile> tiles = packageToTileMap.get(packageName); if (tiles == null) { @@ -149,7 +153,11 @@ public class CategoryManager { final String newCategoryKey = CategoryKey.KEY_COMPAT_MAP.get(tile.category); tile.category = newCategoryKey; // move tile to new category. - final DashboardCategory newCategory = mCategoryByKeyMap.get(newCategoryKey); + DashboardCategory newCategory = categoryByKeyMap.get(newCategoryKey); + if (newCategory == null) { + newCategory = new DashboardCategory(); + categoryByKeyMap.put(newCategoryKey, newCategory); + } newCategory.tiles.add(tile); } } diff --git a/packages/SettingsLib/tests/Android.mk b/packages/SettingsLib/tests/Android.mk index 42085225dd91..333c41d33f40 100644 --- a/packages/SettingsLib/tests/Android.mk +++ b/packages/SettingsLib/tests/Android.mk @@ -1,4 +1,4 @@ -# Copyright (C) 2015 The Android Open Source Project +# Copyright (C) 2016 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. @@ -15,20 +15,5 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) -LOCAL_MODULE_TAGS := tests -LOCAL_CERTIFICATE := platform - -LOCAL_SRC_FILES := $(call all-java-files-under, src) - -LOCAL_JAVA_LIBRARIES := android.test.runner telephony-common - -LOCAL_PACKAGE_NAME := SettingsLibTests - -LOCAL_STATIC_JAVA_LIBRARIES := \ - android-support-test \ - espresso-core \ - mockito-target-minus-junit4 - -include frameworks/base/packages/SettingsLib/common.mk - -include $(BUILD_PACKAGE) +# Include all makefiles in subdirectories +include $(call all-makefiles-under,$(LOCAL_PATH)) diff --git a/packages/SettingsLib/tests/integ/Android.mk b/packages/SettingsLib/tests/integ/Android.mk new file mode 100644 index 000000000000..42085225dd91 --- /dev/null +++ b/packages/SettingsLib/tests/integ/Android.mk @@ -0,0 +1,34 @@ +# Copyright (C) 2015 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. + +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := tests +LOCAL_CERTIFICATE := platform + +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_JAVA_LIBRARIES := android.test.runner telephony-common + +LOCAL_PACKAGE_NAME := SettingsLibTests + +LOCAL_STATIC_JAVA_LIBRARIES := \ + android-support-test \ + espresso-core \ + mockito-target-minus-junit4 + +include frameworks/base/packages/SettingsLib/common.mk + +include $(BUILD_PACKAGE) diff --git a/packages/SettingsLib/tests/AndroidManifest.xml b/packages/SettingsLib/tests/integ/AndroidManifest.xml index 00b21649959b..00b21649959b 100644 --- a/packages/SettingsLib/tests/AndroidManifest.xml +++ b/packages/SettingsLib/tests/integ/AndroidManifest.xml diff --git a/packages/SettingsLib/tests/src/com/android/settingslib/BaseTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/BaseTest.java index 04a568ed4c72..04a568ed4c72 100644 --- a/packages/SettingsLib/tests/src/com/android/settingslib/BaseTest.java +++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/BaseTest.java diff --git a/packages/SettingsLib/tests/src/com/android/settingslib/drawer/ProfileSelectDialogTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/drawer/ProfileSelectDialogTest.java index ac2d759f9f17..ac2d759f9f17 100644 --- a/packages/SettingsLib/tests/src/com/android/settingslib/drawer/ProfileSelectDialogTest.java +++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/drawer/ProfileSelectDialogTest.java diff --git a/packages/SettingsLib/tests/src/com/android/settingslib/drawer/SettingsDrawerActivityTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/drawer/SettingsDrawerActivityTest.java index 1e87ea0f18ef..1e87ea0f18ef 100644 --- a/packages/SettingsLib/tests/src/com/android/settingslib/drawer/SettingsDrawerActivityTest.java +++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/drawer/SettingsDrawerActivityTest.java diff --git a/packages/SettingsLib/tests/src/com/android/settingslib/users/AppRestrictionsHelperTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/users/AppRestrictionsHelperTest.java index 3f989bd20065..3f989bd20065 100644 --- a/packages/SettingsLib/tests/src/com/android/settingslib/users/AppRestrictionsHelperTest.java +++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/users/AppRestrictionsHelperTest.java diff --git a/packages/SettingsLib/tests/src/com/android/settingslib/utils/NetworkPolicyEditorTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/utils/NetworkPolicyEditorTest.java index ee03d5024351..ee03d5024351 100644 --- a/packages/SettingsLib/tests/src/com/android/settingslib/utils/NetworkPolicyEditorTest.java +++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/utils/NetworkPolicyEditorTest.java diff --git a/packages/SettingsLib/tests/src/com/android/settingslib/utils/ZoneGetterTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/utils/ZoneGetterTest.java index 57e06ddb5c04..57e06ddb5c04 100644 --- a/packages/SettingsLib/tests/src/com/android/settingslib/utils/ZoneGetterTest.java +++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/utils/ZoneGetterTest.java diff --git a/packages/SettingsLib/tests/src/com/android/settingslib/wifi/AccessPointTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/AccessPointTest.java index 6481f4d6531d..6481f4d6531d 100644 --- a/packages/SettingsLib/tests/src/com/android/settingslib/wifi/AccessPointTest.java +++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/AccessPointTest.java diff --git a/packages/SettingsLib/tests/src/com/android/settingslib/wifi/WifiTrackerTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/WifiTrackerTest.java index c650190b591f..c650190b591f 100644 --- a/packages/SettingsLib/tests/src/com/android/settingslib/wifi/WifiTrackerTest.java +++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/WifiTrackerTest.java diff --git a/packages/SettingsLib/tests/robotests/Android.mk b/packages/SettingsLib/tests/robotests/Android.mk new file mode 100644 index 000000000000..c1108f6fcc91 --- /dev/null +++ b/packages/SettingsLib/tests/robotests/Android.mk @@ -0,0 +1,82 @@ +# Copyright (C) 2016 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. + + +############################################################ +# SettingsLib Shell app just for Robolectric test target. # +############################################################ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_PACKAGE_NAME := SettingsLibShell +LOCAL_MODULE_TAGS := optional + +LOCAL_PRIVILEGED_MODULE := true + +LOCAL_JAVA_LIBRARIES := \ + junit4-target \ + platform-robolectric-prebuilt + +LOCAL_STATIC_JAVA_LIBRARIES := \ + platform-system-robolectric \ + truth-prebuilt + +LOCAL_AAPT_FLAGS := --auto-add-overlay \ + +LOCAL_SRC_FILES := \ + $(call all-java-files-under, src) + +include frameworks/base/packages/SettingsLib/common.mk + +include $(BUILD_PACKAGE) + +############################################# +# SettingsLib Robolectric test target. # +############################################# +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +# Include the testing libraries (JUnit4 + Robolectric libs). +LOCAL_STATIC_JAVA_LIBRARIES := \ + platform-system-robolectric \ + truth-prebuilt + +LOCAL_JAVA_LIBRARIES := \ + junit4-target \ + platform-robolectric-prebuilt + +LOCAL_INSTRUMENTATION_FOR := SettingsLibShell +LOCAL_MODULE := SettingsLibRoboTests + +LOCAL_MODULE_TAGS := optional + +include $(BUILD_STATIC_JAVA_LIBRARY) + +############################################################# +# SettingsLib runner target to run the previous target. # +############################################################# +include $(CLEAR_VARS) + +LOCAL_MODULE := RunSettingsLibRoboTests + +LOCAL_SDK_VERSION := current + +LOCAL_STATIC_JAVA_LIBRARIES := \ + SettingsLibRoboTests + +LOCAL_TEST_PACKAGE := SettingsLibShell + +include prebuilts/misc/common/robolectric/run_robotests.mk diff --git a/packages/SettingsLib/tests/robotests/AndroidManifest.xml b/packages/SettingsLib/tests/robotests/AndroidManifest.xml new file mode 100644 index 000000000000..9e92fa9d3853 --- /dev/null +++ b/packages/SettingsLib/tests/robotests/AndroidManifest.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2016 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. + --> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + coreApp="true" + package="com.android.settingslib.robotests"> + + <application/> + +</manifest> diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/TestConfig.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/TestConfig.java new file mode 100644 index 000000000000..22fd83c67667 --- /dev/null +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/TestConfig.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2016 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.settingslib; + +public class TestConfig { + public static final int SDK_VERSION = 23; + public static final String MANIFEST_PATH = + "frameworks/base/packages/SettingsLib/robotests/AndroidManifest.xml"; +} diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/CategoryManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/CategoryManagerTest.java new file mode 100644 index 000000000000..380f6226512a --- /dev/null +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/CategoryManagerTest.java @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2016 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.settingslib.drawer; + +import android.content.Context; +import android.util.Pair; + +import com.android.settingslib.TestConfig; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowApplication; + +import java.util.HashMap; +import java.util.Map; + +import static com.google.common.truth.Truth.assertThat; + +@RunWith(RobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class CategoryManagerTest { + + private Context mContext; + private CategoryManager mCategoryManager; + private Map<Pair<String, String>, Tile> mTileByComponentCache; + private Map<String, DashboardCategory> mCategoryByKeyMap; + + @Before + public void setUp() { + mContext = ShadowApplication.getInstance().getApplicationContext(); + mTileByComponentCache = new HashMap<>(); + mCategoryByKeyMap = new HashMap<>(); + mCategoryManager = CategoryManager.get(mContext); + } + + @Test + public void getInstance_shouldBeSingleton() { + assertThat(mCategoryManager).isSameAs(CategoryManager.get(mContext)); + } + + @Test + public void backwardCompatCleanupForCategory_shouldNotChangeCategoryForNewKeys() { + final Tile tile1 = new Tile(); + final Tile tile2 = new Tile(); + tile1.category = CategoryKey.CATEGORY_ACCOUNT; + tile2.category = CategoryKey.CATEGORY_ACCOUNT; + final DashboardCategory category = new DashboardCategory(); + category.addTile(tile1); + category.addTile(tile2); + mCategoryByKeyMap.put(CategoryKey.CATEGORY_ACCOUNT, category); + mTileByComponentCache.put(new Pair<>("PACKAGE", "1"), tile1); + mTileByComponentCache.put(new Pair<>("PACKAGE", "2"), tile2); + + mCategoryManager.backwardCompatCleanupForCategory(mTileByComponentCache, mCategoryByKeyMap); + + assertThat(mCategoryByKeyMap.size()).isEqualTo(1); + assertThat(mCategoryByKeyMap.get(CategoryKey.CATEGORY_ACCOUNT)).isNotNull(); + } + + @Test + public void backwardCompatCleanupForCategory_shouldNotChangeCategoryForMixedKeys() { + final Tile tile1 = new Tile(); + final Tile tile2 = new Tile(); + final String oldCategory = "com.android.settings.category.wireless"; + tile1.category = CategoryKey.CATEGORY_ACCOUNT; + tile2.category = oldCategory; + final DashboardCategory category1 = new DashboardCategory(); + category1.addTile(tile1); + final DashboardCategory category2 = new DashboardCategory(); + category2.addTile(tile2); + mCategoryByKeyMap.put(CategoryKey.CATEGORY_ACCOUNT, category1); + mCategoryByKeyMap.put(oldCategory, category2); + mTileByComponentCache.put(new Pair<>("PACKAGE", "CLASS1"), tile1); + mTileByComponentCache.put(new Pair<>("PACKAGE", "CLASS2"), tile2); + + mCategoryManager.backwardCompatCleanupForCategory(mTileByComponentCache, mCategoryByKeyMap); + + assertThat(mCategoryByKeyMap.size()).isEqualTo(2); + assertThat(mCategoryByKeyMap.get(CategoryKey.CATEGORY_ACCOUNT).tiles.size()).isEqualTo(1); + assertThat(mCategoryByKeyMap.get(oldCategory).tiles.size()).isEqualTo(1); + } + + @Test + public void backwardCompatCleanupForCategory_shouldChangeCategoryForOldKeys() { + final Tile tile1 = new Tile(); + final String oldCategory = "com.android.settings.category.wireless"; + tile1.category = oldCategory; + final DashboardCategory category1 = new DashboardCategory(); + category1.addTile(tile1); + mCategoryByKeyMap.put(oldCategory, category1); + mTileByComponentCache.put(new Pair<>("PACKAGE", "CLASS1"), tile1); + + mCategoryManager.backwardCompatCleanupForCategory(mTileByComponentCache, mCategoryByKeyMap); + + // Added 1 more category to category map. + assertThat(mCategoryByKeyMap.size()).isEqualTo(2); + // The new category map has CATEGORY_NETWORK type now, which contains 1 tile. + assertThat(mCategoryByKeyMap.get(CategoryKey.CATEGORY_NETWORK).tiles.size()).isEqualTo(1); + // Old category still exists. + assertThat(mCategoryByKeyMap.get(oldCategory).tiles.size()).isEqualTo(1); + } +} |