• 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 package com.android.settings.dashboard;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.verifyZeroInteractions;
21 import static org.mockito.Mockito.when;
22 
23 import android.app.Activity;
24 import android.content.Intent;
25 
26 import com.android.settings.testutils.FakeFeatureFactory;
27 import com.android.settings.testutils.SettingsRobolectricTestRunner;
28 import com.android.settingslib.drawer.CategoryKey;
29 import com.android.settingslib.drawer.DashboardCategory;
30 import com.android.settingslib.drawer.Tile;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.Robolectric;
37 
38 @RunWith(SettingsRobolectricTestRunner.class)
39 public class SummaryLoaderTest {
40 
41     private static final String SUMMARY_1 = "summary1";
42     private static final String SUMMARY_2 = "summary2";
43 
44     private SummaryLoader mSummaryLoader;
45     private boolean mCallbackInvoked;
46     private Tile mTile;
47     private FakeFeatureFactory mFeatureFactory;
48 
49     @Before
SetUp()50     public void SetUp() {
51         MockitoAnnotations.initMocks(this);
52         mFeatureFactory = FakeFeatureFactory.setupForTest();
53 
54         mTile = new Tile();
55         mTile.summary = SUMMARY_1;
56         mCallbackInvoked = false;
57 
58         final Activity activity = Robolectric.buildActivity(Activity.class).get();
59 
60         mSummaryLoader = new SummaryLoader(activity, CategoryKey.CATEGORY_HOMEPAGE);
61         mSummaryLoader.setSummaryConsumer(tile -> mCallbackInvoked = true);
62     }
63 
64     @Test
newInstance_shouldNotLoadCategory()65     public void newInstance_shouldNotLoadCategory() {
66         verifyZeroInteractions(mFeatureFactory.dashboardFeatureProvider);
67     }
68 
69     @Test
testUpdateSummaryIfNeeded_SummaryIdentical_NoCallback()70     public void testUpdateSummaryIfNeeded_SummaryIdentical_NoCallback() {
71         mSummaryLoader.updateSummaryIfNeeded(mTile, SUMMARY_1);
72 
73         assertThat(mCallbackInvoked).isFalse();
74     }
75 
76     @Test
testUpdateSummaryIfNeeded_SummaryChanged_HasCallback()77     public void testUpdateSummaryIfNeeded_SummaryChanged_HasCallback() {
78         mSummaryLoader.updateSummaryIfNeeded(mTile, SUMMARY_2);
79 
80         assertThat(mCallbackInvoked).isTrue();
81     }
82 
83     @Test
testUpdateSummaryToCache_hasCache_shouldUpdate()84     public void testUpdateSummaryToCache_hasCache_shouldUpdate() {
85         final String testSummary = "test_summary";
86         final DashboardCategory category = new DashboardCategory();
87         final Tile tile = new Tile();
88         tile.key = "123";
89         tile.intent = new Intent();
90         category.addTile(tile);
91         when(mFeatureFactory.dashboardFeatureProvider.getDashboardKeyForTile(tile))
92                 .thenReturn(tile.key);
93 
94         mSummaryLoader.updateSummaryIfNeeded(tile, testSummary);
95         tile.summary = null;
96         mSummaryLoader.updateSummaryToCache(category);
97 
98         assertThat(tile.summary).isEqualTo(testSummary);
99     }
100 }
101