OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 <set> |
| 6 #include <string> |
| 7 #include <vector> |
| 8 |
| 9 #include "base/strings/string16.h" |
| 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "content/browser/accessibility/accessibility_event_recorder.h" |
| 13 #include "content/browser/accessibility/accessibility_tree_formatter.h" |
| 14 #include "content/browser/accessibility/browser_accessibility.h" |
| 15 #include "content/browser/accessibility/browser_accessibility_manager.h" |
| 16 #include "content/browser/accessibility/dump_accessibility_browsertest_base.h" |
| 17 #include "content/browser/web_contents/web_contents_impl.h" |
| 18 #include "content/shell/browser/shell.h" |
| 19 #include "content/test/accessibility_browser_test_utils.h" |
| 20 |
| 21 namespace content { |
| 22 |
| 23 typedef AccessibilityTreeFormatter::Filter Filter; |
| 24 |
| 25 // Tests that the right platform-specific accessibility events are fired |
| 26 // in response to things that happen in a web document. |
| 27 // |
| 28 // Similar to DumpAccessibilityTree in that each test consists of a |
| 29 // single HTML file, possibly with a few special directives in comments, |
| 30 // and then expectation files in text format for each platform. |
| 31 // |
| 32 // While DumpAccessibilityTree just loads the document and then |
| 33 // prints out a text representation of the accessibility tree, |
| 34 // DumpAccessibilityEvents loads the document, then executes the |
| 35 // JavaScript function "go()", then it records and dumps all accessibility |
| 36 // events generated as a result of that "go" function executing. |
| 37 // |
| 38 // How each event is dumped is platform-specific, but should be of the |
| 39 // form: |
| 40 // |
| 41 // <event> on <node> |
| 42 // |
| 43 // ...where <event> is the name of the event, and <node> is a description |
| 44 // of the node the event fired on, such as the node's role and name. |
| 45 // |
| 46 // As with DumpAccessibilityTree, DumpAccessibilityEvents takes the events |
| 47 // dumped from that particular html file and compares it to the expectation |
| 48 // file in the same directory (for example, test-name-expected-win.txt) |
| 49 // and the test fails if they don't agree. |
| 50 // |
| 51 // Currently it's not possible to test for accessibility events that |
| 52 // don't fire immediately (i.e. within the call scope of the call to "go()"); |
| 53 // the test framework calls "go()" and then sends a sentinel event signaling |
| 54 // the end of the test; anything received after that is too late. |
| 55 class DumpAccessibilityEventsTest : public DumpAccessibilityTestBase { |
| 56 public: |
| 57 void AddDefaultFilters(std::vector<Filter>* filters) override { |
| 58 } |
| 59 |
| 60 std::vector<std::string> Dump() override; |
| 61 |
| 62 void OnDiffFailed() override; |
| 63 |
| 64 private: |
| 65 base::string16 initial_tree_; |
| 66 base::string16 final_tree_; |
| 67 }; |
| 68 |
| 69 std::vector<std::string> DumpAccessibilityEventsTest::Dump() { |
| 70 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( |
| 71 shell()->web_contents()); |
| 72 scoped_ptr<AccessibilityEventRecorder> event_recorder( |
| 73 AccessibilityEventRecorder::Create( |
| 74 web_contents->GetRootBrowserAccessibilityManager())); |
| 75 |
| 76 // Save a copy of the accessibility tree (as a text dump); we'll |
| 77 // log this for the user later if the test fails. |
| 78 initial_tree_ = DumpUnfilteredAccessibilityTreeAsString(); |
| 79 |
| 80 // Create a waiter that waits for any one accessibility event. |
| 81 // This will ensure that after calling the go() function, we |
| 82 // block until we've received an accessibility event generated as |
| 83 // a result of this function. |
| 84 scoped_ptr<AccessibilityNotificationWaiter> waiter; |
| 85 waiter.reset(new AccessibilityNotificationWaiter( |
| 86 shell(), AccessibilityModeComplete, ui::AX_EVENT_NONE)); |
| 87 |
| 88 // Execute the "go" function in the script. |
| 89 web_contents->GetMainFrame()->ExecuteJavaScript( |
| 90 base::ASCIIToUTF16("go()")); |
| 91 |
| 92 // Wait for at least one accessibility event generated in response to |
| 93 // that function. |
| 94 waiter->WaitForNotification(); |
| 95 |
| 96 // More than one accessibility event could have been generated. |
| 97 // To make sure we've received all accessibility events, add a |
| 98 // sentinel by calling AccessibilityHitTest and waiting for a HOVER |
| 99 // event in response. |
| 100 waiter.reset(new AccessibilityNotificationWaiter( |
| 101 shell(), AccessibilityModeComplete, ui::AX_EVENT_HOVER)); |
| 102 BrowserAccessibilityManager* manager = |
| 103 web_contents->GetRootBrowserAccessibilityManager(); |
| 104 manager->delegate()->AccessibilityHitTest(gfx::Point(0, 0)); |
| 105 waiter->WaitForNotification(); |
| 106 |
| 107 // Save a copy of the final accessibility tree (as a text dump); we'll |
| 108 // log this for the user later if the test fails. |
| 109 final_tree_ = DumpUnfilteredAccessibilityTreeAsString(); |
| 110 |
| 111 // Dump the event logs, running them through any filters specified |
| 112 // in the HTML file. |
| 113 std::vector<std::string> event_logs = event_recorder->event_logs(); |
| 114 std::vector<std::string> result; |
| 115 for (size_t i = 0; i < event_logs.size(); ++i) { |
| 116 if (AccessibilityTreeFormatter::MatchesFilters( |
| 117 filters_, base::UTF8ToUTF16(event_logs[i]), true)) { |
| 118 result.push_back(event_logs[i]); |
| 119 } |
| 120 } |
| 121 return result; |
| 122 } |
| 123 |
| 124 void DumpAccessibilityEventsTest::OnDiffFailed() { |
| 125 printf("\n"); |
| 126 printf("Initial accessibility tree (after load complete):\n"); |
| 127 printf("%s\n", base::UTF16ToUTF8(initial_tree_).c_str()); |
| 128 printf("\n"); |
| 129 printf("Final accessibility tree after events fired:\n"); |
| 130 printf("%s\n", base::UTF16ToUTF8(final_tree_).c_str()); |
| 131 printf("\n"); |
| 132 } |
| 133 |
| 134 // TODO(dmazzoni): port these tests to run on all platforms. |
| 135 #if defined(OS_WIN) |
| 136 |
| 137 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest, |
| 138 AccessibilityEventsCheckedStateChanged) { |
| 139 RunTest(FILE_PATH_LITERAL("events-checked-state-changed.html")); |
| 140 } |
| 141 |
| 142 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest, |
| 143 AccessibilityEventsInputTypeTextValueChanged) { |
| 144 RunTest(FILE_PATH_LITERAL("events-input-type-text-value-changed.html")); |
| 145 } |
| 146 |
| 147 #endif // defined(OS_WIN) |
| 148 |
| 149 } // namespace content |
OLD | NEW |