OSDN Git Service

Fixing a bug in suggestion logging and adding more tests.
authorSoroosh Mariooryad <soroosh@google.com>
Tue, 28 Feb 2017 20:55:05 +0000 (12:55 -0800)
committerSoroosh Mariooryad <soroosh@google.com>
Fri, 3 Mar 2017 18:18:52 +0000 (10:18 -0800)
Test: RunSettingsRoboTests
Fixes: b/35802845

Change-Id: I216b3ff344ccdffd038d924649109483e80f5437

src/com/android/settings/dashboard/DashboardAdapter.java
tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java

index beb9477..d9cef65 100644 (file)
@@ -406,7 +406,8 @@ public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.Dash
         popup.show();
     }
 
-    private void onBindSuggestionHeader(final DashboardItemHolder holder, DashboardData
+    @VisibleForTesting
+    void onBindSuggestionHeader(final DashboardItemHolder holder, DashboardData
             .SuggestionHeaderData data) {
         final boolean moreSuggestions = data.hasMoreSuggestions;
         final int undisplayedSuggestionCount = data.undisplayedSuggestionCount;
@@ -436,10 +437,8 @@ public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.Dash
                 final int suggestionMode;
                 if (moreSuggestions) {
                     suggestionMode = DashboardData.SUGGESTION_MODE_EXPANDED;
-                    List<Tile> expandedSuggestions = mDashboardData.getSuggestions().subList(
-                            DashboardData.DEFAULT_SUGGESTION_COUNT,
-                            mDashboardData.getSuggestions().size());
-                    for (Tile suggestion : expandedSuggestions) {
+
+                    for (Tile suggestion : mDashboardData.getSuggestions()) {
                         String suggestionId =
                                 DashboardAdapter.getSuggestionIdentifier(mContext, suggestion);
                         if (!mSuggestionsShownLogged.contains(suggestionId)) {
index e985179..b1144f0 100644 (file)
@@ -18,9 +18,11 @@ package com.android.settings.dashboard;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
-import android.content.res.TypedArray;
+import android.content.res.Resources;
 import android.view.View;
+import android.widget.FrameLayout;
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.settings.R;
 import com.android.settings.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
 import com.android.settings.core.instrumentation.MetricsFeatureProvider;
@@ -35,12 +37,16 @@ import org.mockito.Captor;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.annotation.Config;
+import org.robolectric.RuntimeEnvironment;
 
 import java.util.ArrayList;
 import java.util.List;
 
 import static com.google.common.truth.Truth.assertThat;
+import org.mockito.Matchers;
 import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -48,8 +54,7 @@ import static org.mockito.Mockito.when;
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
 public class DashboardAdapterTest {
-    @Mock
-    private Context mContext;
+
     @Mock
     private View mView;
     @Mock
@@ -57,18 +62,29 @@ public class DashboardAdapterTest {
     @Mock
     private MetricsFeatureProvider mMetricsFeatureProvider;
     @Mock
-    private TypedArray mTypedArray;
+    private Resources mResources;
+    @Mock
+    private DashboardData mDashboardData;
     @Captor
     private ArgumentCaptor<Integer> mActionCategoryCaptor = ArgumentCaptor.forClass(Integer.class);
     @Captor
     private ArgumentCaptor<String> mActionPackageCaptor = ArgumentCaptor.forClass(String.class);
     private DashboardAdapter mDashboardAdapter;
+    private DashboardAdapter.DashboardItemHolder mSuggestionHolder;
+    private DashboardData.SuggestionHeaderData mSuggestionHeaderData;
 
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
-        mDashboardAdapter = new DashboardAdapter(mContext, null, mMetricsFeatureProvider,
+        Context context = RuntimeEnvironment.application;
+        context = spy(context);
+        when(context.getResources()).thenReturn(mResources);
+        when(mResources
+                .getQuantityString(any(int.class), any(int.class), Matchers.<Object>anyVararg()))
+                .thenReturn("");
+        mDashboardAdapter = new DashboardAdapter(context, null, mMetricsFeatureProvider,
                 null, null);
+        mSuggestionHeaderData = new DashboardData.SuggestionHeaderData(true, 1, 0);
         when(mView.getTag()).thenReturn(mCondition);
     }
 
@@ -81,20 +97,29 @@ public class DashboardAdapterTest {
     }
 
     @Test
-    public void testSuggestionsLogs() {
-        when(mTypedArray.getColor(any(int.class), any(int.class))).thenReturn(0);
-        when(mContext.obtainStyledAttributes(any(int[].class))).thenReturn(mTypedArray);
-        List<Tile> suggestions = new ArrayList<Tile>();
-        suggestions.add(makeSuggestion("pkg1", "cls1"));
-        suggestions.add(makeSuggestion("pkg2", "cls2"));
-        suggestions.add(makeSuggestion("pkg3", "cls3"));
-        mDashboardAdapter.setCategoriesAndSuggestions(
-                new ArrayList<DashboardCategory>(), suggestions);
+    public void testSuggestionsLogs_NotExpanded() {
+        setUpSuggestions(makeSuggestions(new String[]{"pkg1", "pkg2", "pkg3"}));
+        verify(mMetricsFeatureProvider, times(2)).action(
+                any(Context.class), mActionCategoryCaptor.capture(),
+                mActionPackageCaptor.capture());
+        String[] expectedPackages = new String[]{"pkg1", "pkg2"};
+        Integer[] expectedActions = new Integer[]{
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION
+        };
+        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
+        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+    }
+
+    @Test
+    public void testSuggestionsLogs_NotExpandedAndPaused() {
+        setUpSuggestions(makeSuggestions(new String[]{"pkg1", "pkg2", "pkg3"}));
         mDashboardAdapter.onPause();
         verify(mMetricsFeatureProvider, times(4)).action(
-             any(Context.class), mActionCategoryCaptor.capture(), mActionPackageCaptor.capture());
-        String[] expectedPackages = new String[] {"pkg1", "pkg2", "pkg1", "pkg2"};
-        Integer[] expectedActions = new Integer[] {
+                any(Context.class), mActionCategoryCaptor.capture(),
+                mActionPackageCaptor.capture());
+        String[] expectedPackages = new String[]{"pkg1", "pkg2", "pkg1", "pkg2"};
+        Integer[] expectedActions = new Integer[]{
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
@@ -103,6 +128,188 @@ public class DashboardAdapterTest {
         assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
     }
 
+    @Test
+    public void testSuggestionsLogs_Expanded() {
+        setUpSuggestions(makeSuggestions(new String[]{"pkg1", "pkg2", "pkg3"}));
+        mDashboardAdapter.onBindSuggestionHeader(
+                mSuggestionHolder, mSuggestionHeaderData);
+        mSuggestionHolder.itemView.callOnClick();
+        verify(mMetricsFeatureProvider, times(3)).action(
+                any(Context.class), mActionCategoryCaptor.capture(),
+                mActionPackageCaptor.capture());
+        String[] expectedPackages = new String[]{"pkg1", "pkg2", "pkg3"};
+        Integer[] expectedActions = new Integer[]{
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION
+        };
+        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
+        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+    }
+
+    @Test
+    public void testSuggestionsLogs_ExpandedAndPaused() {
+        setUpSuggestions(makeSuggestions(new String[]{"pkg1", "pkg2", "pkg3"}));
+        mDashboardAdapter.onBindSuggestionHeader(
+                mSuggestionHolder, mSuggestionHeaderData);
+        mSuggestionHolder.itemView.callOnClick();
+        mDashboardAdapter.onPause();
+        verify(mMetricsFeatureProvider, times(6)).action(
+                any(Context.class), mActionCategoryCaptor.capture(),
+                mActionPackageCaptor.capture());
+        String[] expectedPackages = new String[]{"pkg1", "pkg2", "pkg3", "pkg1", "pkg2", "pkg3"};
+        Integer[] expectedActions = new Integer[]{
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION
+        };
+        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
+        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+    }
+
+    @Test
+    public void testSuggestionsLogs_ExpandedAfterPause() {
+        setUpSuggestions(makeSuggestions(new String[]{"pkg1", "pkg2", "pkg3"}));
+        mDashboardAdapter.onPause();
+        mDashboardAdapter.onBindSuggestionHeader(
+                mSuggestionHolder, mSuggestionHeaderData);
+        mSuggestionHolder.itemView.callOnClick();
+        verify(mMetricsFeatureProvider, times(7)).action(
+                any(Context.class), mActionCategoryCaptor.capture(),
+                mActionPackageCaptor.capture());
+        String[] expectedPackages = new String[]{
+                "pkg1", "pkg2", "pkg1", "pkg2", "pkg1", "pkg2", "pkg3"};
+        Integer[] expectedActions = new Integer[]{
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION
+        };
+        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
+        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+    }
+
+    @Test
+    public void testSuggestionsLogs_ExpandedAfterPauseAndPausedAgain() {
+        setUpSuggestions(makeSuggestions(new String[]{"pkg1", "pkg2", "pkg3"}));
+        mDashboardAdapter.onPause();
+        mDashboardAdapter.onBindSuggestionHeader(
+                mSuggestionHolder, mSuggestionHeaderData);
+        mSuggestionHolder.itemView.callOnClick();
+        mDashboardAdapter.onPause();
+        verify(mMetricsFeatureProvider, times(10)).action(
+                any(Context.class), mActionCategoryCaptor.capture(),
+                mActionPackageCaptor.capture());
+        String[] expectedPackages = new String[]{
+                "pkg1", "pkg2", "pkg1", "pkg2", "pkg1", "pkg2", "pkg3", "pkg1", "pkg2", "pkg3"};
+        Integer[] expectedActions = new Integer[]{
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION
+        };
+        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
+        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+    }
+
+    @Test
+    public void testSuggestionsLogs_ExpandedWithLessThanDefaultShown() {
+        setUpSuggestions(makeSuggestions(new String[]{"pkg1"}));
+        mDashboardAdapter.onBindSuggestionHeader(
+                mSuggestionHolder, mSuggestionHeaderData);
+        mSuggestionHolder.itemView.callOnClick();
+        verify(mMetricsFeatureProvider, times(1)).action(
+                any(Context.class), mActionCategoryCaptor.capture(),
+                mActionPackageCaptor.capture());
+        String[] expectedPackages = new String[]{"pkg1"};
+        Integer[] expectedActions = new Integer[]{
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+        };
+        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
+        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+    }
+
+    @Test
+    public void testSuggestionsLogs_ExpandedWithLessThanDefaultShownAndPaused() {
+        setUpSuggestions(makeSuggestions(new String[]{"pkg1"}));
+        mDashboardAdapter.onBindSuggestionHeader(
+                mSuggestionHolder, mSuggestionHeaderData);
+        mSuggestionHolder.itemView.callOnClick();
+        mDashboardAdapter.onPause();
+        verify(mMetricsFeatureProvider, times(2)).action(
+                any(Context.class), mActionCategoryCaptor.capture(),
+                mActionPackageCaptor.capture());
+        String[] expectedPackages = new String[]{"pkg1", "pkg1"};
+        Integer[] expectedActions = new Integer[]{
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION
+        };
+        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
+        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+    }
+
+    @Test
+    public void testSuggestionsLogs_ExpandedWithLessThanDefaultShownAfterPause() {
+        setUpSuggestions(makeSuggestions(new String[]{"pkg1"}));
+        mDashboardAdapter.onPause();
+        mDashboardAdapter.onBindSuggestionHeader(
+                mSuggestionHolder, mSuggestionHeaderData);
+        mSuggestionHolder.itemView.callOnClick();
+        verify(mMetricsFeatureProvider, times(3)).action(
+                any(Context.class), mActionCategoryCaptor.capture(),
+                mActionPackageCaptor.capture());
+        String[] expectedPackages = new String[]{"pkg1", "pkg1", "pkg1"};
+        Integer[] expectedActions = new Integer[]{
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION
+        };
+        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
+        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+    }
+
+    @Test
+    public void testSuggestionsLogs_ExpandedWithLessThanDefaultShownAfterPauseAndPausedAgain() {
+        setUpSuggestions(makeSuggestions(new String[]{"pkg1"}));
+        mDashboardAdapter.onPause();
+        mDashboardAdapter.onBindSuggestionHeader(
+                mSuggestionHolder, mSuggestionHeaderData);
+        mSuggestionHolder.itemView.callOnClick();
+        mDashboardAdapter.onPause();
+        verify(mMetricsFeatureProvider, times(4)).action(
+                any(Context.class), mActionCategoryCaptor.capture(),
+                mActionPackageCaptor.capture());
+        String[] expectedPackages = new String[]{"pkg1", "pkg1", "pkg1", "pkg1"};
+        Integer[] expectedActions = new Integer[]{
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION
+        };
+        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
+        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+    }
+
+    private List<Tile> makeSuggestions(String[] pkgNames) {
+        List<Tile> suggestions = new ArrayList<Tile>();
+        for (String pkgName : pkgNames) {
+            suggestions.add(makeSuggestion(pkgName, "cls"));
+        }
+        return suggestions;
+    }
+
     private Tile makeSuggestion(String pkgName, String className) {
         Tile suggestion = new Tile();
         suggestion.intent = new Intent("action");
@@ -110,4 +317,12 @@ public class DashboardAdapterTest {
         return suggestion;
     }
 
+    private void setUpSuggestions(List<Tile> suggestions) {
+        mDashboardAdapter.setCategoriesAndSuggestions(
+                new ArrayList<DashboardCategory>(), suggestions);
+        mSuggestionHolder = mDashboardAdapter.onCreateViewHolder(
+                new FrameLayout(RuntimeEnvironment.application),
+                mDashboardAdapter.getItemViewType(0));
+    }
+
 }