1 /* 2 * Copyright 2017, 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.managedprovisioning.model; 17 18 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE; 19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE; 20 21 import static com.android.managedprovisioning.model.CustomizationParams.DEFAULT_STATUS_BAR_COLOR_ID; 22 23 import static org.hamcrest.MatcherAssert.assertThat; 24 import static org.hamcrest.Matchers.equalTo; 25 import static org.hamcrest.Matchers.nullValue; 26 import static org.mockito.ArgumentMatchers.any; 27 import static org.mockito.Mockito.when; 28 29 import android.content.ComponentName; 30 import android.content.Context; 31 import android.graphics.Color; 32 33 import androidx.test.InstrumentationRegistry; 34 import androidx.test.filters.SmallTest; 35 36 import com.android.managedprovisioning.common.Utils; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.junit.MockitoJUnitRunner; 43 44 @SmallTest 45 @RunWith(MockitoJUnitRunner.class) 46 public class CustomizationParamsTest { 47 private static final Context mContext = InstrumentationRegistry.getTargetContext(); 48 private static final ComponentName COMPONENT_NAME = new ComponentName("org.test", "ATestDPC"); 49 private static final int SAMPLE_COLOR = Color.rgb(11, 22, 33); 50 private static final String SAMPLE_URL = "http://d.android.com"; 51 private static final String SAMPLE_ORG_NAME = "Organization Inc."; 52 private static final int DEFAULT_MAIN_COLOR = Color.rgb(99, 99, 99); 53 54 @Mock 55 private Utils mUtils; 56 57 @Before setup()58 public void setup() { 59 when(mUtils.getAccentColor(any())).thenReturn(DEFAULT_MAIN_COLOR); 60 } 61 62 @Test defaultColorManagedProfile()63 public void defaultColorManagedProfile() { 64 // given 65 ProvisioningParams params = createParams(ACTION_PROVISION_MANAGED_PROFILE, null, null, 66 null); 67 68 // when 69 CustomizationParams instance = createInstance(params); 70 71 // then 72 assertThat(instance.statusBarColor, equalTo(getColor(DEFAULT_STATUS_BAR_COLOR_ID))); 73 assertThat(instance.mainColor, equalTo(DEFAULT_MAIN_COLOR)); 74 } 75 76 @Test defaultColorDeviceOwner()77 public void defaultColorDeviceOwner() { 78 // given 79 ProvisioningParams params = createParams(ACTION_PROVISION_MANAGED_DEVICE, null, null, null); 80 81 // when 82 CustomizationParams instance = createInstance(params); 83 84 // then 85 assertThat(instance.statusBarColor, equalTo(getColor(DEFAULT_STATUS_BAR_COLOR_ID))); 86 assertThat(instance.mainColor, equalTo(DEFAULT_MAIN_COLOR)); 87 } 88 89 @Test respectsMainColor()90 public void respectsMainColor() { 91 // given 92 ProvisioningParams params = createParams(null, SAMPLE_COLOR, null, null); 93 94 // when 95 CustomizationParams instance = createInstance(params); 96 97 // then 98 assertThat(instance.statusBarColor, equalTo(SAMPLE_COLOR)); 99 assertThat(instance.mainColor, equalTo(SAMPLE_COLOR)); 100 } 101 102 @Test orgNameDefaultsToNull()103 public void orgNameDefaultsToNull() { 104 // given 105 ProvisioningParams params = createParams(null, null, null, null); 106 107 // when 108 CustomizationParams instance = createInstance(params); 109 110 // then 111 assertThat(instance.orgName, nullValue()); 112 } 113 114 @Test respectsOrgName()115 public void respectsOrgName() { 116 // given 117 ProvisioningParams params = createParams(null, null, null, SAMPLE_ORG_NAME); 118 119 // when 120 CustomizationParams instance = createInstance(params); 121 122 // then 123 assertThat(instance.orgName, equalTo(SAMPLE_ORG_NAME)); 124 } 125 126 @Test respectsUrl()127 public void respectsUrl() { 128 // given 129 ProvisioningParams params = createParams(null, null, SAMPLE_URL, null); 130 131 // when 132 CustomizationParams instance = createInstance(params); 133 134 // then 135 assertThat(instance.supportUrl, equalTo(SAMPLE_URL)); 136 } 137 138 @Test urlDefaultsToNull()139 public void urlDefaultsToNull() { 140 // given 141 ProvisioningParams params = createParams(null, null, null, null); 142 143 // when 144 CustomizationParams instance = createInstance(params); 145 146 // then 147 assertThat(instance.supportUrl, nullValue()); 148 } 149 150 @Test ignoresInvalidUrl()151 public void ignoresInvalidUrl() { 152 // given 153 ProvisioningParams params = createParams(null, null, "not a valid web url", null); 154 155 // when 156 CustomizationParams instance = createInstance(params); 157 158 // then 159 assertThat(instance.supportUrl, nullValue()); 160 } 161 createInstance(ProvisioningParams params)162 private CustomizationParams createInstance(ProvisioningParams params) { 163 return CustomizationParams.createInstance(params, mContext, mUtils); 164 } 165 createParams(String provisioningAction, Integer mainColor, String supportUrl, String orgName)166 private ProvisioningParams createParams(String provisioningAction, Integer mainColor, 167 String supportUrl, String orgName) { 168 ProvisioningParams.Builder builder = 169 new ProvisioningParams.Builder().setDeviceAdminComponentName(COMPONENT_NAME); 170 171 builder.setProvisioningAction(provisioningAction == null ? ACTION_PROVISION_MANAGED_DEVICE 172 : provisioningAction); 173 174 if (mainColor != null) { 175 builder.setMainColor(mainColor); 176 } 177 178 if (supportUrl != null) { 179 builder.setSupportUrl(supportUrl); 180 } 181 182 if (orgName != null) { 183 builder.setOrganizationName(orgName); 184 } 185 186 return builder.build(); 187 } 188 getColor(int colorId)189 private int getColor(int colorId) { 190 return mContext.getColor(colorId); 191 } 192 }