accessibility_manager_browsertest.cc revision 868fa2fe829687343ffae624259930155e16dbd8
1// Copyright (c) 2013 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.
4
5#include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
6
7#include "ash/magnifier/magnification_controller.h"
8#include "ash/shell.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/chromeos/accessibility/magnification_manager.h"
13#include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h"
14#include "chrome/browser/chromeos/login/helper.h"
15#include "chrome/browser/chromeos/login/login_utils.h"
16#include "chrome/browser/chromeos/login/user_manager.h"
17#include "chrome/browser/chromeos/login/user_manager_impl.h"
18#include "chrome/browser/profiles/profile.h"
19#include "chrome/browser/profiles/profile_manager.h"
20#include "chrome/common/chrome_notification_types.h"
21#include "chrome/common/pref_names.h"
22#include "chrome/test/base/testing_profile.h"
23#include "chromeos/chromeos_switches.h"
24#include "content/public/browser/notification_service.h"
25#include "testing/gtest/include/gtest/gtest.h"
26
27namespace chromeos {
28
29namespace {
30
31const char kTestUserName[] = "owner@invalid.domain";
32
33class MockAccessibilityObserver : public content::NotificationObserver {
34 public:
35  MockAccessibilityObserver() : observed_(false),
36                                observed_enabled_(false),
37                                observed_type_(-1) {
38    registrar_.Add(
39        this,
40        chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK,
41        content::NotificationService::AllSources());
42    registrar_.Add(
43        this,
44        chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_HIGH_CONTRAST_MODE,
45        content::NotificationService::AllSources());
46  }
47  virtual ~MockAccessibilityObserver() {}
48
49  bool observed() const { return observed_; }
50  bool observed_enabled() const { return observed_enabled_; }
51  int observed_type() const { return observed_type_; }
52
53  void reset() { observed_ = false; }
54
55 private:
56  // content::NotificationObserver implimentation:
57  virtual void Observe(int type,
58                       const content::NotificationSource& source,
59                       const content::NotificationDetails& details) OVERRIDE {
60    AccessibilityStatusEventDetails* accessibility_status =
61        content::Details<AccessibilityStatusEventDetails>(
62            details).ptr();
63    ASSERT_FALSE(observed_);
64
65    switch (type) {
66      case chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK:
67        observed_ = true;
68        observed_enabled_ = accessibility_status->enabled;
69        observed_type_ = type;
70        break;
71      case chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_HIGH_CONTRAST_MODE:
72        observed_ = true;
73        observed_enabled_ = accessibility_status->enabled;
74        observed_type_ = type;
75        break;
76    }
77  }
78
79  bool observed_;
80  bool observed_enabled_;
81  int observed_type_;
82
83  content::NotificationRegistrar registrar_;
84
85  DISALLOW_COPY_AND_ASSIGN(MockAccessibilityObserver);
86};
87
88void SetLargeCursorEnabled(bool enabled) {
89  return AccessibilityManager::Get()->EnableLargeCursor(enabled);
90}
91
92bool IsLargeCursorEnabled() {
93  return AccessibilityManager::Get()->IsLargeCursorEnabled();
94}
95
96void SetHighContrastEnabled(bool enabled) {
97  return AccessibilityManager::Get()->EnableHighContrast(enabled);
98}
99
100bool IsHighContrastEnabled() {
101  return AccessibilityManager::Get()->IsHighContrastEnabled();
102}
103
104void SetSpokenFeedbackEnabled(bool enabled) {
105  return AccessibilityManager::Get()->EnableSpokenFeedback(
106      enabled, NULL, ash::A11Y_NOTIFICATION_NONE);
107}
108
109bool IsSpokenFeedbackEnabled() {
110  return AccessibilityManager::Get()->IsSpokenFeedbackEnabled();
111}
112
113Profile* GetProfile() {
114  Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
115  DCHECK(profile);
116  return profile;
117}
118
119PrefService* GetPrefs() {
120  return GetProfile()->GetPrefs();
121}
122
123void SetLargeCursorEnabledToPref(bool enabled) {
124  GetPrefs()->SetBoolean(prefs::kLargeCursorEnabled, enabled);
125}
126
127void SetHighContrastEnabledToPref(bool enabled) {
128  GetPrefs()->SetBoolean(prefs::kHighContrastEnabled, enabled);
129}
130
131void SetSpokenFeedbackEnabledToPref(bool enabled) {
132  GetPrefs()->SetBoolean(prefs::kSpokenFeedbackEnabled, enabled);
133}
134
135}  // anonymouse namespace
136
137class AccessibilityManagerTest : public CrosInProcessBrowserTest {
138 protected:
139  AccessibilityManagerTest() {}
140  virtual ~AccessibilityManagerTest() {}
141
142  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
143    command_line->AppendSwitch(chromeos::switches::kLoginManager);
144    command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile,
145                                    TestingProfile::kTestUserProfileDir);
146  }
147
148  virtual void SetUpOnMainThread() OVERRIDE {
149  }
150
151  content::NotificationRegistrar registrar_;
152  DISALLOW_COPY_AND_ASSIGN(AccessibilityManagerTest);
153};
154
155IN_PROC_BROWSER_TEST_F(AccessibilityManagerTest, Login) {
156  // Confirms that a11y features are disabled on the login screen.
157  EXPECT_FALSE(IsLargeCursorEnabled());
158  EXPECT_FALSE(IsSpokenFeedbackEnabled());
159  EXPECT_FALSE(IsHighContrastEnabled());
160
161  // Logs in.
162  UserManager::Get()->UserLoggedIn(kTestUserName, kTestUserName, true);
163
164  // Confirms that the features still disabled just after login.
165  EXPECT_FALSE(IsLargeCursorEnabled());
166  EXPECT_FALSE(IsSpokenFeedbackEnabled());
167  EXPECT_FALSE(IsHighContrastEnabled());
168
169  UserManager::Get()->SessionStarted();
170
171  // Confirms that the features are still disabled just after login.
172  EXPECT_FALSE(IsLargeCursorEnabled());
173  EXPECT_FALSE(IsSpokenFeedbackEnabled());
174  EXPECT_FALSE(IsHighContrastEnabled());
175
176  // Enables large cursor.
177  SetLargeCursorEnabled(true);
178  // Confirms that large cursor is enabled.
179  EXPECT_TRUE(IsLargeCursorEnabled());
180
181  // Enables spoken feedback.
182  SetSpokenFeedbackEnabled(true);
183  // Confirms that the spoken feedback is enabled.
184  EXPECT_TRUE(IsSpokenFeedbackEnabled());
185
186  // Enables high contrast.
187  SetHighContrastEnabled(true);
188  // Confirms that high cotrast is enabled.
189  EXPECT_TRUE(IsHighContrastEnabled());
190}
191
192IN_PROC_BROWSER_TEST_F(AccessibilityManagerTest, TypePref) {
193  // Logs in.
194  UserManager::Get()->UserLoggedIn(kTestUserName, kTestUserName, true);
195  UserManager::Get()->SessionStarted();
196
197  // Confirms that the features are disabled just after login.
198  EXPECT_FALSE(IsLargeCursorEnabled());
199  EXPECT_FALSE(IsSpokenFeedbackEnabled());
200  EXPECT_FALSE(IsHighContrastEnabled());
201
202  // Sets the pref as true to enable the large cursor.
203  SetLargeCursorEnabledToPref(true);
204  // Confirms that the large cursor is enabled.
205  EXPECT_TRUE(IsLargeCursorEnabled());
206
207  // Sets the pref as true to enable the spoken feedback.
208  SetSpokenFeedbackEnabledToPref(true);
209  // Confirms that the spoken feedback is enabled.
210  EXPECT_TRUE(IsSpokenFeedbackEnabled());
211
212  // Enables the high contrast mode.
213  SetHighContrastEnabled(true);
214  // Confirms that the high contrast mode is enabled.
215  EXPECT_TRUE(IsHighContrastEnabled());
216
217  SetLargeCursorEnabledToPref(false);
218  EXPECT_FALSE(IsLargeCursorEnabled());
219
220  SetSpokenFeedbackEnabledToPref(false);
221  EXPECT_FALSE(IsSpokenFeedbackEnabled());
222
223  SetHighContrastEnabledToPref(false);
224  EXPECT_FALSE(IsHighContrastEnabled());
225}
226
227IN_PROC_BROWSER_TEST_F(AccessibilityManagerTest, ResumeSavedPref) {
228  // Loads the profile of the user.
229  UserManager::Get()->UserLoggedIn(kTestUserName, kTestUserName, true);
230
231  // Sets the pref to enable large cursor before login.
232  SetLargeCursorEnabledToPref(true);
233  EXPECT_FALSE(IsLargeCursorEnabled());
234
235  // Sets the pref to enable spoken feedback before login.
236  SetSpokenFeedbackEnabledToPref(true);
237  EXPECT_FALSE(IsSpokenFeedbackEnabled());
238
239  // Sets the pref to enable high contrast before login.
240  SetHighContrastEnabledToPref(true);
241  EXPECT_FALSE(IsHighContrastEnabled());
242
243  // Logs in.
244  UserManager::Get()->SessionStarted();
245
246  // Confirms that features are enabled by restring from pref just after login.
247  EXPECT_TRUE(IsLargeCursorEnabled());
248  EXPECT_TRUE(IsSpokenFeedbackEnabled());
249  EXPECT_TRUE(IsHighContrastEnabled());
250}
251
252IN_PROC_BROWSER_TEST_F(AccessibilityManagerTest,
253                       ChangingTypeInvokesNotification) {
254  MockAccessibilityObserver observer;
255
256  // Logs in.
257  UserManager::Get()->UserLoggedIn(kTestUserName, kTestUserName, true);
258  UserManager::Get()->SessionStarted();
259
260  EXPECT_FALSE(observer.observed());
261  observer.reset();
262
263  SetSpokenFeedbackEnabled(true);
264  EXPECT_TRUE(observer.observed());
265  EXPECT_TRUE(observer.observed_enabled());
266  EXPECT_EQ(observer.observed_type(),
267            chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK);
268  EXPECT_TRUE(IsSpokenFeedbackEnabled());
269
270  observer.reset();
271  SetSpokenFeedbackEnabled(false);
272  EXPECT_TRUE(observer.observed());
273  EXPECT_FALSE(observer.observed_enabled());
274  EXPECT_EQ(observer.observed_type(),
275            chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK);
276  EXPECT_FALSE(IsSpokenFeedbackEnabled());
277
278  observer.reset();
279  SetHighContrastEnabled(true);
280  EXPECT_TRUE(observer.observed());
281  EXPECT_TRUE(observer.observed_enabled());
282  EXPECT_EQ(observer.observed_type(),
283            chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_HIGH_CONTRAST_MODE);
284  EXPECT_TRUE(IsHighContrastEnabled());
285
286  observer.reset();
287  SetHighContrastEnabled(false);
288  EXPECT_TRUE(observer.observed());
289  EXPECT_FALSE(observer.observed_enabled());
290  EXPECT_EQ(observer.observed_type(),
291            chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_HIGH_CONTRAST_MODE);
292  EXPECT_FALSE(IsHighContrastEnabled());
293}
294
295IN_PROC_BROWSER_TEST_F(AccessibilityManagerTest,
296                       ChangingTypePrefInvokesNotification) {
297  MockAccessibilityObserver observer;
298
299  // Logs in.
300  UserManager::Get()->UserLoggedIn(kTestUserName, kTestUserName, true);
301  UserManager::Get()->SessionStarted();
302
303  EXPECT_FALSE(observer.observed());
304  observer.reset();
305
306  SetSpokenFeedbackEnabledToPref(true);
307  EXPECT_TRUE(observer.observed());
308  EXPECT_TRUE(observer.observed_enabled());
309  EXPECT_EQ(observer.observed_type(),
310            chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK);
311  EXPECT_TRUE(IsSpokenFeedbackEnabled());
312
313  observer.reset();
314  SetSpokenFeedbackEnabledToPref(false);
315  EXPECT_TRUE(observer.observed());
316  EXPECT_FALSE(observer.observed_enabled());
317  EXPECT_EQ(observer.observed_type(),
318            chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK);
319  EXPECT_FALSE(IsSpokenFeedbackEnabled());
320
321  observer.reset();
322  SetHighContrastEnabledToPref(true);
323  EXPECT_TRUE(observer.observed());
324  EXPECT_TRUE(observer.observed_enabled());
325  EXPECT_EQ(observer.observed_type(),
326            chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_HIGH_CONTRAST_MODE);
327  EXPECT_TRUE(IsHighContrastEnabled());
328
329  observer.reset();
330  SetHighContrastEnabledToPref(false);
331  EXPECT_TRUE(observer.observed());
332  EXPECT_FALSE(observer.observed_enabled());
333  EXPECT_EQ(observer.observed_type(),
334            chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_HIGH_CONTRAST_MODE);
335  EXPECT_FALSE(IsHighContrastEnabled());
336}
337
338}  // namespace chromeos
339