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

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

Issue 1888913002: [NTP Snippets] Refresh the displayed snippets less frequently (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase and fix tests Created 4 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapterTest.java
diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapterTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapterTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..c4623c657c5aa3cbe7e70363497d58da0a14304b
--- /dev/null
+++ b/chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapterTest.java
@@ -0,0 +1,147 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.ntp.cards;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+
+import org.chromium.base.BaseChromiumApplication;
+import org.chromium.base.test.util.Feature;
+import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager;
+import org.chromium.chrome.browser.ntp.snippets.SnippetArticle;
+import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge.SnippetsObserver;
+import org.chromium.chrome.browser.tab.TabObserver;
+import org.chromium.testing.local.LocalRobolectricTestRunner;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+import org.robolectric.annotation.Config;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+
+/**
+ * Unit tests for {@link NewTabPageAdapter}.
+ */
+@RunWith(LocalRobolectricTestRunner.class)
+@Config(manifest = Config.NONE, application = BaseChromiumApplication.class)
+public class NewTabPageAdapterTest {
+
+ private NewTabPageManager mNewTabPageManager;
+ private SnippetsObserver mSnippetsObserver;
+ private TabObserver mTabObserver;
+
+ @Before
+ public void setUp() {
+ mNewTabPageManager = mock(NewTabPageManager.class);
+ mSnippetsObserver = null;
+ mTabObserver = null;
+
+ // Intercept the observers so that we can mock invocations.
+ doAnswer(new Answer<Void>() {
+ @Override
+ public Void answer(InvocationOnMock invocation) throws Throwable {
+ mSnippetsObserver = invocation.getArgumentAt(0, SnippetsObserver.class);
+ return null;
+ }}).when(mNewTabPageManager).setSnippetsObserver(any(SnippetsObserver.class));
+
+ doAnswer(new Answer<Void>() {
+ @Override
+ public Void answer(InvocationOnMock invocation) throws Throwable {
+ mTabObserver = invocation.getArgumentAt(0, TabObserver.class);
+ return null;
+ }}).when(mNewTabPageManager).addTabObserver(any(TabObserver.class));
+ }
+
+ /**
+ * Tests the content of the adapter under standard conditions: on start and after a snippet
+ * fetch.
+ */
+ @Test
+ @Feature({"Ntp"})
+ public void testSnippetLoading() {
+ NewTabPageAdapter ntpa = new NewTabPageAdapter(mNewTabPageManager, null);
+ assertEquals(1, ntpa.getItemCount());
+ assertEquals(NewTabPageListItem.VIEW_TYPE_ABOVE_THE_FOLD, ntpa.getItemViewType(0));
+
+ List<SnippetArticle> snippets = Arrays.asList(new SnippetArticle[] {
+ new SnippetArticle("title1", "pub1", "txt1", "https://site.com/url1", null, 0, 0),
+ new SnippetArticle("title2", "pub2", "txt2", "https://site.com/url2", null, 0, 0),
+ new SnippetArticle("title3", "pub3", "txt3", "https://site.com/url3", null, 0, 0)});
+ mSnippetsObserver.onSnippetsReceived(snippets);
+
+ List<NewTabPageListItem> loadedItems = ntpa.getItemsForTesting();
+ assertEquals(NewTabPageListItem.VIEW_TYPE_ABOVE_THE_FOLD, ntpa.getItemViewType(0));
+ assertEquals(NewTabPageListItem.VIEW_TYPE_HEADER, ntpa.getItemViewType(1));
+ assertEquals(snippets, loadedItems.subList(2, loadedItems.size()));
+ assertNull(mSnippetsObserver);
+ }
+
+ /**
+ * Tests that the adapter keeps listening for snippet updates if it didn't get anything from
+ * a previous fetch.
+ */
+ @Test
+ @Feature({"Ntp"})
+ public void testSnippetLoadingInitiallyEmpty() {
+ NewTabPageAdapter ntpa = new NewTabPageAdapter(mNewTabPageManager, null);
+
+ // If we don't get anything, we should still have the above-the-fold item present.
+ mSnippetsObserver.onSnippetsReceived(new ArrayList<SnippetArticle>());
+ assertEquals(1, ntpa.getItemCount());
+ assertEquals(NewTabPageListItem.VIEW_TYPE_ABOVE_THE_FOLD, ntpa.getItemViewType(0));
+ assertNotNull(mSnippetsObserver);
+
+ // We should load new snippets when we get notified about them.
+ List<SnippetArticle> snippets = Arrays.asList(new SnippetArticle[] {
+ new SnippetArticle("title1", "pub1", "txt1", "https://site.com/url1", null, 0, 0),
+ new SnippetArticle("title2", "pub2", "txt2", "https://site.com/url2", null, 0, 0)});
+ mSnippetsObserver.onSnippetsReceived(snippets);
+ List<NewTabPageListItem> loadedItems = ntpa.getItemsForTesting();
+ assertEquals(NewTabPageListItem.VIEW_TYPE_ABOVE_THE_FOLD, ntpa.getItemViewType(0));
+ assertEquals(NewTabPageListItem.VIEW_TYPE_HEADER, ntpa.getItemViewType(1));
+ assertEquals(snippets, loadedItems.subList(2, loadedItems.size()));
+ assertNull(mSnippetsObserver);
+ }
+
+ /**
+ * Tests the behavior on multiple loads. The snippet list should contain only the new ones.
+ */
+ @Test
+ @Feature({"Ntp"})
+ public void testSnippetMultipleLoads() {
+ NewTabPageAdapter ntpa = new NewTabPageAdapter(mNewTabPageManager, null);
+
+ List<SnippetArticle> snippets = Arrays.asList(new SnippetArticle[] {
+ new SnippetArticle("title1", "pub1", "txt1", "https://site.com/url1", null, 0, 0),
+ new SnippetArticle("title2", "pub2", "txt2", "https://site.com/url2", null, 0, 0)});
+ mSnippetsObserver.onSnippetsReceived(snippets);
+
+ // Successfull load, so we should have stopped listening to snippet changes
+ assertNull(mSnippetsObserver);
+
+ // We should be listening to tab events though, and use it to request changes.
+ mTabObserver.onShown(null);
+ assertNotNull(mSnippetsObserver);
+
+ snippets = Arrays.asList(new SnippetArticle[] {
+ new SnippetArticle("title2", "pub2", "txt2", "https://site.com/url2", null, 0, 0),
+ new SnippetArticle("title3", "pub3", "txt3", "https://site.com/url3", null, 0, 0)});
+ mSnippetsObserver.onSnippetsReceived(snippets);
+
+ List<NewTabPageListItem> loadedItems = ntpa.getItemsForTesting();
+ assertEquals(NewTabPageListItem.VIEW_TYPE_ABOVE_THE_FOLD, ntpa.getItemViewType(0));
+ assertEquals(NewTabPageListItem.VIEW_TYPE_HEADER, ntpa.getItemViewType(1));
+ assertEquals(snippets, loadedItems.subList(2, loadedItems.size()));
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698