// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.native_test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

// Android's NativeActivity is mostly useful for pure-native code.
// Our tests need to go up to our own java classes, which is not possible using
// the native activity class loader.
public class ChromeNativeTestActivity extends Activity {
    private final String TAG = "ChromeNativeTestActivity";
    // We post a delayed task to run tests so that we do not block onCreate().
    private static long RUN_TESTS_DELAY_IN_MS = 300;

    // Name of our shlib as obtained from a string resource.
    private String mLibrary;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLibrary = getResources().getString(R.string.native_library);
        if ((mLibrary == null) || mLibrary.startsWith("replace")) {
            nativeTestFailed();
            return;
        }

        try {
            loadLibrary();
            // Post a task to run the tests. This allows us to not block onCreate and
            // still run tests on the main thread.
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    runTests();
                }
            }, RUN_TESTS_DELAY_IN_MS);
        } catch (UnsatisfiedLinkError e) {
            Log.e(TAG, "Unable to load lib" + mLibrary + ".so: " + e);
            nativeTestFailed();
            throw e;
        }
    }

    private void runTests() {
        Log.d(TAG, ">>nativeRunTests");
        nativeRunTests(getFilesDir().getAbsolutePath(), getApplicationContext());
        Log.d(TAG, "<<nativeRunTests");
    }

    // Signal a failure of the native test loader to python scripts
    // which run tests.  For example, we look for
    // RUNNER_FAILED build/android/test_package.py.
    private void nativeTestFailed() {
        Log.e(TAG, "[ RUNNER_FAILED ] could not load native library");
    }

    private void loadLibrary() throws UnsatisfiedLinkError {
        Log.i(TAG, "loading: " + mLibrary);
        System.loadLibrary(mLibrary);
        Log.i(TAG, "loaded: " + mLibrary);
    }

    private native void nativeRunTests(String filesDir, Context appContext);
}

@@ -638,16 +638,85 @@
 ivity%22;%0A
+    private final String EXTRA_RUN_IN_SUB_THREAD = %22RunInSubThread%22;%0A
     // W
@@ -786,16 +786,16 @@
 eate().%0A
-
     priv
@@ -1282,24 +1282,434 @@
 dLibrary();%0A
+            Bundle extras = this.getIntent().getExtras();%0A            if (extras != null && extras.containsKey(EXTRA_RUN_IN_SUB_THREAD)) %7B%0A                // Create a new thread and run tests on it.%0A                new Thread() %7B%0A                    @Override%0A                    public void run() %7B%0A                        runTests();%0A                    %7D%0A                %7D.start();%0A            %7D else %7B%0A    
             
@@ -1772,22 +1772,13 @@
 lock
- onCreate and%0A
+%0A    
     
@@ -1787,16 +1787,29 @@
       //
+ onCreate and
  still r
@@ -1837,16 +1837,20 @@
 thread.%0A
+    
         
@@ -1904,32 +1904,36 @@
                 
+    
 @Override%0A      
@@ -1934,32 +1934,36 @@
                 
+ 
+   
 public void run(
@@ -1958,32 +1958,36 @@
 ic void run() %7B%0A
+    
                 
@@ -2010,34 +2010,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D, R
@@ -2067,16 +2067,30 @@
 IN_MS);%0A
+            %7D%0A
         

Allow unit tests to run in a sub thread for chrome on android