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 
17 package com.android.compatibility.common.tradefed.targetprep;
18 
19 import com.android.tradefed.build.IBuildInfo;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.device.ITestDevice;
22 import com.android.tradefed.invoker.IInvocationContext;
23 import com.android.tradefed.invoker.InvocationContext;
24 import com.android.tradefed.invoker.TestInformation;
25 import com.android.tradefed.targetprep.BuildError;
26 import com.android.tradefed.targetprep.TargetSetupError;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.JUnit4;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.MockitoAnnotations;
35 import org.mockito.invocation.InvocationOnMock;
36 import org.mockito.stubbing.Answer;
37 
38 import java.util.HashMap;
39 import java.util.Map;
40 
41 /** Unit tests for {@link VtsDeviceInfoCollector}. */
42 @RunWith(JUnit4.class)
43 public class VtsDeviceInfoCollectorTest {
44     // Device property values.
45     private static final String VENDOR_BUILD_VERSION_INCREMENTAL = "1000000";
46     private static final String VENDOR_FINGERPRINT =
47             "Android/aosp_sailfish/sailfish:8.0.0/OC/1000000:userdebug/test-keys";
48     private static final String ODM_DEVICE = "sailfish";
49     private static final String HARDWARE_SKU = "sku";
50     private static final String ODM_BUILD_VERSION_INCREMENTAL = "4311111";
51     private static final String ODM_FINGERPRINT =
52             "Android/aosp_sailfish/sailfish:8.0.0/OC/4311111:userdebug/test-keys";
53     private static final String ODM_FINGERPRINT_WITH_SKU =
54             "Android/aosp_sailfish/sailfish_sku:8.0.0/OC/4311111:userdebug/test-keys";
55 
56     @Mock private IBuildInfo mMockBuildInfo;
57     @Mock private ITestDevice mMockTestDevice;
58     private TestInformation mTestInfo;
59     private Map<String, String> mProperties;
60 
61     @Before
setUp()62     public void setUp() throws DeviceNotAvailableException {
63         MockitoAnnotations.initMocks(this);
64 
65         Mockito.when(mMockTestDevice.getProperty(Mockito.anyString()))
66                 .then(
67                         new Answer<String>() {
68                             @Override
69                             public String answer(InvocationOnMock invocation) {
70                                 String name = invocation.getArgument(0);
71                                 return mProperties.getOrDefault(name, "");
72                             }
73                         });
74 
75         mProperties = new HashMap<String, String>();
76         mProperties.put("ro.vendor.build.fingerprint", VENDOR_FINGERPRINT);
77         mProperties.put("ro.vendor.build.version.incremental", VENDOR_BUILD_VERSION_INCREMENTAL);
78         IInvocationContext context = new InvocationContext();
79         context.addDeviceBuildInfo("device", mMockBuildInfo);
80         context.addAllocatedDevice("device", mMockTestDevice);
81         mTestInfo = TestInformation.newBuilder().setInvocationContext(context).build();
82     }
83 
84     /** Test reporting vendor properties if ODM fingerprint is empty. */
85     @Test
testVendorProperties()86     public void testVendorProperties()
87             throws TargetSetupError, BuildError, DeviceNotAvailableException {
88         new VtsDeviceInfoCollector().setUp(mTestInfo);
89 
90         Mockito.verify(mMockBuildInfo)
91                 .addBuildAttribute("cts:build_fingerprint", VENDOR_FINGERPRINT);
92         Mockito.verify(mMockBuildInfo)
93                 .addBuildAttribute(
94                         "cts:build_version_incremental", VENDOR_BUILD_VERSION_INCREMENTAL);
95     }
96 
97     /**
98      * Test reporting ODM properties.
99      *
100      * <p>If ODM fingerprint is not empty, ODM build version is reported.
101      *
102      * <p>If ODM fingerprint contains SKU, the SKU is not reported.
103      */
104     @Test
testOdmProperties()105     public void testOdmProperties()
106             throws TargetSetupError, BuildError, DeviceNotAvailableException {
107         mProperties.put("ro.odm.build.fingerprint", ODM_FINGERPRINT_WITH_SKU);
108         mProperties.put("ro.odm.build.version.incremental", ODM_BUILD_VERSION_INCREMENTAL);
109         mProperties.put("ro.product.odm.device", ODM_DEVICE);
110         mProperties.put("ro.boot.product.hardware.sku", HARDWARE_SKU);
111 
112         new VtsDeviceInfoCollector().setUp(mTestInfo);
113 
114         Mockito.verify(mMockBuildInfo).addBuildAttribute("cts:build_fingerprint", ODM_FINGERPRINT);
115         Mockito.verify(mMockBuildInfo)
116                 .addBuildAttribute("cts:build_version_incremental", ODM_BUILD_VERSION_INCREMENTAL);
117     }
118 }
119