OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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.native_test; | 5 package org.chromium.native_test; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.os.Bundle; | 9 import android.os.Bundle; |
| 10 import android.os.Handler; |
10 import android.util.Log; | 11 import android.util.Log; |
11 | 12 |
12 // Android's NativeActivity is mostly useful for pure-native code. | 13 // Android's NativeActivity is mostly useful for pure-native code. |
13 // Our tests need to go up to our own java classes, which is not possible using | 14 // Our tests need to go up to our own java classes, which is not possible using |
14 // the native activity class loader. | 15 // the native activity class loader. |
15 // We start a background thread in here to run the tests and avoid an ANR. | |
16 // TODO(bulach): watch out for tests that implicitly assume they run on the main | |
17 // thread. | |
18 public class ChromeNativeTestActivity extends Activity { | 16 public class ChromeNativeTestActivity extends Activity { |
19 private final String TAG = "ChromeNativeTestActivity"; | 17 private final String TAG = "ChromeNativeTestActivity"; |
| 18 // We post a delayed task to run tests so that we do not block onCreate(). |
| 19 private static long RUN_TESTS_DELAY_IN_MS = 300; |
20 | 20 |
21 // Name of our shlib as obtained from a string resource. | 21 // Name of our shlib as obtained from a string resource. |
22 private String mLibrary; | 22 private String mLibrary; |
23 | 23 |
24 @Override | 24 @Override |
25 public void onCreate(Bundle savedInstanceState) { | 25 public void onCreate(Bundle savedInstanceState) { |
26 super.onCreate(savedInstanceState); | 26 super.onCreate(savedInstanceState); |
27 | 27 |
28 mLibrary = getResources().getString(R.string.native_library); | 28 mLibrary = getResources().getString(R.string.native_library); |
29 if ((mLibrary == null) || mLibrary.startsWith("replace")) { | 29 if ((mLibrary == null) || mLibrary.startsWith("replace")) { |
30 nativeTestFailed(); | 30 nativeTestFailed(); |
31 return; | 31 return; |
32 } | 32 } |
33 | 33 |
34 try { | 34 try { |
35 loadLibrary(); | 35 loadLibrary(); |
36 new Thread() { | 36 // Post a task to run the tests. This allows us to not block onCreat
e and |
| 37 // still run tests on the main thread. |
| 38 new Handler().postDelayed(new Runnable() { |
37 @Override | 39 @Override |
38 public void run() { | 40 public void run() { |
39 Log.d(TAG, ">>nativeRunTests"); | 41 runTests(); |
40 nativeRunTests(getFilesDir().getAbsolutePath(), getApplicati
onContext()); | |
41 // TODO(jrg): make sure a crash in native code | |
42 // triggers nativeTestFailed(). | |
43 Log.d(TAG, "<<nativeRunTests"); | |
44 } | 42 } |
45 }.start(); | 43 }, RUN_TESTS_DELAY_IN_MS); |
46 } catch (UnsatisfiedLinkError e) { | 44 } catch (UnsatisfiedLinkError e) { |
47 Log.e(TAG, "Unable to load lib" + mLibrary + ".so: " + e); | 45 Log.e(TAG, "Unable to load lib" + mLibrary + ".so: " + e); |
48 nativeTestFailed(); | 46 nativeTestFailed(); |
49 throw e; | 47 throw e; |
50 } | 48 } |
51 } | 49 } |
52 | 50 |
| 51 private void runTests() { |
| 52 Log.d(TAG, ">>nativeRunTests"); |
| 53 nativeRunTests(getFilesDir().getAbsolutePath(), getApplicationContext())
; |
| 54 Log.d(TAG, "<<nativeRunTests"); |
| 55 } |
| 56 |
53 // Signal a failure of the native test loader to python scripts | 57 // Signal a failure of the native test loader to python scripts |
54 // which run tests. For example, we look for | 58 // which run tests. For example, we look for |
55 // RUNNER_FAILED build/android/test_package.py. | 59 // RUNNER_FAILED build/android/test_package.py. |
56 private void nativeTestFailed() { | 60 private void nativeTestFailed() { |
57 Log.e(TAG, "[ RUNNER_FAILED ] could not load native library"); | 61 Log.e(TAG, "[ RUNNER_FAILED ] could not load native library"); |
58 } | 62 } |
59 | 63 |
60 private void loadLibrary() throws UnsatisfiedLinkError { | 64 private void loadLibrary() throws UnsatisfiedLinkError { |
61 Log.i(TAG, "loading: " + mLibrary); | 65 Log.i(TAG, "loading: " + mLibrary); |
62 System.loadLibrary(mLibrary); | 66 System.loadLibrary(mLibrary); |
63 Log.i(TAG, "loaded: " + mLibrary); | 67 Log.i(TAG, "loaded: " + mLibrary); |
64 } | 68 } |
65 | 69 |
66 private native void nativeRunTests(String filesDir, Context appContext); | 70 private native void nativeRunTests(String filesDir, Context appContext); |
67 } | 71 } |
OLD | NEW |