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/chromeos/login/login_utils.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/test/base/in_process_browser_test.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "chromeos/chromeos_switches.h"
24 #include "components/user_manager/user_manager.h"
25 #include "components/user_prefs/user_prefs.h"
26 #include "content/public/browser/notification_details.h"
27 #include "content/public/browser/notification_service.h"
28 #include "testing/gtest/include/gtest/gtest.h"
34 const char kTestUserName
[] = "owner@invalid.domain";
36 void SetMagnifierEnabled(bool enabled
) {
37 MagnificationManager::Get()->SetMagnifierEnabled(enabled
);
40 void SetMagnifierType(ash::MagnifierType type
) {
41 MagnificationManager::Get()->SetMagnifierType(type
);
44 void SetFullScreenMagnifierScale(double scale
) {
45 ash::Shell::GetInstance()->
46 magnification_controller()->SetScale(scale
, false);
49 double GetFullScreenMagnifierScale() {
50 return ash::Shell::GetInstance()->magnification_controller()->GetScale();
53 void SetSavedFullScreenMagnifierScale(double scale
) {
54 MagnificationManager::Get()->SaveScreenMagnifierScale(scale
);
57 double GetSavedFullScreenMagnifierScale() {
58 return MagnificationManager::Get()->GetSavedScreenMagnifierScale();
61 ash::MagnifierType
GetMagnifierType() {
62 return MagnificationManager::Get()->GetMagnifierType();
65 bool IsMagnifierEnabled() {
66 return MagnificationManager::Get()->IsMagnifierEnabled();
70 Profile
* profile
= ProfileManager::GetActiveUserProfile();
75 PrefService
* prefs() {
76 return user_prefs::UserPrefs::Get(profile());
79 void SetScreenMagnifierEnabledPref(bool enabled
) {
80 prefs()->SetBoolean(prefs::kAccessibilityScreenMagnifierEnabled
, enabled
);
83 void SetScreenMagnifierTypePref(ash::MagnifierType type
) {
84 prefs()->SetInteger(prefs::kAccessibilityScreenMagnifierType
, type
);
87 void SetFullScreenMagnifierScalePref(double scale
) {
88 prefs()->SetDouble(prefs::kAccessibilityScreenMagnifierScale
, scale
);
91 bool GetScreenMagnifierEnabledFromPref() {
92 return prefs()->GetBoolean(prefs::kAccessibilityScreenMagnifierEnabled
);
95 // Creates and logs into a profile with account |name|, and makes sure that
96 // the profile is regarded as "non new" in the next login. This is used in
97 // PRE_XXX cases so that in the main XXX case we can test non new profiles.
98 void PrepareNonNewProfile(const std::string
& name
) {
99 user_manager::UserManager::Get()->UserLoggedIn(name
, name
, true);
100 // To prepare a non-new profile for tests, we must ensure the profile
101 // directory and the preference files are created, because that's what
102 // Profile::IsNewProfile() checks. UserLoggedIn(), however, does not yet
103 // create the profile directory until GetActiveUserProfile() is called.
104 ProfileManager::GetActiveUserProfile();
109 class MockMagnificationObserver
{
111 MockMagnificationObserver() : observed_(false),
112 observed_enabled_(false),
115 AccessibilityManager
* accessibility_manager
= AccessibilityManager::Get();
116 CHECK(accessibility_manager
);
117 accessibility_subscription_
= accessibility_manager
->RegisterCallback(
118 base::Bind(&MockMagnificationObserver::OnAccessibilityStatusChanged
,
119 base::Unretained(this)));
122 virtual ~MockMagnificationObserver() {}
124 bool observed() const { return observed_
; }
125 bool observed_enabled() const { return observed_enabled_
; }
126 int magnifier_type() const { return magnifier_type_
; }
128 void reset() { observed_
= false; }
131 void OnAccessibilityStatusChanged(
132 const AccessibilityStatusEventDetails
& details
) {
133 if (details
.notification_type
== ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFIER
) {
134 magnifier_type_
= details
.magnifier_type
;
135 observed_enabled_
= details
.enabled
;
141 bool observed_enabled_
;
144 scoped_ptr
<AccessibilityStatusSubscription
> accessibility_subscription_
;
146 DISALLOW_COPY_AND_ASSIGN(MockMagnificationObserver
);
150 class MagnificationManagerTest
: public InProcessBrowserTest
{
152 MagnificationManagerTest() {}
153 virtual ~MagnificationManagerTest() {}
155 virtual void SetUpCommandLine(CommandLine
* command_line
) override
{
156 command_line
->AppendSwitch(switches::kLoginManager
);
157 command_line
->AppendSwitchASCII(switches::kLoginProfile
,
158 TestingProfile::kTestUserProfileDir
);
161 virtual void SetUpOnMainThread() override
{
162 // Set the login-screen profile.
163 MagnificationManager::Get()->SetProfileForTest(
164 ProfileManager::GetActiveUserProfile());
167 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerTest
);
170 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, PRE_LoginOffToOff
) {
171 // Create a new profile once, to run the test with non-new profile.
172 PrepareNonNewProfile(kTestUserName
);
174 // Sets pref to explicitly disable the magnifier.
175 SetScreenMagnifierEnabledPref(false);
178 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginOffToOff
) {
179 // Confirms that magnifier is disabled on the login screen.
180 EXPECT_FALSE(IsMagnifierEnabled());
182 // Disables magnifier on login screen.
183 SetMagnifierEnabled(false);
184 EXPECT_FALSE(IsMagnifierEnabled());
186 // Logs in with existing profile.
187 user_manager::UserManager::Get()->UserLoggedIn(
188 kTestUserName
, kTestUserName
, true);
190 // Confirms that magnifier is still disabled just after login.
191 EXPECT_FALSE(IsMagnifierEnabled());
193 user_manager::UserManager::Get()->SessionStarted();
195 // Confirms that magnifier is still disabled just after session starts.
196 EXPECT_FALSE(IsMagnifierEnabled());
198 // Enables magnifier.
199 SetMagnifierEnabled(true);
200 // Confirms that magnifier is enabled.
201 EXPECT_TRUE(IsMagnifierEnabled());
202 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
203 EXPECT_TRUE(GetScreenMagnifierEnabledFromPref());
206 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, PRE_LoginFullToOff
) {
207 // Create a new profile once, to run the test with non-new profile.
208 PrepareNonNewProfile(kTestUserName
);
210 // Sets pref to explicitly disable the magnifier.
211 SetScreenMagnifierEnabledPref(false);
214 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginFullToOff
) {
215 // Confirms that magnifier is disabled on the login screen.
216 EXPECT_FALSE(IsMagnifierEnabled());
218 // Enables magnifier on login screen.
219 SetMagnifierEnabled(true);
220 SetMagnifierType(ash::MAGNIFIER_FULL
);
221 SetFullScreenMagnifierScale(2.5);
222 EXPECT_TRUE(IsMagnifierEnabled());
223 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
224 EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
226 // Logs in (but the session is not started yet).
227 user_manager::UserManager::Get()->UserLoggedIn(
228 kTestUserName
, kTestUserName
, true);
230 // Confirms that magnifier is keeping enabled.
231 EXPECT_TRUE(IsMagnifierEnabled());
232 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
234 user_manager::UserManager::Get()->SessionStarted();
236 // Confirms that magnifier is disabled just after session start.
237 EXPECT_FALSE(IsMagnifierEnabled());
238 EXPECT_FALSE(GetScreenMagnifierEnabledFromPref());
241 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, PRE_LoginOffToFull
) {
242 // Create a new profile once, to run the test with non-new profile.
243 PrepareNonNewProfile(kTestUserName
);
245 // Sets prefs to explicitly enable the magnifier.
246 SetScreenMagnifierEnabledPref(true);
247 SetScreenMagnifierTypePref(ash::MAGNIFIER_FULL
);
248 SetFullScreenMagnifierScalePref(2.5);
251 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginOffToFull
) {
252 // Disables magnifier on login screen.
253 SetMagnifierEnabled(false);
254 EXPECT_FALSE(IsMagnifierEnabled());
256 // Logs in (but the session is not started yet).
257 user_manager::UserManager::Get()->UserLoggedIn(
258 kTestUserName
, kTestUserName
, true);
260 // Confirms that magnifier is keeping disabled.
261 EXPECT_FALSE(IsMagnifierEnabled());
263 user_manager::UserManager::Get()->SessionStarted();
265 // Confirms that the magnifier is enabled and configured according to the
266 // explicitly set prefs just after session start.
267 EXPECT_TRUE(IsMagnifierEnabled());
268 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
269 EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
270 EXPECT_TRUE(GetScreenMagnifierEnabledFromPref());
273 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, PRE_LoginFullToFull
) {
274 // Create a new profile once, to run the test with non-new profile.
275 PrepareNonNewProfile(kTestUserName
);
277 // Sets prefs to explicitly enable the magnifier.
278 SetScreenMagnifierEnabledPref(true);
279 SetScreenMagnifierTypePref(ash::MAGNIFIER_FULL
);
280 SetFullScreenMagnifierScalePref(2.5);
283 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginFullToFull
) {
284 // Enables magnifier on login screen.
285 SetMagnifierType(ash::MAGNIFIER_FULL
);
286 SetMagnifierEnabled(true);
287 SetFullScreenMagnifierScale(3.0);
288 EXPECT_TRUE(IsMagnifierEnabled());
289 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
290 EXPECT_EQ(3.0, GetFullScreenMagnifierScale());
292 // Logs in (but the session is not started yet).
293 user_manager::UserManager::Get()->UserLoggedIn(
294 kTestUserName
, kTestUserName
, true);
296 // Confirms that magnifier is keeping enabled.
297 EXPECT_TRUE(IsMagnifierEnabled());
298 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
300 user_manager::UserManager::Get()->SessionStarted();
302 // Confirms that the magnifier is enabled and configured according to the
303 // explicitly set prefs just after session start.
304 EXPECT_TRUE(IsMagnifierEnabled());
305 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
306 EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
307 EXPECT_TRUE(GetScreenMagnifierEnabledFromPref());
310 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, PRE_LoginFullToUnset
) {
311 // Creates a new profile once, to run the test with non-new profile.
312 PrepareNonNewProfile(kTestUserName
);
315 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginFullToUnset
) {
316 // Enables full screen magnifier.
317 SetMagnifierType(ash::MAGNIFIER_FULL
);
318 SetMagnifierEnabled(true);
319 EXPECT_TRUE(IsMagnifierEnabled());
320 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
322 // Logs in (but the session is not started yet).
323 user_manager::UserManager::Get()->UserLoggedIn(
324 kTestUserName
, kTestUserName
, true);
326 // Confirms that magnifier is keeping enabled.
327 EXPECT_TRUE(IsMagnifierEnabled());
328 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
330 user_manager::UserManager::Get()->SessionStarted();
332 // Confirms that magnifier is disabled.
333 EXPECT_FALSE(IsMagnifierEnabled());
334 EXPECT_FALSE(GetScreenMagnifierEnabledFromPref());
337 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginAsNewUserOff
) {
338 // Confirms that magnifier is disabled on the login screen.
339 EXPECT_FALSE(IsMagnifierEnabled());
341 // Disables magnifier on login screen explicitly.
342 SetMagnifierEnabled(false);
344 // Logs in (but the session is not started yet).
345 user_manager::UserManager::Get()->UserLoggedIn(
346 kTestUserName
, kTestUserName
, true);
348 // Confirms that magnifier is keeping disabled.
349 EXPECT_FALSE(IsMagnifierEnabled());
351 user_manager::UserManager::Get()->SessionStarted();
353 // Confirms that magnifier is keeping disabled.
354 EXPECT_FALSE(IsMagnifierEnabled());
355 EXPECT_FALSE(GetScreenMagnifierEnabledFromPref());
358 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginAsNewUserFull
) {
359 // Enables magnifier on login screen.
360 SetMagnifierType(ash::MAGNIFIER_FULL
);
361 SetMagnifierEnabled(true);
362 SetFullScreenMagnifierScale(2.5);
363 EXPECT_TRUE(IsMagnifierEnabled());
364 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
365 EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
367 // Logs in (but the session is not started yet).
368 user_manager::UserManager::Get()->UserLoggedIn(
369 kTestUserName
, kTestUserName
, true);
371 // Confirms that magnifier is keeping enabled.
372 EXPECT_TRUE(IsMagnifierEnabled());
373 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
375 user_manager::UserManager::Get()->SessionStarted();
377 // Confirms that magnifier keeps enabled.
378 EXPECT_TRUE(IsMagnifierEnabled());
379 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
380 EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
381 EXPECT_TRUE(GetScreenMagnifierEnabledFromPref());
384 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, LoginAsNewUserUnset
) {
385 // Confirms that magnifier is disabled on the login screen.
386 EXPECT_FALSE(IsMagnifierEnabled());
388 // Logs in (but the session is not started yet).
389 user_manager::UserManager::Get()->UserLoggedIn(
390 kTestUserName
, kTestUserName
, true);
392 // Confirms that magnifier is keeping disabled.
393 EXPECT_FALSE(IsMagnifierEnabled());
395 user_manager::UserManager::Get()->SessionStarted();
397 // Confirms that magnifier is keeping disabled.
398 EXPECT_FALSE(IsMagnifierEnabled());
399 EXPECT_FALSE(GetScreenMagnifierEnabledFromPref());
402 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, ChangeMagnifierType
) {
403 // Enables/disables full screen magnifier.
404 SetMagnifierEnabled(false);
405 SetMagnifierType(ash::MAGNIFIER_FULL
);
406 EXPECT_FALSE(IsMagnifierEnabled());
407 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
409 SetMagnifierEnabled(true);
410 EXPECT_TRUE(IsMagnifierEnabled());
411 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
413 SetMagnifierEnabled(false);
414 EXPECT_FALSE(IsMagnifierEnabled());
415 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
417 // Enables/disables partial screen magnifier.
418 SetMagnifierType(ash::MAGNIFIER_PARTIAL
);
419 EXPECT_FALSE(IsMagnifierEnabled());
420 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
422 SetMagnifierEnabled(true);
423 EXPECT_TRUE(IsMagnifierEnabled());
424 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
426 SetMagnifierEnabled(false);
427 EXPECT_FALSE(IsMagnifierEnabled());
428 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
430 // Changes the magnifier type when the magnifier is enabled.
431 SetMagnifierType(ash::MAGNIFIER_FULL
);
432 SetMagnifierEnabled(true);
433 EXPECT_TRUE(IsMagnifierEnabled());
434 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
436 SetMagnifierType(ash::MAGNIFIER_PARTIAL
);
437 EXPECT_TRUE(IsMagnifierEnabled());
438 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
440 SetMagnifierType(ash::MAGNIFIER_FULL
);
441 EXPECT_TRUE(IsMagnifierEnabled());
442 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
444 // Changes the magnifier type when the magnifier is disabled.
445 SetMagnifierEnabled(false);
446 SetMagnifierType(ash::MAGNIFIER_FULL
);
447 EXPECT_FALSE(IsMagnifierEnabled());
448 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
450 SetMagnifierType(ash::MAGNIFIER_PARTIAL
);
451 EXPECT_FALSE(IsMagnifierEnabled());
452 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
454 SetMagnifierType(ash::MAGNIFIER_FULL
);
455 EXPECT_FALSE(IsMagnifierEnabled());
456 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
459 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, TypePref
) {
461 user_manager::UserManager::Get()->UserLoggedIn(
462 kTestUserName
, kTestUserName
, true);
463 user_manager::UserManager::Get()->SessionStarted();
465 // Confirms that magnifier is disabled just after login.
466 EXPECT_FALSE(IsMagnifierEnabled());
468 // Sets the pref as true to enable magnifier.
469 SetScreenMagnifierTypePref(ash::MAGNIFIER_FULL
);
470 SetScreenMagnifierEnabledPref(true);
471 // Confirms that magnifier is enabled.
472 EXPECT_TRUE(IsMagnifierEnabled());
473 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
476 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, ScalePref
) {
477 SetMagnifierEnabled(false);
478 EXPECT_FALSE(IsMagnifierEnabled());
480 // Sets 2.5x to the pref.
481 SetSavedFullScreenMagnifierScale(2.5);
483 // Enables full screen magnifier.
484 SetMagnifierType(ash::MAGNIFIER_FULL
);
485 SetMagnifierEnabled(true);
486 EXPECT_TRUE(IsMagnifierEnabled());
487 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
489 // Confirms that 2.5x is restored.
490 EXPECT_EQ(2.5, GetFullScreenMagnifierScale());
492 // Sets the scale and confirms that the scale is saved to pref.
493 SetFullScreenMagnifierScale(3.0);
494 EXPECT_EQ(3.0, GetSavedFullScreenMagnifierScale());
497 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
, InvalidScalePref
) {
498 // TEST 1: Sets too small scale
499 SetMagnifierEnabled(false);
500 EXPECT_FALSE(IsMagnifierEnabled());
502 // Sets too small value to the pref.
503 SetSavedFullScreenMagnifierScale(0.5);
505 // Enables full screen magnifier.
506 SetMagnifierType(ash::MAGNIFIER_FULL
);
507 SetMagnifierEnabled(true);
508 EXPECT_TRUE(IsMagnifierEnabled());
509 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
511 // Confirms that the actual scale is set to the minimum scale.
512 EXPECT_EQ(1.0, GetFullScreenMagnifierScale());
514 // TEST 2: Sets too large scale
515 SetMagnifierEnabled(false);
516 EXPECT_FALSE(IsMagnifierEnabled());
518 // Sets too large value to the pref.
519 SetSavedFullScreenMagnifierScale(50.0);
521 // Enables full screen magnifier.
522 SetMagnifierEnabled(true);
523 EXPECT_TRUE(IsMagnifierEnabled());
524 EXPECT_EQ(ash::MAGNIFIER_FULL
, GetMagnifierType());
526 // Confirms that the actual scale is set to the maximum scale.
527 EXPECT_EQ(4.0, GetFullScreenMagnifierScale());
530 IN_PROC_BROWSER_TEST_F(MagnificationManagerTest
,
531 ChangingTypeInvokesNotification
) {
532 MockMagnificationObserver observer
;
534 EXPECT_FALSE(observer
.observed());
536 // Set full screen magnifier, and confirm the observer is called.
537 SetMagnifierEnabled(true);
538 SetMagnifierType(ash::MAGNIFIER_FULL
);
539 EXPECT_TRUE(observer
.observed());
540 EXPECT_TRUE(observer
.observed_enabled());
541 EXPECT_EQ(observer
.magnifier_type(), ash::MAGNIFIER_FULL
);
542 EXPECT_EQ(GetMagnifierType(), ash::MAGNIFIER_FULL
);
545 // Set full screen magnifier again, and confirm the observer is not called.
546 SetMagnifierType(ash::MAGNIFIER_FULL
);
547 EXPECT_FALSE(observer
.observed());
548 EXPECT_EQ(GetMagnifierType(), ash::MAGNIFIER_FULL
);
552 } // namespace chromeos