• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.slices;
18 
19 import static junit.framework.Assert.fail;
20 
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.platform.test.annotations.Presubmit;
24 import android.provider.SearchIndexableResource;
25 import android.support.test.InstrumentationRegistry;
26 import android.support.test.filters.MediumTest;
27 import android.support.test.runner.AndroidJUnit4;
28 import android.text.TextUtils;
29 import android.util.Log;
30 
31 import com.android.settings.core.PreferenceXmlParserUtils;
32 import com.android.settings.overlay.FeatureFactory;
33 import com.android.settings.search.DatabaseIndexingUtils;
34 import com.android.settings.search.Indexable;
35 import com.android.settings.search.SearchIndexableResources;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.xmlpull.v1.XmlPullParserException;
41 
42 import java.io.IOException;
43 import java.util.HashSet;
44 import java.util.List;
45 import java.util.Set;
46 
47 @RunWith(AndroidJUnit4.class)
48 @MediumTest
49 public class SliceDataContractTest {
50 
51     private static final String TAG = "SliceDataContractTest";
52     private Context mContext;
53 
54     @Before
setUp()55     public void setUp() {
56         mContext = InstrumentationRegistry.getTargetContext();
57     }
58 
59     @Test
60     @Presubmit
preferenceWithControllerMustHaveNonEmptyTitle()61     public void preferenceWithControllerMustHaveNonEmptyTitle()
62             throws IOException, XmlPullParserException {
63         final Set<String> nullTitleFragments = new HashSet<>();
64 
65         final SearchIndexableResources resources =
66                 FeatureFactory.getFactory(mContext).getSearchFeatureProvider()
67                         .getSearchIndexableResources();
68 
69         for (Class<?> clazz : resources.getProviderValues()) {
70             verifyPreferenceTitle(nullTitleFragments, clazz);
71         }
72 
73         if (!nullTitleFragments.isEmpty()) {
74             final StringBuilder error = new StringBuilder(
75                     "All preferences with a controller must have a non-empty title by default, "
76                             + "found empty title in the following fragments\n");
77             for (String c : nullTitleFragments) {
78                 error.append(c).append("\n");
79             }
80             fail(error.toString());
81         }
82     }
83 
verifyPreferenceTitle(Set<String> nullTitleFragments, Class<?> clazz)84     private void verifyPreferenceTitle(Set<String> nullTitleFragments, Class<?> clazz)
85             throws IOException, XmlPullParserException {
86         if (clazz == null) {
87             return;
88         }
89         final String className = clazz.getName();
90         final Indexable.SearchIndexProvider provider =
91                 DatabaseIndexingUtils.getSearchIndexProvider(clazz);
92 
93         final List<SearchIndexableResource> resourcesToIndex =
94                 provider.getXmlResourcesToIndex(mContext, true);
95 
96         if (resourcesToIndex == null) {
97             Log.d(TAG, className + "is not providing SearchIndexableResource, skipping");
98             return;
99         }
100 
101         for (SearchIndexableResource sir : resourcesToIndex) {
102             final List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
103                     sir.xmlResId,
104                     PreferenceXmlParserUtils.MetadataFlag.FLAG_INCLUDE_PREF_SCREEN
105                             | PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_PREF_TITLE
106                             | PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_PREF_CONTROLLER);
107 
108             for (Bundle bundle : metadata) {
109                 final String controller = bundle.getString(
110                         PreferenceXmlParserUtils.METADATA_CONTROLLER);
111                 if (TextUtils.isEmpty(controller)) {
112                     continue;
113                 }
114                 final String title = bundle.getString(PreferenceXmlParserUtils.METADATA_TITLE);
115                 if (TextUtils.isEmpty(title)) {
116                     nullTitleFragments.add(className);
117                 }
118             }
119         }
120     }
121 
122 }