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.tradefed.targetprep;
18 
19 import com.android.tradefed.build.DeviceBuildInfo;
20 import com.android.tradefed.build.IDeviceBuildInfo;
21 import com.android.tradefed.config.OptionSetter;
22 import com.android.tradefed.device.ITestDevice;
23 import com.android.tradefed.invoker.IInvocationContext;
24 import com.android.tradefed.invoker.InvocationContext;
25 import com.android.tradefed.invoker.TestInformation;
26 import com.android.tradefed.targetprep.TargetSetupError;
27 
28 import junit.framework.TestCase;
29 
30 import org.easymock.EasyMock;
31 
32 public class PropertyCheckTest extends TestCase {
33 
34     private PropertyCheck mPropertyCheck;
35     private IDeviceBuildInfo mMockBuildInfo;
36     private ITestDevice mMockDevice;
37     private OptionSetter mOptionSetter;
38     private TestInformation mTestInfo;
39 
40     private static final String PROPERTY = "ro.mock.property";
41     private static final String ACTUAL_VALUE = "mock_actual_value";
42     private static final String BAD_VALUE = "mock_bad_value";
43 
44     @Override
setUp()45     public void setUp() throws Exception {
46         super.setUp();
47         mPropertyCheck = new PropertyCheck();
48         mMockDevice = EasyMock.createMock(ITestDevice.class);
49         mMockBuildInfo = new DeviceBuildInfo("0", "");
50         mOptionSetter = new OptionSetter(mPropertyCheck);
51         EasyMock.expect(mMockDevice.getProperty(PROPERTY)).andReturn(ACTUAL_VALUE).anyTimes();
52         EasyMock.expect(mMockDevice.getDeviceDescriptor()).andReturn(null).anyTimes();
53         IInvocationContext context = new InvocationContext();
54         context.addAllocatedDevice("device", mMockDevice);
55         context.addDeviceBuildInfo("device", mMockBuildInfo);
56         mTestInfo = TestInformation.newBuilder().setInvocationContext(context).build();
57     }
58 
testWarningMatch()59     public void testWarningMatch() throws Exception {
60         mOptionSetter.setOptionValue("property-name", PROPERTY);
61         mOptionSetter.setOptionValue("expected-value", ACTUAL_VALUE);
62         mOptionSetter.setOptionValue("throw-error", "false");
63         EasyMock.replay(mMockDevice);
64         mPropertyCheck.run(mTestInfo); // no warnings or errors
65     }
66 
testWarningMismatch()67     public void testWarningMismatch() throws Exception {
68         mOptionSetter.setOptionValue("property-name", PROPERTY);
69         mOptionSetter.setOptionValue("expected-value", BAD_VALUE);
70         mOptionSetter.setOptionValue("throw-error", "false");
71         EasyMock.replay(mMockDevice);
72         mPropertyCheck.run(mTestInfo); // should only print a warning
73     }
74 
testErrorMatch()75     public void testErrorMatch() throws Exception {
76         mOptionSetter.setOptionValue("property-name", PROPERTY);
77         mOptionSetter.setOptionValue("expected-value", ACTUAL_VALUE);
78         mOptionSetter.setOptionValue("throw-error", "true");
79         EasyMock.replay(mMockDevice);
80         mPropertyCheck.run(mTestInfo); // no warnings or errors
81     }
82 
testErrorMismatch()83     public void testErrorMismatch() throws Exception {
84         mOptionSetter.setOptionValue("property-name", PROPERTY);
85         mOptionSetter.setOptionValue("expected-value", BAD_VALUE);
86         mOptionSetter.setOptionValue("throw-error", "true");
87         EasyMock.replay(mMockDevice);
88         try {
89             mPropertyCheck.run(mTestInfo); // expecting TargetSetupError
90             fail("TargetSetupError expected");
91         } catch (TargetSetupError e) {
92             // Expected
93         }
94     }
95 
96 }
97