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 static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_NON_LOW_E ND_DEVICE; | 7 import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_NON_LOW_E ND_DEVICE; |
8 | 8 |
9 import android.app.Application; | 9 import android.app.Application; |
10 import android.content.Context; | 10 import android.content.Context; |
11 import android.content.Intent; | 11 import android.content.Intent; |
12 import android.net.Uri; | 12 import android.net.Uri; |
13 import android.os.Build; | 13 import android.os.Build; |
14 import android.os.Bundle; | 14 import android.os.Bundle; |
15 import android.os.Process; | 15 import android.os.Process; |
16 import android.support.customtabs.CustomTabsService; | 16 import android.support.customtabs.CustomTabsService; |
17 import android.support.customtabs.CustomTabsSessionToken; | 17 import android.support.customtabs.CustomTabsSessionToken; |
18 import android.test.InstrumentationTestCase; | 18 import android.test.InstrumentationTestCase; |
19 import android.test.suitebuilder.annotation.SmallTest; | 19 import android.test.suitebuilder.annotation.SmallTest; |
20 | 20 |
21 import org.chromium.base.ContextUtils; | |
21 import org.chromium.base.PathUtils; | 22 import org.chromium.base.PathUtils; |
22 import org.chromium.base.ThreadUtils; | 23 import org.chromium.base.ThreadUtils; |
23 import org.chromium.base.library_loader.LibraryLoader; | 24 import org.chromium.base.library_loader.LibraryLoader; |
24 import org.chromium.base.library_loader.LibraryProcessType; | 25 import org.chromium.base.library_loader.LibraryProcessType; |
25 import org.chromium.base.test.util.Restriction; | 26 import org.chromium.base.test.util.Restriction; |
26 import org.chromium.chrome.browser.WarmupManager; | 27 import org.chromium.chrome.browser.WarmupManager; |
27 import org.chromium.chrome.browser.preferences.PrefServiceBridge; | 28 import org.chromium.chrome.browser.preferences.PrefServiceBridge; |
28 import org.chromium.content_public.browser.WebContents; | 29 import org.chromium.content_public.browser.WebContents; |
29 | 30 |
30 import java.util.ArrayList; | 31 import java.util.ArrayList; |
31 import java.util.List; | 32 import java.util.List; |
32 import java.util.concurrent.Callable; | 33 import java.util.concurrent.Callable; |
33 import java.util.concurrent.FutureTask; | 34 import java.util.concurrent.FutureTask; |
34 | 35 |
35 /** Tests for CustomTabsConnection. */ | 36 /** Tests for CustomTabsConnection. */ |
36 public class CustomTabsConnectionTest extends InstrumentationTestCase { | 37 public class CustomTabsConnectionTest extends InstrumentationTestCase { |
37 private CustomTabsConnection mCustomTabsConnection; | 38 private CustomTabsConnection mCustomTabsConnection; |
38 private static final String URL = "http://www.google.com"; | 39 private static final String URL = "http://www.google.com"; |
39 private static final String URL2 = "https://www.android.com"; | 40 private static final String URL2 = "https://www.android.com"; |
40 private static final String INVALID_SCHEME_URL = "intent://www.google.com"; | 41 private static final String INVALID_SCHEME_URL = "intent://www.google.com"; |
41 private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "chrome"; | 42 private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "chrome"; |
42 | 43 |
43 private Context mContext; | 44 private Context mAppContext; |
44 | 45 |
45 @Override | 46 @Override |
46 protected void setUp() throws Exception { | 47 protected void setUp() throws Exception { |
47 super.setUp(); | 48 super.setUp(); |
48 mContext = getInstrumentation().getTargetContext().getApplicationContext (); | 49 mAppContext = getInstrumentation().getTargetContext().getApplicationCont ext(); |
Ted C
2016/09/21 18:36:51
Same question, but I wonder if this differs becaus
Peter Wen
2016/09/28 15:55:16
Both are fine removing.
| |
49 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX, | 50 ContextUtils.initApplicationContext(mAppContext); |
50 mContext); | 51 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX, m AppContext); |
51 LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER) | 52 LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER).ensureInitialized( ); |
52 .ensureInitialized(mContext); | 53 mCustomTabsConnection = CustomTabsTestUtils.setUpConnection((Application ) mAppContext); |
53 mCustomTabsConnection = CustomTabsTestUtils.setUpConnection((Application ) mContext); | 54 mCustomTabsConnection.resetThrottling(mAppContext, Process.myUid()); |
54 mCustomTabsConnection.resetThrottling(mContext, Process.myUid()); | |
55 } | 55 } |
56 | 56 |
57 @Override | 57 @Override |
58 protected void tearDown() throws Exception { | 58 protected void tearDown() throws Exception { |
59 super.tearDown(); | 59 super.tearDown(); |
60 CustomTabsTestUtils.cleanupSessions(mCustomTabsConnection); | 60 CustomTabsTestUtils.cleanupSessions(mCustomTabsConnection); |
61 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 61 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
62 @Override | 62 @Override |
63 public void run() { | 63 public void run() { |
64 WarmupManager.getInstance().destroySpareWebContents(); | 64 WarmupManager.getInstance().destroySpareWebContents(); |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
379 } | 379 } |
380 | 380 |
381 /** | 381 /** |
382 * Tests that | 382 * Tests that |
383 * {@link CustomTabsConnection#mayLaunchUrl(CustomTabsSessionToken, Uri, Bun dle, List)} | 383 * {@link CustomTabsConnection#mayLaunchUrl(CustomTabsSessionToken, Uri, Bun dle, List)} |
384 * can be called several times with the same, and different URLs. | 384 * can be called several times with the same, and different URLs. |
385 */ | 385 */ |
386 @SmallTest | 386 @SmallTest |
387 public void testMultipleMayLaunchUrl() { | 387 public void testMultipleMayLaunchUrl() { |
388 CustomTabsSessionToken token = assertWarmupAndMayLaunchUrl(null, URL, tr ue); | 388 CustomTabsSessionToken token = assertWarmupAndMayLaunchUrl(null, URL, tr ue); |
389 mCustomTabsConnection.resetThrottling(mContext, Process.myUid()); | 389 mCustomTabsConnection.resetThrottling(mAppContext, Process.myUid()); |
390 assertWarmupAndMayLaunchUrl(token, URL, true); | 390 assertWarmupAndMayLaunchUrl(token, URL, true); |
391 mCustomTabsConnection.resetThrottling(mContext, Process.myUid()); | 391 mCustomTabsConnection.resetThrottling(mAppContext, Process.myUid()); |
392 assertWarmupAndMayLaunchUrl(token, URL2, true); | 392 assertWarmupAndMayLaunchUrl(token, URL2, true); |
393 } | 393 } |
394 | 394 |
395 /** | 395 /** |
396 * Tests that sessions are forgotten properly. | 396 * Tests that sessions are forgotten properly. |
397 */ | 397 */ |
398 @SmallTest | 398 @SmallTest |
399 public void testForgetsSession() { | 399 public void testForgetsSession() { |
400 CustomTabsSessionToken token = assertWarmupAndMayLaunchUrl(null, URL, tr ue); | 400 CustomTabsSessionToken token = assertWarmupAndMayLaunchUrl(null, URL, tr ue); |
401 CustomTabsTestUtils.cleanupSessions(mCustomTabsConnection); | 401 CustomTabsTestUtils.cleanupSessions(mCustomTabsConnection); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
480 } | 480 } |
481 assertWarmupAndMayLaunchUrl(token, URL, true); | 481 assertWarmupAndMayLaunchUrl(token, URL, true); |
482 } | 482 } |
483 | 483 |
484 /** | 484 /** |
485 * Tests that throttling applies across sessions. | 485 * Tests that throttling applies across sessions. |
486 */ | 486 */ |
487 @SmallTest | 487 @SmallTest |
488 public void testThrottlingAcrossSessions() { | 488 public void testThrottlingAcrossSessions() { |
489 CustomTabsSessionToken token = assertWarmupAndMayLaunchUrl(null, URL, tr ue); | 489 CustomTabsSessionToken token = assertWarmupAndMayLaunchUrl(null, URL, tr ue); |
490 mCustomTabsConnection.resetThrottling(mContext, Process.myUid()); | 490 mCustomTabsConnection.resetThrottling(mAppContext, Process.myUid()); |
491 CustomTabsSessionToken token2 = assertWarmupAndMayLaunchUrl(null, URL, t rue); | 491 CustomTabsSessionToken token2 = assertWarmupAndMayLaunchUrl(null, URL, t rue); |
492 mCustomTabsConnection.resetThrottling(mContext, Process.myUid()); | 492 mCustomTabsConnection.resetThrottling(mAppContext, Process.myUid()); |
493 for (int i = 0; i < 10; i++) { | 493 for (int i = 0; i < 10; i++) { |
494 mCustomTabsConnection.mayLaunchUrl(token, Uri.parse(URL), null, null ); | 494 mCustomTabsConnection.mayLaunchUrl(token, Uri.parse(URL), null, null ); |
495 } | 495 } |
496 assertWarmupAndMayLaunchUrl(token2, URL, false); | 496 assertWarmupAndMayLaunchUrl(token2, URL, false); |
497 } | 497 } |
498 | 498 |
499 @SmallTest | 499 @SmallTest |
500 @Restriction(RESTRICTION_TYPE_NON_LOW_END_DEVICE) | 500 @Restriction(RESTRICTION_TYPE_NON_LOW_END_DEVICE) |
501 public void testBanningWorks() { | 501 public void testBanningWorks() { |
502 mCustomTabsConnection.ban(mContext, Process.myUid()); | 502 mCustomTabsConnection.ban(mAppContext, Process.myUid()); |
503 final CustomTabsSessionToken token = | 503 final CustomTabsSessionToken token = |
504 CustomTabsSessionToken.createDummySessionTokenForTesting(); | 504 CustomTabsSessionToken.createDummySessionTokenForTesting(); |
505 assertTrue(mCustomTabsConnection.newSession(token)); | 505 assertTrue(mCustomTabsConnection.newSession(token)); |
506 | 506 |
507 assertTrue(mCustomTabsConnection.mayLaunchUrl(token, Uri.parse(URL), nul l, null)); | 507 assertTrue(mCustomTabsConnection.mayLaunchUrl(token, Uri.parse(URL), nul l, null)); |
508 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 508 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
509 @Override | 509 @Override |
510 public void run() { | 510 public void run() { |
511 assertSpareWebContentsNotNullAndDestroy(); | 511 assertSpareWebContentsNotNullAndDestroy(); |
512 String referrer = mCustomTabsConnection.getReferrerForSession(to ken).getUrl(); | 512 String referrer = mCustomTabsConnection.getReferrerForSession(to ken).getUrl(); |
513 assertNull(mCustomTabsConnection.takePrerenderedUrl(token, URL, referrer)); | 513 assertNull(mCustomTabsConnection.takePrerenderedUrl(token, URL, referrer)); |
514 } | 514 } |
515 }); | 515 }); |
516 } | 516 } |
517 | 517 |
518 @SmallTest | 518 @SmallTest |
519 @Restriction(RESTRICTION_TYPE_NON_LOW_END_DEVICE) | 519 @Restriction(RESTRICTION_TYPE_NON_LOW_END_DEVICE) |
520 public void testBanningDisabledForCellular() { | 520 public void testBanningDisabledForCellular() { |
521 mCustomTabsConnection.ban(mContext, Process.myUid()); | 521 mCustomTabsConnection.ban(mAppContext, Process.myUid()); |
522 final CustomTabsSessionToken token = | 522 final CustomTabsSessionToken token = |
523 CustomTabsSessionToken.createDummySessionTokenForTesting(); | 523 CustomTabsSessionToken.createDummySessionTokenForTesting(); |
524 assertTrue(mCustomTabsConnection.newSession(token)); | 524 assertTrue(mCustomTabsConnection.newSession(token)); |
525 mCustomTabsConnection.setShouldPrerenderOnCellularForSession(token, true ); | 525 mCustomTabsConnection.setShouldPrerenderOnCellularForSession(token, true ); |
526 | 526 |
527 assertTrue(mCustomTabsConnection.mayLaunchUrl(token, Uri.parse(URL), nul l, null)); | 527 assertTrue(mCustomTabsConnection.mayLaunchUrl(token, Uri.parse(URL), nul l, null)); |
528 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 528 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
529 @Override | 529 @Override |
530 public void run() { | 530 public void run() { |
531 assertNull(WarmupManager.getInstance().takeSpareWebContents(fals e, false)); | 531 assertNull(WarmupManager.getInstance().takeSpareWebContents(fals e, false)); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
568 } finally { | 568 } finally { |
569 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 569 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
570 @Override | 570 @Override |
571 public void run() { | 571 public void run() { |
572 PrefServiceBridge.getInstance().setNetworkPredictionEnabled( enabled); | 572 PrefServiceBridge.getInstance().setNetworkPredictionEnabled( enabled); |
573 } | 573 } |
574 }); | 574 }); |
575 } | 575 } |
576 } | 576 } |
577 } | 577 } |
OLD | NEW |