| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 package org.chromium.native_test; | 5 package org.chromium.native_test; | 
| 6 | 6 | 
| 7 import android.app.Activity; | 7 import android.app.Activity; | 
| 8 import android.content.Context; | 8 import android.content.Context; | 
| 9 import android.os.Bundle; | 9 import android.os.Bundle; | 
| 10 import android.os.Handler; | 10 import android.os.Handler; | 
| 11 import android.util.Log; | 11 import android.util.Log; | 
| 12 | 12 | 
| 13 // Android's NativeActivity is mostly useful for pure-native code. | 13 // Android's NativeActivity is mostly useful for pure-native code. | 
| 14 // Our tests need to go up to our own java classes, which is not possible using | 14 // Our tests need to go up to our own java classes, which is not possible using | 
| 15 // the native activity class loader. | 15 // the native activity class loader. | 
| 16 public class ChromeNativeTestActivity extends Activity { | 16 public class ChromeNativeTestActivity extends Activity { | 
| 17     private final String TAG = "ChromeNativeTestActivity"; | 17     private final String TAG = "ChromeNativeTestActivity"; | 
|  | 18     private final String EXTRA_RUN_IN_SUB_THREAD = "RunInSubThread"; | 
| 18     // We post a delayed task to run tests so that we do not block onCreate(). | 19     // We post a delayed task to run tests so that we do not block onCreate(). | 
| 19     private static long RUN_TESTS_DELAY_IN_MS = 300; | 20     private static long RUN_TESTS_DELAY_IN_MS = 300; | 
| 20 | 21 | 
| 21     // Name of our shlib as obtained from a string resource. | 22     // Name of our shlib as obtained from a string resource. | 
| 22     private String mLibrary; | 23     private String mLibrary; | 
| 23 | 24 | 
| 24     @Override | 25     @Override | 
| 25     public void onCreate(Bundle savedInstanceState) { | 26     public void onCreate(Bundle savedInstanceState) { | 
| 26         super.onCreate(savedInstanceState); | 27         super.onCreate(savedInstanceState); | 
| 27 | 28 | 
| 28         mLibrary = getResources().getString(R.string.native_library); | 29         mLibrary = getResources().getString(R.string.native_library); | 
| 29         if ((mLibrary == null) || mLibrary.startsWith("replace")) { | 30         if ((mLibrary == null) || mLibrary.startsWith("replace")) { | 
| 30             nativeTestFailed(); | 31             nativeTestFailed(); | 
| 31             return; | 32             return; | 
| 32         } | 33         } | 
| 33 | 34 | 
| 34         try { | 35         try { | 
| 35             loadLibrary(); | 36             loadLibrary(); | 
| 36             // Post a task to run the tests. This allows us to not block onCreat
    e and | 37             Bundle extras = this.getIntent().getExtras(); | 
| 37             // still run tests on the main thread. | 38             if (extras != null && extras.containsKey(EXTRA_RUN_IN_SUB_THREAD)) { | 
| 38             new Handler().postDelayed(new Runnable() { | 39                 // Create a new thread and run tests on it. | 
| 39                 @Override | 40                 new Thread() { | 
| 40                 public void run() { | 41                     @Override | 
| 41                     runTests(); | 42                     public void run() { | 
| 42                 } | 43                         runTests(); | 
| 43             }, RUN_TESTS_DELAY_IN_MS); | 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             } | 
| 44         } catch (UnsatisfiedLinkError e) { | 56         } catch (UnsatisfiedLinkError e) { | 
| 45             Log.e(TAG, "Unable to load lib" + mLibrary + ".so: " + e); | 57             Log.e(TAG, "Unable to load lib" + mLibrary + ".so: " + e); | 
| 46             nativeTestFailed(); | 58             nativeTestFailed(); | 
| 47             throw e; | 59             throw e; | 
| 48         } | 60         } | 
| 49     } | 61     } | 
| 50 | 62 | 
| 51     private void runTests() { | 63     private void runTests() { | 
| 52         Log.d(TAG, ">>nativeRunTests"); | 64         Log.d(TAG, ">>nativeRunTests"); | 
| 53         nativeRunTests(getFilesDir().getAbsolutePath(), getApplicationContext())
    ; | 65         nativeRunTests(getFilesDir().getAbsolutePath(), getApplicationContext())
    ; | 
| 54         Log.d(TAG, "<<nativeRunTests"); | 66         Log.d(TAG, "<<nativeRunTests"); | 
| 55     } | 67     } | 
| 56 | 68 | 
| 57     // Signal a failure of the native test loader to python scripts | 69     // Signal a failure of the native test loader to python scripts | 
| 58     // which run tests.  For example, we look for | 70     // which run tests.  For example, we look for | 
| 59     // RUNNER_FAILED build/android/test_package.py. | 71     // RUNNER_FAILED build/android/test_package.py. | 
| 60     private void nativeTestFailed() { | 72     private void nativeTestFailed() { | 
| 61         Log.e(TAG, "[ RUNNER_FAILED ] could not load native library"); | 73         Log.e(TAG, "[ RUNNER_FAILED ] could not load native library"); | 
| 62     } | 74     } | 
| 63 | 75 | 
| 64     private void loadLibrary() throws UnsatisfiedLinkError { | 76     private void loadLibrary() throws UnsatisfiedLinkError { | 
| 65         Log.i(TAG, "loading: " + mLibrary); | 77         Log.i(TAG, "loading: " + mLibrary); | 
| 66         System.loadLibrary(mLibrary); | 78         System.loadLibrary(mLibrary); | 
| 67         Log.i(TAG, "loaded: " + mLibrary); | 79         Log.i(TAG, "loaded: " + mLibrary); | 
| 68     } | 80     } | 
| 69 | 81 | 
| 70     private native void nativeRunTests(String filesDir, Context appContext); | 82     private native void nativeRunTests(String filesDir, Context appContext); | 
| 71 } | 83 } | 
| OLD | NEW | 
|---|