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; | 5 package org.chromium.chrome.browser; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.support.test.InstrumentationRegistry; |
8 import android.support.test.filters.SmallTest; | 9 import android.support.test.filters.SmallTest; |
9 import android.test.InstrumentationTestCase; | 10 |
| 11 import org.junit.After; |
| 12 import org.junit.Assert; |
| 13 import org.junit.Before; |
| 14 import org.junit.Test; |
| 15 import org.junit.runner.RunWith; |
10 | 16 |
11 import org.chromium.base.metrics.RecordHistogram; | 17 import org.chromium.base.metrics.RecordHistogram; |
12 import org.chromium.base.test.util.AdvancedMockContext; | 18 import org.chromium.base.test.util.AdvancedMockContext; |
13 import org.chromium.base.test.util.Feature; | 19 import org.chromium.base.test.util.Feature; |
14 import org.chromium.base.test.util.RetryOnFailure; | 20 import org.chromium.base.test.util.RetryOnFailure; |
| 21 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
15 | 22 |
16 import java.util.concurrent.ExecutionException; | 23 import java.util.concurrent.ExecutionException; |
17 import java.util.concurrent.Semaphore; | 24 import java.util.concurrent.Semaphore; |
18 | 25 |
19 /** | 26 /** |
20 * Tests {@link BackgroundSyncLauncher}. | 27 * Tests {@link BackgroundSyncLauncher}. |
21 */ | 28 */ |
22 public class BackgroundSyncLauncherTest extends InstrumentationTestCase { | 29 @RunWith(ChromeJUnit4ClassRunner.class) |
| 30 public class BackgroundSyncLauncherTest { |
23 private Context mContext; | 31 private Context mContext; |
24 private BackgroundSyncLauncher mLauncher; | 32 private BackgroundSyncLauncher mLauncher; |
25 private Boolean mShouldLaunchResult; | 33 private Boolean mShouldLaunchResult; |
26 | 34 |
27 @Override | 35 @Before |
28 protected void setUp() throws Exception { | 36 public void setUp() throws Exception { |
29 mContext = new AdvancedMockContext(getInstrumentation().getTargetContext
()); | 37 mContext = new AdvancedMockContext( |
| 38 InstrumentationRegistry.getInstrumentation().getTargetContext())
; |
30 BackgroundSyncLauncher.setGCMEnabled(false); | 39 BackgroundSyncLauncher.setGCMEnabled(false); |
31 RecordHistogram.setDisabledForTests(true); | 40 RecordHistogram.setDisabledForTests(true); |
32 mLauncher = BackgroundSyncLauncher.create(mContext); | 41 mLauncher = BackgroundSyncLauncher.create(mContext); |
33 // Ensure that the initial task is given enough time to complete. | 42 // Ensure that the initial task is given enough time to complete. |
34 waitForLaunchBrowserTask(); | 43 waitForLaunchBrowserTask(); |
35 } | 44 } |
36 | 45 |
37 @Override | 46 @After |
38 public void tearDown() throws Exception { | 47 public void tearDown() throws Exception { |
39 super.tearDown(); | |
40 RecordHistogram.setDisabledForTests(false); | 48 RecordHistogram.setDisabledForTests(false); |
41 } | 49 } |
42 | 50 |
43 private void deleteLauncherInstance() { | 51 private void deleteLauncherInstance() { |
44 mLauncher.destroy(); | 52 mLauncher.destroy(); |
45 mLauncher = null; | 53 mLauncher = null; |
46 } | 54 } |
47 | 55 |
48 private Boolean shouldLaunchBrowserIfStoppedSync() { | 56 private Boolean shouldLaunchBrowserIfStoppedSync() { |
49 mShouldLaunchResult = false; | 57 mShouldLaunchResult = false; |
50 | 58 |
51 // Use a semaphore to wait for the callback to be called. | 59 // Use a semaphore to wait for the callback to be called. |
52 final Semaphore semaphore = new Semaphore(0); | 60 final Semaphore semaphore = new Semaphore(0); |
53 | 61 |
54 BackgroundSyncLauncher.ShouldLaunchCallback callback = | 62 BackgroundSyncLauncher.ShouldLaunchCallback callback = |
55 new BackgroundSyncLauncher.ShouldLaunchCallback() { | 63 new BackgroundSyncLauncher.ShouldLaunchCallback() { |
56 @Override | 64 @Override |
57 public void run(Boolean shouldLaunch) { | 65 public void run(Boolean shouldLaunch) { |
58 mShouldLaunchResult = shouldLaunch; | 66 mShouldLaunchResult = shouldLaunch; |
59 semaphore.release(); | 67 semaphore.release(); |
60 } | 68 } |
61 }; | 69 }; |
62 | 70 |
63 BackgroundSyncLauncher.shouldLaunchBrowserIfStopped(callback); | 71 BackgroundSyncLauncher.shouldLaunchBrowserIfStopped(callback); |
64 try { | 72 try { |
65 // Wait on the callback to be called. | 73 // Wait on the callback to be called. |
66 semaphore.acquire(); | 74 semaphore.acquire(); |
67 } catch (InterruptedException e) { | 75 } catch (InterruptedException e) { |
68 fail("Failed to acquire semaphore"); | 76 Assert.fail("Failed to acquire semaphore"); |
69 } | 77 } |
70 return mShouldLaunchResult; | 78 return mShouldLaunchResult; |
71 } | 79 } |
72 | 80 |
73 private void waitForLaunchBrowserTask() { | 81 private void waitForLaunchBrowserTask() { |
74 try { | 82 try { |
75 mLauncher.mLaunchBrowserIfStoppedTask.get(); | 83 mLauncher.mLaunchBrowserIfStoppedTask.get(); |
76 } catch (InterruptedException e) { | 84 } catch (InterruptedException e) { |
77 fail("Launch task was interrupted"); | 85 Assert.fail("Launch task was interrupted"); |
78 } catch (ExecutionException e) { | 86 } catch (ExecutionException e) { |
79 fail("Launch task had execution exception"); | 87 Assert.fail("Launch task had execution exception"); |
80 } | 88 } |
81 } | 89 } |
82 | 90 |
| 91 @Test |
83 @SmallTest | 92 @SmallTest |
84 @Feature({"BackgroundSync"}) | 93 @Feature({"BackgroundSync"}) |
85 @RetryOnFailure | 94 @RetryOnFailure |
86 public void testHasInstance() { | 95 public void testHasInstance() { |
87 assertTrue(BackgroundSyncLauncher.hasInstance()); | 96 Assert.assertTrue(BackgroundSyncLauncher.hasInstance()); |
88 mLauncher.destroy(); | 97 mLauncher.destroy(); |
89 assertFalse(BackgroundSyncLauncher.hasInstance()); | 98 Assert.assertFalse(BackgroundSyncLauncher.hasInstance()); |
90 } | 99 } |
91 | 100 |
| 101 @Test |
92 @SmallTest | 102 @SmallTest |
93 @Feature({"BackgroundSync"}) | 103 @Feature({"BackgroundSync"}) |
94 public void testDefaultNoLaunch() { | 104 public void testDefaultNoLaunch() { |
95 assertFalse(shouldLaunchBrowserIfStoppedSync()); | 105 Assert.assertFalse(shouldLaunchBrowserIfStoppedSync()); |
96 } | 106 } |
97 | 107 |
| 108 @Test |
98 @SmallTest | 109 @SmallTest |
99 @Feature({"BackgroundSync"}) | 110 @Feature({"BackgroundSync"}) |
100 @RetryOnFailure | 111 @RetryOnFailure |
101 public void testSetLaunchWhenNextOnline() { | 112 public void testSetLaunchWhenNextOnline() { |
102 assertFalse(shouldLaunchBrowserIfStoppedSync()); | 113 Assert.assertFalse(shouldLaunchBrowserIfStoppedSync()); |
103 mLauncher.launchBrowserIfStopped(true, 0); | 114 mLauncher.launchBrowserIfStopped(true, 0); |
104 waitForLaunchBrowserTask(); | 115 waitForLaunchBrowserTask(); |
105 assertTrue(shouldLaunchBrowserIfStoppedSync()); | 116 Assert.assertTrue(shouldLaunchBrowserIfStoppedSync()); |
106 mLauncher.launchBrowserIfStopped(false, 0); | 117 mLauncher.launchBrowserIfStopped(false, 0); |
107 waitForLaunchBrowserTask(); | 118 waitForLaunchBrowserTask(); |
108 assertFalse(shouldLaunchBrowserIfStoppedSync()); | 119 Assert.assertFalse(shouldLaunchBrowserIfStoppedSync()); |
109 } | 120 } |
110 | 121 |
| 122 @Test |
111 @SmallTest | 123 @SmallTest |
112 @Feature({"BackgroundSync"}) | 124 @Feature({"BackgroundSync"}) |
113 @RetryOnFailure | 125 @RetryOnFailure |
114 public void testNewLauncherDisablesNextOnline() { | 126 public void testNewLauncherDisablesNextOnline() { |
115 mLauncher.launchBrowserIfStopped(true, 0); | 127 mLauncher.launchBrowserIfStopped(true, 0); |
116 waitForLaunchBrowserTask(); | 128 waitForLaunchBrowserTask(); |
117 assertTrue(shouldLaunchBrowserIfStoppedSync()); | 129 Assert.assertTrue(shouldLaunchBrowserIfStoppedSync()); |
118 | 130 |
119 // Simulate restarting the browser by deleting the launcher and creating
a new one. | 131 // Simulate restarting the browser by deleting the launcher and creating
a new one. |
120 deleteLauncherInstance(); | 132 deleteLauncherInstance(); |
121 mLauncher = BackgroundSyncLauncher.create(mContext); | 133 mLauncher = BackgroundSyncLauncher.create(mContext); |
122 waitForLaunchBrowserTask(); | 134 waitForLaunchBrowserTask(); |
123 assertFalse(shouldLaunchBrowserIfStoppedSync()); | 135 Assert.assertFalse(shouldLaunchBrowserIfStoppedSync()); |
124 } | 136 } |
125 } | 137 } |
OLD | NEW |