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.settings;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.os.SystemProperties;
22 
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.robolectric.Robolectric;
27 import org.robolectric.RobolectricTestRunner;
28 
29 
30 @RunWith(RobolectricTestRunner.class)
31 public class RegulatoryInfoDisplayActivityTest {
32 
33     private static final String SKU_PROP_KEY = "ro.boot.hardware.sku";
34     private static final String COO_PROP_KEY = "ro.boot.hardware.coo";
35 
36     private RegulatoryInfoDisplayActivity mRegulatoryInfoDisplayActivity;
37 
38     @Before
setUp()39     public void setUp() {
40         mRegulatoryInfoDisplayActivity = Robolectric.buildActivity(
41                 RegulatoryInfoDisplayActivity.class).create().get();
42     }
43 
44     @Test
getResourceId_noSkuProperty_shouldReturnDefaultLabel()45     public void getResourceId_noSkuProperty_shouldReturnDefaultLabel() {
46         SystemProperties.set(SKU_PROP_KEY, "");
47 
48         final int expectedResId = getResourceId("regulatory_info");
49         assertThat(mRegulatoryInfoDisplayActivity.getResourceId()).isEqualTo(expectedResId);
50     }
51 
52     @Test
getResourceId_noCooProperty_shouldReturnSkuLabel()53     public void getResourceId_noCooProperty_shouldReturnSkuLabel() {
54         SystemProperties.set(SKU_PROP_KEY, "sku");
55         SystemProperties.set(COO_PROP_KEY, "");
56 
57         final int expectedResId = getResourceId("regulatory_info_sku");
58         assertThat(mRegulatoryInfoDisplayActivity.getResourceId()).isEqualTo(expectedResId);
59     }
60 
61     @Test
getResourceId_hasSkuAndCooProperties_shouldReturnCooLabel()62     public void getResourceId_hasSkuAndCooProperties_shouldReturnCooLabel() {
63         SystemProperties.set(SKU_PROP_KEY, "sku1");
64         SystemProperties.set(COO_PROP_KEY, "coo");
65 
66         final int expectedResId = getResourceId("regulatory_info_sku1_coo");
67         assertThat(mRegulatoryInfoDisplayActivity.getResourceId()).isEqualTo(expectedResId);
68     }
69 
70     @Test
getResourceId_noCorrespondingCooLabel_shouldReturnSkuLabel()71     public void getResourceId_noCorrespondingCooLabel_shouldReturnSkuLabel() {
72         SystemProperties.set(SKU_PROP_KEY, "sku");
73         SystemProperties.set(COO_PROP_KEY, "unknown");
74 
75         final int expectedResId = getResourceId("regulatory_info_sku");
76         assertThat(mRegulatoryInfoDisplayActivity.getResourceId()).isEqualTo(expectedResId);
77     }
78 
getResourceId(String resourceName)79     private int getResourceId(String resourceName) {
80         return mRegulatoryInfoDisplayActivity.getResources().getIdentifier(resourceName, "drawable",
81                 mRegulatoryInfoDisplayActivity.getPackageName());
82     }
83 }
84