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.compatibility.common.util;
18 
19 import com.android.tradefed.util.AbiUtils;
20 
21 import junit.framework.TestCase;
22 
23 /**
24  * Unit tests for {@link ModuleResult}
25  */
26 public class ModuleResultTest extends TestCase {
27 
28     private static final String NAME = "ModuleName";
29     private static final String NAME_2 = "ModuleName2";
30     private static final String ABI = "mips64";
31     private static final String ID = AbiUtils.createId(ABI, NAME);
32     private static final String ID_2 = AbiUtils.createId(ABI, NAME_2);
33     private static final String CLASS = "android.test.FoorBar";
34     private static final String CLASS_2 = "android.test.FoorBar2";
35     private static final String METHOD_1 = "testBlah1";
36     private static final String METHOD_2 = "testBlah2";
37     private static final String METHOD_3 = "testBlah3";
38     private static final String STACK_TRACE = "Something small is not alright\n " +
39             "at four.big.insects.Marley.sing(Marley.java:10)";
40     private ModuleResult mResult;
41 
42     @Override
setUp()43     public void setUp() throws Exception {
44         mResult = new ModuleResult(ID);
45     }
46 
47     @Override
tearDown()48     public void tearDown() throws Exception {
49         mResult = null;
50     }
51 
testAccessors()52     public void testAccessors() throws Exception {
53         assertEquals("Incorrect module ID", ID, mResult.getId());
54         assertEquals("Incorrect module ABI", ABI, mResult.getAbi());
55         assertEquals("Incorrect module name", NAME, mResult.getName());
56     }
57 
testResultCreation()58     public void testResultCreation() throws Exception {
59         ICaseResult caseResult = mResult.getOrCreateResult(CLASS);
60         // Should create one
61         assertEquals("Expected one result", 1, mResult.getResults().size());
62         assertTrue("Expected test result", mResult.getResults().contains(caseResult));
63         // Should not create another one
64         ICaseResult caseResult2 = mResult.getOrCreateResult(CLASS);
65         assertEquals("Expected the same result", caseResult, caseResult2);
66         assertEquals("Expected one result", 1, mResult.getResults().size());
67     }
68 
testCountResults()69     public void testCountResults() throws Exception {
70         ICaseResult testCase = mResult.getOrCreateResult(CLASS);
71         testCase.getOrCreateResult(METHOD_1).failed(STACK_TRACE);
72         testCase.getOrCreateResult(METHOD_2).failed(STACK_TRACE);
73         testCase.getOrCreateResult(METHOD_3).passed(null);
74         assertEquals("Expected two failures", 2, mResult.countResults(TestStatus.FAIL));
75         assertEquals("Expected one pass", 1, mResult.countResults(TestStatus.PASS));
76     }
77 
testMergeModule()78     public void testMergeModule() throws Exception {
79         ICaseResult caseResult = mResult.getOrCreateResult(CLASS);
80         caseResult.getOrCreateResult(METHOD_1).failed(STACK_TRACE);
81         caseResult.getOrCreateResult(METHOD_3).passed(null);
82 
83         ICaseResult caseResult2 = mResult.getOrCreateResult(CLASS_2);
84         caseResult2.getOrCreateResult(METHOD_1).failed(STACK_TRACE);
85         caseResult2.getOrCreateResult(METHOD_3).passed(null);
86 
87         assertEquals("Expected two results", 2, mResult.getResults().size());
88         assertTrue("Expected test result", mResult.getResults().contains(caseResult));
89         assertTrue("Expected test result", mResult.getResults().contains(caseResult2));
90 
91         ModuleResult otherResult = new ModuleResult(ID);
92         // Same class but all passing tests
93         ICaseResult otherCaseResult = otherResult.getOrCreateResult(CLASS);
94         otherCaseResult.getOrCreateResult(METHOD_1).passed(null);
95         otherCaseResult.getOrCreateResult(METHOD_2).passed(null);
96         otherCaseResult.getOrCreateResult(METHOD_3).passed(null);
97         otherResult.setDone(true);
98 
99         mResult.mergeFrom(otherResult);
100 
101         assertEquals("Expected two results", 2, mResult.getResults().size());
102         assertTrue("Expected test result", mResult.getResults().contains(caseResult));
103         assertTrue("Expected test result", mResult.getResults().contains(caseResult2));
104         assertTrue(mResult.isDone());
105     }
106 
testSetDone()107     public void testSetDone() {
108         assertFalse(mResult.isDone());
109         mResult.setDone(true);
110         assertTrue(mResult.isDone());
111     }
112 
testMergeModule_mismatchedModuleId()113     public void testMergeModule_mismatchedModuleId() throws Exception {
114 
115         ModuleResult otherResult = new ModuleResult(ID_2);
116         try {
117             mResult.mergeFrom(otherResult);
118             fail("Expected IlleglArgumentException");
119         } catch (IllegalArgumentException expected) {}
120     }
121 }
122