• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.suggestions;
18 
19 import static com.android.settings.TestConfig.MANIFEST_PATH;
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import android.annotation.StringRes;
23 import android.content.Context;
24 
25 import com.android.settings.R;
26 import com.android.settings.TestConfig;
27 import com.android.settings.fingerprint.FingerprintSuggestionActivity;
28 import com.android.settings.testutils.SettingsRobolectricTestRunner;
29 
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.robolectric.RuntimeEnvironment;
33 import org.robolectric.annotation.Config;
34 import org.robolectric.manifest.ActivityData;
35 import org.robolectric.manifest.AndroidManifest;
36 import org.robolectric.manifest.IntentFilterData;
37 import org.robolectric.shadows.ShadowApplication;
38 
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.Map;
42 
43 @RunWith(SettingsRobolectricTestRunner.class)
44 @Config(manifest = MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
45 public class SettingsSuggestionsTest {
46 
47     private static final String CATEGORY_FIRST_IMPRESSION =
48             "com.android.settings.suggested.category.FIRST_IMPRESSION";
49 
50     private static final String CATEGORY_SETTINGS_ONLY =
51             "com.android.settings.suggested.category.SETTINGS_ONLY";
52 
53     @Test
wallpaperSuggestion_isValid()54     public void wallpaperSuggestion_isValid() {
55         assertSuggestionEquals("com.android.settings.wallpaper.WallpaperSuggestionActivity",
56                 CATEGORY_FIRST_IMPRESSION,
57                 R.string.wallpaper_suggestion_title, R.string.wallpaper_suggestion_summary);
58     }
59 
60     @Test
fingerprintSuggestion_isValid()61     public void fingerprintSuggestion_isValid() {
62         assertSuggestionEquals(
63                 FingerprintSuggestionActivity.class.getName(),
64                 CATEGORY_FIRST_IMPRESSION,
65                 R.string.suggestion_additional_fingerprints,
66                 R.string.suggestion_additional_fingerprints_summary);
67     }
68 
69     @Test
wifiCallingSuggestion_isValid()70     public void wifiCallingSuggestion_isValid() {
71         assertSuggestionEquals("Settings$WifiCallingSuggestionActivity",
72                 CATEGORY_FIRST_IMPRESSION,
73                 R.string.wifi_calling_suggestion_title, R.string.wifi_calling_suggestion_summary);
74     }
75 
76     @Test
nightDisplaySuggestion_isValid()77     public void nightDisplaySuggestion_isValid() {
78         assertSuggestionEquals("Settings$NightDisplaySuggestionActivity",
79             CATEGORY_FIRST_IMPRESSION,
80             R.string.night_display_suggestion_title, R.string.night_display_suggestion_summary);
81     }
82 
assertSuggestionEquals(String activityName, String category, @StringRes int title, @StringRes int summary)83     private void assertSuggestionEquals(String activityName, String category, @StringRes int title,
84             @StringRes int summary) {
85         final AndroidManifest androidManifest = ShadowApplication.getInstance().getAppManifest();
86         final ActivityData activityData = androidManifest.getActivityData(activityName);
87         final Map<String, Object> metaData = activityData.getMetaData().getValueMap();
88         final Context context = RuntimeEnvironment.application;
89         final String expectedTitle = context.getString(title);
90         final String expectedSummary = context.getString(summary);
91 
92         final String pName = context.getPackageName();
93         final String actualTitle = context.getString(context.getResources().getIdentifier(
94                 ((String) metaData.get("com.android.settings.title")).substring(8), "string",
95                 pName));
96         final String actualSummary = context.getString(context.getResources().getIdentifier(
97                 ((String) metaData.get("com.android.settings.summary")).substring(8), "string",
98                 pName));
99         assertThat(actualTitle).isEqualTo(expectedTitle);
100         assertThat(actualSummary).isEqualTo(expectedSummary);
101 
102         final List<IntentFilterData> intentFilters = activityData.getIntentFilters();
103         final List<String> categories = new ArrayList<>();
104         for (IntentFilterData intentFilter : intentFilters) {
105             categories.addAll(intentFilter.getCategories());
106         }
107 
108         assertThat(categories).contains(category);
109     }
110 }
111