OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.blimp; | 5 package org.chromium.blimp; |
6 | 6 |
7 import android.test.InstrumentationTestCase; | 7 import android.test.InstrumentationTestCase; |
8 | 8 |
9 import org.chromium.base.ThreadUtils; | 9 import org.chromium.base.ThreadUtils; |
10 import org.chromium.base.library_loader.ProcessInitException; | 10 import org.chromium.base.library_loader.ProcessInitException; |
11 | 11 |
12 import java.util.concurrent.Semaphore; | 12 import java.util.concurrent.Semaphore; |
13 | 13 |
14 /** | 14 /** |
15 * Base class for loading native library in tests. The setUp() methods must be i
nvoked, and | 15 * Base class for loading native library in tests. The setUp() methods must be i
nvoked, and |
16 * subclasses can call {@link #waitUntilNativeIsReady()} in the start of each te
st-method. | 16 * subclasses can call {@link #waitUntilNativeIsReady()} in the start of each te
st-method. |
17 */ | 17 */ |
18 public class BlimpNativeInstrumentationTestCase extends InstrumentationTestCase
{ | 18 public class BlimpNativeInstrumentationTestCase extends InstrumentationTestCase
{ |
19 private final Semaphore mNativeReadySemaphore = new Semaphore(0); | 19 private final Semaphore mNativeReadySemaphore = new Semaphore(0); |
20 private boolean mSuccess = false; | 20 private boolean mSuccess = false; |
21 | 21 |
22 @Override | 22 @Override |
23 public void setUp() throws ProcessInitException { | 23 public void setUp() throws ProcessInitException { |
24 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 24 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
25 @Override | 25 @Override |
26 public void run() { | 26 public void run() { |
27 try { | 27 try { |
28 BlimpLibraryLoader.startAsync(getInstrumentation().getTarget
Context(), | 28 BlimpLibraryLoader.startAsync(new BlimpLibraryLoader.Callbac
k() { |
29 new BlimpLibraryLoader.Callback() { | 29 @Override |
30 public void onStartupComplete(boolean success) { | 30 public void onStartupComplete(boolean success) { |
31 mSuccess = success; | 31 mSuccess = success; |
32 mNativeReadySemaphore.release(); | 32 mNativeReadySemaphore.release(); |
33 } | 33 } |
34 }); | 34 }); |
35 } catch (ProcessInitException e) { | 35 } catch (ProcessInitException e) { |
36 throw new RuntimeException("Failed to initialize process."); | 36 throw new RuntimeException("Failed to initialize process."); |
37 } | 37 } |
38 } | 38 } |
39 }); | 39 }); |
40 } | 40 } |
41 | 41 |
42 /** | 42 /** |
43 * Blocks until the native library startup is complete. If the startup dit n
ot complete | 43 * Blocks until the native library startup is complete. If the startup dit n
ot complete |
44 * successfully, this method throws a RuntimeException, otherwise it does no
thing. | 44 * successfully, this method throws a RuntimeException, otherwise it does no
thing. |
45 * This method should be called in the start of any test method that require
s native to be | 45 * This method should be called in the start of any test method that require
s native to be |
46 * successfully loaded. | 46 * successfully loaded. |
47 */ | 47 */ |
48 protected final void waitUntilNativeIsReady() throws InterruptedException { | 48 protected final void waitUntilNativeIsReady() throws InterruptedException { |
49 mNativeReadySemaphore.acquire(); | 49 mNativeReadySemaphore.acquire(); |
50 if (!mSuccess) throw new RuntimeException("Native startup failed"); | 50 if (!mSuccess) throw new RuntimeException("Native startup failed"); |
51 } | 51 } |
52 } | 52 } |
OLD | NEW |