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.BuildInfo;
20 import com.android.tradefed.build.IBuildInfo;
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 /**
33  * Tests for {@link SettingsPreparer}
34  */
35 public class SettingsPreparerTest extends TestCase {
36 
37     private SettingsPreparer mSettingsPreparer;
38     private IBuildInfo mMockBuildInfo;
39     private ITestDevice mMockDevice;
40     private OptionSetter mOptionSetter;
41     private TestInformation mTestInfo;
42 
43     private static final String SHELL_CMD_GET = "settings get GLOBAL stay_on_while_plugged_in";
44     private static final String SHELL_CMD_PUT_7 = "settings put GLOBAL stay_on_while_plugged_in 7";
45     private static final String SHELL_CMD_PUT_8 = "settings put GLOBAL stay_on_while_plugged_in 8";
46 
47     @Override
setUp()48     public void setUp() throws Exception {
49         super.setUp();
50         mSettingsPreparer = new SettingsPreparer();
51         mMockDevice = EasyMock.createMock(ITestDevice.class);
52         EasyMock.expect(mMockDevice.getDeviceDescriptor()).andReturn(null).anyTimes();
53         mMockBuildInfo = new BuildInfo("0", "");
54         mOptionSetter = new OptionSetter(mSettingsPreparer);
55         mOptionSetter.setOptionValue("device-setting", "stay_on_while_plugged_in");
56         mOptionSetter.setOptionValue("setting-type", "global");
57         IInvocationContext context = new InvocationContext();
58         context.addAllocatedDevice("device", mMockDevice);
59         context.addDeviceBuildInfo("device", mMockBuildInfo);
60         mTestInfo = TestInformation.newBuilder().setInvocationContext(context).build();
61     }
62 
testCorrectOneExpected()63     public void testCorrectOneExpected() throws Exception {
64         EasyMock.expect(mMockDevice.executeShellCommand(SHELL_CMD_GET)).andReturn("\n3\n").once();
65         mOptionSetter.setOptionValue("expected-values", "3");
66         EasyMock.replay(mMockDevice);
67         mSettingsPreparer.run(mTestInfo);
68     }
69 
testCorrectManyExpected()70     public void testCorrectManyExpected() throws Exception {
71         EasyMock.expect(mMockDevice.executeShellCommand(SHELL_CMD_GET)).andReturn("\n3\n").once();
72         mOptionSetter.setOptionValue("expected-values", "2");
73         mOptionSetter.setOptionValue("expected-values", "3");
74         mOptionSetter.setOptionValue("expected-values", "6");
75         mOptionSetter.setOptionValue("expected-values", "7");
76         EasyMock.replay(mMockDevice);
77         mSettingsPreparer.run(mTestInfo);
78     }
79 
testIncorrectOneExpected()80     public void testIncorrectOneExpected() throws Exception {
81         EasyMock.expect(mMockDevice.executeShellCommand(SHELL_CMD_GET)).andReturn("\n0\n").once();
82         mOptionSetter.setOptionValue("expected-values", "3");
83         EasyMock.replay(mMockDevice);
84         try {
85             mSettingsPreparer.run(mTestInfo);
86             fail("TargetSetupError expected");
87         } catch (TargetSetupError e) {
88             //Expected
89         }
90     }
91 
testIncorrectManyExpected()92     public void testIncorrectManyExpected() throws Exception {
93         EasyMock.expect(mMockDevice.executeShellCommand(SHELL_CMD_GET)).andReturn("\n0\n").once();
94         mOptionSetter.setOptionValue("expected-values", "2");
95         mOptionSetter.setOptionValue("expected-values", "3");
96         mOptionSetter.setOptionValue("expected-values", "6");
97         mOptionSetter.setOptionValue("expected-values", "7");
98         EasyMock.replay(mMockDevice);
99         try {
100             mSettingsPreparer.run(mTestInfo);
101             fail("TargetSetupError expected");
102         } catch (TargetSetupError e) {
103             //Expected
104         }
105     }
106 
testCommandRun()107     public void testCommandRun() throws Exception {
108         EasyMock.expect(mMockDevice.executeShellCommand(SHELL_CMD_PUT_7)).andReturn("\n");
109         mOptionSetter.setOptionValue("set-value", "7");
110         EasyMock.replay(mMockDevice);
111         mSettingsPreparer.run(mTestInfo);
112     }
113 
testCommandRunWrongSetValue()114     public void testCommandRunWrongSetValue() throws Exception {
115         EasyMock.expect(mMockDevice.executeShellCommand(SHELL_CMD_PUT_8)).andReturn("\n");
116         mOptionSetter.setOptionValue("set-value", "8");
117         mOptionSetter.setOptionValue("expected-values", "7");
118         EasyMock.replay(mMockDevice);
119         try {
120             mSettingsPreparer.run(mTestInfo);
121             fail("TargetSetupError expected");
122         } catch (TargetSetupError e) {
123             //Expected
124         }
125     }
126 
testIncorrectOneExpectedCommandRun()127     public void testIncorrectOneExpectedCommandRun() throws Exception {
128         EasyMock.expect(mMockDevice.executeShellCommand(SHELL_CMD_GET)).andReturn("\n0\n").once();
129         EasyMock.expect(mMockDevice.executeShellCommand(SHELL_CMD_PUT_7)).andReturn("\n");
130         mOptionSetter.setOptionValue("set-value", "7");
131         mOptionSetter.setOptionValue("expected-values", "7");
132     }
133 
134 }
135