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;
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;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DashboardAdapterTest {
- @Mock
- private Context mContext;
+
@Mock
private View mView;
@Mock
@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);
}
}
@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,
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");
return suggestion;
}
+ private void setUpSuggestions(List<Tile> suggestions) {
+ mDashboardAdapter.setCategoriesAndSuggestions(
+ new ArrayList<DashboardCategory>(), suggestions);
+ mSuggestionHolder = mDashboardAdapter.onCreateViewHolder(
+ new FrameLayout(RuntimeEnvironment.application),
+ mDashboardAdapter.getItemViewType(0));
+ }
+
}