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.compatibility.common.tradefed.targetprep;
17 
18 import static org.junit.Assert.assertNotNull;
19 import static org.junit.Assert.assertNull;
20 import static org.junit.Assert.assertTrue;
21 
22 import com.android.compatibility.common.tradefed.targetprep.ApkInstrumentationPreparer.When;
23 import com.android.compatibility.common.util.FileUtil;
24 import com.android.tradefed.build.BuildInfo;
25 import com.android.tradefed.build.IBuildInfo;
26 import com.android.tradefed.config.OptionSetter;
27 import com.android.tradefed.device.ITestDevice;
28 import com.android.tradefed.invoker.IInvocationContext;
29 import com.android.tradefed.invoker.InvocationContext;
30 import com.android.tradefed.invoker.TestInformation;
31 
32 import org.easymock.EasyMock;
33 import org.easymock.IAnswer;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.junit.runners.JUnit4;
39 
40 import java.io.File;
41 
42 /** Unit tests for {@link DeviceInfoCollector}. */
43 @RunWith(JUnit4.class)
44 public class DeviceInfoCollectorTest {
45 
46     private DeviceInfoCollector mCollector;
47     private ITestDevice mDevice;
48     private IBuildInfo mBuildInfo;
49     private TestInformation mTestInfo;
50 
51     @Before
setUp()52     public void setUp() throws Exception {
53         mCollector = new DeviceInfoCollector();
54         mDevice = EasyMock.createMock(ITestDevice.class);
55         mBuildInfo = new BuildInfo();
56         IInvocationContext context = new InvocationContext();
57         context.addDeviceBuildInfo("device", mBuildInfo);
58         context.addAllocatedDevice("device", mDevice);
59         OptionSetter setter = new OptionSetter(mCollector);
60         setter.setOptionValue("src-dir", "/sdcard/device-info-files/");
61         mTestInfo = TestInformation.newBuilder().setInvocationContext(context).build();
62     }
63 
64     @After
tearDown()65     public void tearDown() {
66         mBuildInfo.cleanUp();
67     }
68 
69     @Test
pullDeviceInfo_fail()70     public void pullDeviceInfo_fail() throws Exception {
71         mCollector.mWhen = When.AFTER;
72         EasyMock.expect(mDevice.getProperty(EasyMock.anyObject())).andStubReturn("value");
73         EasyMock.expect(
74                         mDevice.pullDir(
75                                 EasyMock.eq("/sdcard/device-info-files/"), EasyMock.anyObject()))
76                 .andAnswer(
77                         new IAnswer<Boolean>() {
78                             @Override
79                             public Boolean answer() throws Throwable {
80                                 File dir = (File) EasyMock.getCurrentArguments()[1];
81                                 FileUtil.recursiveDelete(dir);
82                                 return true;
83                             }
84                         });
85         EasyMock.replay(mDevice);
86         mCollector.setUp(mTestInfo);
87         EasyMock.verify(mDevice);
88         assertNull(mBuildInfo.getFile(DeviceInfoCollector.DEVICE_INFO_DIR));
89     }
90 
91     @Test
pullDeviceInfo()92     public void pullDeviceInfo() throws Exception {
93         mCollector.mWhen = When.AFTER;
94         EasyMock.expect(mDevice.getProperty(EasyMock.anyObject())).andStubReturn("value");
95         EasyMock.expect(
96                         mDevice.pullDir(
97                                 EasyMock.eq("/sdcard/device-info-files/"), EasyMock.anyObject()))
98                 .andReturn(true);
99         EasyMock.replay(mDevice);
100         mCollector.setUp(mTestInfo);
101         EasyMock.verify(mDevice);
102         File infoDir = mBuildInfo.getFile(DeviceInfoCollector.DEVICE_INFO_DIR);
103         assertNotNull(infoDir);
104         assertTrue(infoDir.isDirectory());
105     }
106 }
107