- add sources.
[platform/framework/web/crosswalk.git] / src / testing / android / java / src / org / chromium / native_test / ChromeNativeTestActivity.java
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.Handler;
11 import android.util.Log;
12
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;
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 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;
26
27     @Override
28     public void onCreate(Bundle savedInstanceState) {
29         super.onCreate(savedInstanceState);
30         // Needed by path_utils_unittest.cc
31         PathUtils.setPrivateDataDirectorySuffix("chrome");
32
33         // Needed by system_monitor_unittest.cc
34         PowerMonitor.createForTests(this);
35
36         loadLibraries();
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.
40             new Thread() {
41                 @Override
42                 public void run() {
43                     runTests();
44                 }
45             }.start();
46         } else {
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() {
50                   @Override
51                   public void run() {
52                       runTests();
53                   }
54               }, RUN_TESTS_DELAY_IN_MS);
55         }
56     }
57
58     private void runTests() {
59         // This directory is used by build/android/pylib/test_package_apk.py.
60         nativeRunTests(getFilesDir().getAbsolutePath(), getApplicationContext());
61     }
62
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");
68     }
69
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);
75         }
76     }
77
78     private native void nativeRunTests(String filesDir, Context appContext);
79 }