Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(662)

Side by Side Diff: chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapterTest.java

Issue 2396523002: Unify NewTabPageItem and ItemGroup into a single tree-structured interface. (Closed)
Patch Set: review Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.ntp.cards; 5 package org.chromium.chrome.browser.ntp.cards;
6 6
7 import static org.junit.Assert.assertEquals; 7 import static org.junit.Assert.assertEquals;
8 import static org.junit.Assert.assertFalse; 8 import static org.junit.Assert.assertFalse;
9 import static org.junit.Assert.assertThat; 9 import static org.junit.Assert.assertThat;
10 import static org.junit.Assert.assertTrue; 10 import static org.junit.Assert.assertTrue;
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 */ 100 */
101 private static class ItemsMatcher { // TODO(pke): Find better name. 101 private static class ItemsMatcher { // TODO(pke): Find better name.
102 private final NewTabPageAdapter mAdapter; 102 private final NewTabPageAdapter mAdapter;
103 private int mCurrentIndex; 103 private int mCurrentIndex;
104 104
105 public ItemsMatcher(NewTabPageAdapter adapter, int startingIndex) { 105 public ItemsMatcher(NewTabPageAdapter adapter, int startingIndex) {
106 mAdapter = adapter; 106 mAdapter = adapter;
107 mCurrentIndex = startingIndex; 107 mCurrentIndex = startingIndex;
108 } 108 }
109 109
110 public void expect(@NewTabPageItem.ViewType int expectedItemType) { 110 public void expect(@ItemViewType int expectedItemType) {
111 if (mCurrentIndex >= mAdapter.getItemCount()) { 111 if (mCurrentIndex >= mAdapter.getItemCount()) {
112 fail("Expected item of type " + expectedItemType + " but encount ered end of list"); 112 fail("Expected item of type " + expectedItemType + " but encount ered end of list");
113 } 113 }
114 @NewTabPageItem.ViewType int itemType = mAdapter.getItemViewType(mCu rrentIndex); 114 @ItemViewType int itemType = mAdapter.getItemViewType(mCurrentIndex) ;
115 assertEquals("Type mismatch at position " + mCurrentIndex, expectedI temType, itemType); 115 assertEquals("Type mismatch at position " + mCurrentIndex, expectedI temType, itemType);
116 mCurrentIndex++; 116 mCurrentIndex++;
117 } 117 }
118 118
119 public void expect(SectionDescriptor descriptor) { 119 public void expect(SectionDescriptor descriptor) {
120 expect(NewTabPageItem.VIEW_TYPE_HEADER); 120 expect(ItemViewType.HEADER);
121 if (descriptor.mStatusCard) { 121 if (descriptor.mStatusCard) {
122 expect(NewTabPageItem.VIEW_TYPE_STATUS); 122 expect(ItemViewType.STATUS);
123 expect(NewTabPageItem.VIEW_TYPE_ACTION); 123 expect(ItemViewType.ACTION);
124 expect(NewTabPageItem.VIEW_TYPE_PROGRESS); 124 expect(ItemViewType.PROGRESS);
125 } else { 125 } else {
126 for (int i = 1; i <= descriptor.mNumSuggestions; i++) { 126 for (int i = 1; i <= descriptor.mNumSuggestions; i++) {
127 expect(NewTabPageItem.VIEW_TYPE_SNIPPET); 127 expect(ItemViewType.SNIPPET);
128 } 128 }
129 if (descriptor.mMoreButton) { 129 if (descriptor.mMoreButton) {
130 expect(NewTabPageItem.VIEW_TYPE_ACTION); 130 expect(ItemViewType.ACTION);
131 } 131 }
132 } 132 }
133 } 133 }
134 134
135 public void expectPosition(int expectedPosition) { 135 public void expectPosition(int expectedPosition) {
136 assertEquals(expectedPosition, mCurrentIndex); 136 assertEquals(expectedPosition, mCurrentIndex);
137 } 137 }
138 } 138 }
139 139
140 /** 140 /**
141 * Asserts that the given itemGroup is a {@link SuggestionsSection} that mat ches the given 141 * Asserts that the given itemGroup is a {@link SuggestionsSection} that mat ches the given
142 * {@link SectionDescriptor}. 142 * {@link SectionDescriptor}.
143 * @param descriptor The section descriptor to match against. 143 * @param descriptor The section descriptor to match against.
144 * @param itemGroup The items from the adapter. 144 * @param itemGroup The items from the adapter.
145 */ 145 */
146 private void assertMatches(SectionDescriptor descriptor, ItemGroup itemGroup ) { 146 private void assertMatches(SectionDescriptor descriptor, TreeNode itemGroup) {
147 int offset = mAdapter.getGroupPositionOffset(itemGroup); 147 int offset = mAdapter.getGroupPositionOffset(itemGroup);
148 ItemsMatcher matcher = new ItemsMatcher(mAdapter, offset); 148 ItemsMatcher matcher = new ItemsMatcher(mAdapter, offset);
149 matcher.expect(descriptor); 149 matcher.expect(descriptor);
150 matcher.expectPosition(offset + itemGroup.getItems().size()); 150 matcher.expectPosition(offset + itemGroup.getItemCount());
151 } 151 }
152 152
153 /** 153 /**
154 * Asserts that {@link #mAdapter}.{@link NewTabPageAdapter#getItemCount()} c orresponds to an 154 * Asserts that {@link #mAdapter}.{@link NewTabPageAdapter#getItemCount()} c orresponds to an
155 * NTP with the given sections in it. 155 * NTP with the given sections in it.
156 * @param descriptors A list of descriptors, each describing a section that should be present on 156 * @param descriptors A list of descriptors, each describing a section that should be present on
157 * the UI. 157 * the UI.
158 */ 158 */
159 private void assertItemsFor(SectionDescriptor... descriptors) { 159 private void assertItemsFor(SectionDescriptor... descriptors) {
160 ItemsMatcher matcher = new ItemsMatcher(mAdapter, 0); 160 ItemsMatcher matcher = new ItemsMatcher(mAdapter, 0);
161 matcher.expect(NewTabPageItem.VIEW_TYPE_ABOVE_THE_FOLD); 161 matcher.expect(ItemViewType.ABOVE_THE_FOLD);
162 for (SectionDescriptor descriptor : descriptors) matcher.expect(descript or); 162 for (SectionDescriptor descriptor : descriptors) matcher.expect(descript or);
163 if (descriptors.length == 0) { 163 if (descriptors.length == 0) {
164 matcher.expect(NewTabPageItem.VIEW_TYPE_ALL_DISMISSED); 164 matcher.expect(ItemViewType.ALL_DISMISSED);
165 } else { 165 } else {
166 matcher.expect(NewTabPageItem.VIEW_TYPE_FOOTER); 166 matcher.expect(ItemViewType.FOOTER);
167 } 167 }
168 matcher.expect(NewTabPageItem.VIEW_TYPE_SPACING); 168 matcher.expect(ItemViewType.SPACING);
169 matcher.expectPosition(mAdapter.getItemCount()); 169 matcher.expectPosition(mAdapter.getItemCount());
170 } 170 }
171 171
172 /** 172 /**
173 * To be used with {@link #assertItemsFor(SectionDescriptor...)}, for a sect ion with 173 * To be used with {@link #assertItemsFor(SectionDescriptor...)}, for a sect ion with
174 * {@code numSuggestions} cards in it. 174 * {@code numSuggestions} cards in it.
175 * @param numSuggestions The number of suggestions in the section. If there are zero, use either 175 * @param numSuggestions The number of suggestions in the section. If there are zero, use either
176 * no section at all (if it is not displayed) or 176 * no section at all (if it is not displayed) or
177 * {@link #sectionWithStatusCard()}. 177 * {@link #sectionWithStatusCard()}.
178 * @return A descriptor for the section. 178 * @return A descriptor for the section.
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 assertItemsFor(section(4)); 356 assertItemsFor(section(4));
357 } 357 }
358 358
359 /** 359 /**
360 * Tests how the loading indicator reacts to status changes. 360 * Tests how the loading indicator reacts to status changes.
361 */ 361 */
362 @Test 362 @Test
363 @Feature({"Ntp"}) 363 @Feature({"Ntp"})
364 public void testProgressIndicatorDisplay() { 364 public void testProgressIndicatorDisplay() {
365 int progressPos = mAdapter.getFooterPosition() - 1; 365 int progressPos = mAdapter.getFooterPosition() - 1;
366 SuggestionsSection section = (SuggestionsSection) mAdapter.getGroup(prog ressPos); 366 SuggestionsSection section = mAdapter.getSuggestionsSection(progressPos) ;
367 List<NewTabPageItem> items = section.getItems(); 367 ProgressItem progress = section.getProgressItemForTesting();
368 ProgressItem progress = (ProgressItem) items.get(items.size() - 1);
369 368
370 mSource.setStatusForCategory(KnownCategories.ARTICLES, CategoryStatus.IN ITIALIZING); 369 mSource.setStatusForCategory(KnownCategories.ARTICLES, CategoryStatus.IN ITIALIZING);
371 assertTrue(progress.isVisible()); 370 assertTrue(progress.isVisible());
372 371
373 mSource.setStatusForCategory(KnownCategories.ARTICLES, CategoryStatus.AV AILABLE); 372 mSource.setStatusForCategory(KnownCategories.ARTICLES, CategoryStatus.AV AILABLE);
374 assertFalse(progress.isVisible()); 373 assertFalse(progress.isVisible());
375 374
376 mSource.setStatusForCategory(KnownCategories.ARTICLES, CategoryStatus.AV AILABLE_LOADING); 375 mSource.setStatusForCategory(KnownCategories.ARTICLES, CategoryStatus.AV AILABLE_LOADING);
377 assertTrue(progress.isVisible()); 376 assertTrue(progress.isVisible());
378 377
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
607 public void testCategoryOrder() { 606 public void testCategoryOrder() {
608 final int basicGroupCount = 4; // above-the-fold, sign in promo, footer, spacer. 607 final int basicGroupCount = 4; // above-the-fold, sign in promo, footer, spacer.
609 FakeSuggestionsSource suggestionsSource = new FakeSuggestionsSource(); 608 FakeSuggestionsSource suggestionsSource = new FakeSuggestionsSource();
610 registerCategory(suggestionsSource, KnownCategories.ARTICLES, 0); 609 registerCategory(suggestionsSource, KnownCategories.ARTICLES, 0);
611 registerCategory(suggestionsSource, KnownCategories.BOOKMARKS, 0); 610 registerCategory(suggestionsSource, KnownCategories.BOOKMARKS, 0);
612 registerCategory(suggestionsSource, KnownCategories.PHYSICAL_WEB_PAGES, 0); 611 registerCategory(suggestionsSource, KnownCategories.PHYSICAL_WEB_PAGES, 0);
613 registerCategory(suggestionsSource, KnownCategories.DOWNLOADS, 0); 612 registerCategory(suggestionsSource, KnownCategories.DOWNLOADS, 0);
614 613
615 NewTabPageAdapter ntpAdapter = 614 NewTabPageAdapter ntpAdapter =
616 new NewTabPageAdapter(new MockNewTabPageManager(suggestionsSourc e), null, null); 615 new NewTabPageAdapter(new MockNewTabPageManager(suggestionsSourc e), null, null);
617 List<ItemGroup> groups = ntpAdapter.getGroups(); 616 List<TreeNode> groups = ntpAdapter.getGroups();
618 617
619 assertEquals(basicGroupCount + 4, groups.size()); 618 assertEquals(basicGroupCount + 4, groups.size());
620 assertEquals(AboveTheFoldItem.class, groups.get(0).getClass()); 619 assertEquals(AboveTheFoldItem.class, groups.get(0).getClass());
621 assertEquals(SuggestionsSection.class, groups.get(1).getClass()); 620 assertEquals(SuggestionsSection.class, groups.get(1).getClass());
622 assertEquals(KnownCategories.ARTICLES, getCategory(groups.get(1))); 621 assertEquals(KnownCategories.ARTICLES, getCategory(groups.get(1)));
623 assertEquals(SuggestionsSection.class, groups.get(2).getClass()); 622 assertEquals(SuggestionsSection.class, groups.get(2).getClass());
624 assertEquals(KnownCategories.BOOKMARKS, getCategory(groups.get(2))); 623 assertEquals(KnownCategories.BOOKMARKS, getCategory(groups.get(2)));
625 assertEquals(SuggestionsSection.class, groups.get(3).getClass()); 624 assertEquals(SuggestionsSection.class, groups.get(3).getClass());
626 assertEquals(KnownCategories.PHYSICAL_WEB_PAGES, getCategory(groups.get( 3))); 625 assertEquals(KnownCategories.PHYSICAL_WEB_PAGES, getCategory(groups.get( 3)));
627 assertEquals(SuggestionsSection.class, groups.get(4).getClass()); 626 assertEquals(SuggestionsSection.class, groups.get(4).getClass());
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
753 } 752 }
754 753
755 @Test 754 @Test
756 @Feature({"Ntp"}) 755 @Feature({"Ntp"})
757 public void testSigninPromo() { 756 public void testSigninPromo() {
758 when(mMockSigninManager.isSignInAllowed()).thenReturn(true); 757 when(mMockSigninManager.isSignInAllowed()).thenReturn(true);
759 when(mMockSigninManager.isSignedInOnNative()).thenReturn(false); 758 when(mMockSigninManager.isSignedInOnNative()).thenReturn(false);
760 MockNewTabPageManager ntpManager = new MockNewTabPageManager(mSource); 759 MockNewTabPageManager ntpManager = new MockNewTabPageManager(mSource);
761 NewTabPageAdapter adapter = new NewTabPageAdapter(ntpManager, null, null ); 760 NewTabPageAdapter adapter = new NewTabPageAdapter(ntpManager, null, null );
762 761
763 assertEquals(5, adapter.getGroups().size()); 762 TreeNode signinPromoGroup = adapter.getGroups().get(2);
764 ItemGroup signinPromoGroup = adapter.getGroup(5);
765 763
766 // Adapter content: 764 // Adapter content:
767 // Idx | Item | Group Index 765 // Idx | Item | Group Index
768 // ----|--------------------|------------- 766 // ----|--------------------|-------------
769 // 0 | Above-the-fold | 0 767 // 0 | Above-the-fold | 0
770 // 1 | Header | 1 768 // 1 | Header | 1
771 // 2 | Status | 1 769 // 2 | Status | 1
772 // 3 | Action | 1 770 // 3 | Action | 1
773 // 4 | Progress Indicator | 1 771 // 4 | Progress Indicator | 1
774 // 5 | Sign in promo | 2 772 // 5 | Sign in promo | 2
775 // 6 | Footer | 3 773 // 6 | Footer | 3
776 // 7 | Spacer | 4 774 // 7 | Spacer | 4
777 775
778 assertEquals(1, signinPromoGroup.getItems().size()); 776 assertEquals(1, signinPromoGroup.getItemCount());
779 assertEquals(NewTabPageItem.VIEW_TYPE_PROMO, signinPromoGroup.getItems() .get(0).getType()); 777 assertEquals(ItemViewType.PROMO, signinPromoGroup.getItemViewType(0));
780 778
781 ntpManager.mSignInStateObserver.onSignedIn(); 779 ntpManager.mSignInStateObserver.onSignedIn();
782 assertEquals(0, signinPromoGroup.getItems().size()); 780 assertEquals(0, signinPromoGroup.getItemCount());
783 781
784 ntpManager.mSignInStateObserver.onSignedOut(); 782 ntpManager.mSignInStateObserver.onSignedOut();
785 assertEquals(1, signinPromoGroup.getItems().size()); 783 assertEquals(1, signinPromoGroup.getItemCount());
786 } 784 }
787 785
788 @Test 786 @Test
789 @Feature({"Ntp"}) 787 @Feature({"Ntp"})
790 public void testSigninPromoDismissal() { 788 public void testSigninPromoDismissal() {
791 when(mMockSigninManager.isSignInAllowed()).thenReturn(true); 789 when(mMockSigninManager.isSignInAllowed()).thenReturn(true);
792 when(mMockSigninManager.isSignedInOnNative()).thenReturn(false); 790 when(mMockSigninManager.isSignedInOnNative()).thenReturn(false);
793 ChromePreferenceManager.getInstance(RuntimeEnvironment.application) 791 ChromePreferenceManager.getInstance(RuntimeEnvironment.application)
794 .setNewTabPageSigninPromoDismissed(false); 792 .setNewTabPageSigninPromoDismissed(false);
795 MockNewTabPageManager ntpManager = new MockNewTabPageManager(mSource); 793 MockNewTabPageManager ntpManager = new MockNewTabPageManager(mSource);
796 NewTabPageAdapter adapter = new NewTabPageAdapter(ntpManager, null, null ); 794 NewTabPageAdapter adapter = new NewTabPageAdapter(ntpManager, null, null );
797 final int signInPromoIndex = 5; 795 final int signInPromoIndex = 5;
798 796
799 assertEquals(5, adapter.getGroups().size()); 797 assertEquals(5, adapter.getGroups().size());
800 ItemGroup signinPromoGroup = adapter.getGroup(signInPromoIndex); 798 TreeNode signinPromoGroup = adapter.getGroups().get(2);
801 799
802 // Adapter content: 800 // Adapter content:
803 // Idx | Item 801 // Idx | Item | Group Index
804 // ----|---------------- 802 // ----|--------------------|-------------
805 // 0 | Above-the-fold 803 // 0 | Above-the-fold | 0
806 // 1 | Header 804 // 1 | Header | 1
807 // 2 | Status 805 // 2 | Status | 1
808 // 3 | Action 806 // 3 | Action | 1
809 // 4 | Progress Indicator 807 // 4 | Progress Indicator | 1
810 // 5 | Sign in promo 808 // 5 | Sign in promo | 2
811 // 6 | Footer 809 // 6 | Footer | 3
812 // 7 | Spacer 810 // 7 | Spacer | 4
813 811
814 assertEquals(NewTabPageItem.VIEW_TYPE_PROMO, signinPromoGroup.getItems() .get(0).getType()); 812 assertEquals(ItemViewType.PROMO, signinPromoGroup.getItemViewType(0));
815 813
816 adapter.dismissItem(signInPromoIndex); 814 adapter.dismissItem(signInPromoIndex);
817 assertTrue(signinPromoGroup.getItems().isEmpty()); 815 assertEquals(0, signinPromoGroup.getItemCount());
818 assertTrue(ChromePreferenceManager.getInstance(RuntimeEnvironment.applic ation) 816 assertTrue(ChromePreferenceManager.getInstance(RuntimeEnvironment.applic ation)
819 .getNewTabPageSigninPromoDismissed()); 817 .getNewTabPageSigninPromoDismissed());
820 818
821 adapter = new NewTabPageAdapter(ntpManager, null, null); 819 adapter = new NewTabPageAdapter(ntpManager, null, null);
822 assertEquals(5, adapter.getGroups().size()); 820 assertEquals(5, adapter.getGroups().size());
823 // The items below the signin promo move up, footer is now at the positi on of the promo. 821 // The items below the signin promo move up, footer is now at the positi on of the promo.
824 assertEquals(NewTabPageItem.VIEW_TYPE_FOOTER, adapter.getItemViewType(si gnInPromoIndex)); 822 assertEquals(ItemViewType.FOOTER, adapter.getItemViewType(signInPromoInd ex));
825 } 823 }
826 824
827 /** Registers the category with hasMoreButton=false and showIfEmpty=true*/ 825 /** Registers the category with hasMoreButton=false and showIfEmpty=true*/
828 private void registerCategory(FakeSuggestionsSource suggestionsSource, 826 private void registerCategory(FakeSuggestionsSource suggestionsSource,
829 @CategoryInt int category, int suggestionCount) { 827 @CategoryInt int category, int suggestionCount) {
830 // FakeSuggestionSource does not provide suggestions if the category's s tatus is not 828 // FakeSuggestionSource does not provide suggestions if the category's s tatus is not
831 // AVAILABLE. 829 // AVAILABLE.
832 suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABL E); 830 suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABL E);
833 // Important: showIfEmpty flag to true. 831 // Important: showIfEmpty flag to true.
834 suggestionsSource.setInfoForCategory(category, createInfo(category, fals e, true)); 832 suggestionsSource.setInfoForCategory(category, createInfo(category, fals e, true));
835 suggestionsSource.setSuggestionsForCategory( 833 suggestionsSource.setSuggestionsForCategory(
836 category, createDummySuggestions(suggestionCount)); 834 category, createDummySuggestions(suggestionCount));
837 } 835 }
838 836
839 private int getCategory(ItemGroup itemGroup) { 837 private int getCategory(TreeNode itemGroup) {
840 return ((SuggestionsSection) itemGroup).getCategory(); 838 return ((SuggestionsSection) itemGroup).getCategory();
841 } 839 }
842 840
843 private static class MockNewTabPageManager implements NewTabPageManager { 841 private static class MockNewTabPageManager implements NewTabPageManager {
844 SuggestionsSource mSuggestionsSource; 842 SuggestionsSource mSuggestionsSource;
845 SignInStateObserver mSignInStateObserver; 843 SignInStateObserver mSignInStateObserver;
846 844
847 public MockNewTabPageManager(SuggestionsSource suggestionsSource) { 845 public MockNewTabPageManager(SuggestionsSource suggestionsSource) {
848 mSuggestionsSource = suggestionsSource; 846 mSuggestionsSource = suggestionsSource;
849 } 847 }
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
1007 public void registerSignInStateObserver(SignInStateObserver signInStateO bserver) { 1005 public void registerSignInStateObserver(SignInStateObserver signInStateO bserver) {
1008 mSignInStateObserver = signInStateObserver; 1006 mSignInStateObserver = signInStateObserver;
1009 } 1007 }
1010 1008
1011 @Override 1009 @Override
1012 public boolean isCurrentPage() { 1010 public boolean isCurrentPage() {
1013 return true; 1011 return true;
1014 } 1012 }
1015 } 1013 }
1016 } 1014 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698