1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "ash/magnifier/magnification_controller.h"
#include "ash/shell.h"
#include "base/command_line.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
#include "chrome/browser/chromeos/accessibility/magnification_manager.h"
#include "chrome/browser/chromeos/login/helper.h"
#include "chrome/browser/chromeos/login/login_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/chromeos_switches.h"
#include "components/user_manager/user_manager.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace {
const char kTestUserName[] = "owner@invalid.domain";
void SetMagnifierEnabled(bool enabled) {
MagnificationManager::Get()->SetMagnifierEnabled(enabled);
}
void SetMagnifierType(ui::MagnifierType type) {
MagnificationManager::Get()->SetMagnifierType(type);
}
void SetFullScreenMagnifierScale(double scale) {
ash::Shell::GetInstance()->
magnification_controller()->SetScale(scale, false);
}
double GetFullScreenMagnifierScale() {
return ash::Shell::GetInstance()->magnification_controller()->GetScale();
}
void SetSavedFullScreenMagnifierScale(double scale) {
MagnificationManager::Get()->SaveScreenMagnifierScale(scale);
}
double GetSavedFullScreenMagnifierScale() {
return MagnificationManager::Get()->GetSavedScreenMagnifierScale();
}
ui::MagnifierType GetMagnifierType() {
return MagnificationManager::Get()->GetMagnifierType();
}
bool IsMagnifierEnabled() {
return MagnificationManager::Get()->IsMagnifierEnabled();
}
Profile* profile() {
Profile* profile = ProfileManager::GetActiveUserProfile();
DCHECK(profile);
return profile;
}
PrefService* prefs() {
return user_prefs::UserPrefs::Get(profile());
}
void SetScreenMagnifierEnabledPref(bool enabled) {
prefs()->SetBoolean(prefs::kAccessibilityScreenMagnifierEnabled, enabled);
}
void SetScreenMagnifierTypePref(ui::MagnifierType type) {
prefs()->SetInteger(prefs::kAccessibilityScreenMagnifierType, type);
}
void SetFullScreenMagnifierScalePref(double scale) {
prefs()->SetDouble(prefs::kAccessibilityScreenMagnifierScale, scale);
}
bool GetScreenMagnifierEnabledFromPref() {
return prefs()->GetBoolean(prefs::kAccessibilityScreenMagnifierEnabled);
}
// Creates and logs into a profile with account |name|, and makes sure that
// the profile is regarded as "non new" in the next login. This is used in
// PRE_XXX cases so that in the main XXX case we can test non new profiles.
void PrepareNonNewProfile(const std::string& name) {
user_manager::UserManager::Get()->UserLoggedIn(name, name, true);
// To prepare a non-new profile for tests, we must ensure the profile
// directory and the preference files are created, because that's what
// Profile::IsNewProfile() checks. UserLoggedIn(), however, does not yet
// create the profile directory until GetActiveUserProfile() is called.
ProfileManager::GetActiveUserProfile();
}
} // namespace
class MockMagnificationObserver {
public:
MockMagnificationObserver() : observed_(false),
observed_enabled_(false),
magnifier_type_(-1)
{
AccessibilityManager* accessibility_manager = AccessibilityManager::Get();
CHECK(accessibility_manager);
accessibility_subscription_ = accessibility_manager->RegisterCallback(
base::Bind(&MockMagnificationObserver::OnAccessibilityStatusChanged,
base::Unretained(this)));
}
virtual ~MockMagnificationObserver() {}
bool observed() const { return observed_; }
bool observed_enabled() const { return observed_enabled_; }
int magnifier_type() const { return magnifier_type_; }
void reset() { observed_ = false; }
private:
void OnAccessibilityStatusChanged(
const AccessibilityStatusEventDetails& details) {
if (details.notification_type == ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFIER) {
magnifier_type_ = details.magnifier_type;
observed_enabled_ = details.enabled;
observed_ = true;
}
}
bool observed_;
bool observed_enabled_;
int magnifier_type_;
scoped_ptr<AccessibilityStatusSubscription> accessibility_subscription_;
DISALLOW_COPY_AND_ASSIGN(MockMagnificationObserver);
};
class MagnificationManagerTest : public InProcessBrowserTest {
protected:
MagnificationManagerTest() {}
~MagnificationManagerTest() override {}
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(switches::kLoginManager);
command_line->AppendSwitchASCII(switches::kLoginProfile,
TestingProfile::kTestUserProfileDir);
}
void SetUpOnMainThread() override {
// Set the login-screen profile.
MagnificationManager::Get()->SetProfileForTest(
ProfileManager::GetActiveUserProfile());
}
DISALLOW_COPY_AND_ASSIGN(MagnificationManagerTest);
};
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, PRE_LoginOffToOff) {
// Create a new profile once, to run the test with non-new profile.
PrepareNonNewProfile(kTestUserName);
// Sets pref to explicitly disable the magnifier.
SetScreenMagnifierEnabledPref(false);
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, LoginOffToOff) {
// Confirms that magnifier is disabled on the login screen.
EXPECT_FALSE(IsMagnifierEnabled());
// Disables magnifier on login screen.
SetMagnifierEnabled(false);
EXPECT_FALSE(IsMagnifierEnabled());
// Logs in with existing profile.
user_manager::UserManager::Get()->UserLoggedIn(
kTestUserName, kTestUserName, true);
// Confirms that magnifier is still disabled just after login.
EXPECT_FALSE(IsMagnifierEnabled());
user_manager::UserManager::Get()->SessionStarted();
// Confirms that magnifier is still disabled just after session starts.
EXPECT_FALSE(IsMagnifierEnabled());
// Enables magnifier.
SetMagnifierEnabled(true);
// Confirms that magnifier is enabled.
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
EXPECT_TRUE(GetScreenMagnifierEnabledFromPref());
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, PRE_LoginFullToOff) {
// Create a new profile once, to run the test with non-new profile.
PrepareNonNewProfile(kTestUserName);
// Sets pref to explicitly disable the magnifier.
SetScreenMagnifierEnabledPref(false);
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, LoginFullToOff) {
// Confirms that magnifier is disabled on the login screen.
EXPECT_FALSE(IsMagnifierEnabled());
// Enables magnifier on login screen.
SetMagnifierEnabled(true);
SetMagnifierType(ui::MAGNIFIER_FULL);
SetFullScreenMagnifierScale(2.5);
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
// Logs in (but the session is not started yet).
user_manager::UserManager::Get()->UserLoggedIn(
kTestUserName, kTestUserName, true);
// Confirms that magnifier is keeping enabled.
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
user_manager::UserManager::Get()->SessionStarted();
// Confirms that magnifier is disabled just after session start.
EXPECT_FALSE(IsMagnifierEnabled());
EXPECT_FALSE(GetScreenMagnifierEnabledFromPref());
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, PRE_LoginOffToFull) {
// Create a new profile once, to run the test with non-new profile.
PrepareNonNewProfile(kTestUserName);
// Sets prefs to explicitly enable the magnifier.
SetScreenMagnifierEnabledPref(true);
SetScreenMagnifierTypePref(ui::MAGNIFIER_FULL);
SetFullScreenMagnifierScalePref(2.5);
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, LoginOffToFull) {
// Disables magnifier on login screen.
SetMagnifierEnabled(false);
EXPECT_FALSE(IsMagnifierEnabled());
// Logs in (but the session is not started yet).
user_manager::UserManager::Get()->UserLoggedIn(
kTestUserName, kTestUserName, true);
// Confirms that magnifier is keeping disabled.
EXPECT_FALSE(IsMagnifierEnabled());
user_manager::UserManager::Get()->SessionStarted();
// Confirms that the magnifier is enabled and configured according to the
// explicitly set prefs just after session start.
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
EXPECT_TRUE(GetScreenMagnifierEnabledFromPref());
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, PRE_LoginFullToFull) {
// Create a new profile once, to run the test with non-new profile.
PrepareNonNewProfile(kTestUserName);
// Sets prefs to explicitly enable the magnifier.
SetScreenMagnifierEnabledPref(true);
SetScreenMagnifierTypePref(ui::MAGNIFIER_FULL);
SetFullScreenMagnifierScalePref(2.5);
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, LoginFullToFull) {
// Enables magnifier on login screen.
SetMagnifierType(ui::MAGNIFIER_FULL);
SetMagnifierEnabled(true);
SetFullScreenMagnifierScale(3.0);
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
EXPECT_EQ(3.0, GetFullScreenMagnifierScale());
// Logs in (but the session is not started yet).
user_manager::UserManager::Get()->UserLoggedIn(
kTestUserName, kTestUserName, true);
// Confirms that magnifier is keeping enabled.
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
user_manager::UserManager::Get()->SessionStarted();
// Confirms that the magnifier is enabled and configured according to the
// explicitly set prefs just after session start.
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
EXPECT_TRUE(GetScreenMagnifierEnabledFromPref());
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, PRE_LoginFullToUnset) {
// Creates a new profile once, to run the test with non-new profile.
PrepareNonNewProfile(kTestUserName);
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, LoginFullToUnset) {
// Enables full screen magnifier.
SetMagnifierType(ui::MAGNIFIER_FULL);
SetMagnifierEnabled(true);
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
// Logs in (but the session is not started yet).
user_manager::UserManager::Get()->UserLoggedIn(
kTestUserName, kTestUserName, true);
// Confirms that magnifier is keeping enabled.
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
user_manager::UserManager::Get()->SessionStarted();
// Confirms that magnifier is disabled.
EXPECT_FALSE(IsMagnifierEnabled());
EXPECT_FALSE(GetScreenMagnifierEnabledFromPref());
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, LoginAsNewUserOff) {
// Confirms that magnifier is disabled on the login screen.
EXPECT_FALSE(IsMagnifierEnabled());
// Disables magnifier on login screen explicitly.
SetMagnifierEnabled(false);
// Logs in (but the session is not started yet).
user_manager::UserManager::Get()->UserLoggedIn(
kTestUserName, kTestUserName, true);
// Confirms that magnifier is keeping disabled.
EXPECT_FALSE(IsMagnifierEnabled());
user_manager::UserManager::Get()->SessionStarted();
// Confirms that magnifier is keeping disabled.
EXPECT_FALSE(IsMagnifierEnabled());
EXPECT_FALSE(GetScreenMagnifierEnabledFromPref());
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, LoginAsNewUserFull) {
// Enables magnifier on login screen.
SetMagnifierType(ui::MAGNIFIER_FULL);
SetMagnifierEnabled(true);
SetFullScreenMagnifierScale(2.5);
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
// Logs in (but the session is not started yet).
user_manager::UserManager::Get()->UserLoggedIn(
kTestUserName, kTestUserName, true);
// Confirms that magnifier is keeping enabled.
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
user_manager::UserManager::Get()->SessionStarted();
// Confirms that magnifier keeps enabled.
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
EXPECT_TRUE(GetScreenMagnifierEnabledFromPref());
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, LoginAsNewUserUnset) {
// Confirms that magnifier is disabled on the login screen.
EXPECT_FALSE(IsMagnifierEnabled());
// Logs in (but the session is not started yet).
user_manager::UserManager::Get()->UserLoggedIn(
kTestUserName, kTestUserName, true);
// Confirms that magnifier is keeping disabled.
EXPECT_FALSE(IsMagnifierEnabled());
user_manager::UserManager::Get()->SessionStarted();
// Confirms that magnifier is keeping disabled.
EXPECT_FALSE(IsMagnifierEnabled());
EXPECT_FALSE(GetScreenMagnifierEnabledFromPref());
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, ChangeMagnifierType) {
// Enables/disables full screen magnifier.
SetMagnifierEnabled(false);
SetMagnifierType(ui::MAGNIFIER_FULL);
EXPECT_FALSE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
SetMagnifierEnabled(true);
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
SetMagnifierEnabled(false);
EXPECT_FALSE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
// Enables/disables partial screen magnifier.
SetMagnifierType(ui::MAGNIFIER_PARTIAL);
EXPECT_FALSE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
SetMagnifierEnabled(true);
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
SetMagnifierEnabled(false);
EXPECT_FALSE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
// Changes the magnifier type when the magnifier is enabled.
SetMagnifierType(ui::MAGNIFIER_FULL);
SetMagnifierEnabled(true);
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
SetMagnifierType(ui::MAGNIFIER_PARTIAL);
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
SetMagnifierType(ui::MAGNIFIER_FULL);
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
// Changes the magnifier type when the magnifier is disabled.
SetMagnifierEnabled(false);
SetMagnifierType(ui::MAGNIFIER_FULL);
EXPECT_FALSE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
SetMagnifierType(ui::MAGNIFIER_PARTIAL);
EXPECT_FALSE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
SetMagnifierType(ui::MAGNIFIER_FULL);
EXPECT_FALSE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, TypePref) {
// Logs in
user_manager::UserManager::Get()->UserLoggedIn(
kTestUserName, kTestUserName, true);
user_manager::UserManager::Get()->SessionStarted();
// Confirms that magnifier is disabled just after login.
EXPECT_FALSE(IsMagnifierEnabled());
// Sets the pref as true to enable magnifier.
SetScreenMagnifierTypePref(ui::MAGNIFIER_FULL);
SetScreenMagnifierEnabledPref(true);
// Confirms that magnifier is enabled.
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, ScalePref) {
SetMagnifierEnabled(false);
EXPECT_FALSE(IsMagnifierEnabled());
// Sets 2.5x to the pref.
SetSavedFullScreenMagnifierScale(2.5);
// Enables full screen magnifier.
SetMagnifierType(ui::MAGNIFIER_FULL);
SetMagnifierEnabled(true);
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
// Confirms that 2.5x is restored.
EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
// Sets the scale and confirms that the scale is saved to pref.
SetFullScreenMagnifierScale(3.0);
EXPECT_EQ(3.0, GetSavedFullScreenMagnifierScale());
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest, InvalidScalePref) {
// TEST 1: Sets too small scale
SetMagnifierEnabled(false);
EXPECT_FALSE(IsMagnifierEnabled());
// Sets too small value to the pref.
SetSavedFullScreenMagnifierScale(0.5);
// Enables full screen magnifier.
SetMagnifierType(ui::MAGNIFIER_FULL);
SetMagnifierEnabled(true);
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
// Confirms that the actual scale is set to the minimum scale.
EXPECT_EQ(1.0, GetFullScreenMagnifierScale());
// TEST 2: Sets too large scale
SetMagnifierEnabled(false);
EXPECT_FALSE(IsMagnifierEnabled());
// Sets too large value to the pref.
SetSavedFullScreenMagnifierScale(50.0);
// Enables full screen magnifier.
SetMagnifierEnabled(true);
EXPECT_TRUE(IsMagnifierEnabled());
EXPECT_EQ(ui::MAGNIFIER_FULL, GetMagnifierType());
// Confirms that the actual scale is set to the maximum scale.
EXPECT_EQ(4.0, GetFullScreenMagnifierScale());
}
IN_PROC_BROWSER_TEST_F(MagnificationManagerTest,
ChangingTypeInvokesNotification) {
MockMagnificationObserver observer;
EXPECT_FALSE(observer.observed());
// Set full screen magnifier, and confirm the observer is called.
SetMagnifierEnabled(true);
SetMagnifierType(ui::MAGNIFIER_FULL);
EXPECT_TRUE(observer.observed());
EXPECT_TRUE(observer.observed_enabled());
EXPECT_EQ(observer.magnifier_type(), ui::MAGNIFIER_FULL);
EXPECT_EQ(GetMagnifierType(), ui::MAGNIFIER_FULL);
observer.reset();
// Set full screen magnifier again, and confirm the observer is not called.
SetMagnifierType(ui::MAGNIFIER_FULL);
EXPECT_FALSE(observer.observed());
EXPECT_EQ(GetMagnifierType(), ui::MAGNIFIER_FULL);
observer.reset();
}
} // namespace chromeos
|