OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.customtabs; | 5 package org.chromium.chrome.browser.customtabs; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.net.Uri; |
8 import android.os.Process; | 9 import android.os.Process; |
9 import android.support.customtabs.CustomTabsSessionToken; | 10 import android.support.customtabs.CustomTabsSessionToken; |
10 import android.support.test.InstrumentationRegistry; | 11 import android.support.test.InstrumentationRegistry; |
11 import android.support.test.filters.SmallTest; | 12 import android.support.test.filters.SmallTest; |
12 | 13 |
13 import org.junit.Assert; | 14 import org.junit.Assert; |
14 import org.junit.Before; | 15 import org.junit.Before; |
15 import org.junit.Rule; | 16 import org.junit.Rule; |
16 import org.junit.Test; | 17 import org.junit.Test; |
17 import org.junit.runner.RunWith; | 18 import org.junit.runner.RunWith; |
18 | 19 |
| 20 import org.chromium.base.ContextUtils; |
19 import org.chromium.base.metrics.RecordHistogram; | 21 import org.chromium.base.metrics.RecordHistogram; |
20 import org.chromium.base.test.BaseJUnit4ClassRunner; | 22 import org.chromium.base.test.BaseJUnit4ClassRunner; |
21 import org.chromium.base.test.util.MetricsUtils; | 23 import org.chromium.base.test.util.MetricsUtils; |
22 import org.chromium.base.test.util.RetryOnFailure; | 24 import org.chromium.base.test.util.RetryOnFailure; |
| 25 import org.chromium.chrome.browser.IntentHandler; |
23 import org.chromium.content.browser.test.NativeLibraryTestRule; | 26 import org.chromium.content.browser.test.NativeLibraryTestRule; |
| 27 import org.chromium.content.browser.test.util.Criteria; |
| 28 import org.chromium.content.browser.test.util.CriteriaHelper; |
24 | 29 |
25 /** Tests for ClientManager. */ | 30 /** Tests for ClientManager. */ |
26 @RunWith(BaseJUnit4ClassRunner.class) | 31 @RunWith(BaseJUnit4ClassRunner.class) |
27 public class ClientManagerTest { | 32 public class ClientManagerTest { |
28 @Rule | 33 @Rule |
29 public NativeLibraryTestRule mActivityTestRule = new NativeLibraryTestRule()
; | 34 public NativeLibraryTestRule mActivityTestRule = new NativeLibraryTestRule()
; |
30 | 35 |
31 private static final String URL = "https://www.android.com"; | 36 private static final String URL = "https://www.android.com"; |
32 private ClientManager mClientManager; | 37 private ClientManager mClientManager; |
33 private CustomTabsSessionToken mSession = | 38 private CustomTabsSessionToken mSession = |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 Assert.assertTrue(mClientManager.newSession(mSession, mUid, null, null))
; | 152 Assert.assertTrue(mClientManager.newSession(mSession, mUid, null, null))
; |
148 Assert.assertTrue( | 153 Assert.assertTrue( |
149 mClientManager.updateStatsAndReturnWhetherAllowed(mSession, mUid
, URL, false)); | 154 mClientManager.updateStatsAndReturnWhetherAllowed(mSession, mUid
, URL, false)); |
150 mClientManager.setIgnoreFragmentsForSession(mSession, true); | 155 mClientManager.setIgnoreFragmentsForSession(mSession, true); |
151 Assert.assertEquals(ClientManager.GOOD_PREDICTION, | 156 Assert.assertEquals(ClientManager.GOOD_PREDICTION, |
152 mClientManager.getPredictionOutcome(mSession, URL + "#fragment")
); | 157 mClientManager.getPredictionOutcome(mSession, URL + "#fragment")
); |
153 } | 158 } |
154 | 159 |
155 @Test | 160 @Test |
156 @SmallTest | 161 @SmallTest |
| 162 public void testPostMessageOriginVerification() { |
| 163 Assert.assertTrue( |
| 164 mClientManager.newSession(mSession, mUid, null, new PostMessageH
andler(mSession))); |
| 165 // Should always start with no origin. |
| 166 Assert.assertNull(mClientManager.getPostMessageOriginForSessionForTestin
g(mSession)); |
| 167 |
| 168 // With no prepopulated origins, this verification should fail. |
| 169 mClientManager.verifyAndInitializeWithPostMessageOriginForSession(mSessi
on, Uri.parse(URL)); |
| 170 Assert.assertNull(mClientManager.getPostMessageOriginForSessionForTestin
g(mSession)); |
| 171 |
| 172 // If there is a prepopulated origin, we should get a synchronous verifi
cation. |
| 173 OriginVerifier.prePopulateVerifiedOriginForTesting( |
| 174 ContextUtils.getApplicationContext().getPackageName(), Uri.parse
(URL)); |
| 175 mClientManager.verifyAndInitializeWithPostMessageOriginForSession(mSessi
on, Uri.parse(URL)); |
| 176 CriteriaHelper.pollUiThread(new Criteria() { |
| 177 @Override |
| 178 public boolean isSatisfied() { |
| 179 return mClientManager.getPostMessageOriginForSessionForTesting(m
Session) != null; |
| 180 } |
| 181 }); |
| 182 Uri verifiedOrigin = mClientManager.getPostMessageOriginForSessionForTes
ting(mSession); |
| 183 Assert.assertEquals(IntentHandler.ANDROID_APP_REFERRER_SCHEME, verifiedO
rigin.getScheme()); |
| 184 |
| 185 // initializeWithPostMessageOriginForSession should override without che
cking origin. |
| 186 mClientManager.initializeWithPostMessageOriginForSession(mSession, null)
; |
| 187 Assert.assertNull(mClientManager.getPostMessageOriginForSessionForTestin
g(mSession)); |
| 188 } |
| 189 |
| 190 @Test |
| 191 @SmallTest |
157 public void testFirstLowConfidencePredictionIsNotThrottled() { | 192 public void testFirstLowConfidencePredictionIsNotThrottled() { |
158 Context context = InstrumentationRegistry.getInstrumentation() | 193 Context context = InstrumentationRegistry.getInstrumentation() |
159 .getTargetContext() | 194 .getTargetContext() |
160 .getApplicationContext(); | 195 .getApplicationContext(); |
161 Assert.assertTrue(mClientManager.newSession(mSession, mUid, null, null))
; | 196 Assert.assertTrue(mClientManager.newSession(mSession, mUid, null, null))
; |
162 | 197 |
163 // Two low confidence in a row is OK. | 198 // Two low confidence in a row is OK. |
164 Assert.assertTrue( | 199 Assert.assertTrue( |
165 mClientManager.updateStatsAndReturnWhetherAllowed(mSession, mUid
, null, true)); | 200 mClientManager.updateStatsAndReturnWhetherAllowed(mSession, mUid
, null, true)); |
166 Assert.assertTrue( | 201 Assert.assertTrue( |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 267 |
233 // Low and High confidence, same call. | 268 // Low and High confidence, same call. |
234 RequestThrottler.purgeAllEntriesForTesting(context); | 269 RequestThrottler.purgeAllEntriesForTesting(context); |
235 bothDelta = new MetricsUtils.HistogramDelta(name, ClientManager.BOTH); | 270 bothDelta = new MetricsUtils.HistogramDelta(name, ClientManager.BOTH); |
236 Assert.assertTrue( | 271 Assert.assertTrue( |
237 mClientManager.updateStatsAndReturnWhetherAllowed(mSession, mUid
, URL, true)); | 272 mClientManager.updateStatsAndReturnWhetherAllowed(mSession, mUid
, URL, true)); |
238 mClientManager.registerLaunch(mSession, URL); | 273 mClientManager.registerLaunch(mSession, URL); |
239 Assert.assertEquals(1, bothDelta.getDelta()); | 274 Assert.assertEquals(1, bothDelta.getDelta()); |
240 } | 275 } |
241 } | 276 } |
OLD | NEW |