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