| 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.net.Uri; |
| 9 import android.os.Process; | 9 import android.os.Process; |
| 10 import android.support.customtabs.CustomTabsSessionToken; | 10 import android.support.customtabs.CustomTabsSessionToken; |
| 11 import android.support.test.InstrumentationRegistry; | 11 import android.support.test.InstrumentationRegistry; |
| 12 import android.support.test.filters.SmallTest; | 12 import android.support.test.filters.SmallTest; |
| 13 | 13 |
| 14 import org.junit.Assert; | 14 import org.junit.Assert; |
| 15 import org.junit.Before; | 15 import org.junit.Before; |
| 16 import org.junit.Rule; | 16 import org.junit.Rule; |
| 17 import org.junit.Test; | 17 import org.junit.Test; |
| 18 import org.junit.runner.RunWith; | 18 import org.junit.runner.RunWith; |
| 19 | 19 |
| 20 import org.chromium.base.ContextUtils; | 20 import org.chromium.base.ContextUtils; |
| 21 import org.chromium.base.ThreadUtils; |
| 21 import org.chromium.base.metrics.RecordHistogram; | 22 import org.chromium.base.metrics.RecordHistogram; |
| 22 import org.chromium.base.test.BaseJUnit4ClassRunner; | 23 import org.chromium.base.test.BaseJUnit4ClassRunner; |
| 23 import org.chromium.base.test.util.MetricsUtils; | 24 import org.chromium.base.test.util.MetricsUtils; |
| 24 import org.chromium.base.test.util.RetryOnFailure; | 25 import org.chromium.base.test.util.RetryOnFailure; |
| 25 import org.chromium.chrome.browser.IntentHandler; | 26 import org.chromium.chrome.browser.IntentHandler; |
| 26 import org.chromium.content.browser.test.NativeLibraryTestRule; | 27 import org.chromium.content.browser.test.NativeLibraryTestRule; |
| 27 import org.chromium.content.browser.test.util.Criteria; | 28 import org.chromium.content.browser.test.util.Criteria; |
| 28 import org.chromium.content.browser.test.util.CriteriaHelper; | 29 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 29 | 30 |
| 30 /** Tests for ClientManager. */ | 31 /** Tests for ClientManager. */ |
| (...skipping 12 matching lines...) Expand all Loading... |
| 43 | 44 |
| 44 @Before | 45 @Before |
| 45 public void setUp() throws Exception { | 46 public void setUp() throws Exception { |
| 46 Context context = InstrumentationRegistry.getInstrumentation() | 47 Context context = InstrumentationRegistry.getInstrumentation() |
| 47 .getTargetContext() | 48 .getTargetContext() |
| 48 .getApplicationContext(); | 49 .getApplicationContext(); |
| 49 mActivityTestRule.loadNativeLibraryNoBrowserProcess(); | 50 mActivityTestRule.loadNativeLibraryNoBrowserProcess(); |
| 50 RequestThrottler.purgeAllEntriesForTesting(context); | 51 RequestThrottler.purgeAllEntriesForTesting(context); |
| 51 mClientManager = new ClientManager(context); | 52 mClientManager = new ClientManager(context); |
| 52 RecordHistogram.initialize(); | 53 RecordHistogram.initialize(); |
| 54 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 55 @Override |
| 56 public void run() { |
| 57 OriginVerifier.reset(); |
| 58 } |
| 59 }); |
| 53 } | 60 } |
| 54 | 61 |
| 55 @Test | 62 @Test |
| 56 @SmallTest | 63 @SmallTest |
| 57 public void testNoSessionNoWarmup() { | 64 public void testNoSessionNoWarmup() { |
| 58 Assert.assertEquals( | 65 Assert.assertEquals( |
| 59 ClientManager.NO_SESSION_NO_WARMUP, mClientManager.getWarmupStat
e(null)); | 66 ClientManager.NO_SESSION_NO_WARMUP, mClientManager.getWarmupStat
e(null)); |
| 60 } | 67 } |
| 61 | 68 |
| 62 @Test | 69 @Test |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 Assert.assertTrue( | 162 Assert.assertTrue( |
| 156 mClientManager.updateStatsAndReturnWhetherAllowed(mSession, mUid
, URL, false)); | 163 mClientManager.updateStatsAndReturnWhetherAllowed(mSession, mUid
, URL, false)); |
| 157 mClientManager.setIgnoreFragmentsForSession(mSession, true); | 164 mClientManager.setIgnoreFragmentsForSession(mSession, true); |
| 158 Assert.assertEquals(ClientManager.GOOD_PREDICTION, | 165 Assert.assertEquals(ClientManager.GOOD_PREDICTION, |
| 159 mClientManager.getPredictionOutcome(mSession, URL + "#fragment")
); | 166 mClientManager.getPredictionOutcome(mSession, URL + "#fragment")
); |
| 160 } | 167 } |
| 161 | 168 |
| 162 @Test | 169 @Test |
| 163 @SmallTest | 170 @SmallTest |
| 164 public void testPostMessageOriginVerification() { | 171 public void testPostMessageOriginVerification() { |
| 165 Assert.assertTrue( | 172 final ClientManager cm = mClientManager; |
| 166 mClientManager.newSession(mSession, mUid, null, new PostMessageH
andler(mSession))); | 173 Assert.assertTrue(cm.newSession(mSession, mUid, null, new PostMessageHan
dler(mSession))); |
| 167 // Should always start with no origin. | 174 // Should always start with no origin. |
| 168 Assert.assertNull(mClientManager.getPostMessageOriginForSessionForTestin
g(mSession)); | 175 Assert.assertNull(cm.getPostMessageOriginForSessionForTesting(mSession))
; |
| 169 | 176 |
| 170 // With no prepopulated origins, this verification should fail. | 177 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 171 mClientManager.verifyAndInitializeWithPostMessageOriginForSession(mSessi
on, Uri.parse(URL)); | 178 @Override |
| 172 Assert.assertNull(mClientManager.getPostMessageOriginForSessionForTestin
g(mSession)); | 179 public void run() { |
| 180 // With no prepopulated origins, this verification should fail. |
| 181 cm.verifyAndInitializeWithPostMessageOriginForSession(mSession,
Uri.parse(URL)); |
| 182 Assert.assertNull(cm.getPostMessageOriginForSessionForTesting(mS
ession)); |
| 173 | 183 |
| 174 // If there is a prepopulated origin, we should get a synchronous verifi
cation. | 184 // If there is a prepopulated origin, we should get a synchronou
s verification. |
| 175 OriginVerifier.prePopulateVerifiedOriginForTesting( | 185 OriginVerifier.addVerifiedOriginForPackage( |
| 176 ContextUtils.getApplicationContext().getPackageName(), Uri.parse
(URL)); | 186 ContextUtils.getApplicationContext().getPackageName(), U
ri.parse(URL)); |
| 177 mClientManager.verifyAndInitializeWithPostMessageOriginForSession(mSessi
on, Uri.parse(URL)); | 187 cm.verifyAndInitializeWithPostMessageOriginForSession(mSession,
Uri.parse(URL)); |
| 188 } |
| 189 }); |
| 190 |
| 178 CriteriaHelper.pollUiThread(new Criteria() { | 191 CriteriaHelper.pollUiThread(new Criteria() { |
| 179 @Override | 192 @Override |
| 180 public boolean isSatisfied() { | 193 public boolean isSatisfied() { |
| 181 return mClientManager.getPostMessageOriginForSessionForTesting(m
Session) != null; | 194 return cm.getPostMessageOriginForSessionForTesting(mSession) !=
null; |
| 182 } | 195 } |
| 183 }); | 196 }); |
| 184 Uri verifiedOrigin = mClientManager.getPostMessageOriginForSessionForTes
ting(mSession); | |
| 185 Assert.assertEquals(IntentHandler.ANDROID_APP_REFERRER_SCHEME, verifiedO
rigin.getScheme()); | |
| 186 | 197 |
| 187 // initializeWithPostMessageOriginForSession should override without che
cking origin. | 198 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 188 mClientManager.initializeWithPostMessageOriginForSession(mSession, null)
; | 199 @Override |
| 189 Assert.assertNull(mClientManager.getPostMessageOriginForSessionForTestin
g(mSession)); | 200 public void run() { |
| 201 Uri verifiedOrigin = cm.getPostMessageOriginForSessionForTesting
(mSession); |
| 202 Assert.assertEquals( |
| 203 IntentHandler.ANDROID_APP_REFERRER_SCHEME, verifiedOrigi
n.getScheme()); |
| 204 |
| 205 // initializeWithPostMessageOriginForSession should override wit
hout checking |
| 206 // origin. |
| 207 cm.initializeWithPostMessageOriginForSession(mSession, null); |
| 208 Assert.assertNull(cm.getPostMessageOriginForSessionForTesting(mS
ession)); |
| 209 } |
| 210 }); |
| 190 } | 211 } |
| 191 | 212 |
| 192 @Test | 213 @Test |
| 193 @SmallTest | 214 @SmallTest |
| 194 public void testPostMessageOriginHttpNotAllowed() { | 215 public void testPostMessageOriginHttpNotAllowed() { |
| 195 Assert.assertTrue( | 216 final ClientManager cm = mClientManager; |
| 196 mClientManager.newSession(mSession, mUid, null, new PostMessageH
andler(mSession))); | 217 Assert.assertTrue(cm.newSession(mSession, mUid, null, new PostMessageHan
dler(mSession))); |
| 197 // Should always start with no origin. | 218 // Should always start with no origin. |
| 198 Assert.assertNull(mClientManager.getPostMessageOriginForSessionForTestin
g(mSession)); | 219 Assert.assertNull(cm.getPostMessageOriginForSessionForTesting(mSession))
; |
| 199 | 220 |
| 200 // With no prepopulated origins, this verification should fail. | 221 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 201 mClientManager.verifyAndInitializeWithPostMessageOriginForSession( | 222 @Override |
| 202 mSession, Uri.parse(HTTP_URL)); | 223 public void run() { |
| 203 Assert.assertNull(mClientManager.getPostMessageOriginForSessionForTestin
g(mSession)); | 224 Uri uri = Uri.parse(HTTP_URL); |
| 225 // With no prepopulated origins, this verification should fail. |
| 226 cm.verifyAndInitializeWithPostMessageOriginForSession(mSession,
uri); |
| 227 Assert.assertNull(cm.getPostMessageOriginForSessionForTesting(mS
ession)); |
| 204 | 228 |
| 205 // Even if there is a prepopulated origin, non-https origins should get
an early return with | 229 // Even if there is a prepopulated origin, non-https origins sho
uld get an early |
| 206 // false. | 230 // return with false. |
| 207 OriginVerifier.prePopulateVerifiedOriginForTesting( | 231 OriginVerifier.addVerifiedOriginForPackage( |
| 208 ContextUtils.getApplicationContext().getPackageName(), Uri.parse
(HTTP_URL)); | 232 ContextUtils.getApplicationContext().getPackageName(), u
ri); |
| 209 mClientManager.verifyAndInitializeWithPostMessageOriginForSession( | 233 cm.verifyAndInitializeWithPostMessageOriginForSession(mSession,
uri); |
| 210 mSession, Uri.parse(HTTP_URL)); | 234 Assert.assertNull(cm.getPostMessageOriginForSessionForTesting(mS
ession)); |
| 211 Assert.assertNull(mClientManager.getPostMessageOriginForSessionForTestin
g(mSession)); | 235 } |
| 236 }); |
| 212 } | 237 } |
| 213 | 238 |
| 214 @Test | 239 @Test |
| 215 @SmallTest | 240 @SmallTest |
| 216 public void testFirstLowConfidencePredictionIsNotThrottled() { | 241 public void testFirstLowConfidencePredictionIsNotThrottled() { |
| 217 Context context = InstrumentationRegistry.getInstrumentation() | 242 Context context = InstrumentationRegistry.getInstrumentation() |
| 218 .getTargetContext() | 243 .getTargetContext() |
| 219 .getApplicationContext(); | 244 .getApplicationContext(); |
| 220 Assert.assertTrue(mClientManager.newSession(mSession, mUid, null, null))
; | 245 Assert.assertTrue(mClientManager.newSession(mSession, mUid, null, null))
; |
| 221 | 246 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 | 316 |
| 292 // Low and High confidence, same call. | 317 // Low and High confidence, same call. |
| 293 RequestThrottler.purgeAllEntriesForTesting(context); | 318 RequestThrottler.purgeAllEntriesForTesting(context); |
| 294 bothDelta = new MetricsUtils.HistogramDelta(name, ClientManager.BOTH); | 319 bothDelta = new MetricsUtils.HistogramDelta(name, ClientManager.BOTH); |
| 295 Assert.assertTrue( | 320 Assert.assertTrue( |
| 296 mClientManager.updateStatsAndReturnWhetherAllowed(mSession, mUid
, URL, true)); | 321 mClientManager.updateStatsAndReturnWhetherAllowed(mSession, mUid
, URL, true)); |
| 297 mClientManager.registerLaunch(mSession, URL); | 322 mClientManager.registerLaunch(mSession, URL); |
| 298 Assert.assertEquals(1, bothDelta.getDelta()); | 323 Assert.assertEquals(1, bothDelta.getDelta()); |
| 299 } | 324 } |
| 300 } | 325 } |
| OLD | NEW |