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