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 20 import static org.hamcrest.MatcherAssert.assertThat; 21 import static org.hamcrest.Matchers.equalTo; 22 import static org.hamcrest.Matchers.nullValue; 23 import static org.mockito.ArgumentMatchers.any; 24 import static org.mockito.Mockito.when; 25 26 import android.content.ComponentName; 27 import android.content.Context; 28 import android.graphics.Color; 29 30 import androidx.test.InstrumentationRegistry; 31 import androidx.test.filters.SmallTest; 32 33 import com.android.managedprovisioning.common.Utils; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.junit.MockitoJUnitRunner; 40 41 @SmallTest 42 @RunWith(MockitoJUnitRunner.class) 43 public class CustomizationParamsTest { 44 private static final Context mContext = InstrumentationRegistry.getTargetContext(); 45 private static final ComponentName COMPONENT_NAME = new ComponentName("org.test", "ATestDPC"); 46 private static final String SAMPLE_URL = "http://d.android.com"; 47 private static final int DEFAULT_LOGO_COLOR = Color.rgb(99, 99, 99); 48 49 @Mock 50 private Utils mUtils; 51 52 @Before setup()53 public void setup() { 54 when(mUtils.getAccentColor(any())).thenReturn(DEFAULT_LOGO_COLOR); 55 } 56 57 @Test respectsUrl()58 public void respectsUrl() { 59 // given 60 ProvisioningParams params = createParams(null, SAMPLE_URL, null); 61 62 // when 63 CustomizationParams instance = createInstance(params); 64 65 // then 66 assertThat(instance.supportUrl, equalTo(SAMPLE_URL)); 67 } 68 69 @Test urlDefaultsToNull()70 public void urlDefaultsToNull() { 71 // given 72 ProvisioningParams params = createParams(null, null, null); 73 74 // when 75 CustomizationParams instance = createInstance(params); 76 77 // then 78 assertThat(instance.supportUrl, nullValue()); 79 } 80 81 @Test ignoresInvalidUrl()82 public void ignoresInvalidUrl() { 83 // given 84 ProvisioningParams params = createParams(null, "not a valid web url", null); 85 86 // when 87 CustomizationParams instance = createInstance(params); 88 89 // then 90 assertThat(instance.supportUrl, nullValue()); 91 } 92 createInstance(ProvisioningParams params)93 private CustomizationParams createInstance(ProvisioningParams params) { 94 return CustomizationParams.createInstance(params, mContext, mUtils); 95 } 96 createParams( String provisioningAction, String supportUrl, String orgName)97 private ProvisioningParams createParams( 98 String provisioningAction, String supportUrl, String orgName) { 99 ProvisioningParams.Builder builder = 100 new ProvisioningParams.Builder().setDeviceAdminComponentName(COMPONENT_NAME); 101 102 builder.setProvisioningAction(provisioningAction == null ? ACTION_PROVISION_MANAGED_DEVICE 103 : provisioningAction); 104 105 if (supportUrl != null) { 106 builder.setSupportUrl(supportUrl); 107 } 108 109 if (orgName != null) { 110 builder.setOrganizationName(orgName); 111 } 112 113 return builder.build(); 114 } 115 getColor(int colorId)116 private int getColor(int colorId) { 117 return mContext.getColor(colorId); 118 } 119 } 120