1 /*
2  * Copyright (C) 2019 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 package com.android.scenario;
17 
18 import static org.mockito.Mockito.any;
19 import static org.mockito.Mockito.doNothing;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.eq;
22 import static org.mockito.Mockito.never;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.startsWith;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.MockitoAnnotations.initMocks;
28 
29 import com.android.tradefed.config.OptionSetter;
30 import com.android.tradefed.device.ITestDevice;
31 import com.android.tradefed.result.ITestInvocationListener;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.junit.runners.JUnit4;
37 import org.mockito.Mock;
38 
39 /** Unit tests for {@link AppSetup}. */
40 @RunWith(JUnit4.class)
41 public class AppSetupTest {
42     private static final String DROP_CACHE_OPTION = "drop-cache-when-finished";
43     private static final String KILL_APPS_OPTION = "apps-to-kill-when-finished";
44 
45     private AppSetup mAppSetup;
46     private OptionSetter mOptionSetter;
47 
48     @Mock ITestDevice mTestDevice;
49     @Mock ITestInvocationListener mListener;
50 
51     @Before
setUp()52     public void setUp() throws Exception {
53         initMocks(this);
54         mAppSetup = spy(new AppSetup());
55         doReturn(mTestDevice).when(mAppSetup).getDevice();
56         doNothing().when(mAppSetup).runTest(any());
57         mOptionSetter = new OptionSetter(mAppSetup);
58     }
59 
60     /** Test that the test drops cache when configured thusly. */
61     @Test
testDropCachesOption_set()62     public void testDropCachesOption_set() throws Exception {
63         mOptionSetter.setOptionValue(DROP_CACHE_OPTION, String.valueOf(true));
64         mAppSetup.run(mListener);
65         verify(mTestDevice).executeShellCommand(eq(AppSetup.DROP_CACHE_COMMAND));
66     }
67 
68     /** Test that the test does not drop cache when not configured to. */
69     @Test
testDropCachesOption_notSet()70     public void testDropCachesOption_notSet() throws Exception {
71         mAppSetup.run(mListener);
72         verify(mTestDevice, never()).executeShellCommand(eq(AppSetup.DROP_CACHE_COMMAND));
73     }
74 
75     /** Test that the test kills the list of apps as supplied. */
76     @Test
testKillAppsOption()77     public void testKillAppsOption() throws Exception {
78         mOptionSetter.setOptionValue(KILL_APPS_OPTION, "app1");
79         mOptionSetter.setOptionValue(KILL_APPS_OPTION, "app2");
80         mAppSetup.run(mListener);
81         verify(mTestDevice, times(2))
82                 .executeShellCommand(
83                         startsWith(String.format(AppSetup.KILL_APP_COMMAND_TEMPLATE, "")));
84         verify(mTestDevice, times(1))
85                 .executeShellCommand(eq(String.format(AppSetup.KILL_APP_COMMAND_TEMPLATE, "app1")));
86         verify(mTestDevice, times(1))
87                 .executeShellCommand(eq(String.format(AppSetup.KILL_APP_COMMAND_TEMPLATE, "app2")));
88     }
89 
90     /** Test setting disable option exits from the run method early.*/
91     @Test
testDisableOption()92     public void testDisableOption() throws Exception {
93         mOptionSetter.setOptionValue("disable", String.valueOf(true));
94         mAppSetup.run(mListener);
95         verify(mAppSetup, times(0)).runTest(any());
96     }
97 }
98