From cdbe1d645becca3db7707885e962d1a0b9612d93 Mon Sep 17 00:00:00 2001 From: Fan Zhang Date: Tue, 22 Nov 2016 15:47:05 -0800 Subject: Add test for TileUtils. - Verify it skips non-system apps. - Verify it parses category correctly. Bug: 32460089 Test: make RunSettingsLibRoboTests Change-Id: I743309206e1969dee4b1e62146be64eb4d143614 --- .../android/settingslib/drawer/TileUtilsTest.java | 114 +++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java new file mode 100644 index 000000000000..651a7cb69387 --- /dev/null +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java @@ -0,0 +1,114 @@ +/* + * 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.content.Intent; +import android.content.pm.ActivityInfo; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.os.Bundle; +import android.os.UserHandle; +import android.util.ArrayMap; +import android.util.Pair; + +import com.android.settingslib.TestConfig; + +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.annotation.Config; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.when; + +@RunWith(RobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class TileUtilsTest { + + @Mock + private Context mContext; + @Mock + private PackageManager mPackageManager; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(mContext.getPackageManager()).thenReturn(mPackageManager); + } + + @Test + public void getTilesForIntent_shouldParseCategory() { + final String testCategory = "category1"; + Intent intent = new Intent(); + Map, Tile> addedCache = new ArrayMap<>(); + List outTiles = new ArrayList<>(); + List info = new ArrayList<>(); + info.add(newInfo(true, testCategory)); + + when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) + .thenReturn(info); + + TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, + null /* defaultCategory */, outTiles, false /* usePriority */, + false /* checkCategory */); + + assertThat(outTiles.size()).isEqualTo(1); + assertThat(outTiles.get(0).category).isEqualTo(testCategory); + } + + @Test + public void getTilesForIntent_shouldSkipNonSystemApp() { + final String testCategory = "category1"; + Intent intent = new Intent(); + Map, Tile> addedCache = new ArrayMap<>(); + List outTiles = new ArrayList<>(); + List info = new ArrayList<>(); + info.add(newInfo(false, testCategory)); + + when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) + .thenReturn(info); + + TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, + null /* defaultCategory */, outTiles, false /* usePriority */, + false /* checkCategory */); + + assertThat(outTiles.isEmpty()).isTrue(); + } + + private ResolveInfo newInfo(boolean systemApp, String category) { + ResolveInfo info = new ResolveInfo(); + info.system = systemApp; + info.activityInfo = new ActivityInfo(); + info.activityInfo.packageName = "abc"; + info.activityInfo.name = "123"; + info.activityInfo.metaData = new Bundle(); + info.activityInfo.metaData.putString("com.android.settings.category", category); + info.activityInfo.applicationInfo = new ApplicationInfo(); + return info; + } +} -- cgit v1.2.3-59-g8ed1b