Home | History | Annotate | Download | only in target
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package vogar.target;
     18 
     19 import android.app.Activity;
     20 import android.content.pm.ApplicationInfo;
     21 import android.content.pm.PackageManager;
     22 import android.os.Bundle;
     23 import android.os.Debug;
     24 import android.util.Log;
     25 import android.widget.TextView;
     26 import java.io.IOException;
     27 import java.util.Collections;
     28 import java.util.concurrent.ExecutorService;
     29 
     30 import vogar.util.Threads;
     31 
     32 /**
     33  * Runs a user-supplied {@code main(String[] args)} method in the context of an
     34  * Android activity. The result of the method (success or exception) is reported
     35  * to a file where vogar can pick it up.
     36  */
     37 public class TestActivity extends Activity {
     38 
     39     private final static String TAG = "TestActivity";
     40 
     41     private TextView view;
     42 
     43     @Override public void onCreate(Bundle savedInstanceState) {
     44         super.onCreate(savedInstanceState);
     45 
     46         try {
     47             // If vogar is invoked with the intention to debug code running in an activity (using
     48             // --debug-app) then the manifest is made to support debugging. We detect if we support
     49             // debugging in this Activity and wait for the debugger to connect, in this instance.
     50             ApplicationInfo applicationInfo = getPackageManager()
     51                     .getApplicationInfo(getPackageName(), 0);
     52             boolean useDebugger = (applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
     53             if (useDebugger) {
     54                 Log.d(TAG, "Waiting for debugger to connect.");
     55                 Debug.waitForDebugger();
     56             }
     57         } catch (PackageManager.NameNotFoundException nfe) {
     58             Log.e(TAG, "Malformed manifest: missing debug information.");
     59         }
     60 
     61         this.view = new TextView(this);
     62         log("TestActivity starting...");
     63         setContentView(view);
     64 
     65         AndroidLog log = new AndroidLog(TAG);
     66         ExecutorService executor = Threads.fixedThreadsExecutor(log, "testactivity", 1);
     67         executor.execute(new Runnable() {
     68             public void run() {
     69                 try {
     70                     TestRunner testRunner = new TestRunner(TestRunner.loadProperties(),
     71                             Collections.<String>emptyList());
     72                     testRunner.useSocketMonitor();
     73                     testRunner.run();
     74                 } catch (IOException e) {
     75                     throw new RuntimeException(e);
     76                 }
     77             }
     78         });
     79         executor.shutdown();
     80     }
     81 
     82     private void log(String message) {
     83         Log.i(TAG, message);
     84         view.append(message + "\n");
     85     }
     86 }
     87