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.slices;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.net.Uri;
22 
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.robolectric.RobolectricTestRunner;
26 
27 @RunWith(RobolectricTestRunner.class)
28 public class SliceDataTest {
29 
30     private final String KEY = "KEY";
31     private final String TITLE = "title";
32     private final String SUMMARY = "summary";
33     private final String SCREEN_TITLE = "screen title";
34     private final String KEYWORDS = "a, b, c";
35     private final String FRAGMENT_NAME = "fragment name";
36     private final int ICON = 1234; // I declare a thumb war
37     private final Uri URI = Uri.parse("content://com.android.settings.slices/test");
38     private final String PREF_CONTROLLER = "com.android.settings.slices.tester";
39     private final int SLICE_TYPE = SliceData.SliceType.SWITCH;
40     private final String UNAVAILABLE_SLICE_SUBTITLE = "subtitleOfUnavailableSlice";
41 
42     @Test
testBuilder_buildsMatchingObject()43     public void testBuilder_buildsMatchingObject() {
44         SliceData.Builder builder = new SliceData.Builder()
45                 .setKey(KEY)
46                 .setTitle(TITLE)
47                 .setSummary(SUMMARY)
48                 .setScreenTitle(SCREEN_TITLE)
49                 .setKeywords(KEYWORDS)
50                 .setIcon(ICON)
51                 .setFragmentName(FRAGMENT_NAME)
52                 .setUri(URI)
53                 .setPreferenceControllerClassName(PREF_CONTROLLER)
54                 .setSliceType(SLICE_TYPE)
55                 .setUnavailableSliceSubtitle(UNAVAILABLE_SLICE_SUBTITLE)
56                 .setIsPublicSlice(true);
57 
58         SliceData data = builder.build();
59 
60         assertThat(data.getKey()).isEqualTo(KEY);
61         assertThat(data.getTitle()).isEqualTo(TITLE);
62         assertThat(data.getSummary()).isEqualTo(SUMMARY);
63         assertThat(data.getScreenTitle()).isEqualTo(SCREEN_TITLE);
64         assertThat(data.getKeywords()).isEqualTo(KEYWORDS);
65         assertThat(data.getIconResource()).isEqualTo(ICON);
66         assertThat(data.getFragmentClassName()).isEqualTo(FRAGMENT_NAME);
67         assertThat(data.getUri()).isEqualTo(URI);
68         assertThat(data.getPreferenceController()).isEqualTo(PREF_CONTROLLER);
69         assertThat(data.getSliceType()).isEqualTo(SLICE_TYPE);
70         assertThat(data.getUnavailableSliceSubtitle()).isEqualTo(UNAVAILABLE_SLICE_SUBTITLE);
71         assertThat(data.isPublicSlice()).isEqualTo(true);
72     }
73 
74     @Test(expected = SliceData.InvalidSliceDataException.class)
testBuilder_noKey_throwsIllegalStateException()75     public void testBuilder_noKey_throwsIllegalStateException() {
76         new SliceData.Builder()
77                 .setTitle(TITLE)
78                 .setSummary(SUMMARY)
79                 .setScreenTitle(SCREEN_TITLE)
80                 .setIcon(ICON)
81                 .setFragmentName(FRAGMENT_NAME)
82                 .setUri(URI)
83                 .setPreferenceControllerClassName(PREF_CONTROLLER)
84                 .build();
85     }
86 
87     @Test(expected = SliceData.InvalidSliceDataException.class)
testBuilder_noTitle_throwsIllegalStateException()88     public void testBuilder_noTitle_throwsIllegalStateException() {
89         new SliceData.Builder()
90                 .setKey(KEY)
91                 .setSummary(SUMMARY)
92                 .setScreenTitle(SCREEN_TITLE)
93                 .setIcon(ICON)
94                 .setFragmentName(FRAGMENT_NAME)
95                 .setUri(URI)
96                 .setPreferenceControllerClassName(PREF_CONTROLLER)
97                 .build();
98     }
99 
100     @Test(expected = SliceData.InvalidSliceDataException.class)
testBuilder_noFragment_throwsIllegalStateException()101     public void testBuilder_noFragment_throwsIllegalStateException() {
102         new SliceData.Builder()
103                 .setKey(KEY)
104                 .setFragmentName(FRAGMENT_NAME)
105                 .setSummary(SUMMARY)
106                 .setScreenTitle(SCREEN_TITLE)
107                 .setIcon(ICON)
108                 .setUri(URI)
109                 .setPreferenceControllerClassName(PREF_CONTROLLER)
110                 .build();
111     }
112 
113     @Test(expected = SliceData.InvalidSliceDataException.class)
testBuilder_noPrefController_throwsIllegalStateException()114     public void testBuilder_noPrefController_throwsIllegalStateException() {
115         new SliceData.Builder()
116                 .setKey(KEY)
117                 .setTitle(TITLE)
118                 .setSummary(SUMMARY)
119                 .setScreenTitle(SCREEN_TITLE)
120                 .setIcon(ICON)
121                 .setUri(URI)
122                 .setFragmentName(FRAGMENT_NAME)
123                 .build();
124     }
125 
126     @Test
testBuilder_noSubtitle_buildsMatchingObject()127     public void testBuilder_noSubtitle_buildsMatchingObject() {
128         SliceData.Builder builder = new SliceData.Builder()
129                 .setKey(KEY)
130                 .setTitle(TITLE)
131                 .setScreenTitle(SCREEN_TITLE)
132                 .setIcon(ICON)
133                 .setFragmentName(FRAGMENT_NAME)
134                 .setUri(URI)
135                 .setPreferenceControllerClassName(PREF_CONTROLLER);
136 
137         SliceData data = builder.build();
138 
139         assertThat(data.getKey()).isEqualTo(KEY);
140         assertThat(data.getTitle()).isEqualTo(TITLE);
141         assertThat(data.getSummary()).isNull();
142         assertThat(data.getScreenTitle()).isEqualTo(SCREEN_TITLE);
143         assertThat(data.getIconResource()).isEqualTo(ICON);
144         assertThat(data.getFragmentClassName()).isEqualTo(FRAGMENT_NAME);
145         assertThat(data.getUri()).isEqualTo(URI);
146         assertThat(data.getPreferenceController()).isEqualTo(PREF_CONTROLLER);
147     }
148 
149     @Test
testBuilder_noScreenTitle_buildsMatchingObject()150     public void testBuilder_noScreenTitle_buildsMatchingObject() {
151         SliceData.Builder builder = new SliceData.Builder()
152                 .setKey(KEY)
153                 .setTitle(TITLE)
154                 .setSummary(SUMMARY)
155                 .setIcon(ICON)
156                 .setFragmentName(FRAGMENT_NAME)
157                 .setUri(URI)
158                 .setPreferenceControllerClassName(PREF_CONTROLLER);
159 
160         SliceData data = builder.build();
161 
162         assertThat(data.getKey()).isEqualTo(KEY);
163         assertThat(data.getTitle()).isEqualTo(TITLE);
164         assertThat(data.getSummary()).isEqualTo(SUMMARY);
165         assertThat(data.getScreenTitle()).isNull();
166         assertThat(data.getIconResource()).isEqualTo(ICON);
167         assertThat(data.getFragmentClassName()).isEqualTo(FRAGMENT_NAME);
168         assertThat(data.getUri()).isEqualTo(URI);
169         assertThat(data.getPreferenceController()).isEqualTo(PREF_CONTROLLER);
170     }
171 
172     @Test
testBuilder_noIcon_buildsMatchingObject()173     public void testBuilder_noIcon_buildsMatchingObject() {
174         SliceData.Builder builder = new SliceData.Builder()
175                 .setKey(KEY)
176                 .setTitle(TITLE)
177                 .setSummary(SUMMARY)
178                 .setScreenTitle(SCREEN_TITLE)
179                 .setFragmentName(FRAGMENT_NAME)
180                 .setUri(URI)
181                 .setPreferenceControllerClassName(PREF_CONTROLLER);
182 
183         SliceData data = builder.build();
184 
185         assertThat(data.getKey()).isEqualTo(KEY);
186         assertThat(data.getTitle()).isEqualTo(TITLE);
187         assertThat(data.getSummary()).isEqualTo(SUMMARY);
188         assertThat(data.getScreenTitle()).isEqualTo(SCREEN_TITLE);
189         assertThat(data.getIconResource()).isEqualTo(0);
190         assertThat(data.getFragmentClassName()).isEqualTo(FRAGMENT_NAME);
191         assertThat(data.getUri()).isEqualTo(URI);
192         assertThat(data.getPreferenceController()).isEqualTo(PREF_CONTROLLER);
193     }
194 
195     @Test
testBuilder_noUri_buildsMatchingObject()196     public void testBuilder_noUri_buildsMatchingObject() {
197         SliceData.Builder builder = new SliceData.Builder()
198                 .setKey(KEY)
199                 .setTitle(TITLE)
200                 .setSummary(SUMMARY)
201                 .setScreenTitle(SCREEN_TITLE)
202                 .setIcon(ICON)
203                 .setFragmentName(FRAGMENT_NAME)
204                 .setUri(null)
205                 .setPreferenceControllerClassName(PREF_CONTROLLER);
206 
207         SliceData data = builder.build();
208 
209         assertThat(data.getKey()).isEqualTo(KEY);
210         assertThat(data.getTitle()).isEqualTo(TITLE);
211         assertThat(data.getSummary()).isEqualTo(SUMMARY);
212         assertThat(data.getScreenTitle()).isEqualTo(SCREEN_TITLE);
213         assertThat(data.getIconResource()).isEqualTo(ICON);
214         assertThat(data.getFragmentClassName()).isEqualTo(FRAGMENT_NAME);
215         assertThat(data.getUri()).isNull();
216         assertThat(data.getPreferenceController()).isEqualTo(PREF_CONTROLLER);
217     }
218 
219     @Test
testEquality_identicalObjects()220     public void testEquality_identicalObjects() {
221         SliceData.Builder builder = new SliceData.Builder()
222                 .setKey(KEY)
223                 .setTitle(TITLE)
224                 .setSummary(SUMMARY)
225                 .setScreenTitle(SCREEN_TITLE)
226                 .setIcon(ICON)
227                 .setFragmentName(FRAGMENT_NAME)
228                 .setUri(URI)
229                 .setPreferenceControllerClassName(PREF_CONTROLLER);
230 
231         SliceData dataOne = builder.build();
232         SliceData dataTwo = builder.build();
233 
234         assertThat(dataOne.hashCode()).isEqualTo(dataTwo.hashCode());
235         assertThat(dataOne).isEqualTo(dataTwo);
236     }
237 
238     @Test
testEquality_matchingKey_EqualObjects()239     public void testEquality_matchingKey_EqualObjects() {
240         SliceData.Builder builder = new SliceData.Builder()
241                 .setKey(KEY)
242                 .setTitle(TITLE)
243                 .setSummary(SUMMARY)
244                 .setScreenTitle(SCREEN_TITLE)
245                 .setIcon(ICON)
246                 .setFragmentName(FRAGMENT_NAME)
247                 .setUri(URI)
248                 .setPreferenceControllerClassName(PREF_CONTROLLER);
249 
250         SliceData dataOne = builder.build();
251 
252         builder.setTitle(TITLE + " diff")
253                 .setSummary(SUMMARY + " diff")
254                 .setScreenTitle(SCREEN_TITLE + " diff")
255                 .setIcon(ICON + 1)
256                 .setFragmentName(FRAGMENT_NAME + " diff")
257                 .setUri(URI)
258                 .setPreferenceControllerClassName(PREF_CONTROLLER + " diff");
259 
260         SliceData dataTwo = builder.build();
261 
262         assertThat(dataOne.hashCode()).isEqualTo(dataTwo.hashCode());
263         assertThat(dataOne).isEqualTo(dataTwo);
264     }
265 
266     @Test
testEquality_differentKey_differentObjects()267     public void testEquality_differentKey_differentObjects() {
268         SliceData.Builder builder = new SliceData.Builder()
269                 .setKey(KEY)
270                 .setTitle(TITLE)
271                 .setSummary(SUMMARY)
272                 .setScreenTitle(SCREEN_TITLE)
273                 .setIcon(ICON)
274                 .setFragmentName(FRAGMENT_NAME)
275                 .setUri(URI)
276                 .setPreferenceControllerClassName(PREF_CONTROLLER);
277 
278         SliceData dataOne = builder.build();
279 
280         builder.setKey("not key");
281         SliceData dataTwo = builder.build();
282 
283         assertThat(dataOne.hashCode()).isNotEqualTo(dataTwo.hashCode());
284         assertThat(dataOne).isNotEqualTo(dataTwo);
285     }
286 }
287