development
Revisão | 6dc9883c6e6b3f131cfb1e82485ce814603603b4 (tree) |
---|---|
Hora | 2009-03-25 12:54:44 |
Autor | Brett Chabot <> |
Commiter | The Android Open Source Project |
Automated import from //branches/master/...@141783,141783
@@ -32,23 +32,21 @@ import org.eclipse.core.runtime.CoreException; | ||
32 | 32 | import org.eclipse.core.runtime.IProgressMonitor; |
33 | 33 | import org.eclipse.debug.core.ILaunchConfiguration; |
34 | 34 | import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; |
35 | -import org.eclipse.jdt.core.IJavaProject; | |
36 | 35 | import org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants; |
37 | 36 | import org.eclipse.jdt.internal.junit.launcher.TestKindRegistry; |
38 | 37 | |
39 | 38 | /** |
40 | - * Run configuration that can execute JUnit tests on an Android platform | |
39 | + * Run configuration that can execute JUnit tests on an Android platform. | |
41 | 40 | * <p/> |
42 | - * Will deploy apps on target Android platform by reusing functionality from ADT | |
43 | - * LaunchConfigDelegate, and then run JUnits tests by reusing functionality from JDT | |
41 | + * Will deploy apps on target Android platform by reusing functionality from ADT | |
42 | + * LaunchConfigDelegate, and then run JUnits tests by reusing functionality from JDT | |
44 | 43 | * JUnitLaunchConfigDelegate. |
45 | 44 | */ |
46 | -@SuppressWarnings("restriction") //$NON-NLS-1$ | |
45 | +@SuppressWarnings("restriction") | |
47 | 46 | public class AndroidJUnitLaunchConfigDelegate extends LaunchConfigDelegate { |
48 | 47 | |
49 | - /** Launch config attribute that stores instrumentation runner */ | |
48 | + /** Launch config attribute that stores instrumentation runner. */ | |
50 | 49 | static final String ATTR_INSTR_NAME = AdtPlugin.PLUGIN_ID + ".instrumentation"; //$NON-NLS-1$ |
51 | - static final String INSTRUMENTATION_OK = null; | |
52 | 50 | private static final String EMPTY_STRING = ""; //$NON-NLS-1$ |
53 | 51 | |
54 | 52 | @Override |
@@ -58,7 +56,7 @@ public class AndroidJUnitLaunchConfigDelegate extends LaunchConfigDelegate { | ||
58 | 56 | IFile applicationPackage, AndroidManifestParser manifestParser) { |
59 | 57 | |
60 | 58 | String testPackage = manifestParser.getPackage(); |
61 | - String runner = getRunnerFromConfig(configuration); | |
59 | + String runner = getRunner(project, configuration, manifestParser); | |
62 | 60 | if (runner == null) { |
63 | 61 | AdtPlugin.displayError("Android Launch", |
64 | 62 | "An instrumention test runner is not specified!"); |
@@ -72,14 +70,51 @@ public class AndroidJUnitLaunchConfigDelegate extends LaunchConfigDelegate { | ||
72 | 70 | manifestParser.getDebuggable(), manifestParser.getApiLevelRequirement(), |
73 | 71 | junitLaunch, config, androidLaunch, monitor); |
74 | 72 | } |
75 | - | |
76 | - private String getRunnerFromConfig(ILaunchConfiguration configuration) { | |
77 | - String runner = EMPTY_STRING; | |
73 | + | |
74 | + /** | |
75 | + * Gets a instrumentation runner for the launch. | |
76 | + * <p/> | |
77 | + * If a runner is stored in the given <code>configuration</code>, will return that. | |
78 | + * Otherwise, will try to find the first valid runner for the project. | |
79 | + * If a runner can still not be found, will return <code>null</code>. | |
80 | + * | |
81 | + * @param project the {@link IProject} for the app | |
82 | + * @param configuration the {@link ILaunchConfiguration} for the launch | |
83 | + * @param manifestParser the {@link AndroidManifestParser} for the project | |
84 | + * | |
85 | + * @return <code>null</code> if no instrumentation runner can be found, otherwise return | |
86 | + * the fully qualified runner name. | |
87 | + */ | |
88 | + private String getRunner(IProject project, ILaunchConfiguration configuration, | |
89 | + AndroidManifestParser manifestParser) { | |
78 | 90 | try { |
79 | - runner = configuration.getAttribute(ATTR_INSTR_NAME, EMPTY_STRING); | |
91 | + String runner = getRunnerFromConfig(configuration); | |
92 | + if (runner != null) { | |
93 | + return runner; | |
94 | + } | |
95 | + final InstrumentationRunnerValidator instrFinder = new InstrumentationRunnerValidator( | |
96 | + BaseProjectHelper.getJavaProject(project), manifestParser); | |
97 | + runner = instrFinder.getValidInstrumentationTestRunner(); | |
98 | + if (runner != null) { | |
99 | + AdtPlugin.printErrorToConsole(project, | |
100 | + String.format("Warning: No instrumentation runner found for the launch, " + | |
101 | + "using %1$s", runner)); | |
102 | + return runner; | |
103 | + } | |
104 | + AdtPlugin.printErrorToConsole(project, | |
105 | + String.format("ERROR: Application does not specify a %1$s instrumentation or does not declare uses-library %2$s", | |
106 | + AndroidConstants.CLASS_INSTRUMENTATION_RUNNER, | |
107 | + AndroidConstants.LIBRARY_TEST_RUNNER)); | |
108 | + return null; | |
80 | 109 | } catch (CoreException e) { |
81 | - AdtPlugin.log(e, "Error when retrieving instrumentation info from launch config"); //$NON-NLS-1$ | |
110 | + AdtPlugin.log(e, "Error when retrieving instrumentation info"); //$NON-NLS-1$ | |
82 | 111 | } |
112 | + return null; | |
113 | + | |
114 | + } | |
115 | + | |
116 | + private String getRunnerFromConfig(ILaunchConfiguration configuration) throws CoreException { | |
117 | + String runner = configuration.getAttribute(ATTR_INSTR_NAME, EMPTY_STRING); | |
83 | 118 | if (runner.length() < 1) { |
84 | 119 | return null; |
85 | 120 | } |
@@ -87,31 +122,6 @@ public class AndroidJUnitLaunchConfigDelegate extends LaunchConfigDelegate { | ||
87 | 122 | } |
88 | 123 | |
89 | 124 | /** |
90 | - * Helper method to return the set of instrumentations for the Android project | |
91 | - * | |
92 | - * @param project the {@link IProject} to get instrumentations for | |
93 | - * @return null if error occurred parsing instrumentations, otherwise returns array of | |
94 | - * instrumentation class names | |
95 | - */ | |
96 | - static String[] getInstrumentationsForProject(IProject project) { | |
97 | - if (project != null) { | |
98 | - try { | |
99 | - // parse the manifest for the list of instrumentations | |
100 | - AndroidManifestParser manifestParser = AndroidManifestParser.parse( | |
101 | - BaseProjectHelper.getJavaProject(project), null /* errorListener */, | |
102 | - true /* gatherData */, false /* markErrors */); | |
103 | - if (manifestParser != null) { | |
104 | - return manifestParser.getInstrumentations(); | |
105 | - } | |
106 | - } catch (CoreException e) { | |
107 | - AdtPlugin.log(e, "%s: Error parsing AndroidManifest.xml", //$NON-NLS-1$ | |
108 | - project.getName()); | |
109 | - } | |
110 | - } | |
111 | - return null; | |
112 | - } | |
113 | - | |
114 | - /** | |
115 | 125 | * Helper method to set JUnit-related attributes expected by JDT JUnit runner |
116 | 126 | * |
117 | 127 | * @param config the launch configuration to modify |
@@ -121,56 +131,4 @@ public class AndroidJUnitLaunchConfigDelegate extends LaunchConfigDelegate { | ||
121 | 131 | config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND, |
122 | 132 | TestKindRegistry.JUNIT3_TEST_KIND_ID); |
123 | 133 | } |
124 | - | |
125 | - /** | |
126 | - * Helper method to determine if specified instrumentation can be used as a test runner | |
127 | - * | |
128 | - * @param project the {@link IJavaProject} to validate | |
129 | - * @param instrumentation the instrumentation class name to validate | |
130 | - * @return <code>INSTRUMENTATION_OK</code> if valid, otherwise returns error message | |
131 | - */ | |
132 | - static String validateInstrumentationRunner(IJavaProject project, String instrumentation) { | |
133 | - AndroidManifestParser manifestParser; | |
134 | - try { | |
135 | - manifestParser = AndroidManifestParser.parse( | |
136 | - project, null /* errorListener */, | |
137 | - true /* gatherData */, false /* markErrors */); | |
138 | - // check if this instrumentation is the standard test runner | |
139 | - if (!instrumentation.equals(AndroidConstants.CLASS_INSTRUMENTATION_RUNNER)) { | |
140 | - // check if it extends the standard test runner | |
141 | - String result = BaseProjectHelper.testClassForManifest(project, | |
142 | - instrumentation, AndroidConstants.CLASS_INSTRUMENTATION_RUNNER, true); | |
143 | - if (result != BaseProjectHelper.TEST_CLASS_OK) { | |
144 | - return String.format("The instrumentation runner must be of type %s", | |
145 | - AndroidConstants.CLASS_INSTRUMENTATION_RUNNER); | |
146 | - } | |
147 | - } | |
148 | - if (!hasTestRunnerLibrary(manifestParser)) { | |
149 | - return String.format("%s does not not use the %s library", | |
150 | - project.getProject().getName(), AndroidConstants.LIBRARY_TEST_RUNNER); | |
151 | - } | |
152 | - } catch (CoreException e) { | |
153 | - String err = String.format("Error parsing AndroidManifest for %s", | |
154 | - project.getProject().getName()); | |
155 | - AdtPlugin.log(e, err); | |
156 | - return err; | |
157 | - } | |
158 | - return INSTRUMENTATION_OK; | |
159 | - } | |
160 | - | |
161 | - /** | |
162 | - * Helper method to determine if given manifest has a <code>AndroidConstants.LIBRARY_TEST_RUNNER | |
163 | - * </code> library reference | |
164 | - * | |
165 | - * @param manifestParser the {@link AndroidManifestParser} to search | |
166 | - * @return true if test runner library found, false otherwise | |
167 | - */ | |
168 | - static boolean hasTestRunnerLibrary(AndroidManifestParser manifestParser) { | |
169 | - for (String lib : manifestParser.getUsesLibraries()) { | |
170 | - if (lib.equals(AndroidConstants.LIBRARY_TEST_RUNNER)) { | |
171 | - return true; | |
172 | - } | |
173 | - } | |
174 | - return false; | |
175 | - } | |
176 | 134 | } |
@@ -118,7 +118,9 @@ public class AndroidJUnitLaunchConfigurationTab extends AbstractLaunchConfigurat | ||
118 | 118 | private Image mTabIcon = null; |
119 | 119 | private Combo mInstrumentationCombo; |
120 | 120 | private static final String EMPTY_STRING = ""; //$NON-NLS-1$ |
121 | + private static final String TAG = "AndroidJUnitLaunchConfigurationTab"; //$NON-NLS-1$ | |
121 | 122 | private String[] mInstrumentations = null; |
123 | + private InstrumentationRunnerValidator mInstrValidator = null; | |
122 | 124 | private ProjectChooserHelper mProjectChooserHelper; |
123 | 125 | |
124 | 126 | /* (non-Javadoc) |
@@ -349,7 +351,7 @@ public class AndroidJUnitLaunchConfigurationTab extends AbstractLaunchConfigurat | ||
349 | 351 | break; |
350 | 352 | } |
351 | 353 | } |
352 | - } | |
354 | + } | |
353 | 355 | } catch (CoreException ce) { |
354 | 356 | // ignore |
355 | 357 | } |
@@ -454,7 +456,7 @@ public class AndroidJUnitLaunchConfigurationTab extends AbstractLaunchConfigurat | ||
454 | 456 | mapResources(config); |
455 | 457 | } catch (CoreException e) { |
456 | 458 | // TODO: does the real error need to be extracted out of CoreException |
457 | - AdtPlugin.log(e, "Error occurred saving configuration"); | |
459 | + AdtPlugin.log(e, "Error occurred saving configuration"); //$NON-NLS-1$ | |
458 | 460 | } |
459 | 461 | AndroidJUnitLaunchConfigDelegate.setJUnitDefaults(config); |
460 | 462 |
@@ -486,7 +488,7 @@ public class AndroidJUnitLaunchConfigurationTab extends AbstractLaunchConfigurat | ||
486 | 488 | public Image getImage() { |
487 | 489 | // reuse icon from the Android App Launch config tab |
488 | 490 | if (mTabIcon == null) { |
489 | - mTabIcon= AdtPlugin.getImageLoader().loadImage(MainLaunchConfigTab.LAUNCH_TAB_IMAGE, | |
491 | + mTabIcon = AdtPlugin.getImageLoader().loadImage(MainLaunchConfigTab.LAUNCH_TAB_IMAGE, | |
490 | 492 | null); |
491 | 493 | } |
492 | 494 | return mTabIcon; |
@@ -514,7 +516,7 @@ public class AndroidJUnitLaunchConfigurationTab extends AbstractLaunchConfigurat | ||
514 | 516 | setErrorMessage(e.getMessage()); |
515 | 517 | return; |
516 | 518 | } catch (InvocationTargetException e) { |
517 | - AdtPlugin.log(e.getTargetException(), "Error finding test types"); | |
519 | + AdtPlugin.log(e.getTargetException(), "Error finding test types"); //$NON-NLS-1$ | |
518 | 520 | return; |
519 | 521 | } finally { |
520 | 522 | mTestRadioButton.setSelection(radioSetting[0]); |
@@ -675,10 +677,10 @@ public class AndroidJUnitLaunchConfigurationTab extends AbstractLaunchConfigurat | ||
675 | 677 | return; |
676 | 678 | } |
677 | 679 | } catch (CoreException e) { |
678 | - AdtPlugin.log(e, "validatePage failed"); | |
680 | + AdtPlugin.log(e, "validatePage failed"); //$NON-NLS-1$ | |
679 | 681 | } |
680 | 682 | |
681 | - validateInstrumentation(javaProject); | |
683 | + validateInstrumentation(); | |
682 | 684 | } |
683 | 685 | |
684 | 686 | private void validateJavaProject(IJavaProject javaProject) { |
@@ -688,19 +690,14 @@ public class AndroidJUnitLaunchConfigurationTab extends AbstractLaunchConfigurat | ||
688 | 690 | } |
689 | 691 | } |
690 | 692 | |
691 | - private void validateInstrumentation(IJavaProject javaProject) { | |
692 | - if (mInstrumentations == null || mInstrumentations.length < 1) { | |
693 | - setErrorMessage("Specified project has no defined instrumentations"); | |
694 | - return; | |
695 | - } | |
693 | + private void validateInstrumentation() { | |
696 | 694 | String instrumentation = getSelectedInstrumentation(); |
697 | 695 | if (instrumentation == null) { |
698 | - setErrorMessage("Instrumentation not specified"); | |
696 | + setErrorMessage("Instrumentation runner not specified"); | |
699 | 697 | return; |
700 | 698 | } |
701 | - String result = AndroidJUnitLaunchConfigDelegate.validateInstrumentationRunner( | |
702 | - javaProject, instrumentation); | |
703 | - if (result != AndroidJUnitLaunchConfigDelegate.INSTRUMENTATION_OK) { | |
699 | + String result = mInstrValidator.validateInstrumentationRunner(instrumentation); | |
700 | + if (result != InstrumentationRunnerValidator.INSTRUMENTATION_OK) { | |
704 | 701 | setErrorMessage(result); |
705 | 702 | return; |
706 | 703 | } |
@@ -949,14 +946,15 @@ public class AndroidJUnitLaunchConfigurationTab extends AbstractLaunchConfigurat | ||
949 | 946 | |
950 | 947 | /** |
951 | 948 | * Loads the UI with the instrumentations of the specified project, and stores the |
952 | - * activities in <code>mActivities</code>. | |
953 | - * <p/> | |
954 | - * First activity is selected by default if present. | |
949 | + * instrumentations in <code>mInstrumentations</code>. | |
955 | 950 | * |
956 | 951 | * @param project the {@link IProject} to load the instrumentations from. |
957 | 952 | */ |
958 | 953 | private void loadInstrumentations(IProject project) { |
959 | - mInstrumentations = AndroidJUnitLaunchConfigDelegate.getInstrumentationsForProject(project); | |
954 | + try { | |
955 | + mInstrValidator = new InstrumentationRunnerValidator(project); | |
956 | + mInstrumentations = (mInstrValidator == null ? null : | |
957 | + mInstrValidator.getInstrumentations()); | |
960 | 958 | if (mInstrumentations != null) { |
961 | 959 | mInstrumentationCombo.removeAll(); |
962 | 960 | for (String instrumentation : mInstrumentations) { |
@@ -966,9 +964,13 @@ public class AndroidJUnitLaunchConfigurationTab extends AbstractLaunchConfigurat | ||
966 | 964 | // config object. |
967 | 965 | return; |
968 | 966 | } |
969 | - | |
967 | + } catch (CoreException e) { | |
968 | + AdtPlugin.logAndPrintError(e, TAG, "ERROR: Failed to get instrumentations for %1$s", | |
969 | + project.getName()); | |
970 | + } | |
970 | 971 | // if we reach this point, either project is null, or we got an exception during |
971 | 972 | // the parsing. In either case, we empty the instrumentation list. |
973 | + mInstrValidator = null; | |
972 | 974 | mInstrumentations = null; |
973 | 975 | mInstrumentationCombo.removeAll(); |
974 | 976 | } |
@@ -16,10 +16,6 @@ | ||
16 | 16 | |
17 | 17 | package com.android.ide.eclipse.adt.launch.junit; |
18 | 18 | |
19 | -import com.android.ide.eclipse.adt.AdtPlugin; | |
20 | -import com.android.ide.eclipse.common.AndroidConstants; | |
21 | - | |
22 | -import org.eclipse.core.resources.IProject; | |
23 | 19 | import org.eclipse.core.runtime.CoreException; |
24 | 20 | import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; |
25 | 21 | import org.eclipse.jdt.core.IJavaElement; |
@@ -43,33 +39,18 @@ public class AndroidJUnitLaunchShortcut extends JUnitLaunchShortcut { | ||
43 | 39 | protected ILaunchConfigurationWorkingCopy createLaunchConfiguration(IJavaElement element) |
44 | 40 | throws CoreException { |
45 | 41 | ILaunchConfigurationWorkingCopy config = super.createLaunchConfiguration(element); |
46 | - IProject project = element.getResource().getProject(); | |
47 | - String[] instrumentations = | |
48 | - AndroidJUnitLaunchConfigDelegate.getInstrumentationsForProject(project); | |
49 | - boolean runnerFound = false; | |
50 | - if (instrumentations != null) { | |
51 | - // just pick the first valid runner | |
52 | - for (String instr : instrumentations) { | |
53 | - if (AndroidJUnitLaunchConfigDelegate.validateInstrumentationRunner( | |
54 | - element.getJavaProject(), instr) == | |
55 | - AndroidJUnitLaunchConfigDelegate.INSTRUMENTATION_OK) { | |
56 | - | |
57 | - config.setAttribute(AndroidJUnitLaunchConfigDelegate.ATTR_INSTR_NAME, | |
58 | - instr); | |
59 | - runnerFound = true; | |
60 | - break; | |
61 | - } | |
62 | - } | |
63 | - } | |
64 | - if (!runnerFound) { | |
65 | - // TODO: put this in a string properties | |
66 | - String msg = String.format("ERROR: Application does not specify a %s instrumentation or does not declare uses-library %s", | |
67 | - AndroidConstants.CLASS_INSTRUMENTATION_RUNNER, | |
68 | - AndroidConstants.LIBRARY_TEST_RUNNER); | |
69 | - AdtPlugin.printErrorToConsole(project, msg); | |
42 | + // just get first valid instrumentation runner | |
43 | + String instrumentation = new InstrumentationRunnerValidator(element.getJavaProject()). | |
44 | + getValidInstrumentationTestRunner(); | |
45 | + if (instrumentation != null) { | |
46 | + config.setAttribute(AndroidJUnitLaunchConfigDelegate.ATTR_INSTR_NAME, | |
47 | + instrumentation); | |
70 | 48 | } |
71 | - AndroidJUnitLaunchConfigDelegate.setJUnitDefaults(config); | |
49 | + // if a valid runner is not found, rely on launch delegate to log error. | |
50 | + // This method is called without explicit user action to launch Android JUnit, so avoid | |
51 | + // logging an error here. | |
72 | 52 | |
53 | + AndroidJUnitLaunchConfigDelegate.setJUnitDefaults(config); | |
73 | 54 | return config; |
74 | 55 | } |
75 | 56 | } |
@@ -0,0 +1,145 @@ | ||
1 | +/* | |
2 | + * Copyright (C) 2009 The Android Open Source Project | |
3 | + * | |
4 | + * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php | |
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 | +package com.android.ide.eclipse.adt.launch.junit; | |
17 | + | |
18 | +import com.android.ide.eclipse.adt.AdtPlugin; | |
19 | +import com.android.ide.eclipse.common.AndroidConstants; | |
20 | +import com.android.ide.eclipse.common.project.AndroidManifestParser; | |
21 | +import com.android.ide.eclipse.common.project.BaseProjectHelper; | |
22 | + | |
23 | +import org.eclipse.core.resources.IProject; | |
24 | +import org.eclipse.core.runtime.CoreException; | |
25 | +import org.eclipse.jdt.core.IJavaProject; | |
26 | + | |
27 | +/** | |
28 | + * Provides validation for Android instrumentation test runner | |
29 | + */ | |
30 | +class InstrumentationRunnerValidator { | |
31 | + private final IJavaProject mJavaProject; | |
32 | + private String[] mInstrumentations = null; | |
33 | + private boolean mHasRunnerLibrary = false; | |
34 | + | |
35 | + static final String INSTRUMENTATION_OK = null; | |
36 | + | |
37 | + /** | |
38 | + * Initializes the InstrumentationRunnerValidator. | |
39 | + * | |
40 | + * @param javaProject the {@link IJavaProject} for the Android project to validate | |
41 | + */ | |
42 | + InstrumentationRunnerValidator(IJavaProject javaProject) { | |
43 | + mJavaProject = javaProject; | |
44 | + try { | |
45 | + AndroidManifestParser manifestParser = AndroidManifestParser.parse(javaProject, | |
46 | + null /* errorListener */, true /* gatherData */, false /* markErrors */); | |
47 | + init(manifestParser); | |
48 | + } catch (CoreException e) { | |
49 | + AdtPlugin.printErrorToConsole(javaProject.getProject(), "ERROR: Failed to parse %1$s", | |
50 | + AndroidConstants.FN_ANDROID_MANIFEST); | |
51 | + } | |
52 | + } | |
53 | + | |
54 | + /** | |
55 | + * Initializes the InstrumentationRunnerValidator. | |
56 | + * | |
57 | + * @param project the {@link IProject} for the Android project to validate | |
58 | + * @throws CoreException if a fatal error occurred in initialization | |
59 | + */ | |
60 | + InstrumentationRunnerValidator(IProject project) throws CoreException { | |
61 | + this(BaseProjectHelper.getJavaProject(project)); | |
62 | + } | |
63 | + | |
64 | + /** | |
65 | + * Initializes the InstrumentationRunnerValidator with an existing {@link AndroidManifestParser} | |
66 | + * | |
67 | + * @param javaProject the {@link IJavaProject} for the Android project to validate | |
68 | + * @param manifestParser the {@link AndroidManifestParser} for the Android project | |
69 | + */ | |
70 | + InstrumentationRunnerValidator(IJavaProject javaProject, AndroidManifestParser manifestParser) { | |
71 | + mJavaProject = javaProject; | |
72 | + init(manifestParser); | |
73 | + } | |
74 | + | |
75 | + private void init(AndroidManifestParser manifestParser) { | |
76 | + mInstrumentations = manifestParser.getInstrumentations(); | |
77 | + mHasRunnerLibrary = hasTestRunnerLibrary(manifestParser); | |
78 | + } | |
79 | + | |
80 | + /** | |
81 | + * Helper method to determine if given manifest has a <code>AndroidConstants.LIBRARY_TEST_RUNNER | |
82 | + * </code> library reference | |
83 | + * | |
84 | + * @param manifestParser the {@link AndroidManifestParser} to search | |
85 | + * @return true if test runner library found, false otherwise | |
86 | + */ | |
87 | + private boolean hasTestRunnerLibrary(AndroidManifestParser manifestParser) { | |
88 | + for (String lib : manifestParser.getUsesLibraries()) { | |
89 | + if (lib.equals(AndroidConstants.LIBRARY_TEST_RUNNER)) { | |
90 | + return true; | |
91 | + } | |
92 | + } | |
93 | + return false; | |
94 | + } | |
95 | + | |
96 | + /** | |
97 | + * Return the set of instrumentations for the Android project. | |
98 | + * | |
99 | + * @return <code>null</code if error occurred parsing instrumentations, otherwise returns array | |
100 | + * of instrumentation class names | |
101 | + */ | |
102 | + String[] getInstrumentations() { | |
103 | + return mInstrumentations; | |
104 | + } | |
105 | + | |
106 | + /** | |
107 | + * Helper method to get the first instrumentation that can be used as a test runner. | |
108 | + * | |
109 | + * @return fully qualified instrumentation class name. <code>null</code> if no valid | |
110 | + * instrumentation can be found. | |
111 | + */ | |
112 | + String getValidInstrumentationTestRunner() { | |
113 | + for (String instrumentation : getInstrumentations()) { | |
114 | + if (validateInstrumentationRunner(instrumentation) == INSTRUMENTATION_OK) { | |
115 | + return instrumentation; | |
116 | + } | |
117 | + } | |
118 | + return null; | |
119 | + } | |
120 | + | |
121 | + /** | |
122 | + * Helper method to determine if specified instrumentation can be used as a test runner | |
123 | + * | |
124 | + * @param instrumentation the instrumentation class name to validate. Assumes this | |
125 | + * instrumentation is one of {@link #getInstrumentations()} | |
126 | + * @return <code>INSTRUMENTATION_OK</code> if valid, otherwise returns error message | |
127 | + */ | |
128 | + String validateInstrumentationRunner(String instrumentation) { | |
129 | + if (!mHasRunnerLibrary) { | |
130 | + return String.format("The application does not declare uses-library %1$s", | |
131 | + AndroidConstants.LIBRARY_TEST_RUNNER); | |
132 | + } | |
133 | + // check if this instrumentation is the standard test runner | |
134 | + if (!instrumentation.equals(AndroidConstants.CLASS_INSTRUMENTATION_RUNNER)) { | |
135 | + // check if it extends the standard test runner | |
136 | + String result = BaseProjectHelper.testClassForManifest(mJavaProject, | |
137 | + instrumentation, AndroidConstants.CLASS_INSTRUMENTATION_RUNNER, true); | |
138 | + if (result != BaseProjectHelper.TEST_CLASS_OK) { | |
139 | + return String.format("The instrumentation runner must be of type %s", | |
140 | + AndroidConstants.CLASS_INSTRUMENTATION_RUNNER); | |
141 | + } | |
142 | + } | |
143 | + return INSTRUMENTATION_OK; | |
144 | + } | |
145 | +} |