• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.testing.uiautomation;
2 
3 import android.os.Bundle;
4 import android.test.AndroidTestRunner;
5 import android.test.InstrumentationTestRunner;
6 
7 import java.io.IOException;
8 import java.lang.reflect.Field;
9 
10 import junit.framework.AssertionFailedError;
11 import junit.framework.Test;
12 import junit.framework.TestListener;
13 
14 public class UiAutomationTestRunner extends InstrumentationTestRunner {
15 
16     // there's nothing fan
17     private Bundle mParams;
18     private AutomationProvider mAutomationProvider;
19 
20     @Override
onCreate(Bundle arguments)21     public void onCreate(Bundle arguments) {
22         mParams = new Bundle(arguments);
23         super.onCreate(arguments);
24     }
25 
getInitialParams()26     public Bundle getInitialParams() {
27         return mParams;
28     }
29 
getAutomationProvider()30     private AutomationProvider getAutomationProvider() throws IOException {
31         if (mAutomationProvider == null) {
32             mAutomationProvider = new AutomationProvider(getTargetContext());
33         }
34         return mAutomationProvider;
35     }
36 
37     @Override
getAndroidTestRunner()38     protected AndroidTestRunner getAndroidTestRunner() {
39         // TODO Auto-generated method stub
40         AndroidTestRunner testRunner = super.getAndroidTestRunner();
41         testRunner.addTestListener(new TestListener() {
42 
43             @Override
44             public void startTest(Test test) {
45                 Field[] fields = test.getClass().getDeclaredFields();
46                 for (Field field : fields) {
47                     if (field.getAnnotation(InjectParams.class) != null) {
48                         if (Bundle.class.equals(field.getType())) {
49                             field.setAccessible(true);
50                             try {
51                                 field.set(test, mParams);
52                             } catch (IllegalAccessException e) {
53                                 throw new RuntimeException("failed to inject Bundle parameter", e);
54                             }
55                         } else {
56                             throw new IllegalArgumentException("Need Bundle type for injection");
57                         }
58                     }
59                     if (field.getAnnotation(InjectAutomationProvider.class) != null) {
60                         if (AutomationProvider.class.equals(field.getType())) {
61                             field.setAccessible(true);
62                             try {
63                                 field.set(test, getAutomationProvider());
64                             } catch (IllegalAccessException e) {
65                                 throw new RuntimeException("failed to inject AutomationProvider", e);
66                             } catch (IOException e) {
67                                 throw new RuntimeException("failed to init AutomationProvider", e);
68                             }
69                         }
70                     }
71                 }
72             }
73 
74             @Override
75             public void endTest(Test test) {
76             }
77 
78             @Override
79             public void addFailure(Test test, AssertionFailedError t) {
80             }
81 
82             @Override
83             public void addError(Test test, Throwable t) {
84             }
85         });
86         return testRunner;
87     }
88 }
89