| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.support.test.InstrumentationRegistry; |
| 7 import android.support.test.filters.SmallTest; | 8 import android.support.test.filters.SmallTest; |
| 8 import android.test.InstrumentationTestCase; | 9 |
| 10 import org.junit.Assert; |
| 11 import org.junit.Before; |
| 12 import org.junit.Test; |
| 13 import org.junit.runner.RunWith; |
| 9 | 14 |
| 10 import org.chromium.base.ThreadUtils; | 15 import org.chromium.base.ThreadUtils; |
| 11 import org.chromium.base.library_loader.LibraryProcessType; | 16 import org.chromium.base.library_loader.LibraryProcessType; |
| 12 import org.chromium.base.library_loader.LoaderErrors; | 17 import org.chromium.base.library_loader.LoaderErrors; |
| 13 import org.chromium.base.library_loader.ProcessInitException; | 18 import org.chromium.base.library_loader.ProcessInitException; |
| 19 import org.chromium.base.test.BaseJUnit4ClassRunner; |
| 14 | 20 |
| 15 /** | 21 /** |
| 16 * Test of BrowserStartupController | 22 * Test of BrowserStartupController |
| 17 */ | 23 */ |
| 18 public class BrowserStartupControllerTest extends InstrumentationTestCase { | 24 @RunWith(BaseJUnit4ClassRunner.class) |
| 19 | 25 public class BrowserStartupControllerTest { |
| 20 private TestBrowserStartupController mController; | 26 private TestBrowserStartupController mController; |
| 21 | 27 |
| 22 private static class TestBrowserStartupController extends BrowserStartupCont
roller { | 28 private static class TestBrowserStartupController extends BrowserStartupCont
roller { |
| 23 | 29 |
| 24 private int mStartupResult; | 30 private int mStartupResult; |
| 25 private boolean mLibraryLoadSucceeds; | 31 private boolean mLibraryLoadSucceeds; |
| 26 private int mInitializedCounter = 0; | 32 private int mInitializedCounter = 0; |
| 27 | 33 |
| 28 @Override | 34 @Override |
| 29 void prepareToStartBrowserProcess(boolean singleProcess, Runnable comple
tionCallback) | 35 void prepareToStartBrowserProcess(boolean singleProcess, Runnable comple
tionCallback) |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 } | 83 } |
| 78 | 84 |
| 79 @Override | 85 @Override |
| 80 public void onFailure() { | 86 public void onFailure() { |
| 81 assert !mHasStartupResult; | 87 assert !mHasStartupResult; |
| 82 mWasFailure = true; | 88 mWasFailure = true; |
| 83 mHasStartupResult = true; | 89 mHasStartupResult = true; |
| 84 } | 90 } |
| 85 } | 91 } |
| 86 | 92 |
| 87 @Override | 93 @Before |
| 88 protected void setUp() throws Exception { | 94 public void setUp() throws Exception { |
| 89 super.setUp(); | |
| 90 mController = new TestBrowserStartupController(); | 95 mController = new TestBrowserStartupController(); |
| 91 // Setting the static singleton instance field enables more correct test
ing, since it is | 96 // Setting the static singleton instance field enables more correct test
ing, since it is |
| 92 // is possible to call {@link BrowserStartupController#browserStartupCom
plete(int)} instead | 97 // is possible to call {@link BrowserStartupController#browserStartupCom
plete(int)} instead |
| 93 // of {@link BrowserStartupController#executeEnqueuedCallbacks(int, bool
ean)} directly. | 98 // of {@link BrowserStartupController#executeEnqueuedCallbacks(int, bool
ean)} directly. |
| 94 BrowserStartupController.overrideInstanceForTest(mController); | 99 BrowserStartupController.overrideInstanceForTest(mController); |
| 95 } | 100 } |
| 96 | 101 |
| 102 @Test |
| 97 @SmallTest | 103 @SmallTest |
| 98 public void testSingleAsynchronousStartupRequest() { | 104 public void testSingleAsynchronousStartupRequest() { |
| 99 mController.mStartupResult = BrowserStartupController.STARTUP_SUCCESS; | 105 mController.mStartupResult = BrowserStartupController.STARTUP_SUCCESS; |
| 100 mController.mLibraryLoadSucceeds = true; | 106 mController.mLibraryLoadSucceeds = true; |
| 101 final TestStartupCallback callback = new TestStartupCallback(); | 107 final TestStartupCallback callback = new TestStartupCallback(); |
| 102 | 108 |
| 103 // Kick off the asynchronous startup request. | 109 // Kick off the asynchronous startup request. |
| 104 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 110 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 105 @Override | 111 @Override |
| 106 public void run() { | 112 public void run() { |
| 107 try { | 113 try { |
| 108 mController.startBrowserProcessesAsync(true, callback); | 114 mController.startBrowserProcessesAsync(true, callback); |
| 109 } catch (Exception e) { | 115 } catch (Exception e) { |
| 110 fail("Browser should have started successfully"); | 116 Assert.fail("Browser should have started successfully"); |
| 111 } | 117 } |
| 112 } | 118 } |
| 113 }); | 119 }); |
| 114 | 120 |
| 115 assertTrue("Asynchronous mode should have been set.", | 121 Assert.assertTrue("Asynchronous mode should have been set.", |
| 116 BrowserStartupController.browserMayStartAsynchonously()); | 122 BrowserStartupController.browserMayStartAsynchonously()); |
| 117 assertEquals("The browser process should have been initialized one time.
", 1, | 123 Assert.assertEquals("The browser process should have been initialized on
e time.", 1, |
| 118 mController.initializedCounter()); | 124 mController.initializedCounter()); |
| 119 | 125 |
| 120 // Wait for callbacks to complete. | 126 // Wait for callbacks to complete. |
| 121 getInstrumentation().waitForIdleSync(); | 127 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 122 | 128 |
| 123 assertTrue("Callback should have been executed.", callback.mHasStartupRe
sult); | 129 Assert.assertTrue("Callback should have been executed.", callback.mHasSt
artupResult); |
| 124 assertTrue("Callback should have been a success.", callback.mWasSuccess)
; | 130 Assert.assertTrue("Callback should have been a success.", callback.mWasS
uccess); |
| 125 assertFalse("Callback should be told that the browser process was not al
ready started.", | 131 Assert.assertFalse( |
| 132 "Callback should be told that the browser process was not alread
y started.", |
| 126 callback.mAlreadyStarted); | 133 callback.mAlreadyStarted); |
| 127 } | 134 } |
| 128 | 135 |
| 136 @Test |
| 129 @SmallTest | 137 @SmallTest |
| 130 public void testMultipleAsynchronousStartupRequests() { | 138 public void testMultipleAsynchronousStartupRequests() { |
| 131 mController.mStartupResult = BrowserStartupController.STARTUP_SUCCESS; | 139 mController.mStartupResult = BrowserStartupController.STARTUP_SUCCESS; |
| 132 mController.mLibraryLoadSucceeds = true; | 140 mController.mLibraryLoadSucceeds = true; |
| 133 final TestStartupCallback callback1 = new TestStartupCallback(); | 141 final TestStartupCallback callback1 = new TestStartupCallback(); |
| 134 final TestStartupCallback callback2 = new TestStartupCallback(); | 142 final TestStartupCallback callback2 = new TestStartupCallback(); |
| 135 final TestStartupCallback callback3 = new TestStartupCallback(); | 143 final TestStartupCallback callback3 = new TestStartupCallback(); |
| 136 | 144 |
| 137 // Kick off the asynchronous startup requests. | 145 // Kick off the asynchronous startup requests. |
| 138 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 146 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 139 @Override | 147 @Override |
| 140 public void run() { | 148 public void run() { |
| 141 try { | 149 try { |
| 142 mController.startBrowserProcessesAsync(true, callback1); | 150 mController.startBrowserProcessesAsync(true, callback1); |
| 143 } catch (Exception e) { | 151 } catch (Exception e) { |
| 144 fail("Browser should have started successfully"); | 152 Assert.fail("Browser should have started successfully"); |
| 145 } | 153 } |
| 146 } | 154 } |
| 147 }); | 155 }); |
| 148 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 156 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 149 @Override | 157 @Override |
| 150 public void run() { | 158 public void run() { |
| 151 try { | 159 try { |
| 152 mController.startBrowserProcessesAsync(true, callback2); | 160 mController.startBrowserProcessesAsync(true, callback2); |
| 153 } catch (Exception e) { | 161 } catch (Exception e) { |
| 154 fail("Browser should have started successfully"); | 162 Assert.fail("Browser should have started successfully"); |
| 155 } | 163 } |
| 156 } | 164 } |
| 157 }); | 165 }); |
| 158 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 166 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 159 @Override | 167 @Override |
| 160 public void run() { | 168 public void run() { |
| 161 mController.addStartupCompletedObserver(callback3); | 169 mController.addStartupCompletedObserver(callback3); |
| 162 } | 170 } |
| 163 }); | 171 }); |
| 164 | 172 |
| 165 assertTrue("Asynchronous mode should have been set.", | 173 Assert.assertTrue("Asynchronous mode should have been set.", |
| 166 BrowserStartupController.browserMayStartAsynchonously()); | 174 BrowserStartupController.browserMayStartAsynchonously()); |
| 167 assertEquals("The browser process should have been initialized one time.
", 1, | 175 Assert.assertEquals("The browser process should have been initialized on
e time.", 1, |
| 168 mController.initializedCounter()); | 176 mController.initializedCounter()); |
| 169 | 177 |
| 170 // Wait for callbacks to complete. | 178 // Wait for callbacks to complete. |
| 171 getInstrumentation().waitForIdleSync(); | 179 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 172 | 180 |
| 173 assertTrue("Callback 1 should have been executed.", callback1.mHasStartu
pResult); | 181 Assert.assertTrue("Callback 1 should have been executed.", callback1.mHa
sStartupResult); |
| 174 assertTrue("Callback 1 should have been a success.", callback1.mWasSucce
ss); | 182 Assert.assertTrue("Callback 1 should have been a success.", callback1.mW
asSuccess); |
| 175 assertTrue("Callback 2 should have been executed.", callback2.mHasStartu
pResult); | 183 Assert.assertTrue("Callback 2 should have been executed.", callback2.mHa
sStartupResult); |
| 176 assertTrue("Callback 2 should have been a success.", callback2.mWasSucce
ss); | 184 Assert.assertTrue("Callback 2 should have been a success.", callback2.mW
asSuccess); |
| 177 assertTrue("Callback 3 should have been executed.", callback3.mHasStartu
pResult); | 185 Assert.assertTrue("Callback 3 should have been executed.", callback3.mHa
sStartupResult); |
| 178 assertTrue("Callback 3 should have been a success.", callback3.mWasSucce
ss); | 186 Assert.assertTrue("Callback 3 should have been a success.", callback3.mW
asSuccess); |
| 179 // Some startup tasks might have been enqueued after the browser process
was started, but | 187 // Some startup tasks might have been enqueued after the browser process
was started, but |
| 180 // not the first one which kicked of the startup. | 188 // not the first one which kicked of the startup. |
| 181 assertFalse("Callback 1 should be told that the browser process was not
already started.", | 189 Assert.assertFalse( |
| 190 "Callback 1 should be told that the browser process was not alre
ady started.", |
| 182 callback1.mAlreadyStarted); | 191 callback1.mAlreadyStarted); |
| 183 } | 192 } |
| 184 | 193 |
| 194 @Test |
| 185 @SmallTest | 195 @SmallTest |
| 186 public void testConsecutiveAsynchronousStartupRequests() { | 196 public void testConsecutiveAsynchronousStartupRequests() { |
| 187 mController.mStartupResult = BrowserStartupController.STARTUP_SUCCESS; | 197 mController.mStartupResult = BrowserStartupController.STARTUP_SUCCESS; |
| 188 mController.mLibraryLoadSucceeds = true; | 198 mController.mLibraryLoadSucceeds = true; |
| 189 final TestStartupCallback callback1 = new TestStartupCallback(); | 199 final TestStartupCallback callback1 = new TestStartupCallback(); |
| 190 final TestStartupCallback callback2 = new TestStartupCallback(); | 200 final TestStartupCallback callback2 = new TestStartupCallback(); |
| 191 | 201 |
| 192 // Kick off the asynchronous startup requests. | 202 // Kick off the asynchronous startup requests. |
| 193 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 203 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 194 @Override | 204 @Override |
| 195 public void run() { | 205 public void run() { |
| 196 try { | 206 try { |
| 197 mController.startBrowserProcessesAsync(true, callback1); | 207 mController.startBrowserProcessesAsync(true, callback1); |
| 198 } catch (Exception e) { | 208 } catch (Exception e) { |
| 199 fail("Browser should have started successfully"); | 209 Assert.fail("Browser should have started successfully"); |
| 200 } | 210 } |
| 201 } | 211 } |
| 202 }); | 212 }); |
| 203 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 213 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 204 @Override | 214 @Override |
| 205 public void run() { | 215 public void run() { |
| 206 mController.addStartupCompletedObserver(callback2); | 216 mController.addStartupCompletedObserver(callback2); |
| 207 } | 217 } |
| 208 }); | 218 }); |
| 209 | 219 |
| 210 assertTrue("Asynchronous mode should have been set.", | 220 Assert.assertTrue("Asynchronous mode should have been set.", |
| 211 BrowserStartupController.browserMayStartAsynchonously()); | 221 BrowserStartupController.browserMayStartAsynchonously()); |
| 212 assertEquals("The browser process should have been initialized one time.
", 1, | 222 Assert.assertEquals("The browser process should have been initialized on
e time.", 1, |
| 213 mController.initializedCounter()); | 223 mController.initializedCounter()); |
| 214 | 224 |
| 215 // Wait for callbacks to complete. | 225 // Wait for callbacks to complete. |
| 216 getInstrumentation().waitForIdleSync(); | 226 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 217 | 227 |
| 218 assertTrue("Callback 1 should have been executed.", callback1.mHasStartu
pResult); | 228 Assert.assertTrue("Callback 1 should have been executed.", callback1.mHa
sStartupResult); |
| 219 assertTrue("Callback 1 should have been a success.", callback1.mWasSucce
ss); | 229 Assert.assertTrue("Callback 1 should have been a success.", callback1.mW
asSuccess); |
| 220 assertTrue("Callback 2 should have been executed.", callback2.mHasStartu
pResult); | 230 Assert.assertTrue("Callback 2 should have been executed.", callback2.mHa
sStartupResult); |
| 221 assertTrue("Callback 2 should have been a success.", callback2.mWasSucce
ss); | 231 Assert.assertTrue("Callback 2 should have been a success.", callback2.mW
asSuccess); |
| 222 | 232 |
| 223 final TestStartupCallback callback3 = new TestStartupCallback(); | 233 final TestStartupCallback callback3 = new TestStartupCallback(); |
| 224 final TestStartupCallback callback4 = new TestStartupCallback(); | 234 final TestStartupCallback callback4 = new TestStartupCallback(); |
| 225 | 235 |
| 226 // Kick off more asynchronous startup requests. | 236 // Kick off more asynchronous startup requests. |
| 227 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 237 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 228 @Override | 238 @Override |
| 229 public void run() { | 239 public void run() { |
| 230 try { | 240 try { |
| 231 mController.startBrowserProcessesAsync(true, callback3); | 241 mController.startBrowserProcessesAsync(true, callback3); |
| 232 } catch (Exception e) { | 242 } catch (Exception e) { |
| 233 fail("Browser should have started successfully"); | 243 Assert.fail("Browser should have started successfully"); |
| 234 } | 244 } |
| 235 } | 245 } |
| 236 }); | 246 }); |
| 237 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 247 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 238 @Override | 248 @Override |
| 239 public void run() { | 249 public void run() { |
| 240 mController.addStartupCompletedObserver(callback4); | 250 mController.addStartupCompletedObserver(callback4); |
| 241 } | 251 } |
| 242 }); | 252 }); |
| 243 | 253 |
| 244 // Wait for callbacks to complete. | 254 // Wait for callbacks to complete. |
| 245 getInstrumentation().waitForIdleSync(); | 255 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 246 | 256 |
| 247 assertTrue("Callback 3 should have been executed.", callback3.mHasStartu
pResult); | 257 Assert.assertTrue("Callback 3 should have been executed.", callback3.mHa
sStartupResult); |
| 248 assertTrue("Callback 3 should have been a success.", callback3.mWasSucce
ss); | 258 Assert.assertTrue("Callback 3 should have been a success.", callback3.mW
asSuccess); |
| 249 assertTrue("Callback 3 should be told that the browser process was alrea
dy started.", | 259 Assert.assertTrue("Callback 3 should be told that the browser process wa
s already started.", |
| 250 callback3.mAlreadyStarted); | 260 callback3.mAlreadyStarted); |
| 251 assertTrue("Callback 4 should have been executed.", callback4.mHasStartu
pResult); | 261 Assert.assertTrue("Callback 4 should have been executed.", callback4.mHa
sStartupResult); |
| 252 assertTrue("Callback 4 should have been a success.", callback4.mWasSucce
ss); | 262 Assert.assertTrue("Callback 4 should have been a success.", callback4.mW
asSuccess); |
| 253 assertTrue("Callback 4 should be told that the browser process was alrea
dy started.", | 263 Assert.assertTrue("Callback 4 should be told that the browser process wa
s already started.", |
| 254 callback4.mAlreadyStarted); | 264 callback4.mAlreadyStarted); |
| 255 } | 265 } |
| 256 | 266 |
| 267 @Test |
| 257 @SmallTest | 268 @SmallTest |
| 258 public void testSingleFailedAsynchronousStartupRequest() { | 269 public void testSingleFailedAsynchronousStartupRequest() { |
| 259 mController.mStartupResult = BrowserStartupController.STARTUP_FAILURE; | 270 mController.mStartupResult = BrowserStartupController.STARTUP_FAILURE; |
| 260 mController.mLibraryLoadSucceeds = true; | 271 mController.mLibraryLoadSucceeds = true; |
| 261 final TestStartupCallback callback = new TestStartupCallback(); | 272 final TestStartupCallback callback = new TestStartupCallback(); |
| 262 | 273 |
| 263 // Kick off the asynchronous startup request. | 274 // Kick off the asynchronous startup request. |
| 264 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 275 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 265 @Override | 276 @Override |
| 266 public void run() { | 277 public void run() { |
| 267 try { | 278 try { |
| 268 mController.startBrowserProcessesAsync(true, callback); | 279 mController.startBrowserProcessesAsync(true, callback); |
| 269 } catch (Exception e) { | 280 } catch (Exception e) { |
| 270 fail("Browser should have started successfully"); | 281 Assert.fail("Browser should have started successfully"); |
| 271 } | 282 } |
| 272 } | 283 } |
| 273 }); | 284 }); |
| 274 | 285 |
| 275 assertTrue("Asynchronous mode should have been set.", | 286 Assert.assertTrue("Asynchronous mode should have been set.", |
| 276 BrowserStartupController.browserMayStartAsynchonously()); | 287 BrowserStartupController.browserMayStartAsynchonously()); |
| 277 assertEquals("The browser process should have been initialized one time.
", 1, | 288 Assert.assertEquals("The browser process should have been initialized on
e time.", 1, |
| 278 mController.initializedCounter()); | 289 mController.initializedCounter()); |
| 279 | 290 |
| 280 // Wait for callbacks to complete. | 291 // Wait for callbacks to complete. |
| 281 getInstrumentation().waitForIdleSync(); | 292 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 282 | 293 |
| 283 assertTrue("Callback should have been executed.", callback.mHasStartupRe
sult); | 294 Assert.assertTrue("Callback should have been executed.", callback.mHasSt
artupResult); |
| 284 assertTrue("Callback should have been a failure.", callback.mWasFailure)
; | 295 Assert.assertTrue("Callback should have been a failure.", callback.mWasF
ailure); |
| 285 } | 296 } |
| 286 | 297 |
| 298 @Test |
| 287 @SmallTest | 299 @SmallTest |
| 288 public void testConsecutiveFailedAsynchronousStartupRequests() { | 300 public void testConsecutiveFailedAsynchronousStartupRequests() { |
| 289 mController.mStartupResult = BrowserStartupController.STARTUP_FAILURE; | 301 mController.mStartupResult = BrowserStartupController.STARTUP_FAILURE; |
| 290 mController.mLibraryLoadSucceeds = true; | 302 mController.mLibraryLoadSucceeds = true; |
| 291 final TestStartupCallback callback1 = new TestStartupCallback(); | 303 final TestStartupCallback callback1 = new TestStartupCallback(); |
| 292 final TestStartupCallback callback2 = new TestStartupCallback(); | 304 final TestStartupCallback callback2 = new TestStartupCallback(); |
| 293 | 305 |
| 294 // Kick off the asynchronous startup requests. | 306 // Kick off the asynchronous startup requests. |
| 295 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 307 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 296 @Override | 308 @Override |
| 297 public void run() { | 309 public void run() { |
| 298 try { | 310 try { |
| 299 mController.startBrowserProcessesAsync(true, callback1); | 311 mController.startBrowserProcessesAsync(true, callback1); |
| 300 } catch (Exception e) { | 312 } catch (Exception e) { |
| 301 fail("Browser should have started successfully"); | 313 Assert.fail("Browser should have started successfully"); |
| 302 } | 314 } |
| 303 } | 315 } |
| 304 }); | 316 }); |
| 305 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 317 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 306 @Override | 318 @Override |
| 307 public void run() { | 319 public void run() { |
| 308 mController.addStartupCompletedObserver(callback2); | 320 mController.addStartupCompletedObserver(callback2); |
| 309 } | 321 } |
| 310 }); | 322 }); |
| 311 | 323 |
| 312 assertTrue("Asynchronous mode should have been set.", | 324 Assert.assertTrue("Asynchronous mode should have been set.", |
| 313 BrowserStartupController.browserMayStartAsynchonously()); | 325 BrowserStartupController.browserMayStartAsynchonously()); |
| 314 assertEquals("The browser process should have been initialized one time.
", 1, | 326 Assert.assertEquals("The browser process should have been initialized on
e time.", 1, |
| 315 mController.initializedCounter()); | 327 mController.initializedCounter()); |
| 316 | 328 |
| 317 // Wait for callbacks to complete. | 329 // Wait for callbacks to complete. |
| 318 getInstrumentation().waitForIdleSync(); | 330 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 319 | 331 |
| 320 assertTrue("Callback 1 should have been executed.", callback1.mHasStartu
pResult); | 332 Assert.assertTrue("Callback 1 should have been executed.", callback1.mHa
sStartupResult); |
| 321 assertTrue("Callback 1 should have been a failure.", callback1.mWasFailu
re); | 333 Assert.assertTrue("Callback 1 should have been a failure.", callback1.mW
asFailure); |
| 322 assertTrue("Callback 2 should have been executed.", callback2.mHasStartu
pResult); | 334 Assert.assertTrue("Callback 2 should have been executed.", callback2.mHa
sStartupResult); |
| 323 assertTrue("Callback 2 should have been a failure.", callback2.mWasFailu
re); | 335 Assert.assertTrue("Callback 2 should have been a failure.", callback2.mW
asFailure); |
| 324 | 336 |
| 325 final TestStartupCallback callback3 = new TestStartupCallback(); | 337 final TestStartupCallback callback3 = new TestStartupCallback(); |
| 326 final TestStartupCallback callback4 = new TestStartupCallback(); | 338 final TestStartupCallback callback4 = new TestStartupCallback(); |
| 327 | 339 |
| 328 // Kick off more asynchronous startup requests. | 340 // Kick off more asynchronous startup requests. |
| 329 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 341 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 330 @Override | 342 @Override |
| 331 public void run() { | 343 public void run() { |
| 332 try { | 344 try { |
| 333 mController.startBrowserProcessesAsync(true, callback3); | 345 mController.startBrowserProcessesAsync(true, callback3); |
| 334 } catch (Exception e) { | 346 } catch (Exception e) { |
| 335 fail("Browser should have started successfully"); | 347 Assert.fail("Browser should have started successfully"); |
| 336 } | 348 } |
| 337 } | 349 } |
| 338 }); | 350 }); |
| 339 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 351 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 340 @Override | 352 @Override |
| 341 public void run() { | 353 public void run() { |
| 342 mController.addStartupCompletedObserver(callback4); | 354 mController.addStartupCompletedObserver(callback4); |
| 343 } | 355 } |
| 344 }); | 356 }); |
| 345 | 357 |
| 346 // Wait for callbacks to complete. | 358 // Wait for callbacks to complete. |
| 347 getInstrumentation().waitForIdleSync(); | 359 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 348 | 360 |
| 349 assertTrue("Callback 3 should have been executed.", callback3.mHasStartu
pResult); | 361 Assert.assertTrue("Callback 3 should have been executed.", callback3.mHa
sStartupResult); |
| 350 assertTrue("Callback 3 should have been a failure.", callback3.mWasFailu
re); | 362 Assert.assertTrue("Callback 3 should have been a failure.", callback3.mW
asFailure); |
| 351 assertTrue("Callback 4 should have been executed.", callback4.mHasStartu
pResult); | 363 Assert.assertTrue("Callback 4 should have been executed.", callback4.mHa
sStartupResult); |
| 352 assertTrue("Callback 4 should have been a failure.", callback4.mWasFailu
re); | 364 Assert.assertTrue("Callback 4 should have been a failure.", callback4.mW
asFailure); |
| 353 } | 365 } |
| 354 | 366 |
| 367 @Test |
| 355 @SmallTest | 368 @SmallTest |
| 356 public void testSingleSynchronousRequest() { | 369 public void testSingleSynchronousRequest() { |
| 357 mController.mStartupResult = BrowserStartupController.STARTUP_SUCCESS; | 370 mController.mStartupResult = BrowserStartupController.STARTUP_SUCCESS; |
| 358 mController.mLibraryLoadSucceeds = true; | 371 mController.mLibraryLoadSucceeds = true; |
| 359 // Kick off the synchronous startup. | 372 // Kick off the synchronous startup. |
| 360 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 373 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 361 @Override | 374 @Override |
| 362 public void run() { | 375 public void run() { |
| 363 try { | 376 try { |
| 364 mController.startBrowserProcessesSync(false); | 377 mController.startBrowserProcessesSync(false); |
| 365 } catch (Exception e) { | 378 } catch (Exception e) { |
| 366 fail("Browser should have started successfully"); | 379 Assert.fail("Browser should have started successfully"); |
| 367 } | 380 } |
| 368 } | 381 } |
| 369 }); | 382 }); |
| 370 assertFalse("Synchronous mode should have been set", | 383 Assert.assertFalse("Synchronous mode should have been set", |
| 371 BrowserStartupController.browserMayStartAsynchonously()); | 384 BrowserStartupController.browserMayStartAsynchonously()); |
| 372 | 385 |
| 373 assertEquals("The browser process should have been initialized one time.
", 1, | 386 Assert.assertEquals("The browser process should have been initialized on
e time.", 1, |
| 374 mController.initializedCounter()); | 387 mController.initializedCounter()); |
| 375 } | 388 } |
| 376 | 389 |
| 390 @Test |
| 377 @SmallTest | 391 @SmallTest |
| 378 public void testAsyncThenSyncRequests() { | 392 public void testAsyncThenSyncRequests() { |
| 379 mController.mStartupResult = BrowserStartupController.STARTUP_SUCCESS; | 393 mController.mStartupResult = BrowserStartupController.STARTUP_SUCCESS; |
| 380 mController.mLibraryLoadSucceeds = true; | 394 mController.mLibraryLoadSucceeds = true; |
| 381 final TestStartupCallback callback = new TestStartupCallback(); | 395 final TestStartupCallback callback = new TestStartupCallback(); |
| 382 | 396 |
| 383 // Kick off the startups. | 397 // Kick off the startups. |
| 384 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 398 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 385 @Override | 399 @Override |
| 386 public void run() { | 400 public void run() { |
| 387 try { | 401 try { |
| 388 mController.startBrowserProcessesAsync(true, callback); | 402 mController.startBrowserProcessesAsync(true, callback); |
| 389 } catch (Exception e) { | 403 } catch (Exception e) { |
| 390 fail("Browser should have started successfully"); | 404 Assert.fail("Browser should have started successfully"); |
| 391 } | 405 } |
| 392 // To ensure that the async startup doesn't complete too soon we
have | 406 // To ensure that the async startup doesn't complete too soon we
have |
| 393 // to do both these in a since Runnable instance. This avoids th
e | 407 // to do both these in a since Runnable instance. This avoids th
e |
| 394 // unpredictable race that happens in real situations. | 408 // unpredictable race that happens in real situations. |
| 395 try { | 409 try { |
| 396 mController.startBrowserProcessesSync(false); | 410 mController.startBrowserProcessesSync(false); |
| 397 } catch (Exception e) { | 411 } catch (Exception e) { |
| 398 fail("Browser should have started successfully"); | 412 Assert.fail("Browser should have started successfully"); |
| 399 } | 413 } |
| 400 } | 414 } |
| 401 }); | 415 }); |
| 402 assertFalse("Synchronous mode should have been set", | 416 Assert.assertFalse("Synchronous mode should have been set", |
| 403 BrowserStartupController.browserMayStartAsynchonously()); | 417 BrowserStartupController.browserMayStartAsynchonously()); |
| 404 | 418 |
| 405 assertEquals("The browser process should have been initialized twice.",
2, | 419 Assert.assertEquals("The browser process should have been initialized tw
ice.", 2, |
| 406 mController.initializedCounter()); | 420 mController.initializedCounter()); |
| 407 | 421 |
| 408 assertTrue("Callback should have been executed.", callback.mHasStartupRe
sult); | 422 Assert.assertTrue("Callback should have been executed.", callback.mHasSt
artupResult); |
| 409 assertTrue("Callback should have been a success.", callback.mWasSuccess)
; | 423 Assert.assertTrue("Callback should have been a success.", callback.mWasS
uccess); |
| 410 assertFalse("Callback should be told that the browser process was not al
ready started.", | 424 Assert.assertFalse( |
| 425 "Callback should be told that the browser process was not alread
y started.", |
| 411 callback.mAlreadyStarted); | 426 callback.mAlreadyStarted); |
| 412 } | 427 } |
| 413 | 428 |
| 429 @Test |
| 414 @SmallTest | 430 @SmallTest |
| 415 public void testSyncThenAsyncRequests() { | 431 public void testSyncThenAsyncRequests() { |
| 416 mController.mStartupResult = BrowserStartupController.STARTUP_SUCCESS; | 432 mController.mStartupResult = BrowserStartupController.STARTUP_SUCCESS; |
| 417 mController.mLibraryLoadSucceeds = true; | 433 mController.mLibraryLoadSucceeds = true; |
| 418 final TestStartupCallback callback = new TestStartupCallback(); | 434 final TestStartupCallback callback = new TestStartupCallback(); |
| 419 | 435 |
| 420 // Do a synchronous startup first. | 436 // Do a synchronous startup first. |
| 421 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 437 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 422 @Override | 438 @Override |
| 423 public void run() { | 439 public void run() { |
| 424 try { | 440 try { |
| 425 mController.startBrowserProcessesSync(false); | 441 mController.startBrowserProcessesSync(false); |
| 426 } catch (Exception e) { | 442 } catch (Exception e) { |
| 427 fail("Browser should have started successfully"); | 443 Assert.fail("Browser should have started successfully"); |
| 428 } | 444 } |
| 429 } | 445 } |
| 430 }); | 446 }); |
| 431 | 447 |
| 432 assertEquals("The browser process should have been initialized once.", 1
, | 448 Assert.assertEquals("The browser process should have been initialized on
ce.", 1, |
| 433 mController.initializedCounter()); | 449 mController.initializedCounter()); |
| 434 | 450 |
| 435 assertFalse("Synchronous mode should have been set", | 451 Assert.assertFalse("Synchronous mode should have been set", |
| 436 BrowserStartupController.browserMayStartAsynchonously()); | 452 BrowserStartupController.browserMayStartAsynchonously()); |
| 437 | 453 |
| 438 // Kick off the asynchronous startup request. This should just queue the
callback. | 454 // Kick off the asynchronous startup request. This should just queue the
callback. |
| 439 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 455 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 440 @Override | 456 @Override |
| 441 public void run() { | 457 public void run() { |
| 442 try { | 458 try { |
| 443 mController.startBrowserProcessesAsync(true, callback); | 459 mController.startBrowserProcessesAsync(true, callback); |
| 444 } catch (Exception e) { | 460 } catch (Exception e) { |
| 445 fail("Browser should have started successfully"); | 461 Assert.fail("Browser should have started successfully"); |
| 446 } | 462 } |
| 447 } | 463 } |
| 448 }); | 464 }); |
| 449 | 465 |
| 450 assertEquals("The browser process should not have been initialized a sec
ond time.", 1, | 466 Assert.assertEquals("The browser process should not have been initialize
d a second time.", |
| 451 mController.initializedCounter()); | 467 1, mController.initializedCounter()); |
| 452 | 468 |
| 453 // Wait for callbacks to complete. | 469 // Wait for callbacks to complete. |
| 454 getInstrumentation().waitForIdleSync(); | 470 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 455 | 471 |
| 456 assertTrue("Callback should have been executed.", callback.mHasStartupRe
sult); | 472 Assert.assertTrue("Callback should have been executed.", callback.mHasSt
artupResult); |
| 457 assertTrue("Callback should have been a success.", callback.mWasSuccess)
; | 473 Assert.assertTrue("Callback should have been a success.", callback.mWasS
uccess); |
| 458 assertTrue("Callback should be told that the browser process was already
started.", | 474 Assert.assertTrue("Callback should be told that the browser process was
already started.", |
| 459 callback.mAlreadyStarted); | 475 callback.mAlreadyStarted); |
| 460 } | 476 } |
| 461 | 477 |
| 478 @Test |
| 462 @SmallTest | 479 @SmallTest |
| 463 public void testLibraryLoadFails() { | 480 public void testLibraryLoadFails() { |
| 464 mController.mLibraryLoadSucceeds = false; | 481 mController.mLibraryLoadSucceeds = false; |
| 465 final TestStartupCallback callback = new TestStartupCallback(); | 482 final TestStartupCallback callback = new TestStartupCallback(); |
| 466 | 483 |
| 467 // Kick off the asynchronous startup request. | 484 // Kick off the asynchronous startup request. |
| 468 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 485 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 469 @Override | 486 @Override |
| 470 public void run() { | 487 public void run() { |
| 471 try { | 488 try { |
| 472 mController.startBrowserProcessesAsync(true, callback); | 489 mController.startBrowserProcessesAsync(true, callback); |
| 473 fail("Browser should not have started successfully"); | 490 Assert.fail("Browser should not have started successfully"); |
| 474 } catch (Exception e) { | 491 } catch (Exception e) { |
| 475 // Exception expected, ignore. | 492 // Exception expected, ignore. |
| 476 } | 493 } |
| 477 } | 494 } |
| 478 }); | 495 }); |
| 479 | 496 |
| 480 assertEquals("The browser process should not have been initialized.", 0, | 497 Assert.assertEquals("The browser process should not have been initialize
d.", 0, |
| 481 mController.initializedCounter()); | 498 mController.initializedCounter()); |
| 482 | 499 |
| 483 // Wait for callbacks to complete. | 500 // Wait for callbacks to complete. |
| 484 getInstrumentation().waitForIdleSync(); | 501 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 485 } | 502 } |
| 486 | 503 |
| 487 } | 504 } |
| OLD | NEW |