OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.physicalweb; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.content.SharedPreferences; |
| 9 import android.preference.PreferenceManager; |
| 10 import android.test.InstrumentationTestCase; |
| 11 import android.test.suitebuilder.annotation.SmallTest; |
| 12 |
| 13 import org.chromium.chrome.test.util.browser.notifications.MockNotificationManag
erProxy; |
| 14 import org.chromium.chrome.test.util.browser.notifications.MockNotificationManag
erProxy.NotificationEntry; |
| 15 |
| 16 import java.util.ArrayList; |
| 17 import java.util.Collection; |
| 18 import java.util.List; |
| 19 import java.util.Set; |
| 20 |
| 21 /** |
| 22 * Tests for the UrlManager class. |
| 23 */ |
| 24 public class UrlManagerTest extends InstrumentationTestCase { |
| 25 static final String URL1 = "https://example.com/"; |
| 26 static final String TITLE1 = "Example"; |
| 27 static final String DESC1 = "Example Website"; |
| 28 static final String PREF_PHYSICAL_WEB = "physical_web"; |
| 29 static final int PHYSICAL_WEB_OFF = 0; |
| 30 static final int PHYSICAL_WEB_ON = 1; |
| 31 static final int PHYSICAL_WEB_ONBOARDING = 2; |
| 32 UrlManager mUrlManager = null; |
| 33 MockPwsClient mMockPwsClient = null; |
| 34 MockNotificationManagerProxy mMockNotificationManagerProxy = null; |
| 35 SharedPreferences mSharedPreferences = null; |
| 36 |
| 37 @Override |
| 38 protected void setUp() throws Exception { |
| 39 super.setUp(); |
| 40 Context context = getInstrumentation().getTargetContext().getApplication
Context(); |
| 41 mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(conte
xt); |
| 42 mSharedPreferences.edit().putInt(PREF_PHYSICAL_WEB, PHYSICAL_WEB_ON).app
ly(); |
| 43 mUrlManager = new UrlManager(context); |
| 44 mMockPwsClient = new MockPwsClient(); |
| 45 mUrlManager.overridePwsClientForTesting(mMockPwsClient); |
| 46 mMockNotificationManagerProxy = new MockNotificationManagerProxy(); |
| 47 mUrlManager.overrideNotificationManagerForTesting(mMockNotificationManag
erProxy); |
| 48 } |
| 49 |
| 50 private void setOnboarding() { |
| 51 mSharedPreferences.edit().putInt(PREF_PHYSICAL_WEB, PHYSICAL_WEB_ONBOARD
ING).apply(); |
| 52 } |
| 53 |
| 54 @SmallTest |
| 55 public void testAddUrlWhileOnboardingMakesNotification() throws Exception { |
| 56 setOnboarding(); |
| 57 ArrayList<PwsResult> results = new ArrayList<>(); |
| 58 results.add(new PwsResult(URL1, URL1, null, TITLE1, DESC1)); |
| 59 mMockPwsClient.addPwsResults(results); |
| 60 mUrlManager.addUrl(URL1); |
| 61 |
| 62 // Make sure that a resolution was *not* attempted. |
| 63 List<Collection<String>> resolveCalls = mMockPwsClient.getResolveCalls()
; |
| 64 assertEquals(0, resolveCalls.size()); |
| 65 |
| 66 // Make sure that we have no resolved URLs. |
| 67 Set<String> urls = mUrlManager.getUrls(); |
| 68 assertEquals(0, urls.size()); |
| 69 |
| 70 // Make sure that a notification was shown. |
| 71 List<NotificationEntry> notifications = mMockNotificationManagerProxy.ge
tNotifications(); |
| 72 assertEquals(1, notifications.size()); |
| 73 } |
| 74 |
| 75 @SmallTest |
| 76 public void testAddUrlNoResolutionDoesNothing() throws Exception { |
| 77 mMockPwsClient.addPwsResults(new ArrayList<PwsResult>()); |
| 78 mUrlManager.addUrl(URL1); |
| 79 |
| 80 // Make sure that a resolution was attempted. |
| 81 List<Collection<String>> resolveCalls = mMockPwsClient.getResolveCalls()
; |
| 82 assertEquals(1, resolveCalls.size()); |
| 83 |
| 84 // Make sure that we have no resolved URLs. |
| 85 Set<String> urls = mUrlManager.getUrls(); |
| 86 assertEquals(0, urls.size()); |
| 87 // Make sure that we do have unresolved URLs. |
| 88 urls = mUrlManager.getUrls(true); |
| 89 assertEquals(1, urls.size()); |
| 90 |
| 91 // Make sure that a notification was not shown. |
| 92 List<NotificationEntry> notifications = mMockNotificationManagerProxy.ge
tNotifications(); |
| 93 assertEquals(0, notifications.size()); |
| 94 } |
| 95 |
| 96 @SmallTest |
| 97 public void testAddUrlWithResolutionMakesNotification() throws Exception { |
| 98 ArrayList<PwsResult> results = new ArrayList<>(); |
| 99 results.add(new PwsResult(URL1, URL1, null, TITLE1, DESC1)); |
| 100 mMockPwsClient.addPwsResults(results); |
| 101 mUrlManager.addUrl(URL1); |
| 102 |
| 103 // Make sure that a resolution was attempted. |
| 104 List<Collection<String>> resolveCalls = mMockPwsClient.getResolveCalls()
; |
| 105 assertEquals(1, resolveCalls.size()); |
| 106 |
| 107 // Make sure that we have our resolved URLs. |
| 108 Set<String> urls = mUrlManager.getUrls(); |
| 109 assertEquals(1, urls.size()); |
| 110 |
| 111 // Make sure that a notification was shown. |
| 112 List<NotificationEntry> notifications = mMockNotificationManagerProxy.ge
tNotifications(); |
| 113 assertEquals(1, notifications.size()); |
| 114 } |
| 115 } |
OLD | NEW |