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