Bug 1062377 - Add JUnit test for new GeckoBackgroundThread; r=rnewman

This commit is contained in:
Jim Chen 2014-09-09 14:03:54 -04:00
parent bf48b2d349
commit 8e7ab555f8
3 changed files with 60 additions and 0 deletions

View File

@ -182,6 +182,7 @@ public final class ThreadUtils {
return isOnThread(getUiThread());
}
@RobocopTarget
public static boolean isOnBackgroundThread() {
if (sBackgroundThread == null) {
return false;
@ -190,6 +191,7 @@ public final class ThreadUtils {
return isOnThread(sBackgroundThread);
}
@RobocopTarget
public static boolean isOnThread(Thread thread) {
return (Thread.currentThread().getId() == thread.getId());
}

View File

@ -12,6 +12,9 @@ jar.sources += [
'src/harness/BrowserInstrumentationTestRunner.java',
'src/harness/BrowserTestListener.java',
'src/TestDistribution.java',
'src/TestGeckoBackgroundThread.java',
'src/TestGeckoMenu.java',
'src/TestGeckoProfilesProvider.java',
'src/TestGeckoSharedPrefs.java',
'src/TestImageDownloader.java',
'src/TestJarReader.java',

View File

@ -0,0 +1,55 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko;
import org.mozilla.gecko.util.ThreadUtils;
public class TestGeckoBackgroundThread extends BrowserTestCase {
private boolean finishedTest;
private boolean ranFirstRunnable;
public void testGeckoBackgroundThread() throws InterruptedException {
final Thread testThread = Thread.currentThread();
ThreadUtils.postToBackgroundThread(new Runnable() {
@Override
public void run() {
// Must *not* be on thread that posted the Runnable.
assertFalse(ThreadUtils.isOnThread(testThread));
// Must be on background thread.
assertTrue(ThreadUtils.isOnBackgroundThread());
ranFirstRunnable = true;
}
});
// Post a second Runnable to make sure it still runs on the background thread,
// and it only runs after the first Runnable has run.
ThreadUtils.postToBackgroundThread(new Runnable() {
@Override
public void run() {
// Must still be on background thread.
assertTrue(ThreadUtils.isOnBackgroundThread());
// This Runnable must be run after the first Runnable had finished.
assertTrue(ranFirstRunnable);
synchronized (TestGeckoBackgroundThread.this) {
finishedTest = true;
TestGeckoBackgroundThread.this.notify();
}
}
});
synchronized (this) {
while (!finishedTest) {
wait();
}
}
}
}