Chromium Code Reviews| 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.util.Log; | 11 import android.util.Log; |
| 11 | 12 |
| 12 // Android's NativeActivity is mostly useful for pure-native code. | 13 // Android's NativeActivity is mostly useful for pure-native code. |
| 13 // 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 |
| 14 // the native activity class loader. | 15 // the native activity class loader. |
| 15 // We start a background thread in here to run the tests and avoid an ANR. | |
| 16 // TODO(bulach): watch out for tests that implicitly assume they run on the main | |
| 17 // thread. | |
| 18 public class ChromeNativeTestActivity extends Activity { | 16 public class ChromeNativeTestActivity extends Activity { |
| 19 private final String TAG = "ChromeNativeTestActivity"; | 17 private final String TAG = "ChromeNativeTestActivity"; |
| 18 private static long RUN_TESTS_DELAY_IN_MS = 300; | |
|
John Grabowski
2012/06/25 20:51:37
Comment why we need a delay of any kind
nilesh
2012/06/25 21:26:26
Done.
| |
| 20 | 19 |
| 21 // Name of our shlib as obtained from a string resource. | 20 // Name of our shlib as obtained from a string resource. |
| 22 private String mLibrary; | 21 private String mLibrary; |
| 23 | 22 |
| 24 @Override | 23 @Override |
| 25 public void onCreate(Bundle savedInstanceState) { | 24 public void onCreate(Bundle savedInstanceState) { |
| 26 super.onCreate(savedInstanceState); | 25 super.onCreate(savedInstanceState); |
| 27 | 26 |
| 28 mLibrary = getResources().getString(R.string.native_library); | 27 mLibrary = getResources().getString(R.string.native_library); |
| 29 if ((mLibrary == null) || mLibrary.startsWith("replace")) { | 28 if ((mLibrary == null) || mLibrary.startsWith("replace")) { |
| 30 nativeTestFailed(); | 29 nativeTestFailed(); |
| 31 return; | 30 return; |
| 32 } | 31 } |
| 33 | 32 |
| 34 try { | 33 try { |
| 35 loadLibrary(); | 34 loadLibrary(); |
| 36 new Thread() { | 35 // Post a task to run the tests. This allows us to not block onCreat e and |
| 36 // still run tests on the main thread. | |
| 37 new Handler().postDelayed(new Runnable() { | |
| 37 @Override | 38 @Override |
| 38 public void run() { | 39 public void run() { |
| 39 Log.d(TAG, ">>nativeRunTests"); | 40 runTests(); |
| 40 nativeRunTests(getFilesDir().getAbsolutePath(), getApplicati onContext()); | |
| 41 // TODO(jrg): make sure a crash in native code | |
| 42 // triggers nativeTestFailed(). | |
| 43 Log.d(TAG, "<<nativeRunTests"); | |
| 44 } | 41 } |
| 45 }.start(); | 42 }, RUN_TESTS_DELAY_IN_MS); |
| 46 } catch (UnsatisfiedLinkError e) { | 43 } catch (UnsatisfiedLinkError e) { |
| 47 Log.e(TAG, "Unable to load lib" + mLibrary + ".so: " + e); | 44 Log.e(TAG, "Unable to load lib" + mLibrary + ".so: " + e); |
| 48 nativeTestFailed(); | 45 nativeTestFailed(); |
| 49 throw e; | 46 throw e; |
| 50 } | 47 } |
| 51 } | 48 } |
| 52 | 49 |
| 50 private void runTests() { | |
| 51 Log.d(TAG, ">>nativeRunTests"); | |
| 52 nativeRunTests(getFilesDir().getAbsolutePath(), getApplicationContext()) ; | |
| 53 // TODO(jrg): make sure a crash in native code | |
|
John Grabowski
2012/06/25 20:51:37
You fixed this, right Nilesh?
Can we remove the TO
nilesh
2012/06/25 21:26:26
Yes, removed.
| |
| 54 // triggers nativeTestFailed(). | |
| 55 Log.d(TAG, "<<nativeRunTests"); | |
| 56 } | |
| 57 | |
| 53 // Signal a failure of the native test loader to python scripts | 58 // Signal a failure of the native test loader to python scripts |
| 54 // which run tests. For example, we look for | 59 // which run tests. For example, we look for |
| 55 // RUNNER_FAILED build/android/test_package.py. | 60 // RUNNER_FAILED build/android/test_package.py. |
| 56 private void nativeTestFailed() { | 61 private void nativeTestFailed() { |
| 57 Log.e(TAG, "[ RUNNER_FAILED ] could not load native library"); | 62 Log.e(TAG, "[ RUNNER_FAILED ] could not load native library"); |
| 58 } | 63 } |
| 59 | 64 |
| 60 private void loadLibrary() throws UnsatisfiedLinkError { | 65 private void loadLibrary() throws UnsatisfiedLinkError { |
| 61 Log.i(TAG, "loading: " + mLibrary); | 66 Log.i(TAG, "loading: " + mLibrary); |
| 62 System.loadLibrary(mLibrary); | 67 System.loadLibrary(mLibrary); |
| 63 Log.i(TAG, "loaded: " + mLibrary); | 68 Log.i(TAG, "loaded: " + mLibrary); |
| 64 } | 69 } |
| 65 | 70 |
| 66 private native void nativeRunTests(String filesDir, Context appContext); | 71 private native void nativeRunTests(String filesDir, Context appContext); |
| 67 } | 72 } |
| OLD | NEW |