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

Side by Side Diff: chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/SuggestionsSectionTest.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
« no previous file with comments | « chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapterTest.java ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.assertNotNull; 8 import static org.junit.Assert.assertNotNull;
9 import static org.mockito.ArgumentMatchers.any; 9 import static org.mockito.ArgumentMatchers.any;
10 import static org.mockito.ArgumentMatchers.anyInt; 10 import static org.mockito.ArgumentMatchers.anyInt;
11 import static org.mockito.ArgumentMatchers.eq;
12 import static org.mockito.Mockito.never; 11 import static org.mockito.Mockito.never;
13 import static org.mockito.Mockito.times; 12 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify; 13 import static org.mockito.Mockito.verify;
15 14
15 import static org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils. createDummySuggestions;
16 import static org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils. createSection;
17
16 import org.junit.Before; 18 import org.junit.Before;
17 import org.junit.Test; 19 import org.junit.Test;
18 import org.junit.runner.RunWith; 20 import org.junit.runner.RunWith;
19 import org.mockito.Mock; 21 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations; 22 import org.mockito.MockitoAnnotations;
21 import org.robolectric.annotation.Config; 23 import org.robolectric.annotation.Config;
22 24
23 import static org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils. createDummySuggestions;
24 import static org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils. createSection;
25
26 import org.chromium.base.test.util.Feature; 25 import org.chromium.base.test.util.Feature;
27 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus; 26 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus;
28 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; 27 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle;
29 import org.chromium.testing.local.LocalRobolectricTestRunner; 28 import org.chromium.testing.local.LocalRobolectricTestRunner;
30 29
31 import java.util.List; 30 import java.util.List;
32 31
33 /** 32 /**
34 * Unit tests for {@link SuggestionsSection}. 33 * Unit tests for {@link SuggestionsSection}.
35 */ 34 */
36 @RunWith(LocalRobolectricTestRunner.class) 35 @RunWith(LocalRobolectricTestRunner.class)
37 @Config(manifest = Config.NONE) 36 @Config(manifest = Config.NONE)
38 public class SuggestionsSectionTest { 37 public class SuggestionsSectionTest {
39 /** 38 /**
40 * Number of items in a section when there are no suggestions: header, statu s, action, progress. 39 * Number of items in a section when there are no suggestions: header, statu s, action, progress.
41 */ 40 */
42 private static final int EMPTY_SECTION_COUNT = 4; 41 private static final int EMPTY_SECTION_COUNT = 4;
43 42
44 @Mock 43 @Mock
45 private ItemGroup.Observer mObserver; 44 private NodeParent mParent;
46 45
47 @Before 46 @Before
48 public void setUp() { 47 public void setUp() {
49 MockitoAnnotations.initMocks(this); 48 MockitoAnnotations.initMocks(this);
50 } 49 }
51 50
52 @Test 51 @Test
53 @Feature({"Ntp"}) 52 @Feature({"Ntp"})
54 public void testDismissSibling() { 53 public void testDismissSibling() {
55 List<SnippetArticle> snippets = createDummySuggestions(3); 54 List<SnippetArticle> snippets = createDummySuggestions(3);
56 SuggestionsSection section; 55 SuggestionsSection section;
57 56
58 section = ContentSuggestionsTestUtils.createSection(true, true, mObserve r); 57 section = ContentSuggestionsTestUtils.createSection(true, true, mParent) ;
59 section.setStatus(CategoryStatus.AVAILABLE); 58 section.setStatus(CategoryStatus.AVAILABLE);
60 assertNotNull(section.getActionItem()); 59 assertNotNull(section.getActionItem());
61 60
62 // Without snippets. 61 // Without snippets.
63 assertEquals(-1, section.getDismissSiblingPosDelta(section.getActionItem ())); 62 assertEquals(ItemViewType.ACTION, section.getItemViewType(2));
64 assertEquals(1, section.getDismissSiblingPosDelta(section.getStatusItem( ))); 63 assertEquals(-1, section.getDismissSiblingPosDelta(2));
64 assertEquals(ItemViewType.STATUS, section.getItemViewType(1));
65 assertEquals(1, section.getDismissSiblingPosDelta(1));
65 66
66 // With snippets. 67 // With snippets.
67 section.setSuggestions(snippets, CategoryStatus.AVAILABLE); 68 section.setSuggestions(snippets, CategoryStatus.AVAILABLE);
68 assertEquals(0, section.getDismissSiblingPosDelta(section.getActionItem( ))); 69 assertEquals(ItemViewType.SNIPPET, section.getItemViewType(1));
69 assertEquals(0, section.getDismissSiblingPosDelta(section.getStatusItem( ))); 70 assertEquals(0, section.getDismissSiblingPosDelta(1));
70 assertEquals(0, section.getDismissSiblingPosDelta(snippets.get(0)));
71 } 71 }
72 72
73 @Test 73 @Test
74 @Feature({"Ntp"}) 74 @Feature({"Ntp"})
75 public void testSetSuggestionsNotification() { 75 public void testSetSuggestionsNotification() {
76 final int suggestionCount = 5; 76 final int suggestionCount = 5;
77 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount); 77 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount);
78 78
79 SuggestionsSection section = createSection(false, true, mObserver); 79 SuggestionsSection section = createSection(false, true, mParent);
80 // Note: when status is not initialised, we insert an item for the statu s card, but it's 80 // Note: when status is not initialised, we insert an item for the statu s card, but it's
81 // null! 81 // null!
82 assertEquals(EMPTY_SECTION_COUNT, section.getItems().size()); 82 assertEquals(EMPTY_SECTION_COUNT, section.getItemCount());
83 83
84 section.setSuggestions(snippets, CategoryStatus.AVAILABLE); 84 section.setSuggestions(snippets, CategoryStatus.AVAILABLE);
85 verify(mObserver).onItemRangeChanged(section, 1, EMPTY_SECTION_COUNT - 1 ); 85 verify(mParent).onItemRangeChanged(section, 1, EMPTY_SECTION_COUNT - 1);
86 verify(mObserver).onItemRangeInserted( 86 verify(mParent).onItemRangeInserted(
87 section, EMPTY_SECTION_COUNT, suggestionCount - EMPTY_SECTION_CO UNT + 1); 87 section, EMPTY_SECTION_COUNT, suggestionCount - EMPTY_SECTION_CO UNT + 1);
88 } 88 }
89 89
90 @Test 90 @Test
91 @Feature({"Ntp"}) 91 @Feature({"Ntp"})
92 public void testSetStatusNotification() { 92 public void testSetStatusNotification() {
93 final int suggestionCount = 5; 93 final int suggestionCount = 5;
94 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount); 94 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount);
95 95
96 SuggestionsSection section = createSection(false, true, mObserver); 96 SuggestionsSection section = createSection(false, true, mParent);
97 97
98 section.setStatus(CategoryStatus.AVAILABLE); 98 section.setStatus(CategoryStatus.AVAILABLE);
99 verify(mObserver).onItemRangeChanged(section, 1, EMPTY_SECTION_COUNT - 1 ); 99 verify(mParent).onItemRangeChanged(section, 1, EMPTY_SECTION_COUNT - 1);
100 100
101 section.setSuggestions(snippets, CategoryStatus.AVAILABLE); 101 section.setSuggestions(snippets, CategoryStatus.AVAILABLE);
102 102
103 // We don't clear suggestions when the status is AVAILABLE. 103 // We don't clear suggestions when the status is AVAILABLE.
104 section.setStatus(CategoryStatus.AVAILABLE); 104 section.setStatus(CategoryStatus.AVAILABLE);
105 verify(mObserver, times(2)).onItemRangeChanged(section, 1, EMPTY_SECTION _COUNT - 1); 105 verify(mParent, times(2)).onItemRangeChanged(section, 1, EMPTY_SECTION_C OUNT - 1);
106 verify(mObserver).onItemRangeInserted( 106 verify(mParent).onItemRangeInserted(
107 section, EMPTY_SECTION_COUNT, suggestionCount - EMPTY_SECTION_CO UNT + 1); 107 section, EMPTY_SECTION_COUNT, suggestionCount - EMPTY_SECTION_CO UNT + 1);
108 108
109 // We clear existing suggestions when the status is not AVAILABLE. 109 // We clear existing suggestions when the status is not AVAILABLE.
110 section.setStatus(CategoryStatus.SIGNED_OUT); 110 section.setStatus(CategoryStatus.SIGNED_OUT);
111 verify(mObserver, times(3)).onItemRangeChanged(section, 1, EMPTY_SECTION _COUNT - 1); 111 verify(mParent, times(3)).onItemRangeChanged(section, 1, EMPTY_SECTION_C OUNT - 1);
112 verify(mObserver).onItemRangeRemoved( 112 verify(mParent).onItemRangeRemoved(
113 section, EMPTY_SECTION_COUNT, suggestionCount - EMPTY_SECTION_CO UNT + 1); 113 section, EMPTY_SECTION_COUNT, suggestionCount - EMPTY_SECTION_CO UNT + 1);
114 } 114 }
115 115
116 @Test 116 @Test
117 @Feature({"Ntp"}) 117 @Feature({"Ntp"})
118 public void testRemoveSuggestionNotification() { 118 public void testRemoveSuggestionNotification() {
119 final int suggestionCount = 2; 119 final int suggestionCount = 2;
120 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount); 120 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount);
121 121
122 SuggestionsSection section = createSection(false, true, mObserver); 122 SuggestionsSection section = createSection(false, true, mParent);
123 123
124 section.removeSuggestion(snippets.get(0)); 124 section.removeSuggestion(snippets.get(0));
125 verify(mObserver, never()) 125 verify(mParent, never())
126 .onItemRangeChanged(any(SuggestionsSection.class), anyInt(), any Int()); 126 .onItemRangeChanged(any(SuggestionsSection.class), anyInt(), any Int());
127 verify(mObserver, never()) 127 verify(mParent, never())
128 .onItemRangeInserted(any(SuggestionsSection.class), anyInt(), an yInt()); 128 .onItemRangeInserted(any(SuggestionsSection.class), anyInt(), an yInt());
129 verify(mObserver, never()) 129 verify(mParent, never())
130 .onItemRangeRemoved(any(SuggestionsSection.class), anyInt(), any Int()); 130 .onItemRangeRemoved(any(SuggestionsSection.class), anyInt(), any Int());
131 131
132 section.setSuggestions(snippets, CategoryStatus.AVAILABLE); 132 section.setSuggestions(snippets, CategoryStatus.AVAILABLE);
133 133
134 section.removeSuggestion(snippets.get(1)); 134 section.removeSuggestion(snippets.get(1));
135 verify(mObserver).onItemRangeRemoved(eq(section), eq(2), eq(1)); 135 verify(mParent).onItemRangeRemoved(section, 2, 1);
136 136
137 section.removeSuggestion(snippets.get(0)); 137 section.removeSuggestion(snippets.get(0));
138 verify(mObserver).onItemRangeRemoved(eq(section), eq(1), eq(1)); 138 verify(mParent).onItemRangeRemoved(section, 1, 1);
139 verify(mObserver).onItemRangeInserted(eq(section), eq(1), eq(1)); 139 verify(mParent).onItemRangeInserted(section, 1, 1);
140 verify(mObserver).onItemRangeInserted(eq(section), eq(2), eq(1)); 140 verify(mParent).onItemRangeInserted(section, 2, 1);
141 } 141 }
142 142
143 @Test 143 @Test
144 @Feature({"Ntp"}) 144 @Feature({"Ntp"})
145 public void testRemoveSuggestionNotificationWithButton() { 145 public void testRemoveSuggestionNotificationWithButton() {
146 final int suggestionCount = 2; 146 final int suggestionCount = 2;
147 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount); 147 List<SnippetArticle> snippets = createDummySuggestions(suggestionCount);
148 148
149 SuggestionsSection section = createSection(true, true, mObserver); 149 SuggestionsSection section = createSection(true, true, mParent);
150 150
151 section.removeSuggestion(snippets.get(0)); 151 section.removeSuggestion(snippets.get(0));
152 verify(mObserver, never()) 152 verify(mParent, never())
153 .onItemRangeChanged(any(SuggestionsSection.class), anyInt(), any Int()); 153 .onItemRangeChanged(any(SuggestionsSection.class), anyInt(), any Int());
154 verify(mObserver, never()) 154 verify(mParent, never())
155 .onItemRangeInserted(any(SuggestionsSection.class), anyInt(), an yInt()); 155 .onItemRangeInserted(any(SuggestionsSection.class), anyInt(), an yInt());
156 verify(mObserver, never()) 156 verify(mParent, never())
157 .onItemRangeRemoved(any(SuggestionsSection.class), anyInt(), any Int()); 157 .onItemRangeRemoved(any(SuggestionsSection.class), anyInt(), any Int());
158 158
159 section.setSuggestions(snippets, CategoryStatus.AVAILABLE); 159 section.setSuggestions(snippets, CategoryStatus.AVAILABLE);
160 160
161 section.removeSuggestion(snippets.get(0)); 161 section.removeSuggestion(snippets.get(0));
162 verify(mObserver).onItemRangeRemoved(eq(section), eq(1), eq(1)); 162 verify(mParent).onItemRangeRemoved(section, 1, 1);
163 163
164 section.removeSuggestion(snippets.get(1)); 164 section.removeSuggestion(snippets.get(1));
165 verify(mObserver, times(2)).onItemRangeRemoved(eq(section), eq(1), eq(1) ); 165 verify(mParent, times(2)).onItemRangeRemoved(section, 1, 1);
166 verify(mObserver).onItemRangeInserted(eq(section), eq(1), eq(1)); 166 verify(mParent).onItemRangeInserted(section, 1, 1);
167 verify(mObserver).onItemRangeInserted(eq(section), eq(3), eq(1)); 167 verify(mParent).onItemRangeInserted(section, 3, 1);
168 } 168 }
169 } 169 }
OLDNEW
« no previous file with comments | « chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapterTest.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698