Chromium Code Reviews| Index: content/public/android/javatests/src/org/chromium/content/browser/BrowserStartupControllerTest.java |
| diff --git a/content/public/android/javatests/src/org/chromium/content/browser/BrowserStartupControllerTest.java b/content/public/android/javatests/src/org/chromium/content/browser/BrowserStartupControllerTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8e4dc5dc97ec20064d656f6f2b005e4ac7181a98 |
| --- /dev/null |
| +++ b/content/public/android/javatests/src/org/chromium/content/browser/BrowserStartupControllerTest.java |
| @@ -0,0 +1,159 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.content.browser; |
| + |
| +import android.content.Context; |
| +import android.test.InstrumentationTestCase; |
| +import android.test.suitebuilder.annotation.SmallTest; |
| + |
| +import org.chromium.base.ThreadUtils; |
| +import org.chromium.base.test.util.AdvancedMockContext; |
| + |
| +import java.util.concurrent.atomic.AtomicBoolean; |
| + |
| +public class BrowserStartupControllerTest extends InstrumentationTestCase { |
| + |
| + private TestBrowserStartupController mController; |
| + |
| + private static class TestBrowserStartupController extends BrowserStartupController { |
| + |
| + private boolean mInitializationResult = true; |
| + private int mInitializedCounter = 0; |
| + private boolean mCallbackWasSetup; |
| + |
| + private TestBrowserStartupController(Context context) { |
| + super(context); |
| + } |
| + |
| + @Override |
| + void enableAsynchronousStartup() { |
| + mCallbackWasSetup = true; |
| + } |
| + |
| + @Override |
| + boolean initializeAndroidBrowserProcess() { |
| + mInitializedCounter++; |
| + return mInitializationResult; |
| + } |
| + |
| + private boolean hasBeenInitializedOneTime() { |
| + return mInitializedCounter == 1; |
| + } |
| + } |
| + |
| + private static class TestStartupCallback implements BrowserStartupController.StartupCallback { |
| + private int mStartupResult; |
| + private boolean mHasStartupResult; |
| + |
| + @Override |
| + public void run(int startupResult) { |
| + mStartupResult = startupResult; |
| + mHasStartupResult = true; |
| + } |
| + } |
| + @Override |
| + protected void setUp() throws Exception { |
| + super.setUp(); |
| + Context context = new AdvancedMockContext(getInstrumentation().getTargetContext()); |
| + mController = new TestBrowserStartupController(context); |
| + } |
| + |
| + @SmallTest |
| + public void testSingleAsynchronousStartup() { |
| + final TestStartupCallback callback = new TestStartupCallback(); |
| + final AtomicBoolean startupResult = new AtomicBoolean(); |
|
bulach
2013/08/15 15:09:12
startupResult is confusing with callback.mStartupR
nyquist
2013/08/16 02:01:01
Done.
|
| + |
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| + @Override |
| + public void run() { |
| + // Kick off the asynchronous startup |
| + startupResult.set(mController.startBrowserProcessesAsync(callback)); |
| + } |
| + }); |
| + |
| + // The callback should have been setup. |
| + assertTrue(mController.mCallbackWasSetup); |
| + // Ensure that it ends up trying to initialize the browser process. |
| + assertTrue(mController.hasBeenInitializedOneTime()); |
| + // Ensure the result is returned correctly. |
| + assertTrue(startupResult.get()); |
| + |
| + // Browser process was initialized, so assume process load is complete. |
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| + @Override |
| + public void run() { |
| + mController.executeEnqueuedCallbacks(-42); |
| + } |
| + }); |
| + |
| + // All done, so our callback should have been executed. |
| + assertTrue(callback.mHasStartupResult); |
| + assertEquals(-42, callback.mStartupResult); |
| + } |
| + |
| + @SmallTest |
| + public void testTwoAsynchronousStartups() { |
| + final TestStartupCallback callback1 = new TestStartupCallback(); |
| + final TestStartupCallback callback2 = new TestStartupCallback(); |
| + final AtomicBoolean startupResult1 = new AtomicBoolean(); |
|
bulach
2013/08/15 15:09:12
ditto
nyquist
2013/08/16 02:01:01
Done.
|
| + final AtomicBoolean startupResult2 = new AtomicBoolean(); |
| + |
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| + @Override |
| + public void run() { |
| + // Kick off the asynchronous startups. |
| + startupResult1.set(mController.startBrowserProcessesAsync(callback1)); |
| + startupResult2.set(mController.startBrowserProcessesAsync(callback2)); |
|
bulach
2013/08/15 15:09:12
would be more realistic as two separate "runOnUiTh
nyquist
2013/08/16 02:01:01
Done.
|
| + } |
| + }); |
| + |
| + // The callback should have been setup. |
| + assertTrue(mController.mCallbackWasSetup); |
| + // Ensure that it ends up trying to initialize the browser process. |
| + assertTrue(mController.hasBeenInitializedOneTime()); |
| + // Ensure the results are returned correctly. |
| + assertTrue(startupResult1.get()); |
| + assertTrue(startupResult2.get()); |
| + |
| + // Browser process was initialized, so assume process load is complete. |
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| + @Override |
| + public void run() { |
| + mController.executeEnqueuedCallbacks(-1); |
| + } |
| + }); |
| + |
| + // All done, so our callbacks should have been executed. |
| + assertTrue(callback1.mHasStartupResult); |
| + assertEquals(-1, callback1.mStartupResult); |
| + assertTrue(callback2.mHasStartupResult); |
| + assertEquals(-1, callback2.mStartupResult); |
| + } |
| + |
| + @SmallTest |
| + public void testSingleAsynchronousStartupFailed() { |
| + // Make sure the call to initialize AndroidBrowserProcess returns false, faking a |
| + // {@link org.chromium.content.common.ProcessInitException}. |
| + mController.mInitializationResult = false; |
| + |
| + final TestStartupCallback callback = new TestStartupCallback(); |
| + final AtomicBoolean startupResult = new AtomicBoolean(); |
|
bulach
2013/08/15 15:09:12
ditto
nyquist
2013/08/16 02:01:01
Done.
|
| + |
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| + @Override |
| + public void run() { |
| + // Kick off the asynchronous startup |
| + startupResult.set(mController.startBrowserProcessesAsync(callback)); |
| + } |
| + }); |
| + |
| + // The callback should have been setup. |
| + assertTrue(mController.mCallbackWasSetup); |
| + // Ensure that it ends up trying to initialize the browser process. |
| + assertTrue(mController.hasBeenInitializedOneTime()); |
| + // Ensure the results are returned correctly. |
| + assertFalse(startupResult.get()); |
| + } |
| +} |