Chromium Code Reviews| Index: chrome/android/junit/src/org/chromium/chrome/browser/AppIndexingUtilTest.java |
| diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/AppIndexingUtilTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/AppIndexingUtilTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5e9cbf269e7533d745f4a2438db17905050fc4cf |
| --- /dev/null |
| +++ b/chrome/android/junit/src/org/chromium/chrome/browser/AppIndexingUtilTest.java |
| @@ -0,0 +1,126 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
|
wychen
2017/04/13 07:05:01
nit: 2017
dproctor
2017/04/13 17:23:09
Done.
|
| +// 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; |
| + |
| +import static org.mockito.ArgumentMatchers.any; |
| +import static org.mockito.Mockito.doAnswer; |
| +import static org.mockito.Mockito.doReturn; |
| +import static org.mockito.Mockito.eq; |
| +import static org.mockito.Mockito.never; |
| +import static org.mockito.Mockito.times; |
| +import static org.mockito.Mockito.verify; |
| + |
| +import org.junit.Before; |
| +import org.junit.Test; |
| +import org.junit.runner.RunWith; |
| + |
| +import org.mockito.Mock; |
| +import org.mockito.MockitoAnnotations; |
| +import org.mockito.Spy; |
| +import org.mockito.invocation.InvocationOnMock; |
| +import org.mockito.stubbing.Answer; |
| +import org.robolectric.annotation.Config; |
| + |
| +import org.chromium.blink.mojom.document_metadata.CopylessPaste; |
| +import org.chromium.blink.mojom.document_metadata.WebPage; |
| +import org.chromium.chrome.browser.historyreport.AppIndexingReporter; |
| +import org.chromium.chrome.browser.tab.Tab; |
| +import org.chromium.testing.local.LocalRobolectricTestRunner; |
| +import org.chromium.url.mojom.Url; |
| + |
| +/** |
| + * Unit tests for {@link org.chromium.chrome.browser.AppIndexingUtil}. |
| + */ |
| +@RunWith(LocalRobolectricTestRunner.class) |
| +@Config(manifest = Config.NONE) |
| +public class AppIndexingUtilTest { |
| + @Spy |
| + AppIndexingUtil mUtil = new AppIndexingUtil(); |
| + @Mock |
| + private AppIndexingReporter mReporter; |
| + @Mock |
| + private CopylessPasteTestImpl mCopylessPaste; |
| + @Mock |
| + private Tab mTab; |
| + |
| + @Before |
| + public void setUp() { |
| + MockitoAnnotations.initMocks(this); |
| + doReturn(mReporter).when(mUtil).getAppIndexingReporter(); |
| + doReturn(mCopylessPaste).when(mUtil).getCopylessPasteInterface(any(Tab.class)); |
| + doReturn(true).when(mUtil).isEnabledForDevice(); |
| + doReturn(false).when(mTab).isIncognito(); |
| + |
| + doReturn("http://www.test.com").when(mTab).getUrl(); |
| + doReturn("My neat website").when(mTab).getTitle(); |
| + doReturn(0L).when(mUtil).getElapsedTime(); |
| + doAnswer(new Answer<Void>() { |
| + public Void answer(InvocationOnMock invocation) { |
| + CopylessPaste.GetEntitiesResponse callback = |
| + (CopylessPaste.GetEntitiesResponse) invocation.getArguments()[0]; |
| + WebPage webpage = new WebPage(); |
| + webpage.url = createUrl("http://www.test.com"); |
| + webpage.title = "My neat website"; |
| + callback.call(webpage); |
| + return null; |
| + } |
| + }) |
| + .when(mCopylessPaste) |
| + .getEntities(any(CopylessPaste.GetEntitiesResponse.class)); |
| + } |
| + |
| + @Test |
| + public void testNoCacheHit() { |
| + mUtil.extractCopylessPasteMetadata(mTab); |
| + verify(mCopylessPaste).getEntities(any(CopylessPaste.GetEntitiesResponse.class)); |
|
wychen
2017/04/13 07:05:00
verify(mReporter).reportWebPageView(any(WebPage.cl
dproctor
2017/04/13 17:23:09
Done.
wychen
2017/04/13 18:04:45
Sorry, I meant keeping both.
dproctor
2017/04/13 18:12:24
Done.
|
| + } |
| + |
| + @Test |
| + public void testCacheHit() { |
| + mUtil.extractCopylessPasteMetadata(mTab); |
| + verify(mCopylessPaste).getEntities(any(CopylessPaste.GetEntitiesResponse.class)); |
| + |
| + doReturn(1L).when(mUtil).getElapsedTime(); |
| + mUtil.extractCopylessPasteMetadata(mTab); |
| + verify(mReporter).reportWebPageView(eq("http://www.test.com"), eq("My neat website")); |
|
wychen
2017/04/13 07:05:00
Do we want a verifyNoMoreInteractions(mCopylessPas
dproctor
2017/04/13 17:23:09
Done.
wychen
2017/04/13 18:04:45
Why removing verify(mCopylessPaste)? We still want
dproctor
2017/04/13 18:12:24
Done.
|
| + } |
| + |
| + @Test |
| + public void testCacheHit_expired() { |
| + mUtil.extractCopylessPasteMetadata(mTab); |
| + |
| + doReturn(60 * 60 * 1000L + 1).when(mUtil).getElapsedTime(); |
| + mUtil.extractCopylessPasteMetadata(mTab); |
| + verify(mCopylessPaste, times(2)).getEntities(any(CopylessPaste.GetEntitiesResponse.class)); |
| + } |
| + |
| + @Test |
| + public void testCacheHit_noEntity() { |
| + doAnswer(new Answer<Void>() { |
| + public Void answer(InvocationOnMock invocation) { |
| + CopylessPaste.GetEntitiesResponse callback = |
| + (CopylessPaste.GetEntitiesResponse) invocation.getArguments()[0]; |
| + callback.call(null); |
| + return null; |
| + } |
| + }) |
| + .when(mCopylessPaste) |
| + .getEntities(any(CopylessPaste.GetEntitiesResponse.class)); |
| + mUtil.extractCopylessPasteMetadata(mTab); |
| + |
| + doReturn(1L).when(mUtil).getElapsedTime(); |
| + mUtil.extractCopylessPasteMetadata(mTab); |
| + verify(mCopylessPaste, times(1)).getEntities(any(CopylessPaste.GetEntitiesResponse.class)); |
| + verify(mReporter, never()).reportWebPageView(any(String.class), any(String.class)); |
|
wychen
2017/04/13 07:05:01
Probably verifyZeroInteractions(mReporter) to catc
dproctor
2017/04/13 17:23:09
Done.
|
| + } |
| + |
| + private Url createUrl(String s) { |
| + Url url = new Url(); |
| + url.url = s; |
| + return url; |
| + } |
| + |
| + abstract static class CopylessPasteTestImpl implements CopylessPaste {} |
| +} |