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