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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "base/win/scoped_bstr.h"
8 #include "base/win/scoped_comptr.h"
9 #include "base/win/scoped_variant.h"
10 #include "content/browser/accessibility/browser_accessibility_manager.h"
11 #include "content/browser/accessibility/browser_accessibility_manager_win.h"
12 #include "content/browser/accessibility/browser_accessibility_state_impl.h"
13 #include "content/browser/accessibility/browser_accessibility_win.h"
14 #include "content/browser/renderer_host/legacy_render_widget_host_win.h"
15 #include "content/common/accessibility_messages.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/base/win/atl_module.h"
23 // CountedBrowserAccessibility ------------------------------------------------
25 // Subclass of BrowserAccessibilityWin that counts the number of instances.
26 class CountedBrowserAccessibility
: public BrowserAccessibilityWin
{
28 CountedBrowserAccessibility();
29 virtual ~CountedBrowserAccessibility();
31 static void reset() { num_instances_
= 0; }
32 static int num_instances() { return num_instances_
; }
35 static int num_instances_
;
37 DISALLOW_COPY_AND_ASSIGN(CountedBrowserAccessibility
);
41 int CountedBrowserAccessibility::num_instances_
= 0;
43 CountedBrowserAccessibility::CountedBrowserAccessibility() {
47 CountedBrowserAccessibility::~CountedBrowserAccessibility() {
52 // CountedBrowserAccessibilityFactory -----------------------------------------
54 // Factory that creates a CountedBrowserAccessibility.
55 class CountedBrowserAccessibilityFactory
: public BrowserAccessibilityFactory
{
57 CountedBrowserAccessibilityFactory();
60 virtual ~CountedBrowserAccessibilityFactory();
62 virtual BrowserAccessibility
* Create() OVERRIDE
;
64 DISALLOW_COPY_AND_ASSIGN(CountedBrowserAccessibilityFactory
);
67 CountedBrowserAccessibilityFactory::CountedBrowserAccessibilityFactory() {
70 CountedBrowserAccessibilityFactory::~CountedBrowserAccessibilityFactory() {
73 BrowserAccessibility
* CountedBrowserAccessibilityFactory::Create() {
74 CComObject
<CountedBrowserAccessibility
>* instance
;
75 HRESULT hr
= CComObject
<CountedBrowserAccessibility
>::CreateInstance(
77 DCHECK(SUCCEEDED(hr
));
85 // BrowserAccessibilityTest ---------------------------------------------------
87 class BrowserAccessibilityTest
: public testing::Test
{
89 BrowserAccessibilityTest();
90 virtual ~BrowserAccessibilityTest();
93 virtual void SetUp() OVERRIDE
;
95 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibilityTest
);
98 BrowserAccessibilityTest::BrowserAccessibilityTest() {
101 BrowserAccessibilityTest::~BrowserAccessibilityTest() {
104 void BrowserAccessibilityTest::SetUp() {
105 ui::win::CreateATLModuleIfNeeded();
109 // Actual tests ---------------------------------------------------------------
111 // Test that BrowserAccessibilityManager correctly releases the tree of
112 // BrowserAccessibility instances upon delete.
113 TEST_F(BrowserAccessibilityTest
, TestNoLeaks
) {
114 // Create ui::AXNodeData objects for a simple document tree,
115 // representing the accessibility information used to initialize
116 // BrowserAccessibilityManager.
117 ui::AXNodeData button
;
119 button
.SetName("Button");
120 button
.role
= ui::AX_ROLE_BUTTON
;
123 ui::AXNodeData checkbox
;
125 checkbox
.SetName("Checkbox");
126 checkbox
.role
= ui::AX_ROLE_CHECK_BOX
;
131 root
.SetName("Document");
132 root
.role
= ui::AX_ROLE_ROOT_WEB_AREA
;
134 root
.child_ids
.push_back(2);
135 root
.child_ids
.push_back(3);
137 // Construct a BrowserAccessibilityManager with this
138 // ui::AXNodeData tree and a factory for an instance-counting
139 // BrowserAccessibility, and ensure that exactly 3 instances were
140 // created. Note that the manager takes ownership of the factory.
141 CountedBrowserAccessibility::reset();
142 scoped_ptr
<BrowserAccessibilityManager
> manager(
143 BrowserAccessibilityManager::Create(
144 root
, NULL
, new CountedBrowserAccessibilityFactory()));
145 manager
->UpdateNodesForTesting(button
, checkbox
);
146 ASSERT_EQ(3, CountedBrowserAccessibility::num_instances());
148 // Delete the manager and test that all 3 instances are deleted.
150 ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
152 // Construct a manager again, and this time use the IAccessible interface
153 // to get new references to two of the three nodes in the tree.
154 manager
.reset(BrowserAccessibilityManager::Create(
155 root
, NULL
, new CountedBrowserAccessibilityFactory()));
156 manager
->UpdateNodesForTesting(button
, checkbox
);
157 ASSERT_EQ(3, CountedBrowserAccessibility::num_instances());
158 IAccessible
* root_accessible
=
159 manager
->GetRoot()->ToBrowserAccessibilityWin();
160 IDispatch
* root_iaccessible
= NULL
;
161 IDispatch
* child1_iaccessible
= NULL
;
162 base::win::ScopedVariant
childid_self(CHILDID_SELF
);
163 HRESULT hr
= root_accessible
->get_accChild(childid_self
, &root_iaccessible
);
165 base::win::ScopedVariant
one(1);
166 hr
= root_accessible
->get_accChild(one
, &child1_iaccessible
);
169 // Now delete the manager, and only one of the three nodes in the tree
170 // should be released.
172 ASSERT_EQ(2, CountedBrowserAccessibility::num_instances());
174 // Release each of our references and make sure that each one results in
175 // the instance being deleted as its reference count hits zero.
176 root_iaccessible
->Release();
177 ASSERT_EQ(1, CountedBrowserAccessibility::num_instances());
178 child1_iaccessible
->Release();
179 ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
182 TEST_F(BrowserAccessibilityTest
, TestChildrenChange
) {
183 // Create ui::AXNodeData objects for a simple document tree,
184 // representing the accessibility information used to initialize
185 // BrowserAccessibilityManager.
188 text
.role
= ui::AX_ROLE_STATIC_TEXT
;
189 text
.SetName("old text");
194 root
.SetName("Document");
195 root
.role
= ui::AX_ROLE_ROOT_WEB_AREA
;
197 root
.child_ids
.push_back(2);
199 // Construct a BrowserAccessibilityManager with this
200 // ui::AXNodeData tree and a factory for an instance-counting
201 // BrowserAccessibility.
202 CountedBrowserAccessibility::reset();
203 scoped_ptr
<BrowserAccessibilityManager
> manager(
204 BrowserAccessibilityManager::Create(
205 root
, NULL
, new CountedBrowserAccessibilityFactory()));
206 manager
->UpdateNodesForTesting(text
);
208 // Query for the text IAccessible and verify that it returns "old text" as its
210 base::win::ScopedVariant
one(1);
211 base::win::ScopedComPtr
<IDispatch
> text_dispatch
;
212 HRESULT hr
= manager
->GetRoot()->ToBrowserAccessibilityWin()->get_accChild(
213 one
, text_dispatch
.Receive());
216 base::win::ScopedComPtr
<IAccessible
> text_accessible
;
217 hr
= text_dispatch
.QueryInterface(text_accessible
.Receive());
220 base::win::ScopedVariant
childid_self(CHILDID_SELF
);
221 base::win::ScopedBstr name
;
222 hr
= text_accessible
->get_accName(childid_self
, name
.Receive());
224 EXPECT_EQ(L
"old text", base::string16(name
));
227 text_dispatch
.Release();
228 text_accessible
.Release();
230 // Notify the BrowserAccessibilityManager that the text child has changed.
231 ui::AXNodeData text2
;
233 text2
.role
= ui::AX_ROLE_STATIC_TEXT
;
234 text2
.SetName("new text");
235 text2
.SetName("old text");
236 AccessibilityHostMsg_EventParams param
;
237 param
.event_type
= ui::AX_EVENT_CHILDREN_CHANGED
;
238 param
.nodes
.push_back(text2
);
240 std::vector
<AccessibilityHostMsg_EventParams
> events
;
241 events
.push_back(param
);
242 manager
->OnAccessibilityEvents(events
);
244 // Query for the text IAccessible and verify that it now returns "new text"
246 hr
= manager
->GetRoot()->ToBrowserAccessibilityWin()->get_accChild(
247 one
, text_dispatch
.Receive());
250 hr
= text_dispatch
.QueryInterface(text_accessible
.Receive());
253 hr
= text_accessible
->get_accName(childid_self
, name
.Receive());
255 EXPECT_EQ(L
"new text", base::string16(name
));
257 text_dispatch
.Release();
258 text_accessible
.Release();
260 // Delete the manager and test that all BrowserAccessibility instances are
263 ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
266 TEST_F(BrowserAccessibilityTest
, TestChildrenChangeNoLeaks
) {
267 // Create ui::AXNodeData objects for a simple document tree,
268 // representing the accessibility information used to initialize
269 // BrowserAccessibilityManager.
272 div
.role
= ui::AX_ROLE_GROUP
;
275 ui::AXNodeData text3
;
277 text3
.role
= ui::AX_ROLE_STATIC_TEXT
;
280 ui::AXNodeData text4
;
282 text4
.role
= ui::AX_ROLE_STATIC_TEXT
;
285 div
.child_ids
.push_back(3);
286 div
.child_ids
.push_back(4);
290 root
.role
= ui::AX_ROLE_ROOT_WEB_AREA
;
292 root
.child_ids
.push_back(2);
294 // Construct a BrowserAccessibilityManager with this
295 // ui::AXNodeData tree and a factory for an instance-counting
296 // BrowserAccessibility and ensure that exactly 4 instances were
297 // created. Note that the manager takes ownership of the factory.
298 CountedBrowserAccessibility::reset();
299 scoped_ptr
<BrowserAccessibilityManager
> manager(
300 BrowserAccessibilityManager::Create(
301 root
, NULL
, new CountedBrowserAccessibilityFactory()));
302 manager
->UpdateNodesForTesting(div
, text3
, text4
);
303 ASSERT_EQ(4, CountedBrowserAccessibility::num_instances());
305 // Notify the BrowserAccessibilityManager that the div node and its children
306 // were removed and ensure that only one BrowserAccessibility instance exists.
307 root
.child_ids
.clear();
308 AccessibilityHostMsg_EventParams param
;
309 param
.event_type
= ui::AX_EVENT_CHILDREN_CHANGED
;
310 param
.nodes
.push_back(root
);
312 std::vector
<AccessibilityHostMsg_EventParams
> events
;
313 events
.push_back(param
);
314 manager
->OnAccessibilityEvents(events
);
315 ASSERT_EQ(1, CountedBrowserAccessibility::num_instances());
317 // Delete the manager and test that all BrowserAccessibility instances are
320 ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
323 TEST_F(BrowserAccessibilityTest
, TestTextBoundaries
) {
324 std::string text1_value
= "One two three.\nFour five six.";
326 ui::AXNodeData text1
;
328 text1
.role
= ui::AX_ROLE_TEXT_FIELD
;
330 text1
.AddStringAttribute(ui::AX_ATTR_VALUE
, text1_value
);
331 std::vector
<int32
> line_breaks
;
332 line_breaks
.push_back(15);
333 text1
.AddIntListAttribute(
334 ui::AX_ATTR_LINE_BREAKS
, line_breaks
);
338 root
.role
= ui::AX_ROLE_ROOT_WEB_AREA
;
340 root
.child_ids
.push_back(11);
342 CountedBrowserAccessibility::reset();
343 scoped_ptr
<BrowserAccessibilityManager
> manager(
344 BrowserAccessibilityManager::Create(
345 root
, NULL
, new CountedBrowserAccessibilityFactory()));
346 manager
->UpdateNodesForTesting(text1
);
347 ASSERT_EQ(2, CountedBrowserAccessibility::num_instances());
349 BrowserAccessibilityWin
* root_obj
=
350 manager
->GetRoot()->ToBrowserAccessibilityWin();
351 BrowserAccessibilityWin
* text1_obj
=
352 root_obj
->PlatformGetChild(0)->ToBrowserAccessibilityWin();
355 ASSERT_EQ(S_OK
, text1_obj
->get_nCharacters(&text1_len
));
357 base::win::ScopedBstr text
;
358 ASSERT_EQ(S_OK
, text1_obj
->get_text(0, text1_len
, text
.Receive()));
359 ASSERT_EQ(text1_value
, base::UTF16ToUTF8(base::string16(text
)));
362 ASSERT_EQ(S_OK
, text1_obj
->get_text(0, 4, text
.Receive()));
363 ASSERT_STREQ(L
"One ", text
);
368 ASSERT_EQ(S_OK
, text1_obj
->get_textAtOffset(
369 1, IA2_TEXT_BOUNDARY_CHAR
, &start
, &end
, text
.Receive()));
372 ASSERT_STREQ(L
"n", text
);
375 ASSERT_EQ(S_FALSE
, text1_obj
->get_textAtOffset(
376 text1_len
, IA2_TEXT_BOUNDARY_CHAR
, &start
, &end
, text
.Receive()));
377 ASSERT_EQ(text1_len
, start
);
378 ASSERT_EQ(text1_len
, end
);
381 ASSERT_EQ(S_OK
, text1_obj
->get_textAtOffset(
382 1, IA2_TEXT_BOUNDARY_WORD
, &start
, &end
, text
.Receive()));
385 ASSERT_STREQ(L
"One", text
);
388 ASSERT_EQ(S_OK
, text1_obj
->get_textAtOffset(
389 6, IA2_TEXT_BOUNDARY_WORD
, &start
, &end
, text
.Receive()));
392 ASSERT_STREQ(L
"two", text
);
395 ASSERT_EQ(S_OK
, text1_obj
->get_textAtOffset(
396 text1_len
, IA2_TEXT_BOUNDARY_WORD
, &start
, &end
, text
.Receive()));
397 ASSERT_EQ(25, start
);
399 ASSERT_STREQ(L
"six.", text
);
402 ASSERT_EQ(S_OK
, text1_obj
->get_textAtOffset(
403 1, IA2_TEXT_BOUNDARY_LINE
, &start
, &end
, text
.Receive()));
406 ASSERT_STREQ(L
"One two three.\n", text
);
410 text1_obj
->get_text(0, IA2_TEXT_OFFSET_LENGTH
, text
.Receive()));
411 ASSERT_STREQ(L
"One two three.\nFour five six.", text
);
413 // Delete the manager and test that all BrowserAccessibility instances are
416 ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
419 TEST_F(BrowserAccessibilityTest
, TestSimpleHypertext
) {
420 const std::string text1_name
= "One two three.";
421 const std::string text2_name
= " Four five six.";
423 ui::AXNodeData text1
;
425 text1
.role
= ui::AX_ROLE_STATIC_TEXT
;
426 text1
.state
= 1 << ui::AX_STATE_READ_ONLY
;
427 text1
.SetName(text1_name
);
429 ui::AXNodeData text2
;
431 text2
.role
= ui::AX_ROLE_STATIC_TEXT
;
432 text2
.state
= 1 << ui::AX_STATE_READ_ONLY
;
433 text2
.SetName(text2_name
);
437 root
.role
= ui::AX_ROLE_ROOT_WEB_AREA
;
438 root
.state
= 1 << ui::AX_STATE_READ_ONLY
;
439 root
.child_ids
.push_back(11);
440 root
.child_ids
.push_back(12);
442 CountedBrowserAccessibility::reset();
443 scoped_ptr
<BrowserAccessibilityManager
> manager(
444 BrowserAccessibilityManager::Create(
445 root
, NULL
, new CountedBrowserAccessibilityFactory()));
446 manager
->UpdateNodesForTesting(root
, text1
, text2
);
447 ASSERT_EQ(3, CountedBrowserAccessibility::num_instances());
449 BrowserAccessibilityWin
* root_obj
=
450 manager
->GetRoot()->ToBrowserAccessibilityWin();
453 ASSERT_EQ(S_OK
, root_obj
->get_nCharacters(&text_len
));
455 base::win::ScopedBstr text
;
456 ASSERT_EQ(S_OK
, root_obj
->get_text(0, text_len
, text
.Receive()));
457 EXPECT_EQ(text1_name
+ text2_name
, base::UTF16ToUTF8(base::string16(text
)));
459 long hyperlink_count
;
460 ASSERT_EQ(S_OK
, root_obj
->get_nHyperlinks(&hyperlink_count
));
461 EXPECT_EQ(0, hyperlink_count
);
463 base::win::ScopedComPtr
<IAccessibleHyperlink
> hyperlink
;
464 EXPECT_EQ(E_INVALIDARG
, root_obj
->get_hyperlink(-1, hyperlink
.Receive()));
465 EXPECT_EQ(E_INVALIDARG
, root_obj
->get_hyperlink(0, hyperlink
.Receive()));
466 EXPECT_EQ(E_INVALIDARG
, root_obj
->get_hyperlink(28, hyperlink
.Receive()));
467 EXPECT_EQ(E_INVALIDARG
, root_obj
->get_hyperlink(29, hyperlink
.Receive()));
469 long hyperlink_index
;
470 EXPECT_EQ(E_FAIL
, root_obj
->get_hyperlinkIndex(0, &hyperlink_index
));
471 EXPECT_EQ(-1, hyperlink_index
);
472 EXPECT_EQ(E_FAIL
, root_obj
->get_hyperlinkIndex(28, &hyperlink_index
));
473 EXPECT_EQ(-1, hyperlink_index
);
474 EXPECT_EQ(E_INVALIDARG
, root_obj
->get_hyperlinkIndex(-1, &hyperlink_index
));
475 EXPECT_EQ(-1, hyperlink_index
);
476 EXPECT_EQ(E_INVALIDARG
, root_obj
->get_hyperlinkIndex(29, &hyperlink_index
));
477 EXPECT_EQ(-1, hyperlink_index
);
479 // Delete the manager and test that all BrowserAccessibility instances are
482 ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
485 TEST_F(BrowserAccessibilityTest
, TestComplexHypertext
) {
486 const std::string text1_name
= "One two three.";
487 const std::string text2_name
= " Four five six.";
488 const std::string button1_text_name
= "red";
489 const std::string link1_text_name
= "blue";
491 ui::AXNodeData text1
;
493 text1
.role
= ui::AX_ROLE_STATIC_TEXT
;
494 text1
.state
= 1 << ui::AX_STATE_READ_ONLY
;
495 text1
.SetName(text1_name
);
497 ui::AXNodeData text2
;
499 text2
.role
= ui::AX_ROLE_STATIC_TEXT
;
500 text2
.state
= 1 << ui::AX_STATE_READ_ONLY
;
501 text2
.SetName(text2_name
);
503 ui::AXNodeData button1
, button1_text
;
505 button1_text
.id
= 15;
506 button1_text
.SetName(button1_text_name
);
507 button1
.role
= ui::AX_ROLE_BUTTON
;
508 button1_text
.role
= ui::AX_ROLE_STATIC_TEXT
;
509 button1
.state
= 1 << ui::AX_STATE_READ_ONLY
;
510 button1_text
.state
= 1 << ui::AX_STATE_READ_ONLY
;
511 button1
.child_ids
.push_back(15);
513 ui::AXNodeData link1
, link1_text
;
516 link1_text
.SetName(link1_text_name
);
517 link1
.role
= ui::AX_ROLE_LINK
;
518 link1_text
.role
= ui::AX_ROLE_STATIC_TEXT
;
519 link1
.state
= 1 << ui::AX_STATE_READ_ONLY
;
520 link1_text
.state
= 1 << ui::AX_STATE_READ_ONLY
;
521 link1
.child_ids
.push_back(16);
525 root
.role
= ui::AX_ROLE_ROOT_WEB_AREA
;
526 root
.state
= 1 << ui::AX_STATE_READ_ONLY
;
527 root
.child_ids
.push_back(11);
528 root
.child_ids
.push_back(13);
529 root
.child_ids
.push_back(12);
530 root
.child_ids
.push_back(14);
532 CountedBrowserAccessibility::reset();
533 scoped_ptr
<BrowserAccessibilityManager
> manager(
534 BrowserAccessibilityManager::Create(
535 root
, NULL
, new CountedBrowserAccessibilityFactory()));
536 manager
->UpdateNodesForTesting(root
,
537 text1
, button1
, button1_text
,
538 text2
, link1
, link1_text
);
540 ASSERT_EQ(7, CountedBrowserAccessibility::num_instances());
542 BrowserAccessibilityWin
* root_obj
=
543 manager
->GetRoot()->ToBrowserAccessibilityWin();
546 ASSERT_EQ(S_OK
, root_obj
->get_nCharacters(&text_len
));
548 base::win::ScopedBstr text
;
549 ASSERT_EQ(S_OK
, root_obj
->get_text(0, text_len
, text
.Receive()));
550 const std::string embed
= base::UTF16ToUTF8(
551 BrowserAccessibilityWin::kEmbeddedCharacter
);
552 EXPECT_EQ(text1_name
+ embed
+ text2_name
+ embed
,
553 base::UTF16ToUTF8(base::string16(text
)));
556 long hyperlink_count
;
557 ASSERT_EQ(S_OK
, root_obj
->get_nHyperlinks(&hyperlink_count
));
558 EXPECT_EQ(2, hyperlink_count
);
560 base::win::ScopedComPtr
<IAccessibleHyperlink
> hyperlink
;
561 base::win::ScopedComPtr
<IAccessibleText
> hypertext
;
562 EXPECT_EQ(E_INVALIDARG
, root_obj
->get_hyperlink(-1, hyperlink
.Receive()));
563 EXPECT_EQ(E_INVALIDARG
, root_obj
->get_hyperlink(2, hyperlink
.Receive()));
564 EXPECT_EQ(E_INVALIDARG
, root_obj
->get_hyperlink(28, hyperlink
.Receive()));
566 EXPECT_EQ(S_OK
, root_obj
->get_hyperlink(0, hyperlink
.Receive()));
568 hyperlink
.QueryInterface
<IAccessibleText
>(hypertext
.Receive()));
569 EXPECT_EQ(S_OK
, hypertext
->get_text(0, 3, text
.Receive()));
570 EXPECT_STREQ(button1_text_name
.c_str(),
571 base::UTF16ToUTF8(base::string16(text
)).c_str());
576 EXPECT_EQ(S_OK
, root_obj
->get_hyperlink(1, hyperlink
.Receive()));
578 hyperlink
.QueryInterface
<IAccessibleText
>(hypertext
.Receive()));
579 EXPECT_EQ(S_OK
, hypertext
->get_text(0, 4, text
.Receive()));
580 EXPECT_STREQ(link1_text_name
.c_str(),
581 base::UTF16ToUTF8(base::string16(text
)).c_str());
586 long hyperlink_index
;
587 EXPECT_EQ(E_FAIL
, root_obj
->get_hyperlinkIndex(0, &hyperlink_index
));
588 EXPECT_EQ(-1, hyperlink_index
);
589 EXPECT_EQ(E_FAIL
, root_obj
->get_hyperlinkIndex(28, &hyperlink_index
));
590 EXPECT_EQ(-1, hyperlink_index
);
591 EXPECT_EQ(S_OK
, root_obj
->get_hyperlinkIndex(14, &hyperlink_index
));
592 EXPECT_EQ(0, hyperlink_index
);
593 EXPECT_EQ(S_OK
, root_obj
->get_hyperlinkIndex(30, &hyperlink_index
));
594 EXPECT_EQ(1, hyperlink_index
);
596 // Delete the manager and test that all BrowserAccessibility instances are
599 ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
602 TEST_F(BrowserAccessibilityTest
, TestCreateEmptyDocument
) {
603 // Try creating an empty document with busy state. Readonly is
604 // set automatically.
605 CountedBrowserAccessibility::reset();
606 const int32 busy_state
= 1 << ui::AX_STATE_BUSY
;
607 const int32 readonly_state
= 1 << ui::AX_STATE_READ_ONLY
;
608 const int32 enabled_state
= 1 << ui::AX_STATE_ENABLED
;
609 scoped_ptr
<content::LegacyRenderWidgetHostHWND
> accessible_hwnd(
610 content::LegacyRenderWidgetHostHWND::Create(GetDesktopWindow()));
611 scoped_ptr
<BrowserAccessibilityManager
> manager(
612 new BrowserAccessibilityManagerWin(
613 accessible_hwnd
.get(),
615 BrowserAccessibilityManagerWin::GetEmptyDocument(),
617 new CountedBrowserAccessibilityFactory()));
619 // Verify the root is as we expect by default.
620 BrowserAccessibility
* root
= manager
->GetRoot();
621 EXPECT_EQ(0, root
->renderer_id());
622 EXPECT_EQ(ui::AX_ROLE_ROOT_WEB_AREA
, root
->role());
623 EXPECT_EQ(busy_state
| readonly_state
| enabled_state
, root
->state());
625 // Tree with a child textfield.
626 ui::AXNodeData tree1_1
;
628 tree1_1
.role
= ui::AX_ROLE_ROOT_WEB_AREA
;
629 tree1_1
.child_ids
.push_back(2);
631 ui::AXNodeData tree1_2
;
633 tree1_2
.role
= ui::AX_ROLE_TEXT_FIELD
;
635 // Process a load complete.
636 std::vector
<AccessibilityHostMsg_EventParams
> params
;
637 params
.push_back(AccessibilityHostMsg_EventParams());
638 AccessibilityHostMsg_EventParams
* msg
= ¶ms
[0];
639 msg
->event_type
= ui::AX_EVENT_LOAD_COMPLETE
;
640 msg
->nodes
.push_back(tree1_1
);
641 msg
->nodes
.push_back(tree1_2
);
642 msg
->id
= tree1_1
.id
;
643 manager
->OnAccessibilityEvents(params
);
645 // Save for later comparison.
646 BrowserAccessibility
* acc1_2
= manager
->GetFromRendererID(2);
648 // Verify the root has changed.
649 EXPECT_NE(root
, manager
->GetRoot());
651 // And the proper child remains.
652 EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD
, acc1_2
->role());
653 EXPECT_EQ(2, acc1_2
->renderer_id());
655 // Tree with a child button.
656 ui::AXNodeData tree2_1
;
658 tree2_1
.role
= ui::AX_ROLE_ROOT_WEB_AREA
;
659 tree2_1
.child_ids
.push_back(3);
661 ui::AXNodeData tree2_2
;
663 tree2_2
.role
= ui::AX_ROLE_BUTTON
;
666 msg
->nodes
.push_back(tree2_1
);
667 msg
->nodes
.push_back(tree2_2
);
668 msg
->id
= tree2_1
.id
;
670 // Fire another load complete.
671 manager
->OnAccessibilityEvents(params
);
673 BrowserAccessibility
* acc2_2
= manager
->GetFromRendererID(3);
675 // Verify the root has changed.
676 EXPECT_NE(root
, manager
->GetRoot());
678 // And the new child exists.
679 EXPECT_EQ(ui::AX_ROLE_BUTTON
, acc2_2
->role());
680 EXPECT_EQ(3, acc2_2
->renderer_id());
682 // Ensure we properly cleaned up.
684 ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
687 TEST(BrowserAccessibilityManagerWinTest
, TestAccessibleHWND
) {
688 HWND desktop_hwnd
= GetDesktopWindow();
689 base::win::ScopedComPtr
<IAccessible
> desktop_hwnd_iaccessible
;
690 ASSERT_EQ(S_OK
, AccessibleObjectFromWindow(
691 desktop_hwnd
, OBJID_CLIENT
,
693 reinterpret_cast<void**>(desktop_hwnd_iaccessible
.Receive())));
695 scoped_ptr
<content::LegacyRenderWidgetHostHWND
> accessible_hwnd(
696 content::LegacyRenderWidgetHostHWND::Create(GetDesktopWindow()));
698 scoped_ptr
<BrowserAccessibilityManagerWin
> manager(
699 new BrowserAccessibilityManagerWin(
700 accessible_hwnd
.get(),
701 desktop_hwnd_iaccessible
,
702 BrowserAccessibilityManagerWin::GetEmptyDocument(),
704 ASSERT_EQ(desktop_hwnd
, manager
->parent_hwnd());
706 // Enabling screen reader support and calling MaybeCallNotifyWinEvent
707 // should trigger creating the AccessibleHWND, and we should now get a
708 // new parent_hwnd with the right window class to fool older screen
710 BrowserAccessibilityStateImpl::GetInstance()->OnScreenReaderDetected();
711 manager
->MaybeCallNotifyWinEvent(0, 0);
712 HWND new_parent_hwnd
= manager
->parent_hwnd();
713 ASSERT_NE(desktop_hwnd
, new_parent_hwnd
);
714 WCHAR hwnd_class_name
[256];
715 ASSERT_NE(0, GetClassName(new_parent_hwnd
, hwnd_class_name
, 256));
716 ASSERT_STREQ(L
"Chrome_RenderWidgetHostHWND", hwnd_class_name
);
718 // Destroy the hwnd explicitly; that should trigger clearing parent_hwnd().
719 DestroyWindow(new_parent_hwnd
);
720 ASSERT_EQ(NULL
, manager
->parent_hwnd());
722 // Now create it again.
723 accessible_hwnd
= content::LegacyRenderWidgetHostHWND::Create(
726 new BrowserAccessibilityManagerWin(
727 accessible_hwnd
.get(),
728 desktop_hwnd_iaccessible
,
729 BrowserAccessibilityManagerWin::GetEmptyDocument(),
731 manager
->MaybeCallNotifyWinEvent(0, 0);
732 new_parent_hwnd
= manager
->parent_hwnd();
733 ASSERT_FALSE(NULL
== new_parent_hwnd
);
735 // This time, destroy the manager first, make sure the AccessibleHWND doesn't
736 // crash on destruction (to be caught by SyzyASAN or other tools).
740 } // namespace content