• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 
18 package com.android.settings.search;
19 
20 import android.content.res.XmlResourceParser;
21 import android.content.Context;
22 import android.util.AttributeSet;
23 import android.util.Xml;
24 
25 import com.android.settings.SettingsRobolectricTestRunner;
26 import com.android.settings.TestConfig;
27 import com.android.settings.R;
28 
29 import com.android.settings.search2.XmlParserUtils;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.robolectric.annotation.Config;
34 import org.robolectric.shadows.ShadowApplication;
35 import org.xmlpull.v1.XmlPullParser;
36 
37 import static com.google.common.truth.Truth.assertThat;
38 
39 /**
40  * These tests use a series of preferences that have specific attributes which are sometimes
41  * uncommon (such as summaryOn).
42  *
43  * If changing a preference file breaks a test in this test file, please replace its reference
44  * with another preference with a matchin replacement attribute.
45  */
46 @RunWith(SettingsRobolectricTestRunner.class)
47 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
48 public class XmlParserUtilTest {
49 
50     private Context mContext;
51 
52     @Before
setUp()53     public void setUp() {
54         mContext = ShadowApplication.getInstance().getApplicationContext();
55     }
56 
57     @Test
testDataTitleValid_ReturnsPreferenceTitle()58     public void testDataTitleValid_ReturnsPreferenceTitle() {
59         XmlResourceParser parser = getChildByType(R.xml.display_settings,
60                 "com.android.settings.TimeoutListPreference");
61         final AttributeSet attrs = Xml.asAttributeSet(parser);
62         String title = XmlParserUtils.getDataTitle(mContext, attrs);
63         String expTitle = mContext.getString(R.string.screen_timeout);
64         assertThat(title).isEqualTo(expTitle);
65     }
66 
67     @Test
testDataKeywordsValid_ReturnsPreferenceKeywords()68     public void testDataKeywordsValid_ReturnsPreferenceKeywords() {
69         XmlResourceParser parser = getParentPrimedParser(R.xml.display_settings);
70         final AttributeSet attrs = Xml.asAttributeSet(parser);
71         String keywords = XmlParserUtils.getDataKeywords(mContext, attrs);
72         String expKeywords = mContext.getString(R.string.keywords_display);
73         assertThat(keywords).isEqualTo(expKeywords);
74     }
75 
76     @Test
testDataKeyValid_ReturnsPreferenceKey()77     public void testDataKeyValid_ReturnsPreferenceKey() {
78         XmlResourceParser parser = getChildByType(R.xml.display_settings,
79                 "com.android.settings.TimeoutListPreference");
80         final AttributeSet attrs = Xml.asAttributeSet(parser);
81         String key = XmlParserUtils.getDataKey(mContext, attrs);
82         String expKey = "screen_timeout";
83         assertThat(key).isEqualTo(expKey);
84     }
85 
86     @Test
testDataSummaryValid_ReturnsPreferenceSummary()87     public void testDataSummaryValid_ReturnsPreferenceSummary() {
88         XmlResourceParser parser = getChildByType(R.xml.display_settings,
89                 "com.android.settings.TimeoutListPreference");
90         final AttributeSet attrs = Xml.asAttributeSet(parser);
91         String summary = XmlParserUtils.getDataSummary(mContext, attrs);
92         String expSummary = mContext.getString(R.string.summary_placeholder);
93         assertThat(summary).isEqualTo(expSummary);
94 
95     }
96 
97     @Test
testDataSummaryOnValid_ReturnsPreferenceSummaryOn()98     public void testDataSummaryOnValid_ReturnsPreferenceSummaryOn() {
99         XmlResourceParser parser = getChildByType(R.xml.application_settings, "CheckBoxPreference");
100         final AttributeSet attrs = Xml.asAttributeSet(parser);
101         String summary = XmlParserUtils.getDataSummaryOn(mContext, attrs);
102         String expSummary = mContext.getString(R.string.advanced_settings_summary);
103         assertThat(summary).isEqualTo(expSummary);
104     }
105 
106     @Test
testDataSummaryOffValid_ReturnsPreferenceSummaryOff()107     public void testDataSummaryOffValid_ReturnsPreferenceSummaryOff() {
108         XmlResourceParser parser = getChildByType(R.xml.application_settings, "CheckBoxPreference");
109         final AttributeSet attrs = Xml.asAttributeSet(parser);
110         String summary = XmlParserUtils.getDataSummaryOff(mContext, attrs);
111         String expSummary = mContext.getString(R.string.advanced_settings_summary);
112         assertThat(summary).isEqualTo(expSummary);
113     }
114 
115     @Test
testDataEntriesValid_ReturnsPreferenceEntries()116     public void testDataEntriesValid_ReturnsPreferenceEntries() {
117         XmlResourceParser parser = getChildByType(R.xml.application_settings, "ListPreference");
118         final AttributeSet attrs = Xml.asAttributeSet(parser);
119         String entries = XmlParserUtils.getDataEntries(mContext, attrs);
120         String[] expEntries = mContext.getResources()
121                 .getStringArray(R.array.app_install_location_entries);
122         for (int i = 0; i < expEntries.length; i++) {
123             assertThat(entries).contains(expEntries[i]);
124         }
125     }
126 
127     // Null checks
128 
129     @Test
testDataKeyInvalid_ReturnsNull()130     public void testDataKeyInvalid_ReturnsNull() {
131         XmlResourceParser parser = getParentPrimedParser(R.xml.display_settings);
132         final AttributeSet attrs = Xml.asAttributeSet(parser);
133         String key = XmlParserUtils.getDataKey(mContext, attrs);
134         assertThat(key).isNull();
135     }
136 
137     @Test
testDataSummaryInvalid_ReturnsNull()138     public void testDataSummaryInvalid_ReturnsNull() {
139         XmlResourceParser parser = getParentPrimedParser(R.xml.display_settings);
140         final AttributeSet attrs = Xml.asAttributeSet(parser);
141         String summary = XmlParserUtils.getDataSummary(mContext, attrs);
142         assertThat(summary).isNull();
143     }
144 
145     @Test
testDataSummaryOffInvalid_ReturnsNull()146     public void testDataSummaryOffInvalid_ReturnsNull() {
147         XmlResourceParser parser = getParentPrimedParser(R.xml.display_settings);
148         final AttributeSet attrs = Xml.asAttributeSet(parser);
149         String summaryOff = XmlParserUtils.getDataSummaryOff(mContext, attrs);
150         assertThat(summaryOff).isNull();
151     }
152 
153     @Test
testDataEntriesInvalid_ReturnsNull()154     public void testDataEntriesInvalid_ReturnsNull() {
155         XmlResourceParser parser = getParentPrimedParser(R.xml.display_settings);
156         final AttributeSet attrs = Xml.asAttributeSet(parser);
157         String entries = XmlParserUtils.getDataEntries(mContext, attrs);
158         assertThat(entries).isNull();
159     }
160 
161     /**
162      * @param resId the ID for the XML preference
163      * @return an XML resource parser that points to the start tag
164      */
getParentPrimedParser(int resId)165     private XmlResourceParser getParentPrimedParser(int resId) {
166         XmlResourceParser parser = null;
167         try {
168             parser = mContext.getResources().getXml(resId);
169 
170             int type;
171             while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
172                     && type != XmlPullParser.START_TAG) {
173             }
174         } catch (Exception e) {
175 
176         }
177         return parser;
178     }
179 
getChildByType(int resId, String xmlType)180     private XmlResourceParser getChildByType(int resId, String xmlType) {
181         XmlResourceParser parser = null;
182         try {
183             parser = mContext.getResources().getXml(resId);
184 
185             int type;
186             while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
187                     && type != XmlPullParser.START_TAG) {
188             }
189             while(parser.getName() != xmlType) {
190                 parser.next();
191             }
192         } catch (Exception e) {
193 
194         }
195         return parser;
196     }
197 }
198