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.os.Bundle; 21 import android.util.Log; 22 import android.widget.TextView; 23 import java.io.IOException; 24 import java.util.Collections; 25 import java.util.concurrent.ExecutorService; 26 import vogar.util.Threads; 27 28 /** 29 * Runs a user-supplied {@code main(String[] args)} method in the context of an 30 * Android activity. The result of the method (success or exception) is reported 31 * to a file where vogar can pick it up. 32 */ 33 public class TestActivity extends Activity { 34 35 private final static String TAG = "TestActivity"; 36 37 private TextView view; 38 onCreate(Bundle savedInstanceState)39 @Override public void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 42 this.view = new TextView(this); 43 log("TestActivity starting..."); 44 setContentView(view); 45 46 AndroidLog log = new AndroidLog(TAG); 47 ExecutorService executor = Threads.fixedThreadsExecutor(log, "testactivity", 1); 48 executor.execute(new Runnable() { 49 public void run() { 50 try { 51 TestRunner testRunner = new TestRunner(Collections.<String>emptyList()); 52 testRunner.useSocketMonitor(); 53 testRunner.run(); 54 } catch (IOException e) { 55 throw new RuntimeException(e); 56 } 57 } 58 }); 59 executor.shutdown(); 60 } 61 log(String message)62 private void log(String message) { 63 Log.i(TAG, message); 64 view.append(message + "\n"); 65 } 66 } 67