• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.test.multiuser;
18 
19 import static android.provider.Settings.Secure.FONT_WEIGHT_ADJUSTMENT;
20 import static android.provider.Settings.System.FONT_SCALE;
21 
22 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
23 
24 import static org.junit.Assert.assertEquals;
25 
26 import android.content.ContentResolver;
27 import android.content.Context;
28 import android.os.UserManager;
29 import android.provider.Settings;
30 
31 import androidx.test.platform.app.InstrumentationRegistry;
32 
33 import org.junit.Assume;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.junit.runners.JUnit4;
37 
38 @RunWith(JUnit4.class)
39 public class MultiUserSettingsTests {
40     private final Context mContext = getInstrumentation().getTargetContext();
41     private final ContentResolver mContentResolver = mContext.getContentResolver();
42 
waitForBroadcastIdle()43     private static void waitForBroadcastIdle() throws InterruptedException {
44         final int sleepDuration = 1000;
45         final String cmdAmWaitForBroadcastIdle = "am wait-for-broadcast-idle";
46 
47         Thread.sleep(sleepDuration);
48         InstrumentationRegistry.getInstrumentation().getUiAutomation()
49                 .executeShellCommand(cmdAmWaitForBroadcastIdle);
50         Thread.sleep(sleepDuration);
51     }
52 
getGlobalFontScale()53     private float getGlobalFontScale() {
54         return mContext.getResources().getConfiguration().fontScale;
55     }
56 
getGlobalFontWeight()57     private int getGlobalFontWeight() {
58         return mContext.getResources().getConfiguration().fontWeightAdjustment;
59     }
60 
getFontScaleOfUser(int userId)61     private float getFontScaleOfUser(int userId) {
62         return Settings.System.getFloatForUser(mContentResolver, FONT_SCALE, 1, userId);
63     }
64 
getFontWeightOfUser(int userId)65     private int getFontWeightOfUser(int userId) {
66         return Settings.Secure.getIntForUser(mContentResolver, FONT_WEIGHT_ADJUSTMENT, 1, userId);
67     }
68 
setFontScaleOfUser(float fontScale, int userId)69     private void setFontScaleOfUser(float fontScale, int userId) throws InterruptedException {
70         Settings.System.putFloatForUser(mContentResolver, FONT_SCALE, fontScale, userId);
71         waitForBroadcastIdle();
72     }
73 
setFontWeightOfUser(int fontWeight, int userId)74     private void setFontWeightOfUser(int fontWeight, int userId) throws InterruptedException {
75         Settings.Secure.putIntForUser(mContentResolver, FONT_WEIGHT_ADJUSTMENT, fontWeight, userId);
76         waitForBroadcastIdle();
77     }
78 
79     @Test
testChangingFontScaleOfABackgroundUser_shouldNotAffectUI()80     public void testChangingFontScaleOfABackgroundUser_shouldNotAffectUI()
81             throws InterruptedException {
82 
83         Assume.assumeTrue(UserManager.supportsMultipleUsers());
84 
85         UserManager userManager = UserManager.get(mContext);
86 
87         final int backgroundUserId = userManager.createUser("test_user",
88                 UserManager.USER_TYPE_FULL_SECONDARY, 0).id;
89         final float oldFontScaleOfBgUser = getFontScaleOfUser(backgroundUserId);
90         final float oldGlobalFontScale = getGlobalFontScale();
91         final float newFontScaleOfBgUser = 1 + Math.max(oldGlobalFontScale, oldFontScaleOfBgUser);
92 
93         try {
94             setFontScaleOfUser(newFontScaleOfBgUser, backgroundUserId);
95             final float newGlobalFontScale = getGlobalFontScale();
96             assertEquals(oldGlobalFontScale, newGlobalFontScale, 0);
97         } finally {
98             setFontScaleOfUser(oldFontScaleOfBgUser, backgroundUserId);
99             userManager.removeUser(backgroundUserId);
100         }
101     }
102 
103     @Test
testChangingFontWeightOfABackgroundUser_shouldNotAffectUI()104     public void testChangingFontWeightOfABackgroundUser_shouldNotAffectUI()
105             throws InterruptedException {
106 
107         Assume.assumeTrue(UserManager.supportsMultipleUsers());
108 
109         UserManager userManager = UserManager.get(mContext);
110 
111         final int backgroundUserId = userManager.createUser("test_user",
112                 UserManager.USER_TYPE_FULL_SECONDARY, 0).id;
113         final int oldFontWeightOfBgUser = getFontWeightOfUser(backgroundUserId);
114         final int oldGlobalFontWeight = getGlobalFontWeight();
115         final int newFontWeightOfBgUser = 2 * Math.max(oldGlobalFontWeight, oldFontWeightOfBgUser);
116 
117         try {
118             setFontWeightOfUser(newFontWeightOfBgUser, backgroundUserId);
119             final int newGlobalFontWeight = getGlobalFontWeight();
120             assertEquals(oldGlobalFontWeight, newGlobalFontWeight);
121         } finally {
122             setFontWeightOfUser(oldFontWeightOfBgUser, backgroundUserId);
123             userManager.removeUser(backgroundUserId);
124         }
125     }
126 
127     @Test
testChangingFontScaleOfTheForegroundUser_shouldAffectUI()128     public void testChangingFontScaleOfTheForegroundUser_shouldAffectUI()
129             throws InterruptedException {
130 
131         Assume.assumeTrue(UserManager.supportsMultipleUsers());
132 
133         final int currentUserId = mContext.getUserId();
134         final float oldFontScale = getFontScaleOfUser(currentUserId);
135         final float newFontScale = 1 + oldFontScale;
136 
137         try {
138             setFontScaleOfUser(newFontScale, currentUserId);
139             final float globalFontScale = getGlobalFontScale();
140             assertEquals(newFontScale, globalFontScale, 0);
141         } finally {
142             setFontScaleOfUser(oldFontScale, currentUserId);
143         }
144     }
145 
146     @Test
testChangingFontWeightOfTheForegroundUser_shouldAffectUI()147     public void testChangingFontWeightOfTheForegroundUser_shouldAffectUI()
148             throws InterruptedException {
149 
150         Assume.assumeTrue(UserManager.supportsMultipleUsers());
151 
152         final int currentUserId = mContext.getUserId();
153         final int oldFontWeight = getFontWeightOfUser(currentUserId);
154         final int newFontWeight = 2 * oldFontWeight;
155 
156         try {
157             setFontWeightOfUser(newFontWeight, currentUserId);
158             final int globalFontWeight = getGlobalFontWeight();
159             assertEquals(newFontWeight, globalFontWeight);
160         } finally {
161             setFontWeightOfUser(oldFontWeight, currentUserId);
162         }
163     }
164 }
165