Chromium Code Reviews| Index: chrome/android/javatests/src/org/chromium/chrome/browser/physicalweb/UrlManagerTest.java |
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/physicalweb/UrlManagerTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/physicalweb/UrlManagerTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6d4c139eabb784bb4f8b6f6b0d76eaa3c77d6957 |
| --- /dev/null |
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/physicalweb/UrlManagerTest.java |
| @@ -0,0 +1,117 @@ |
| +// Copyright 2015 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.physicalweb; |
| + |
| +import android.app.Activity; |
| +import android.content.Context; |
| +import android.content.SharedPreferences; |
| +import android.preference.PreferenceManager; |
| +import android.test.suitebuilder.annotation.SmallTest; |
| + |
| +import org.chromium.base.test.BaseActivityInstrumentationTestCase; |
| +import org.chromium.chrome.test.util.browser.notifications.MockNotificationManagerProxy; |
| +import org.chromium.chrome.test.util.browser.notifications.MockNotificationManagerProxy.NotificationEntry; |
| + |
| +import java.util.ArrayList; |
| +import java.util.Collection; |
| +import java.util.List; |
| +import java.util.Set; |
| + |
| +/** |
| + * Tests for the UrlManager class. |
| + */ |
| +public class UrlManagerTest extends BaseActivityInstrumentationTestCase<Activity> { |
|
newt (away)
2016/01/05 19:49:09
If you don't actually need an activity to run this
cco3
2016/01/06 20:02:15
Done.
|
| + static final String URL1 = "https://example.com/"; |
| + static final String TITLE1 = "Example"; |
| + static final String DESC1 = "Example Website"; |
| + static final String PREF_PHYSICAL_WEB = "physical_web"; |
|
mattreynolds
2016/01/05 19:27:21
Nit: Can we make this and the PHYSICAL_WEB_* const
cco3
2016/01/06 20:02:15
That's a different package. We can move them to P
|
| + static final int PHYSICAL_WEB_OFF = 0; |
| + static final int PHYSICAL_WEB_ON = 1; |
| + static final int PHYSICAL_WEB_ONBOARDING = 2; |
| + UrlManager mUrlManager = null; |
| + MockPwsClient mMockPwsClient = null; |
| + MockNotificationManagerProxy mMockNotificationManagerProxy = null; |
| + SharedPreferences mSharedPreferences = null; |
| + |
| + public UrlManagerTest() { |
| + super(Activity.class); |
| + } |
| + |
| + @Override |
| + protected void setUp() throws Exception { |
| + super.setUp(); |
| + Context context = getInstrumentation().getTargetContext().getApplicationContext(); |
| + mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); |
| + mSharedPreferences.edit().putInt(PREF_PHYSICAL_WEB, PHYSICAL_WEB_ON).apply(); |
| + mUrlManager = new UrlManager(context); |
| + mMockPwsClient = new MockPwsClient(); |
| + mUrlManager.overridePwsClientForTesting(mMockPwsClient); |
| + mMockNotificationManagerProxy = new MockNotificationManagerProxy(); |
| + mUrlManager.overrideNotificationManagerForTesting(mMockNotificationManagerProxy); |
| + } |
| + |
| + private void setOnboarding() { |
| + mSharedPreferences.edit().putInt(PREF_PHYSICAL_WEB, PHYSICAL_WEB_ONBOARDING).apply(); |
| + } |
| + |
| + @SmallTest |
| + public void testAddUrlWhileOnboardingMakesNotification() throws Exception { |
| + setOnboarding(); |
| + ArrayList<PwsResult> results = new ArrayList<>(); |
| + results.add(new PwsResult(URL1, URL1, null, TITLE1, DESC1)); |
| + mMockPwsClient.addPwsResults(results); |
| + mUrlManager.addUrl(URL1); |
| + |
| + // Make sure that a resolution was *not* attempted. |
| + List<Collection<String>> resolveCalls = mMockPwsClient.getResolveCalls(); |
| + assertEquals(0, resolveCalls.size()); |
| + |
| + // Make sure that we have no resolved URLs. |
| + Set<String> urls = mUrlManager.getUrls(); |
| + assertEquals(0, urls.size()); |
| + |
| + // Make sure that a notification was shown. |
| + List<NotificationEntry> notifications = mMockNotificationManagerProxy.getNotifications(); |
| + assertEquals(1, notifications.size()); |
| + } |
| + |
| + @SmallTest |
| + public void testAddUrlNoResolutionDoesNothing() throws Exception { |
| + mMockPwsClient.addPwsResults(new ArrayList<PwsResult>()); |
| + mUrlManager.addUrl(URL1); |
| + |
| + // Make sure that a resolution was attempted. |
| + List<Collection<String>> resolveCalls = mMockPwsClient.getResolveCalls(); |
| + assertEquals(1, resolveCalls.size()); |
| + |
| + // Make sure that we have no resolved URLs. |
| + Set<String> urls = mUrlManager.getUrls(); |
|
mattreynolds
2016/01/05 19:27:21
This would be a convenient place to test that getU
cco3
2016/01/06 20:02:15
Done.
|
| + assertEquals(0, urls.size()); |
| + |
| + // Make sure that a notification was not shown. |
| + List<NotificationEntry> notifications = mMockNotificationManagerProxy.getNotifications(); |
| + assertEquals(0, notifications.size()); |
| + } |
| + |
| + @SmallTest |
| + public void testAddUrlWithResolutionMakesNotification() throws Exception { |
| + ArrayList<PwsResult> results = new ArrayList<>(); |
| + results.add(new PwsResult(URL1, URL1, null, TITLE1, DESC1)); |
| + mMockPwsClient.addPwsResults(results); |
| + mUrlManager.addUrl(URL1); |
| + |
| + // Make sure that a resolution was attempted. |
| + List<Collection<String>> resolveCalls = mMockPwsClient.getResolveCalls(); |
| + assertEquals(1, resolveCalls.size()); |
| + |
| + // Make sure that we have our resolved URLs. |
| + Set<String> urls = mUrlManager.getUrls(); |
| + assertEquals(1, urls.size()); |
| + |
| + // Make sure that a notification was shown. |
| + List<NotificationEntry> notifications = mMockNotificationManagerProxy.getNotifications(); |
| + assertEquals(1, notifications.size()); |
| + } |
| +} |