1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "ash/magnifier/magnification_controller.h"
9 #include "base/command_line.h"
10 #include "base/prefs/pref_service.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
14 #include "chrome/browser/chromeos/accessibility/magnification_manager.h"
15 #include "chrome/browser/chromeos/login/helper.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "chromeos/chromeos_switches.h"
23 #include "components/user_manager/user_manager.h"
24 #include "components/user_prefs/user_prefs.h"
25 #include "content/public/browser/notification_details.h"
26 #include "content/public/browser/notification_service.h"
27 #include "testing/gtest/include/gtest/gtest.h"
33 const char kTestUserName
[] = "owner@invalid.domain";
35 void SetMagnifierEnabled(bool enabled
) {
36 MagnificationManager::Get()->SetMagnifierEnabled(enabled
);
39 void SetMagnifierType(ui::MagnifierType type
) {
40 MagnificationManager::Get()->SetMagnifierType(type
);
43 void SetFullScreenMagnifierScale(double scale
) {
44 ash::Shell::GetInstance()->
45 magnification_controller()->SetScale(scale
, false);
48 double GetFullScreenMagnifierScale() {
49 return ash::Shell::GetInstance()->magnification_controller()->GetScale();
52 void SetSavedFullScreenMagnifierScale(double scale
) {
53 MagnificationManager::Get()->SaveScreenMagnifierScale(scale
);
56 double GetSavedFullScreenMagnifierScale() {
57 return MagnificationManager::Get()->GetSavedScreenMagnifierScale();
60 ui::MagnifierType
GetMagnifierType() {
61 return MagnificationManager::Get()->GetMagnifierType();
64 bool IsMagnifierEnabled() {
65 return MagnificationManager::Get()->IsMagnifierEnabled();
69 Profile
* profile
= ProfileManager::GetActiveUserProfile();
74 PrefService
* prefs() {
75 return user_prefs::UserPrefs::Get(profile());
78 void SetScreenMagnifierEnabledPref(bool enabled
) {
79 prefs()->SetBoolean(prefs::kAccessibilityScreenMagnifierEnabled
, enabled
);
82 void SetScreenMagnifierTypePref(ui::MagnifierType type
) {
83 prefs()->SetInteger(prefs::kAccessibilityScreenMagnifierType
, type
);
86 void SetFullScreenMagnifierScalePref(double scale
) {
87 prefs()->SetDouble(prefs::kAccessibilityScreenMagnifierScale
, scale
);
90 bool GetScreenMagnifierEnabledFromPref() {
91 return prefs()->GetBoolean(prefs::kAccessibilityScreenMagnifierEnabled
);
94 // Creates and logs into a profile with account |name|, and makes sure that
95 // the profile is regarded as "non new" in the next login. This is used in
96 // PRE_XXX cases so that in the main XXX case we can test non new profiles.
97 void PrepareNonNewProfile(const std::string
& name
) {
98 user_manager::UserManager::Get()->UserLoggedIn(name
, name
, true);
99 // To prepare a non-new profile for tests, we must ensure the profile
100 // directory and the preference files are created, because that's what
101 // Profile::IsNewProfile() checks. UserLoggedIn(), however, does not yet
102 // create the profile directory until GetActiveUserProfile() is called.
103 ProfileManager::GetActiveUserProfile();
108 class MockMagnificationObserver
{
110 MockMagnificationObserver() : observed_(false),
111 observed_enabled_(false),
114 AccessibilityManager
* accessibility_manager
= AccessibilityManager::Get();
115 CHECK(accessibility_manager
);
116 accessibility_subscription_
= accessibility_manager
->RegisterCallback(
117 base::Bind(&MockMagnificationObserver::OnAccessibilityStatusChanged
,
118 base::Unretained(this)));
121 virtual ~MockMagnificationObserver() {}
123 bool observed() const { return observed_
; }
124 bool observed_enabled() const { return observed_enabled_
; }
125 int magnifier_type() const { return magnifier_type_
; }
127 void reset() { observed_
= false; }
130 void OnAccessibilityStatusChanged(
131 const AccessibilityStatusEventDetails
& details
) {
132 if (details
.notification_type
== ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFIER
) {
133 magnifier_type_
= details
.magnifier_type
;
134 observed_enabled_
= details
.enabled
;
140 bool observed_enabled_
;
143 scoped_ptr
<AccessibilityStatusSubscription
> accessibility_subscription_
;
145 DISALLOW_COPY_AND_ASSIGN(MockMagnificationObserver
);
149 class MagnificationManagerTest
: public InProcessBrowserTest
{
151 MagnificationManagerTest() {}
152 ~MagnificationManagerTest() override
{}
154 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
155 command_line
->AppendSwitch(switches::kLoginManager
);
156 command_line
->AppendSwitchASCII(switches::kLoginProfile
,
157 TestingProfile::kTestUserProfileDir
);
160 void SetUpOnMainThread() override
{
161 // Set the login-screen profile.
162 MagnificationManager::Get()->SetProfileForTest(
163 ProfileManager::GetActiveUserProfile());
166 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerTest
);
169 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, PRE_LoginOffToOff
) {
170 // Create a new profile once, to run the test with non-new profile.
171 PrepareNonNewProfile(kTestUserName
);
173 // Sets pref to explicitly disable the magnifier.
174 SetScreenMagnifierEnabledPref(false);
177 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginOffToOff
) {
178 // Confirms that magnifier is disabled on the login screen.
179 EXPECT_FALSE(IsMagnifierEnabled());
181 // Disables magnifier on login screen.
182 SetMagnifierEnabled(false);
183 EXPECT_FALSE(IsMagnifierEnabled());
185 // Logs in with existing profile.
186 user_manager::UserManager::Get()->UserLoggedIn(
187 kTestUserName
, kTestUserName
, true);
189 // Confirms that magnifier is still disabled just after login.
190 EXPECT_FALSE(IsMagnifierEnabled());
192 user_manager::UserManager::Get()->SessionStarted();
194 // Confirms that magnifier is still disabled just after session starts.
195 EXPECT_FALSE(IsMagnifierEnabled());
197 // Enables magnifier.
198 SetMagnifierEnabled(true);
199 // Confirms that magnifier is enabled.
200 EXPECT_TRUE(IsMagnifierEnabled());
201 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
202 EXPECT_TRUE(GetScreenMagnifierEnabledFromPref());
205 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, PRE_LoginFullToOff
) {
206 // Create a new profile once, to run the test with non-new profile.
207 PrepareNonNewProfile(kTestUserName
);
209 // Sets pref to explicitly disable the magnifier.
210 SetScreenMagnifierEnabledPref(false);
213 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginFullToOff
) {
214 // Confirms that magnifier is disabled on the login screen.
215 EXPECT_FALSE(IsMagnifierEnabled());
217 // Enables magnifier on login screen.
218 SetMagnifierEnabled(true);
219 SetMagnifierType(ui::MAGNIFIER_FULL
);
220 SetFullScreenMagnifierScale(2.5);
221 EXPECT_TRUE(IsMagnifierEnabled());
222 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
223 EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
225 // Logs in (but the session is not started yet).
226 user_manager::UserManager::Get()->UserLoggedIn(
227 kTestUserName
, kTestUserName
, true);
229 // Confirms that magnifier is keeping enabled.
230 EXPECT_TRUE(IsMagnifierEnabled());
231 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
233 user_manager::UserManager::Get()->SessionStarted();
235 // Confirms that magnifier is disabled just after session start.
236 EXPECT_FALSE(IsMagnifierEnabled());
237 EXPECT_FALSE(GetScreenMagnifierEnabledFromPref());
240 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, PRE_LoginOffToFull
) {
241 // Create a new profile once, to run the test with non-new profile.
242 PrepareNonNewProfile(kTestUserName
);
244 // Sets prefs to explicitly enable the magnifier.
245 SetScreenMagnifierEnabledPref(true);
246 SetScreenMagnifierTypePref(ui::MAGNIFIER_FULL
);
247 SetFullScreenMagnifierScalePref(2.5);
250 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginOffToFull
) {
251 // Disables magnifier on login screen.
252 SetMagnifierEnabled(false);
253 EXPECT_FALSE(IsMagnifierEnabled());
255 // Logs in (but the session is not started yet).
256 user_manager::UserManager::Get()->UserLoggedIn(
257 kTestUserName
, kTestUserName
, true);
259 // Confirms that magnifier is keeping disabled.
260 EXPECT_FALSE(IsMagnifierEnabled());
262 user_manager::UserManager::Get()->SessionStarted();
264 // Confirms that the magnifier is enabled and configured according to the
265 // explicitly set prefs just after session start.
266 EXPECT_TRUE(IsMagnifierEnabled());
267 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
268 EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
269 EXPECT_TRUE(GetScreenMagnifierEnabledFromPref());
272 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, PRE_LoginFullToFull
) {
273 // Create a new profile once, to run the test with non-new profile.
274 PrepareNonNewProfile(kTestUserName
);
276 // Sets prefs to explicitly enable the magnifier.
277 SetScreenMagnifierEnabledPref(true);
278 SetScreenMagnifierTypePref(ui::MAGNIFIER_FULL
);
279 SetFullScreenMagnifierScalePref(2.5);
282 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginFullToFull
) {
283 // Enables magnifier on login screen.
284 SetMagnifierType(ui::MAGNIFIER_FULL
);
285 SetMagnifierEnabled(true);
286 SetFullScreenMagnifierScale(3.0);
287 EXPECT_TRUE(IsMagnifierEnabled());
288 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
289 EXPECT_EQ(3.0, GetFullScreenMagnifierScale());
291 // Logs in (but the session is not started yet).
292 user_manager::UserManager::Get()->UserLoggedIn(
293 kTestUserName
, kTestUserName
, true);
295 // Confirms that magnifier is keeping enabled.
296 EXPECT_TRUE(IsMagnifierEnabled());
297 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
299 user_manager::UserManager::Get()->SessionStarted();
301 // Confirms that the magnifier is enabled and configured according to the
302 // explicitly set prefs just after session start.
303 EXPECT_TRUE(IsMagnifierEnabled());
304 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
305 EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
306 EXPECT_TRUE(GetScreenMagnifierEnabledFromPref());
309 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, PRE_LoginFullToUnset
) {
310 // Creates a new profile once, to run the test with non-new profile.
311 PrepareNonNewProfile(kTestUserName
);
314 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginFullToUnset
) {
315 // Enables full screen magnifier.
316 SetMagnifierType(ui::MAGNIFIER_FULL
);
317 SetMagnifierEnabled(true);
318 EXPECT_TRUE(IsMagnifierEnabled());
319 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
321 // Logs in (but the session is not started yet).
322 user_manager::UserManager::Get()->UserLoggedIn(
323 kTestUserName
, kTestUserName
, true);
325 // Confirms that magnifier is keeping enabled.
326 EXPECT_TRUE(IsMagnifierEnabled());
327 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
329 user_manager::UserManager::Get()->SessionStarted();
331 // Confirms that magnifier is disabled.
332 EXPECT_FALSE(IsMagnifierEnabled());
333 EXPECT_FALSE(GetScreenMagnifierEnabledFromPref());
336 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginAsNewUserOff
) {
337 // Confirms that magnifier is disabled on the login screen.
338 EXPECT_FALSE(IsMagnifierEnabled());
340 // Disables magnifier on login screen explicitly.
341 SetMagnifierEnabled(false);
343 // Logs in (but the session is not started yet).
344 user_manager::UserManager::Get()->UserLoggedIn(
345 kTestUserName
, kTestUserName
, true);
347 // Confirms that magnifier is keeping disabled.
348 EXPECT_FALSE(IsMagnifierEnabled());
350 user_manager::UserManager::Get()->SessionStarted();
352 // Confirms that magnifier is keeping disabled.
353 EXPECT_FALSE(IsMagnifierEnabled());
354 EXPECT_FALSE(GetScreenMagnifierEnabledFromPref());
357 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginAsNewUserFull
) {
358 // Enables magnifier on login screen.
359 SetMagnifierType(ui::MAGNIFIER_FULL
);
360 SetMagnifierEnabled(true);
361 SetFullScreenMagnifierScale(2.5);
362 EXPECT_TRUE(IsMagnifierEnabled());
363 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
364 EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
366 // Logs in (but the session is not started yet).
367 user_manager::UserManager::Get()->UserLoggedIn(
368 kTestUserName
, kTestUserName
, true);
370 // Confirms that magnifier is keeping enabled.
371 EXPECT_TRUE(IsMagnifierEnabled());
372 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
374 user_manager::UserManager::Get()->SessionStarted();
376 // Confirms that magnifier keeps enabled.
377 EXPECT_TRUE(IsMagnifierEnabled());
378 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
379 EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
380 EXPECT_TRUE(GetScreenMagnifierEnabledFromPref());
383 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginAsNewUserUnset
) {
384 // Confirms that magnifier is disabled on the login screen.
385 EXPECT_FALSE(IsMagnifierEnabled());
387 // Logs in (but the session is not started yet).
388 user_manager::UserManager::Get()->UserLoggedIn(
389 kTestUserName
, kTestUserName
, true);
391 // Confirms that magnifier is keeping disabled.
392 EXPECT_FALSE(IsMagnifierEnabled());
394 user_manager::UserManager::Get()->SessionStarted();
396 // Confirms that magnifier is keeping disabled.
397 EXPECT_FALSE(IsMagnifierEnabled());
398 EXPECT_FALSE(GetScreenMagnifierEnabledFromPref());
401 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, ChangeMagnifierType
) {
402 // Enables/disables full screen magnifier.
403 SetMagnifierEnabled(false);
404 SetMagnifierType(ui::MAGNIFIER_FULL
);
405 EXPECT_FALSE(IsMagnifierEnabled());
406 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
408 SetMagnifierEnabled(true);
409 EXPECT_TRUE(IsMagnifierEnabled());
410 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
412 SetMagnifierEnabled(false);
413 EXPECT_FALSE(IsMagnifierEnabled());
414 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
416 // Enables/disables partial screen magnifier.
417 SetMagnifierType(ui::MAGNIFIER_PARTIAL
);
418 EXPECT_FALSE(IsMagnifierEnabled());
419 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
421 SetMagnifierEnabled(true);
422 EXPECT_TRUE(IsMagnifierEnabled());
423 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
425 SetMagnifierEnabled(false);
426 EXPECT_FALSE(IsMagnifierEnabled());
427 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
429 // Changes the magnifier type when the magnifier is enabled.
430 SetMagnifierType(ui::MAGNIFIER_FULL
);
431 SetMagnifierEnabled(true);
432 EXPECT_TRUE(IsMagnifierEnabled());
433 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
435 SetMagnifierType(ui::MAGNIFIER_PARTIAL
);
436 EXPECT_TRUE(IsMagnifierEnabled());
437 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
439 SetMagnifierType(ui::MAGNIFIER_FULL
);
440 EXPECT_TRUE(IsMagnifierEnabled());
441 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
443 // Changes the magnifier type when the magnifier is disabled.
444 SetMagnifierEnabled(false);
445 SetMagnifierType(ui::MAGNIFIER_FULL
);
446 EXPECT_FALSE(IsMagnifierEnabled());
447 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
449 SetMagnifierType(ui::MAGNIFIER_PARTIAL
);
450 EXPECT_FALSE(IsMagnifierEnabled());
451 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
453 SetMagnifierType(ui::MAGNIFIER_FULL
);
454 EXPECT_FALSE(IsMagnifierEnabled());
455 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
458 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, TypePref
) {
460 user_manager::UserManager::Get()->UserLoggedIn(
461 kTestUserName
, kTestUserName
, true);
462 user_manager::UserManager::Get()->SessionStarted();
464 // Confirms that magnifier is disabled just after login.
465 EXPECT_FALSE(IsMagnifierEnabled());
467 // Sets the pref as true to enable magnifier.
468 SetScreenMagnifierTypePref(ui::MAGNIFIER_FULL
);
469 SetScreenMagnifierEnabledPref(true);
470 // Confirms that magnifier is enabled.
471 EXPECT_TRUE(IsMagnifierEnabled());
472 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
475 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, ScalePref
) {
476 SetMagnifierEnabled(false);
477 EXPECT_FALSE(IsMagnifierEnabled());
479 // Sets 2.5x to the pref.
480 SetSavedFullScreenMagnifierScale(2.5);
482 // Enables full screen magnifier.
483 SetMagnifierType(ui::MAGNIFIER_FULL
);
484 SetMagnifierEnabled(true);
485 EXPECT_TRUE(IsMagnifierEnabled());
486 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
488 // Confirms that 2.5x is restored.
489 EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
491 // Sets the scale and confirms that the scale is saved to pref.
492 SetFullScreenMagnifierScale(3.0);
493 EXPECT_EQ(3.0, GetSavedFullScreenMagnifierScale());
496 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, InvalidScalePref
) {
497 // TEST 1: Sets too small scale
498 SetMagnifierEnabled(false);
499 EXPECT_FALSE(IsMagnifierEnabled());
501 // Sets too small value to the pref.
502 SetSavedFullScreenMagnifierScale(0.5);
504 // Enables full screen magnifier.
505 SetMagnifierType(ui::MAGNIFIER_FULL
);
506 SetMagnifierEnabled(true);
507 EXPECT_TRUE(IsMagnifierEnabled());
508 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
510 // Confirms that the actual scale is set to the minimum scale.
511 EXPECT_EQ(1.0, GetFullScreenMagnifierScale());
513 // TEST 2: Sets too large scale
514 SetMagnifierEnabled(false);
515 EXPECT_FALSE(IsMagnifierEnabled());
517 // Sets too large value to the pref.
518 SetSavedFullScreenMagnifierScale(50.0);
520 // Enables full screen magnifier.
521 SetMagnifierEnabled(true);
522 EXPECT_TRUE(IsMagnifierEnabled());
523 EXPECT_EQ(ui::MAGNIFIER_FULL
, GetMagnifierType());
525 // Confirms that the actual scale is set to the maximum scale.
526 EXPECT_EQ(4.0, GetFullScreenMagnifierScale());
529 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
,
530 ChangingTypeInvokesNotification
) {
531 MockMagnificationObserver observer
;
533 EXPECT_FALSE(observer
.observed());
535 // Set full screen magnifier, and confirm the observer is called.
536 SetMagnifierEnabled(true);
537 SetMagnifierType(ui::MAGNIFIER_FULL
);
538 EXPECT_TRUE(observer
.observed());
539 EXPECT_TRUE(observer
.observed_enabled());
540 EXPECT_EQ(observer
.magnifier_type(), ui::MAGNIFIER_FULL
);
541 EXPECT_EQ(GetMagnifierType(), ui::MAGNIFIER_FULL
);
544 // Set full screen magnifier again, and confirm the observer is not called.
545 SetMagnifierType(ui::MAGNIFIER_FULL
);
546 EXPECT_FALSE(observer
.observed());
547 EXPECT_EQ(GetMagnifierType(), ui::MAGNIFIER_FULL
);
551 } // namespace chromeos