1// Copyright 2014 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 "content/browser/accessibility/accessibility_mode_helper.h"
6#include "content/browser/renderer_host/render_view_host_impl.h"
7#include "content/browser/renderer_host/render_widget_host_impl.h"
8#include "content/browser/renderer_host/render_widget_host_view_base.h"
9#include "content/browser/web_contents/web_contents_impl.h"
10#include "content/public/browser/render_view_host.h"
11#include "content/public/browser/render_widget_host.h"
12#include "content/public/browser/render_widget_host_view.h"
13#include "content/public/browser/web_contents.h"
14#include "content/public/common/url_constants.h"
15#include "content/public/test/content_browser_test.h"
16#include "content/public/test/content_browser_test_utils.h"
17#include "content/shell/browser/shell.h"
18#include "content/test/accessibility_browser_test_utils.h"
19#include "testing/gtest/include/gtest/gtest.h"
20
21namespace content {
22
23const char kMinimalPageDataURL[] =
24    "data:text/html,<html><head></head><body>Hello, world</body></html>";
25
26class AccessibilityModeTest : public ContentBrowserTest {
27 protected:
28  WebContentsImpl* web_contents() {
29    return static_cast<WebContentsImpl*>(shell()->web_contents());
30  }
31
32  void ExpectBrowserAccessibilityManager(bool expect_bam,
33                                         std::string message = "") {
34    if (expect_bam) {
35      EXPECT_NE(
36          (BrowserAccessibilityManager*)NULL,
37          web_contents()->GetRootBrowserAccessibilityManager()) << message;
38    } else {
39      EXPECT_EQ(
40          (BrowserAccessibilityManager*)NULL,
41          web_contents()->GetRootBrowserAccessibilityManager()) << message;
42    }
43  }
44
45  AccessibilityMode CorrectedAccessibility(AccessibilityMode mode) {
46    return AddAccessibilityModeTo(GetBaseAccessibilityMode(), mode);
47  }
48
49  bool ShouldBeBrowserAccessibilityManager(AccessibilityMode mode) {
50    mode = CorrectedAccessibility(mode);
51    switch (mode) {
52      case AccessibilityModeOff:
53      case AccessibilityModeTreeOnly:
54        return false;
55      case AccessibilityModeEditableTextOnly:
56      case AccessibilityModeComplete:
57        return true;
58      default:
59        NOTREACHED();
60    }
61    return false;
62  }
63};
64
65IN_PROC_BROWSER_TEST_F(AccessibilityModeTest, AccessibilityModeOff) {
66  NavigateToURL(shell(), GURL(kMinimalPageDataURL));
67
68  EXPECT_EQ(CorrectedAccessibility(AccessibilityModeOff),
69            web_contents()->GetAccessibilityMode());
70  ExpectBrowserAccessibilityManager(
71      ShouldBeBrowserAccessibilityManager(AccessibilityModeOff));
72}
73
74IN_PROC_BROWSER_TEST_F(AccessibilityModeTest, AccessibilityModeComplete) {
75  NavigateToURL(shell(), GURL(kMinimalPageDataURL));
76  ASSERT_EQ(CorrectedAccessibility(AccessibilityModeOff),
77            web_contents()->GetAccessibilityMode());
78
79  AccessibilityNotificationWaiter waiter(shell());
80  web_contents()->AddAccessibilityMode(AccessibilityModeComplete);
81  EXPECT_EQ(AccessibilityModeComplete, web_contents()->GetAccessibilityMode());
82  waiter.WaitForNotification();
83  ExpectBrowserAccessibilityManager(
84      ShouldBeBrowserAccessibilityManager(AccessibilityModeComplete));
85}
86
87IN_PROC_BROWSER_TEST_F(AccessibilityModeTest, AccessibilityModeTreeOnly) {
88  NavigateToURL(shell(), GURL(kMinimalPageDataURL));
89  ASSERT_EQ(CorrectedAccessibility(AccessibilityModeOff),
90            web_contents()->GetAccessibilityMode());
91
92  AccessibilityNotificationWaiter waiter(shell());
93  web_contents()->AddAccessibilityMode(AccessibilityModeTreeOnly);
94  EXPECT_EQ(CorrectedAccessibility(AccessibilityModeTreeOnly),
95            web_contents()->GetAccessibilityMode());
96  waiter.WaitForNotification();
97  // No BrowserAccessibilityManager expected for AccessibilityModeTreeOnly
98  ExpectBrowserAccessibilityManager(
99      ShouldBeBrowserAccessibilityManager(AccessibilityModeTreeOnly));
100}
101
102IN_PROC_BROWSER_TEST_F(AccessibilityModeTest, AddingModes) {
103  NavigateToURL(shell(), GURL(kMinimalPageDataURL));
104
105  AccessibilityNotificationWaiter waiter(shell());
106  web_contents()->AddAccessibilityMode(AccessibilityModeTreeOnly);
107  EXPECT_EQ(CorrectedAccessibility(AccessibilityModeTreeOnly),
108            web_contents()->GetAccessibilityMode());
109  waiter.WaitForNotification();
110  ExpectBrowserAccessibilityManager(ShouldBeBrowserAccessibilityManager(
111                                        AccessibilityModeTreeOnly),
112                                    "Should be no BrowserAccessibilityManager "
113                                    "for AccessibilityModeTreeOnly");
114
115  AccessibilityNotificationWaiter waiter2(shell());
116  web_contents()->AddAccessibilityMode(AccessibilityModeComplete);
117  EXPECT_EQ(AccessibilityModeComplete, web_contents()->GetAccessibilityMode());
118  waiter2.WaitForNotification();
119  ExpectBrowserAccessibilityManager(ShouldBeBrowserAccessibilityManager(
120                                          AccessibilityModeComplete),
121                                    "Should be a BrowserAccessibilityManager "
122                                    "for AccessibilityModeComplete");
123}
124
125}  // namespace content
126