From 606cb640979b5ed1c29f5a1e33ba79466b0f5faa Mon Sep 17 00:00:00 2001 From: Soroosh Mariooryad Date: Tue, 28 Feb 2017 12:55:05 -0800 Subject: [PATCH] Fixing a bug in suggestion logging and adding more tests. Test: RunSettingsRoboTests Fixes: b/35802845 Change-Id: I216b3ff344ccdffd038d924649109483e80f5437 --- .../settings/dashboard/DashboardAdapter.java | 9 +- .../settings/dashboard/DashboardAdapterTest.java | 249 +++++++++++++++++++-- 2 files changed, 236 insertions(+), 22 deletions(-) diff --git a/src/com/android/settings/dashboard/DashboardAdapter.java b/src/com/android/settings/dashboard/DashboardAdapter.java index beb94771d6..d9cef65b36 100644 --- a/src/com/android/settings/dashboard/DashboardAdapter.java +++ b/src/com/android/settings/dashboard/DashboardAdapter.java @@ -406,7 +406,8 @@ public class DashboardAdapter extends RecyclerView.Adapter 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)) { diff --git a/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java b/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java index e9851798f2..b1144f0049 100644 --- a/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java +++ b/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java @@ -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 mActionCategoryCaptor = ArgumentCaptor.forClass(Integer.class); @Captor private ArgumentCaptor 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.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 suggestions = new ArrayList(); - suggestions.add(makeSuggestion("pkg1", "cls1")); - suggestions.add(makeSuggestion("pkg2", "cls2")); - suggestions.add(makeSuggestion("pkg3", "cls3")); - mDashboardAdapter.setCategoriesAndSuggestions( - new ArrayList(), 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 makeSuggestions(String[] pkgNames) { + List suggestions = new ArrayList(); + 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 suggestions) { + mDashboardAdapter.setCategoriesAndSuggestions( + new ArrayList(), suggestions); + mSuggestionHolder = mDashboardAdapter.onCreateViewHolder( + new FrameLayout(RuntimeEnvironment.application), + mDashboardAdapter.getItemViewType(0)); + } + } -- 2.11.0