1 // Copyright 2019 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 "base/command_line.h"
6 #include "chrome/browser/profiles/profile.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_commands.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/common/pref_names.h"
11 #include "chrome/test/base/in_process_browser_test.h"
12 #include "chrome/test/base/ui_test_utils.h"
13 #include "components/prefs/pref_service.h"
14 #include "content/public/browser/browser_accessibility_state.h"
15 #include "content/public/common/content_features.h"
16 #include "content/public/test/browser_test.h"
17 #if defined(OS_CHROMEOS)
18 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
19 #else
20 #include "content/public/browser/browser_accessibility_state.h"
21 #endif // defined(OS_CHROMEOS)
22
23 class AccessibilityLabelsBrowserTest : public InProcessBrowserTest {
24 public:
AccessibilityLabelsBrowserTest()25 AccessibilityLabelsBrowserTest() {}
26
27 // InProcessBrowserTest overrides:
SetUp()28 void SetUp() override {
29 scoped_feature_list_.InitAndEnableFeature(
30 features::kExperimentalAccessibilityLabels);
31 InProcessBrowserTest::SetUp();
32 }
33
TearDownOnMainThread()34 void TearDownOnMainThread() override { EnableScreenReader(false); }
35
EnableScreenReader(bool enabled)36 void EnableScreenReader(bool enabled) {
37 #if defined(OS_CHROMEOS)
38 // Enable Chromevox.
39 chromeos::AccessibilityManager::Get()->EnableSpokenFeedback(enabled);
40 #else
41 // Spoof a screen reader.
42 if (enabled) {
43 content::BrowserAccessibilityState::GetInstance()
44 ->AddAccessibilityModeFlags(ui::AXMode::kScreenReader);
45 } else {
46 content::BrowserAccessibilityState::GetInstance()
47 ->RemoveAccessibilityModeFlags(ui::AXMode::kScreenReader);
48 }
49 #endif // defined(OS_CHROMEOS)
50 }
51
52 private:
53 base::test::ScopedFeatureList scoped_feature_list_;
54 DISALLOW_COPY_AND_ASSIGN(AccessibilityLabelsBrowserTest);
55 };
56
57 // Changing the kAccessibilityImageLabelsEnabled pref should affect the
58 // accessibility mode of a new WebContents for this profile.
IN_PROC_BROWSER_TEST_F(AccessibilityLabelsBrowserTest,NewWebContents)59 IN_PROC_BROWSER_TEST_F(AccessibilityLabelsBrowserTest, NewWebContents) {
60 EnableScreenReader(true);
61 ui::AXMode ax_mode =
62 content::BrowserAccessibilityState::GetInstance()->GetAccessibilityMode();
63 EXPECT_FALSE(ax_mode.has_mode(ui::AXMode::kLabelImages));
64
65 chrome::NewTab(browser());
66 content::WebContents* web_contents =
67 browser()->tab_strip_model()->GetActiveWebContents();
68 ax_mode = web_contents->GetAccessibilityMode();
69 EXPECT_FALSE(ax_mode.has_mode(ui::AXMode::kLabelImages));
70
71 browser()->profile()->GetPrefs()->SetBoolean(
72 prefs::kAccessibilityImageLabelsEnabled, true);
73
74 chrome::NewTab(browser());
75 web_contents = browser()->tab_strip_model()->GetActiveWebContents();
76 ax_mode = web_contents->GetAccessibilityMode();
77 EXPECT_TRUE(ax_mode.has_mode(ui::AXMode::kLabelImages));
78
79 browser()->profile()->GetPrefs()->SetBoolean(
80 prefs::kAccessibilityImageLabelsEnabled, false);
81
82 chrome::NewTab(browser());
83 web_contents = browser()->tab_strip_model()->GetActiveWebContents();
84 ax_mode = web_contents->GetAccessibilityMode();
85 EXPECT_FALSE(ax_mode.has_mode(ui::AXMode::kLabelImages));
86 }
87
88 // Changing the kAccessibilityImageLabelsEnabled pref should affect the
89 // accessibility mode of existing WebContents in this profile.
IN_PROC_BROWSER_TEST_F(AccessibilityLabelsBrowserTest,ExistingWebContents)90 IN_PROC_BROWSER_TEST_F(AccessibilityLabelsBrowserTest, ExistingWebContents) {
91 EnableScreenReader(true);
92 content::WebContents* web_contents =
93 browser()->tab_strip_model()->GetActiveWebContents();
94 ui::AXMode ax_mode = web_contents->GetAccessibilityMode();
95 EXPECT_FALSE(ax_mode.has_mode(ui::AXMode::kLabelImages));
96
97 browser()->profile()->GetPrefs()->SetBoolean(
98 prefs::kAccessibilityImageLabelsEnabled, true);
99
100 ax_mode = web_contents->GetAccessibilityMode();
101 EXPECT_TRUE(ax_mode.has_mode(ui::AXMode::kLabelImages));
102
103 browser()->profile()->GetPrefs()->SetBoolean(
104 prefs::kAccessibilityImageLabelsEnabled, false);
105
106 ax_mode = web_contents->GetAccessibilityMode();
107 EXPECT_FALSE(ax_mode.has_mode(ui::AXMode::kLabelImages));
108 }
109
IN_PROC_BROWSER_TEST_F(AccessibilityLabelsBrowserTest,NotEnabledWithoutScreenReader)110 IN_PROC_BROWSER_TEST_F(AccessibilityLabelsBrowserTest,
111 NotEnabledWithoutScreenReader) {
112 EnableScreenReader(false);
113 content::WebContents* web_contents =
114 browser()->tab_strip_model()->GetActiveWebContents();
115 ui::AXMode ax_mode = web_contents->GetAccessibilityMode();
116 EXPECT_FALSE(ax_mode.has_mode(ui::AXMode::kLabelImages));
117
118 browser()->profile()->GetPrefs()->SetBoolean(
119 prefs::kAccessibilityImageLabelsEnabled, true);
120
121 ax_mode = web_contents->GetAccessibilityMode();
122 EXPECT_FALSE(ax_mode.has_mode(ui::AXMode::kLabelImages));
123
124 // Reset state.
125 browser()->profile()->GetPrefs()->SetBoolean(
126 prefs::kAccessibilityImageLabelsEnabled, false);
127 }
128