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 package com.android.settings.dashboard; 17 18 import android.content.ComponentName; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.res.Resources; 22 import android.view.View; 23 import android.widget.FrameLayout; 24 25 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 26 import com.android.settings.SettingsRobolectricTestRunner; 27 import com.android.settings.TestConfig; 28 import com.android.settings.dashboard.conditional.Condition; 29 import com.android.settings.testutils.FakeFeatureFactory; 30 import com.android.settings.testutils.shadow.SettingsShadowResources; 31 import com.android.settings.testutils.shadow.ShadowDynamicIndexableContentMonitor; 32 import com.android.settingslib.drawer.Tile; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Answers; 38 import org.mockito.ArgumentCaptor; 39 import org.mockito.Captor; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.RuntimeEnvironment; 43 import org.robolectric.annotation.Config; 44 45 import java.util.ArrayList; 46 import java.util.List; 47 48 import static com.google.common.truth.Truth.assertThat; 49 import static org.mockito.Mockito.any; 50 import static org.mockito.Mockito.times; 51 import static org.mockito.Mockito.verify; 52 import static org.mockito.Mockito.when; 53 54 @RunWith(SettingsRobolectricTestRunner.class) 55 @Config(manifest = TestConfig.MANIFEST_PATH, 56 sdk = TestConfig.SDK_VERSION, 57 shadows = { 58 SettingsShadowResources.class, 59 SettingsShadowResources.SettingsShadowTheme.class, 60 ShadowDynamicIndexableContentMonitor.class 61 }) 62 public class DashboardAdapterTest { 63 64 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 65 private Context mContext; 66 @Mock 67 private View mView; 68 @Mock 69 private Condition mCondition; 70 @Mock 71 private Resources mResources; 72 @Captor 73 private ArgumentCaptor<Integer> mActionCategoryCaptor = ArgumentCaptor.forClass(Integer.class); 74 @Captor 75 private ArgumentCaptor<String> mActionPackageCaptor = ArgumentCaptor.forClass(String.class); 76 private FakeFeatureFactory mFactory; 77 private DashboardAdapter mDashboardAdapter; 78 private DashboardAdapter.DashboardItemHolder mSuggestionHolder; 79 private DashboardData.SuggestionHeaderData mSuggestionHeaderData; 80 81 @Before 82 public void setUp() { 83 MockitoAnnotations.initMocks(this); 84 FakeFeatureFactory.setupForTest(mContext); 85 mFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); 86 when(mFactory.suggestionsFeatureProvider 87 .getSuggestionIdentifier(any(Context.class), any(Tile.class))) 88 .thenAnswer(invocation -> { 89 final Object[] args = invocation.getArguments(); 90 return ((Tile)args[1]).intent.getComponent().getPackageName(); 91 }); 92 93 when(mContext.getResources()).thenReturn(mResources); 94 when(mResources.getQuantityString(any(int.class), any(int.class), any())) 95 .thenReturn(""); 96 97 mDashboardAdapter = new DashboardAdapter(mContext, null, null); 98 mSuggestionHeaderData = new DashboardData.SuggestionHeaderData(true, 1, 0); 99 when(mView.getTag()).thenReturn(mCondition); 100 } 101 102 @Test 103 public void testSetConditions_AfterSetConditions_ExpandedConditionNull() { 104 mDashboardAdapter.onExpandClick(mView); 105 assertThat(mDashboardAdapter.mDashboardData.getExpandedCondition()).isEqualTo(mCondition); 106 mDashboardAdapter.setConditions(null); 107 assertThat(mDashboardAdapter.mDashboardData.getExpandedCondition()).isNull(); 108 } 109 110 @Test 111 public void testSuggestionsLogs_NotExpanded() { 112 setUpSuggestions(makeSuggestions(new String[]{"pkg1", "pkg2", "pkg3"})); 113 verify(mFactory.metricsFeatureProvider, times(2)).action( 114 any(Context.class), mActionCategoryCaptor.capture(), 115 mActionPackageCaptor.capture()); 116 String[] expectedPackages = new String[]{"pkg1", "pkg2"}; 117 Integer[] expectedActions = new Integer[]{ 118 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 119 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION 120 }; 121 assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions); 122 assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages); 123 } 124 125 @Test 126 public void testSuggestionsLogs_NotExpandedAndPaused() { 127 setUpSuggestions(makeSuggestions(new String[]{"pkg1", "pkg2", "pkg3"})); 128 mDashboardAdapter.onPause(); 129 verify(mFactory.metricsFeatureProvider, times(4)).action( 130 any(Context.class), mActionCategoryCaptor.capture(), 131 mActionPackageCaptor.capture()); 132 String[] expectedPackages = new String[]{"pkg1", "pkg2", "pkg1", "pkg2"}; 133 Integer[] expectedActions = new Integer[]{ 134 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 135 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 136 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION, 137 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION}; 138 assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions); 139 assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages); 140 } 141 142 @Test 143 public void testSuggestionsLogs_Expanded() { 144 setUpSuggestions(makeSuggestions(new String[]{"pkg1", "pkg2", "pkg3"})); 145 mDashboardAdapter.onBindSuggestionHeader( 146 mSuggestionHolder, mSuggestionHeaderData); 147 mSuggestionHolder.itemView.callOnClick(); 148 verify(mFactory.metricsFeatureProvider, times(3)).action( 149 any(Context.class), mActionCategoryCaptor.capture(), 150 mActionPackageCaptor.capture()); 151 String[] expectedPackages = new String[]{"pkg1", "pkg2", "pkg3"}; 152 Integer[] expectedActions = new Integer[]{ 153 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 154 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 155 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION 156 }; 157 assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages); 158 assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions); 159 } 160 161 @Test 162 public void testSuggestionsLogs_ExpandedAndPaused() { 163 setUpSuggestions(makeSuggestions(new String[]{"pkg1", "pkg2", "pkg3"})); 164 mDashboardAdapter.onBindSuggestionHeader( 165 mSuggestionHolder, mSuggestionHeaderData); 166 mSuggestionHolder.itemView.callOnClick(); 167 mDashboardAdapter.onPause(); 168 verify(mFactory.metricsFeatureProvider, times(6)).action( 169 any(Context.class), mActionCategoryCaptor.capture(), 170 mActionPackageCaptor.capture()); 171 String[] expectedPackages = new String[]{"pkg1", "pkg2", "pkg3", "pkg1", "pkg2", "pkg3"}; 172 Integer[] expectedActions = new Integer[]{ 173 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 174 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 175 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 176 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION, 177 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION, 178 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION 179 }; 180 assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages); 181 assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions); 182 } 183 184 @Test 185 public void testSuggestionsLogs_ExpandedAfterPause() { 186 setUpSuggestions(makeSuggestions(new String[]{"pkg1", "pkg2", "pkg3"})); 187 mDashboardAdapter.onPause(); 188 mDashboardAdapter.onBindSuggestionHeader( 189 mSuggestionHolder, mSuggestionHeaderData); 190 mSuggestionHolder.itemView.callOnClick(); 191 verify(mFactory.metricsFeatureProvider, times(7)).action( 192 any(Context.class), mActionCategoryCaptor.capture(), 193 mActionPackageCaptor.capture()); 194 String[] expectedPackages = new String[]{ 195 "pkg1", "pkg2", "pkg1", "pkg2", "pkg1", "pkg2", "pkg3"}; 196 Integer[] expectedActions = new Integer[]{ 197 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 198 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 199 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION, 200 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION, 201 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 202 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 203 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION 204 }; 205 assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages); 206 assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions); 207 } 208 209 @Test 210 public void testSuggestionsLogs_ExpandedAfterPauseAndPausedAgain() { 211 setUpSuggestions(makeSuggestions(new String[]{"pkg1", "pkg2", "pkg3"})); 212 mDashboardAdapter.onPause(); 213 mDashboardAdapter.onBindSuggestionHeader( 214 mSuggestionHolder, mSuggestionHeaderData); 215 mSuggestionHolder.itemView.callOnClick(); 216 mDashboardAdapter.onPause(); 217 verify(mFactory.metricsFeatureProvider, times(10)).action( 218 any(Context.class), mActionCategoryCaptor.capture(), 219 mActionPackageCaptor.capture()); 220 String[] expectedPackages = new String[]{ 221 "pkg1", "pkg2", "pkg1", "pkg2", "pkg1", "pkg2", "pkg3", "pkg1", "pkg2", "pkg3"}; 222 Integer[] expectedActions = new Integer[]{ 223 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 224 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 225 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION, 226 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION, 227 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 228 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 229 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 230 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION, 231 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION, 232 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION 233 }; 234 assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages); 235 assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions); 236 } 237 238 @Test 239 public void testSuggestionsLogs_ExpandedWithLessThanDefaultShown() { 240 setUpSuggestions(makeSuggestions(new String[]{"pkg1"})); 241 mDashboardAdapter.onBindSuggestionHeader( 242 mSuggestionHolder, mSuggestionHeaderData); 243 mSuggestionHolder.itemView.callOnClick(); 244 verify(mFactory.metricsFeatureProvider, times(1)).action( 245 any(Context.class), mActionCategoryCaptor.capture(), 246 mActionPackageCaptor.capture()); 247 String[] expectedPackages = new String[]{"pkg1"}; 248 Integer[] expectedActions = new Integer[]{ 249 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 250 }; 251 assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages); 252 assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions); 253 } 254 255 @Test 256 public void testSuggestionsLogs_ExpandedWithLessThanDefaultShownAndPaused() { 257 setUpSuggestions(makeSuggestions(new String[]{"pkg1"})); 258 mDashboardAdapter.onBindSuggestionHeader( 259 mSuggestionHolder, mSuggestionHeaderData); 260 mSuggestionHolder.itemView.callOnClick(); 261 mDashboardAdapter.onPause(); 262 verify(mFactory.metricsFeatureProvider, times(2)).action( 263 any(Context.class), mActionCategoryCaptor.capture(), 264 mActionPackageCaptor.capture()); 265 String[] expectedPackages = new String[]{"pkg1", "pkg1"}; 266 Integer[] expectedActions = new Integer[]{ 267 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 268 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION 269 }; 270 assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages); 271 assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions); 272 } 273 274 @Test 275 public void testSuggestionsLogs_ExpandedWithLessThanDefaultShownAfterPause() { 276 setUpSuggestions(makeSuggestions(new String[]{"pkg1"})); 277 mDashboardAdapter.onPause(); 278 mDashboardAdapter.onBindSuggestionHeader( 279 mSuggestionHolder, mSuggestionHeaderData); 280 mSuggestionHolder.itemView.callOnClick(); 281 verify(mFactory.metricsFeatureProvider, times(3)).action( 282 any(Context.class), mActionCategoryCaptor.capture(), 283 mActionPackageCaptor.capture()); 284 String[] expectedPackages = new String[]{"pkg1", "pkg1", "pkg1"}; 285 Integer[] expectedActions = new Integer[]{ 286 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 287 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION, 288 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION 289 }; 290 assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages); 291 assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions); 292 } 293 294 @Test 295 public void testSuggestionsLogs_ExpandedWithLessThanDefaultShownAfterPauseAndPausedAgain() { 296 setUpSuggestions(makeSuggestions(new String[]{"pkg1"})); 297 mDashboardAdapter.onPause(); 298 mDashboardAdapter.onBindSuggestionHeader( 299 mSuggestionHolder, mSuggestionHeaderData); 300 mSuggestionHolder.itemView.callOnClick(); 301 mDashboardAdapter.onPause(); 302 verify(mFactory.metricsFeatureProvider, times(4)).action( 303 any(Context.class), mActionCategoryCaptor.capture(), 304 mActionPackageCaptor.capture()); 305 String[] expectedPackages = new String[]{"pkg1", "pkg1", "pkg1", "pkg1"}; 306 Integer[] expectedActions = new Integer[]{ 307 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 308 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION, 309 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 310 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION 311 }; 312 assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages); 313 assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions); 314 } 315 316 private List<Tile> makeSuggestions(String[] pkgNames) { 317 final List<Tile> suggestions = new ArrayList<>(); 318 for (String pkgName : pkgNames) { 319 Tile suggestion = new Tile(); 320 suggestion.intent = new Intent("action"); 321 suggestion.intent.setComponent(new ComponentName(pkgName, "cls")); 322 suggestions.add(suggestion); 323 } 324 return suggestions; 325 } 326 327 private void setUpSuggestions(List<Tile> suggestions) { 328 mDashboardAdapter.setCategoriesAndSuggestions(new ArrayList<>(), suggestions); 329 mSuggestionHolder = mDashboardAdapter.onCreateViewHolder( 330 new FrameLayout(RuntimeEnvironment.application), 331 mDashboardAdapter.getItemViewType(0)); 332 } 333 334 } 335