OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.blimp; | |
6 | |
7 import android.test.InstrumentationTestCase; | |
8 | |
9 import org.chromium.base.ThreadUtils; | |
10 import org.chromium.base.library_loader.ProcessInitException; | |
11 | |
12 import java.util.concurrent.Semaphore; | |
13 | |
14 /** | |
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. | |
17 */ | |
18 public class BlimpNativeInstrumentationTestCase extends InstrumentationTestCase
{ | |
19 private final Semaphore mNativeReadySemaphore = new Semaphore(0); | |
20 private boolean mSuccess = false; | |
21 | |
22 @Override | |
23 public void setUp() throws ProcessInitException { | |
24 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
25 @Override | |
26 public void run() { | |
27 try { | |
28 BlimpLibraryLoader.startAsync(new BlimpLibraryLoader.Callbac
k() { | |
29 @Override | |
30 public void onStartupComplete(boolean success) { | |
31 mSuccess = success; | |
32 mNativeReadySemaphore.release(); | |
33 } | |
34 }); | |
35 } catch (ProcessInitException e) { | |
36 throw new RuntimeException("Failed to initialize process."); | |
37 } | |
38 } | |
39 }); | |
40 } | |
41 | |
42 /** | |
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. | |
45 * This method should be called in the start of any test method that require
s native to be | |
46 * successfully loaded. | |
47 */ | |
48 protected final void waitUntilNativeIsReady() throws InterruptedException { | |
49 mNativeReadySemaphore.acquire(); | |
50 if (!mSuccess) throw new RuntimeException("Native startup failed"); | |
51 } | |
52 } | |
OLD | NEW |