1 /*
2  * Copyright (C) 2015 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 com.android.tradefed.util;
18 
19 import com.android.tradefed.util.ListInstrumentationParser.InstrumentationTarget;
20 import com.google.common.collect.Lists;
21 
22 import junit.framework.TestCase;
23 
24 import java.util.List;
25 import java.util.Objects;
26 
27 /** Simple unit test for {@link ListInstrumentationParser}. */
28 public class ListInstrumentationParserTest extends TestCase {
29 
30     // Example instrumentation test values
31     private static final String EXAMPLE_TEST_PACKAGE_1 = "com.example.test";
32     private static final String EXAMPLE_TARGET_1 = "com.example";
33     private static final String EXAMPLE_RUNNER_1 = "android.test.InstrumentationTestRunner";
34     private static final String LIST_INSTRUMENTATION_OUTPUT_1 =
35             "instrumentation:com.example.test/android.test.InstrumentationTestRunner (target=com.example)";
36 
37     private static final String EXAMPLE_TEST_PACKAGE_2 = "com.foobar.test";
38     private static final String EXAMPLE_TARGET_2 = "com.example2";
39     private static final String EXAMPLE_RUNNER_2 = "android.support.test.runner.AndroidJUnitRunner";
40     private static final String LIST_INSTRUMENTATION_OUTPUT_2 =
41             "instrumentation:com.foobar.test/android.support.test.runner.AndroidJUnitRunner (target=com.example2)";
42 
43     private static final String EXAMPLE_TEST_PACKAGE_3 = "com.example.test";
44     private static final String EXAMPLE_TARGET_3 = "com.example";
45     private static final String EXAMPLE_RUNNER_3 = "android.support.test.runner.AndroidJUnitRunner";
46     private static final String LIST_INSTRUMENTATION_OUTPUT_3 =
47             "instrumentation:com.example.test/android.support.test.runner.AndroidJUnitRunner (target=com.example)";
48 
49 
50     private ListInstrumentationParser mParser;
51 
52     @Override
setUp()53     public void setUp() {
54         mParser = new ListInstrumentationParser();
55     }
56 
57     /**
58      * Test behavior when there are no lines to parse
59      */
testEmptyParse()60     public void testEmptyParse() {
61         List<InstrumentationTarget> targets = mParser.getInstrumentationTargets();
62         assertTrue("getInstrumentationTargets() not empty", targets.isEmpty());
63     }
64 
65     /**
66      * Simple test case for list instrumentation parsing
67      */
testSimpleParse()68     public void testSimpleParse() {
69         // Build example `pm list instrumentation` output and send it to the parser
70         String[] pmListOutput = {
71             LIST_INSTRUMENTATION_OUTPUT_1,
72             LIST_INSTRUMENTATION_OUTPUT_2,
73             LIST_INSTRUMENTATION_OUTPUT_3
74         };
75         mParser.processNewLines(pmListOutput);
76 
77         // Expected targets
78         InstrumentationTarget target1 = new InstrumentationTarget(
79                 EXAMPLE_TEST_PACKAGE_1, EXAMPLE_RUNNER_1, EXAMPLE_TARGET_1);
80         InstrumentationTarget target2 = new InstrumentationTarget(
81                 EXAMPLE_TEST_PACKAGE_2, EXAMPLE_RUNNER_2, EXAMPLE_TARGET_2);
82         InstrumentationTarget target3 = new InstrumentationTarget(
83                 EXAMPLE_TEST_PACKAGE_3, EXAMPLE_RUNNER_3, EXAMPLE_TARGET_3);
84 
85         // Targets should be alphabetized by test package name, runner name, target name.
86         List<InstrumentationTarget> expectedTargets = Lists.newArrayList(target1, target3, target2);
87 
88         // Get the parsed targets and make sure they contain the expected targets
89         List<InstrumentationTarget> parsedTargets = mParser.getInstrumentationTargets();
90         validateInstrumentationTargets(expectedTargets, parsedTargets);
91 
92         assertFalse("Nonshardable targets treated as shardable", target1.isShardable());
93         assertTrue("Shardable targets not treated as shardable", target2.isShardable());
94     }
95 
96     /**
97      * Helper function to compare two Lists and assert if they do not contain identical
98      * InstrumentationTargets
99      */
validateInstrumentationTargets(List<InstrumentationTarget> expectedTargets, List<InstrumentationTarget> actualTargets)100     private void validateInstrumentationTargets(List<InstrumentationTarget> expectedTargets,
101             List<InstrumentationTarget> actualTargets) {
102 
103         // Lists must be the same size
104         assertEquals("Unexpected number of parsed targets",
105                 expectedTargets.size(), actualTargets.size());
106         // And contain the same elements
107         for (InstrumentationTarget actualTarget : actualTargets) {
108             // Must equal one of the expected targets
109             boolean matched = false;
110             for (InstrumentationTarget expectedTarget : expectedTargets) {
111                 matched = areTargetsEqual(expectedTarget, actualTarget);
112                 if (matched) {
113                     expectedTargets.remove(expectedTarget);
114                     break;
115                 }
116             }
117             if (!matched) {
118                 fail(String.format("Unexpected InstrumentationTarget(%s, %s, %s)",
119                       actualTarget.packageName, actualTarget.runnerName, actualTarget.targetName));
120             }
121         }
122     }
123 
124     /**
125      * Helper function to determine if two {@link InstrumentationTarget}s are equal
126      */
areTargetsEqual(InstrumentationTarget expected, InstrumentationTarget actual)127     private static boolean areTargetsEqual(InstrumentationTarget expected,
128             InstrumentationTarget actual) {
129 
130         return Objects.equals(expected.packageName, actual.packageName) &&
131                 Objects.equals(expected.runnerName, actual.runnerName) &&
132                 Objects.equals(expected.targetName, actual.targetName);
133     }
134 }
135