1 /*
2  * Copyright (C) 2016 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.cts.managedprofile;
18 
19 import android.graphics.Color;
20 
21 public class OrganizationInfoTest extends BaseManagedProfileTest {
22 
23     // needs to match DevicePolicyManagerService.ActiveAdmin.DEF_ORGANIZATION_COLOR
24     private static final int DEFAULT_ORGANIZATION_COLOR = Color.parseColor("#00796B");
25 
testDefaultOrganizationColor()26     public void testDefaultOrganizationColor() {
27         int defaultColor = mDevicePolicyManager.getOrganizationColor(ADMIN_RECEIVER_COMPONENT);
28         assertEquals("Default color returned: " + Integer.toHexString(defaultColor),
29                 DEFAULT_ORGANIZATION_COLOR, defaultColor);
30     }
31 
testSetOrganizationColor()32     public void testSetOrganizationColor() {
33         int previousColor = mDevicePolicyManager.getOrganizationColor(ADMIN_RECEIVER_COMPONENT);
34 
35         try {
36             final int[] colors = {
37                 Color.TRANSPARENT,
38                 Color.WHITE,
39                 Color.RED,
40                 Color.GREEN,
41                 Color.BLUE,
42                 0x7FFE5B35 /* HTML name: "Sunset orange". Opacity: 50%. */
43             };
44 
45             for (int color : colors) {
46                 mDevicePolicyManager.setOrganizationColor(ADMIN_RECEIVER_COMPONENT, color);
47                 assertEquals(color | 0xFF000000 /* opacity always enforced to 100% */,
48                         mDevicePolicyManager.getOrganizationColor(ADMIN_RECEIVER_COMPONENT));
49             }
50         } finally {
51             // Put the organization color back how it was.
52             mDevicePolicyManager.setOrganizationColor(ADMIN_RECEIVER_COMPONENT, previousColor);
53         }
54     }
55 
testSetOrGetOrganizationColorWithNullAdminFails()56     public void testSetOrGetOrganizationColorWithNullAdminFails() {
57         try {
58             mDevicePolicyManager.setOrganizationColor(null, Color.GRAY);
59             fail("Exception should have been thrown for null admin ComponentName");
60         } catch (Exception expected) {
61         }
62 
63         try {
64             int color = mDevicePolicyManager.getOrganizationColor(null);
65             fail("Exception should have been thrown for null admin ComponentName");
66         } catch (Exception expected) {
67         }
68     }
69 
testDefaultOrganizationNameIsNull()70     public void testDefaultOrganizationNameIsNull() {
71         CharSequence organizationName = mDevicePolicyManager.getOrganizationName(
72                 ADMIN_RECEIVER_COMPONENT);
73         assertNull(organizationName);
74     }
75 
testSetOrganizationName()76     public void testSetOrganizationName() {
77         CharSequence previousOrganizationName = mDevicePolicyManager.getOrganizationName(
78                 ADMIN_RECEIVER_COMPONENT);
79 
80         try {
81             final CharSequence name = "test-set-name";
82             mDevicePolicyManager.setOrganizationName(ADMIN_RECEIVER_COMPONENT, name);
83             CharSequence organizationName = mDevicePolicyManager.getOrganizationName(
84                     ADMIN_RECEIVER_COMPONENT);
85             assertEquals(name, organizationName);
86         } finally {
87             mDevicePolicyManager.setOrganizationName(ADMIN_RECEIVER_COMPONENT,
88                     previousOrganizationName);
89         }
90     }
91 
testSetOrGetOrganizationNameWithNullAdminFails()92     public void testSetOrGetOrganizationNameWithNullAdminFails() {
93         try {
94             mDevicePolicyManager.setOrganizationName(null, "null-admin-fails");
95             fail("Exception should have been thrown for null admin ComponentName");
96         } catch (Exception expected) {
97         }
98 
99         try {
100             mDevicePolicyManager.getOrganizationName(null);
101             fail("Exception should have been thrown for null admin ComponentName");
102         } catch (Exception expected) {
103         }
104     }
105 }
106