Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / testing / android / java / src / org / chromium / native_test / ChromeNativeTestActivity.java
1 // Copyright 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.Handler;
11 import android.util.Log;
12
13 import org.chromium.base.PathUtils;
14 import org.chromium.base.PowerMonitor;
15 import org.chromium.base.library_loader.NativeLibraries;
16
17 /**
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  */
22 public class ChromeNativeTestActivity extends Activity {
23     private static final String TAG = "ChromeNativeTestActivity";
24     private static final String EXTRA_RUN_IN_SUB_THREAD = "RunInSubThread";
25     // We post a delayed task to run tests so that we do not block onCreate().
26     private static final long RUN_TESTS_DELAY_IN_MS = 300;
27
28     @Override
29     public void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31         // Needed by path_utils_unittest.cc
32         PathUtils.setPrivateDataDirectorySuffix("chrome");
33
34         // Needed by system_monitor_unittest.cc
35         PowerMonitor.createForTests(this);
36
37         loadLibraries();
38         Bundle extras = this.getIntent().getExtras();
39         if (extras != null && extras.containsKey(EXTRA_RUN_IN_SUB_THREAD)) {
40             // Create a new thread and run tests on it.
41             new Thread() {
42                 @Override
43                 public void run() {
44                     runTests();
45                 }
46             }.start();
47         } else {
48             // Post a task to run the tests. This allows us to not block
49             // onCreate and still run tests on the main thread.
50             new Handler().postDelayed(new Runnable() {
51                   @Override
52                   public void run() {
53                       runTests();
54                   }
55               }, RUN_TESTS_DELAY_IN_MS);
56         }
57     }
58
59     private void runTests() {
60         // This directory is used by build/android/pylib/test_package_apk.py.
61         nativeRunTests(getFilesDir().getAbsolutePath(), getApplicationContext());
62     }
63
64     // Signal a failure of the native test loader to python scripts
65     // which run tests.  For example, we look for
66     // RUNNER_FAILED build/android/test_package.py.
67     private void nativeTestFailed() {
68         Log.e(TAG, "[ RUNNER_FAILED ] could not load native library");
69     }
70
71     private void loadLibraries() {
72         for (String library : NativeLibraries.LIBRARIES) {
73             Log.i(TAG, "loading: " + library);
74             System.loadLibrary(library);
75             Log.i(TAG, "loaded: " + library);
76         }
77     }
78
79     private native void nativeRunTests(String filesDir, Context appContext);
80 }