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 |
3 | // found in the LICENSE file. |
4 | |
5 | package org.chromium.native_test; |
6 | |
7 | import android.app.Activity; |
8 | import android.content.Context; |
9 | import android.os.Bundle; |
10 | import android.os.Environment; |
11 | import android.os.Handler; |
12 | import android.util.Log; |
13 | |
14 | import org.chromium.base.ChromiumActivity; |
15 | import org.chromium.base.PathUtils; |
16 | import org.chromium.base.PowerMonitor; |
17 | |
18 | // TODO(cjhopman): This should not refer to content. NativeLibraries should be moved to base. |
19 | import org.chromium.content.app.NativeLibraries; |
20 | |
21 | import java.io.File; |
22 | |
23 | // Android's NativeActivity is mostly useful for pure-native code. |
24 | // Our tests need to go up to our own java classes, which is not possible using |
25 | // the native activity class loader. |
26 | public class ChromeNativeTestActivity extends ChromiumActivity { |
27 | private static final String TAG = "ChromeNativeTestActivity"; |
28 | private static final String EXTRA_RUN_IN_SUB_THREAD = "RunInSubThread"; |
29 | // We post a delayed task to run tests so that we do not block onCreate(). |
30 | private static final long RUN_TESTS_DELAY_IN_MS = 300; |
31 | |
32 | @Override |
33 | public void onCreate(Bundle savedInstanceState) { |
34 | super.onCreate(savedInstanceState); |
35 | // Needed by path_utils_unittest.cc |
36 | PathUtils.setPrivateDataDirectorySuffix("chrome"); |
37 | |
38 | // Needed by system_monitor_unittest.cc |
39 | PowerMonitor.createForTests(this); |
40 | |
41 | loadLibraries(); |
42 | Bundle extras = this.getIntent().getExtras(); |
43 | if (extras != null && extras.containsKey(EXTRA_RUN_IN_SUB_THREAD)) { |
44 | // Create a new thread and run tests on it. |
45 | new Thread() { |
46 | @Override |
47 | public void run() { |
48 | runTests(); |
49 | } |
50 | }.start(); |
51 | } else { |
52 | // Post a task to run the tests. This allows us to not block |
53 | // onCreate and still run tests on the main thread. |
54 | new Handler().postDelayed(new Runnable() { |
55 | @Override |
56 | public void run() { |
57 | runTests(); |
58 | } |
59 | }, RUN_TESTS_DELAY_IN_MS); |
60 | } |
61 | } |
62 | |
63 | private void runTests() { |
64 | // This directory is used by build/android/pylib/test_package_apk.py. |
65 | nativeRunTests(getFilesDir().getAbsolutePath(), getApplicationContext()); |
66 | } |
67 | |
68 | // Signal a failure of the native test loader to python scripts |
69 | // which run tests. For example, we look for |
70 | // RUNNER_FAILED build/android/test_package.py. |
71 | private void nativeTestFailed() { |
72 | Log.e(TAG, "[ RUNNER_FAILED ] could not load native library"); |
73 | } |
74 | |
75 | private void loadLibraries() { |
76 | for (String library: NativeLibraries.libraries) { |
77 | Log.i(TAG, "loading: " + library); |
78 | System.loadLibrary(library); |
79 | Log.i(TAG, "loaded: " + library); |
80 | } |
81 | } |
82 | |
83 | private native void nativeRunTests(String filesDir, Context appContext); |
84 | } |