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

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

Issue 2513453004: [Android NTP] Move suggestion sections into a separate node. (Closed)
Patch Set: sync Created 4 years 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.hamcrest.CoreMatchers.is; 7 import static org.hamcrest.CoreMatchers.is;
8 import static org.junit.Assert.assertThat; 8 import static org.junit.Assert.assertThat;
9 import static org.mockito.Mockito.mock; 9 import static org.mockito.Mockito.mock;
10 import static org.mockito.Mockito.verify; 10 import static org.mockito.Mockito.verify;
11 import static org.mockito.Mockito.verifyNoMoreInteractions;
11 import static org.mockito.Mockito.when; 12 import static org.mockito.Mockito.when;
12 13
13 import org.junit.Before; 14 import org.junit.Before;
14 import org.junit.Test; 15 import org.junit.Test;
15 import org.junit.runner.RunWith; 16 import org.junit.runner.RunWith;
16 import org.mockito.Mock; 17 import org.mockito.Mock;
17 import org.mockito.MockitoAnnotations; 18 import org.mockito.MockitoAnnotations;
18 import org.robolectric.annotation.Config; 19 import org.robolectric.annotation.Config;
19 20
20 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; 21 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle;
21 import org.chromium.testing.local.LocalRobolectricTestRunner; 22 import org.chromium.testing.local.LocalRobolectricTestRunner;
22 23
23 import java.util.Arrays; 24 import java.util.ArrayList;
24 import java.util.List; 25 import java.util.List;
25 26
26 /** 27 /**
27 * JUnit tests for {@link InnerNode}. 28 * JUnit tests for {@link InnerNode}.
28 */ 29 */
29 @RunWith(LocalRobolectricTestRunner.class) 30 @RunWith(LocalRobolectricTestRunner.class)
30 @Config(manifest = Config.NONE) 31 @Config(manifest = Config.NONE)
31 public class InnerNodeTest { 32 public class InnerNodeTest {
32 33
33 private static final int[] ITEM_COUNTS = {1, 2, 3, 0, 3, 2, 1}; 34 private static final int[] ITEM_COUNTS = {1, 2, 3, 0, 3, 2, 1};
34 private final List<TreeNode> mChildren = Arrays.asList(new TreeNode[ITEM_COU NTS.length]); 35 private final List<TreeNode> mChildren = new ArrayList<>();
35 @Mock private NodeParent mParent; 36 @Mock private NodeParent mParent;
36 private InnerNode mInnerNode; 37 private InnerNode mInnerNode;
37 38
38 @Before 39 @Before
39 public void setUp() { 40 public void setUp() {
40 MockitoAnnotations.initMocks(this); 41 MockitoAnnotations.initMocks(this);
41 42
42 for (int i = 0; i < ITEM_COUNTS.length; i++) { 43 for (int i = 0; i < ITEM_COUNTS.length; i++) {
43 TreeNode child = mock(TreeNode.class); 44 TreeNode child = mock(TreeNode.class);
44 when(child.getItemCount()).thenReturn(ITEM_COUNTS[i]); 45 when(child.getItemCount()).thenReturn(ITEM_COUNTS[i]);
45 mChildren.set(i, child); 46 mChildren.add(child);
46 } 47 }
47 mInnerNode = new InnerNode(mParent) { 48 mInnerNode = new InnerNode(mParent) {
48 @Override 49 @Override
49 protected List<TreeNode> getChildren() { 50 protected List<TreeNode> getChildren() {
50 return mChildren; 51 return mChildren;
51 } 52 }
52 }; 53 };
53 } 54 }
54 55
55 @Test 56 @Test
57 public void testInit() {
58 mInnerNode.init();
59 for (TreeNode child : mChildren) {
60 verify(child).init();
61 }
62 }
63
64 @Test
56 public void testItemCount() { 65 public void testItemCount() {
57 assertThat(mInnerNode.getItemCount(), is(12)); 66 assertThat(mInnerNode.getItemCount(), is(12));
58 } 67 }
59 68
60 @Test 69 @Test
61 public void testGetItemViewType() { 70 public void testGetItemViewType() {
62 when(mChildren.get(0).getItemViewType(0)).thenReturn(42); 71 when(mChildren.get(0).getItemViewType(0)).thenReturn(42);
63 when(mChildren.get(2).getItemViewType(2)).thenReturn(23); 72 when(mChildren.get(2).getItemViewType(2)).thenReturn(23);
64 when(mChildren.get(4).getItemViewType(0)).thenReturn(4711); 73 when(mChildren.get(4).getItemViewType(0)).thenReturn(4711);
65 when(mChildren.get(6).getItemViewType(0)).thenReturn(1729); 74 when(mChildren.get(6).getItemViewType(0)).thenReturn(1729);
(...skipping 30 matching lines...) Expand all
96 when(mChildren.get(4).getSuggestionAt(0)).thenReturn(article3); 105 when(mChildren.get(4).getSuggestionAt(0)).thenReturn(article3);
97 when(mChildren.get(6).getSuggestionAt(0)).thenReturn(article4); 106 when(mChildren.get(6).getSuggestionAt(0)).thenReturn(article4);
98 107
99 assertThat(mInnerNode.getSuggestionAt(0), is(article1)); 108 assertThat(mInnerNode.getSuggestionAt(0), is(article1));
100 assertThat(mInnerNode.getSuggestionAt(5), is(article2)); 109 assertThat(mInnerNode.getSuggestionAt(5), is(article2));
101 assertThat(mInnerNode.getSuggestionAt(6), is(article3)); 110 assertThat(mInnerNode.getSuggestionAt(6), is(article3));
102 assertThat(mInnerNode.getSuggestionAt(11), is(article4)); 111 assertThat(mInnerNode.getSuggestionAt(11), is(article4));
103 } 112 }
104 113
105 @Test 114 @Test
115 public void testDidAddChild() {
116 TreeNode child = mock(TreeNode.class);
117 when(child.getItemCount()).thenReturn(23);
118 mChildren.add(3, child);
119 mInnerNode.didAddChild(child);
120
121 // The child should have been initialized and the parent notified about the added items.
122 verify(child).init();
123 verify(mParent).onItemRangeInserted(mInnerNode, 6, 23);
124
125 TreeNode child2 = mock(TreeNode.class);
126 when(child2.getItemCount()).thenReturn(0);
127 mChildren.add(4, child2);
128 mInnerNode.didAddChild(child2);
129 verify(child2).init();
130
131 // The empty child should have been initialized, but there should be no change
132 // notifications.
133 verifyNoMoreInteractions(mParent);
134 }
135
136 @Test
137 public void testWillRemoveChild() {
138 mInnerNode.willRemoveChild(mChildren.get(4));
139 mChildren.remove(4);
140
141 // The parent should have been notified about the removed items.
142 verify(mParent).onItemRangeRemoved(mInnerNode, 6, 3);
143
144 mInnerNode.willRemoveChild(mChildren.get(3));
145 mChildren.remove(3);
146
147 // There should be no change notifications about the empty child.
148 verifyNoMoreInteractions(mParent);
149 }
150
151 @Test
106 public void testNotifications() { 152 public void testNotifications() {
107 mInnerNode.onItemRangeInserted(mChildren.get(0), 0, 23); 153 mInnerNode.onItemRangeInserted(mChildren.get(0), 0, 23);
108 mInnerNode.onItemRangeChanged(mChildren.get(2), 2, 9000); 154 mInnerNode.onItemRangeChanged(mChildren.get(2), 2, 9000);
109 mInnerNode.onItemRangeChanged(mChildren.get(4), 0, 6502); 155 mInnerNode.onItemRangeChanged(mChildren.get(4), 0, 6502);
110 mInnerNode.onItemRangeRemoved(mChildren.get(6), 0, 8086); 156 mInnerNode.onItemRangeRemoved(mChildren.get(6), 0, 8086);
111 157
112 verify(mParent).onItemRangeInserted(mInnerNode, 0, 23); 158 verify(mParent).onItemRangeInserted(mInnerNode, 0, 23);
113 verify(mParent).onItemRangeChanged(mInnerNode, 5, 9000); 159 verify(mParent).onItemRangeChanged(mInnerNode, 5, 9000);
114 verify(mParent).onItemRangeChanged(mInnerNode, 6, 6502); 160 verify(mParent).onItemRangeChanged(mInnerNode, 6, 6502);
115 verify(mParent).onItemRangeRemoved(mInnerNode, 11, 8086); 161 verify(mParent).onItemRangeRemoved(mInnerNode, 11, 8086);
116 } 162 }
117 } 163 }
118 164
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698