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.text.TextUtils;
26 import android.util.Log;
27 
28 import androidx.test.InstrumentationRegistry;
29 import androidx.test.filters.MediumTest;
30 import androidx.test.runner.AndroidJUnit4;
31 
32 import com.android.settings.core.PreferenceXmlParserUtils;
33 import com.android.settings.overlay.FeatureFactory;
34 import com.android.settingslib.search.Indexable;
35 import com.android.settingslib.search.SearchIndexableData;
36 import com.android.settingslib.search.SearchIndexableResources;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.xmlpull.v1.XmlPullParserException;
42 
43 import java.io.IOException;
44 import java.util.HashSet;
45 import java.util.List;
46 import java.util.Set;
47 
48 @RunWith(AndroidJUnit4.class)
49 @MediumTest
50 public class SliceDataContractTest {
51 
52     private static final String TAG = "SliceDataContractTest";
53     private Context mContext;
54 
55     @Before
setUp()56     public void setUp() {
57         mContext = InstrumentationRegistry.getTargetContext();
58     }
59 
60     @Test
61     @Presubmit
preferenceWithControllerMustHaveNonEmptyTitle()62     public void preferenceWithControllerMustHaveNonEmptyTitle()
63             throws IOException, XmlPullParserException {
64         final Set<String> nullTitleFragments = new HashSet<>();
65 
66         final SearchIndexableResources resources =
67                 FeatureFactory.getFactory(mContext).getSearchFeatureProvider()
68                         .getSearchIndexableResources();
69 
70         for (SearchIndexableData SearchIndexableData : resources.getProviderValues()) {
71             verifyPreferenceTitle(nullTitleFragments, SearchIndexableData);
72         }
73 
74         if (!nullTitleFragments.isEmpty()) {
75             final StringBuilder error = new StringBuilder(
76                     "All preferences with a controller must have a non-empty title by default, "
77                             + "found empty title in the following fragments\n");
78             for (String c : nullTitleFragments) {
79                 error.append(c).append("\n");
80             }
81             fail(error.toString());
82         }
83     }
84 
verifyPreferenceTitle(Set<String> nullTitleFragments, SearchIndexableData searchIndexableData)85     private void verifyPreferenceTitle(Set<String> nullTitleFragments,
86             SearchIndexableData searchIndexableData)
87             throws IOException, XmlPullParserException {
88 
89         final String className = searchIndexableData.getTargetClass().getName();
90         final Indexable.SearchIndexProvider provider =
91                 searchIndexableData.getSearchIndexProvider();
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 }