[email protected] | 208fadbf | 2012-02-21 08:37:12 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 020a50c | 2010-07-07 21:59:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #import <Cocoa/Cocoa.h> |
| 6 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 7 | #include "base/memory/scoped_ptr.h" |
[email protected] | 10994d13 | 2013-06-11 07:16:18 | [diff] [blame] | 8 | #include "base/strings/string_util.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
[email protected] | ba1fa65 | 2011-06-25 05:16:22 | [diff] [blame] | 10 | #include "content/browser/accessibility/browser_accessibility_cocoa.h" |
| 11 | #include "content/browser/accessibility/browser_accessibility_manager.h" |
[email protected] | a6120e3 | 2013-03-15 11:07:35 | [diff] [blame] | 12 | #include "content/browser/accessibility/browser_accessibility_manager_mac.h" |
[email protected] | 020a50c | 2010-07-07 21:59:06 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 70d5c18 | 2010-08-06 20:43:07 | [diff] [blame] | 14 | #import "testing/gtest_mac.h" |
[email protected] | 5651c58 | 2014-02-09 02:55:43 | [diff] [blame] | 15 | #import "ui/gfx/test/ui_cocoa_test_helper.h" |
[email protected] | 020a50c | 2010-07-07 21:59:06 | [diff] [blame] | 16 | |
[email protected] | e6b3487 | 2012-10-24 20:51:32 | [diff] [blame] | 17 | namespace content { |
[email protected] | 020a50c | 2010-07-07 21:59:06 | [diff] [blame] | 18 | |
[email protected] | 208fadbf | 2012-02-21 08:37:12 | [diff] [blame] | 19 | class BrowserAccessibilityTest : public ui::CocoaTest { |
[email protected] | 020a50c | 2010-07-07 21:59:06 | [diff] [blame] | 20 | public: |
dcheng | 6013533 | 2015-01-09 02:05:34 | [diff] [blame] | 21 | void SetUp() override { |
[email protected] | 020a50c | 2010-07-07 21:59:06 | [diff] [blame] | 22 | CocoaTest::SetUp(); |
[email protected] | 3911ab8 | 2013-06-21 05:15:26 | [diff] [blame] | 23 | RebuildAccessibilityTree(); |
| 24 | } |
| 25 | |
| 26 | protected: |
| 27 | void RebuildAccessibilityTree() { |
[email protected] | 5eec2f5 | 2014-01-06 22:30:54 | [diff] [blame] | 28 | ui::AXNodeData root; |
[email protected] | d3540c2 | 2011-08-17 23:12:07 | [diff] [blame] | 29 | root.id = 1000; |
[email protected] | 0aed2f5 | 2011-03-23 18:06:36 | [diff] [blame] | 30 | root.location.set_width(500); |
| 31 | root.location.set_height(100); |
[email protected] | 5eec2f5 | 2014-01-06 22:30:54 | [diff] [blame] | 32 | root.role = ui::AX_ROLE_ROOT_WEB_AREA; |
| 33 | root.AddStringAttribute(ui::AX_ATTR_HELP, "HelpText"); |
[email protected] | c47754096 | 2013-03-07 00:43:10 | [diff] [blame] | 34 | root.child_ids.push_back(1001); |
| 35 | root.child_ids.push_back(1002); |
[email protected] | 020a50c | 2010-07-07 21:59:06 | [diff] [blame] | 36 | |
[email protected] | 5eec2f5 | 2014-01-06 22:30:54 | [diff] [blame] | 37 | ui::AXNodeData child1; |
[email protected] | d3540c2 | 2011-08-17 23:12:07 | [diff] [blame] | 38 | child1.id = 1001; |
[email protected] | eecf89f0 | 2013-08-20 23:41:51 | [diff] [blame] | 39 | child1.SetName("Child1"); |
[email protected] | 0aed2f5 | 2011-03-23 18:06:36 | [diff] [blame] | 40 | child1.location.set_width(250); |
| 41 | child1.location.set_height(100); |
[email protected] | 5eec2f5 | 2014-01-06 22:30:54 | [diff] [blame] | 42 | child1.role = ui::AX_ROLE_BUTTON; |
[email protected] | 198d45d | 2010-07-27 00:20:56 | [diff] [blame] | 43 | |
[email protected] | 5eec2f5 | 2014-01-06 22:30:54 | [diff] [blame] | 44 | ui::AXNodeData child2; |
[email protected] | d3540c2 | 2011-08-17 23:12:07 | [diff] [blame] | 45 | child2.id = 1002; |
[email protected] | 0aed2f5 | 2011-03-23 18:06:36 | [diff] [blame] | 46 | child2.location.set_x(250); |
| 47 | child2.location.set_width(250); |
| 48 | child2.location.set_height(100); |
[email protected] | 5eec2f5 | 2014-01-06 22:30:54 | [diff] [blame] | 49 | child2.role = ui::AX_ROLE_HEADING; |
[email protected] | 198d45d | 2010-07-27 00:20:56 | [diff] [blame] | 50 | |
[email protected] | 0d7dad6 | 2010-10-19 21:18:50 | [diff] [blame] | 51 | manager_.reset( |
[email protected] | d96f384 | 2014-04-21 18:07:29 | [diff] [blame] | 52 | new BrowserAccessibilityManagerMac( |
[email protected] | 678e136 | 2014-04-30 05:47:34 | [diff] [blame] | 53 | nil, |
[email protected] | d96f384 | 2014-04-21 18:07:29 | [diff] [blame] | 54 | MakeAXTreeUpdate(root, child1, child2), |
| 55 | NULL)); |
[email protected] | cd3e2d90 | 2012-05-09 07:05:20 | [diff] [blame] | 56 | accessibility_.reset([manager_->GetRoot()->ToBrowserAccessibilityCocoa() |
[email protected] | 95b3f544 | 2012-05-06 17:10:07 | [diff] [blame] | 57 | retain]); |
[email protected] | 020a50c | 2010-07-07 21:59:06 | [diff] [blame] | 58 | } |
[email protected] | 198d45d | 2010-07-27 00:20:56 | [diff] [blame] | 59 | |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 60 | base::scoped_nsobject<BrowserAccessibilityCocoa> accessibility_; |
[email protected] | 0d7dad6 | 2010-10-19 21:18:50 | [diff] [blame] | 61 | scoped_ptr<BrowserAccessibilityManager> manager_; |
[email protected] | 020a50c | 2010-07-07 21:59:06 | [diff] [blame] | 62 | }; |
| 63 | |
[email protected] | 198d45d | 2010-07-27 00:20:56 | [diff] [blame] | 64 | // Standard hit test. |
[email protected] | 020a50c | 2010-07-07 21:59:06 | [diff] [blame] | 65 | TEST_F(BrowserAccessibilityTest, HitTestTest) { |
[email protected] | 1dbadbd | 2010-10-13 18:50:10 | [diff] [blame] | 66 | BrowserAccessibilityCocoa* firstChild = |
[email protected] | 020a50c | 2010-07-07 21:59:06 | [diff] [blame] | 67 | [accessibility_ accessibilityHitTest:NSMakePoint(50, 50)]; |
[email protected] | 70d5c18 | 2010-08-06 20:43:07 | [diff] [blame] | 68 | EXPECT_NSEQ(@"Child1", |
| 69 | [firstChild accessibilityAttributeValue:NSAccessibilityTitleAttribute]); |
[email protected] | 020a50c | 2010-07-07 21:59:06 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | 198d45d | 2010-07-27 00:20:56 | [diff] [blame] | 72 | // Test doing a hit test on the edge of a child. |
| 73 | TEST_F(BrowserAccessibilityTest, EdgeHitTest) { |
[email protected] | 1dbadbd | 2010-10-13 18:50:10 | [diff] [blame] | 74 | BrowserAccessibilityCocoa* firstChild = |
[email protected] | 58d0043f | 2013-07-13 04:30:46 | [diff] [blame] | 75 | [accessibility_ accessibilityHitTest:NSZeroPoint]; |
[email protected] | 70d5c18 | 2010-08-06 20:43:07 | [diff] [blame] | 76 | EXPECT_NSEQ(@"Child1", |
| 77 | [firstChild accessibilityAttributeValue:NSAccessibilityTitleAttribute]); |
[email protected] | 198d45d | 2010-07-27 00:20:56 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // This will test a hit test with invalid coordinates. It is assumed that |
| 81 | // the hit test has been narrowed down to this object or one of its children |
| 82 | // so it should return itself since it has no better hit result. |
| 83 | TEST_F(BrowserAccessibilityTest, InvalidHitTestCoordsTest) { |
[email protected] | 1dbadbd | 2010-10-13 18:50:10 | [diff] [blame] | 84 | BrowserAccessibilityCocoa* hitTestResult = |
[email protected] | 198d45d | 2010-07-27 00:20:56 | [diff] [blame] | 85 | [accessibility_ accessibilityHitTest:NSMakePoint(-50, 50)]; |
[email protected] | 70d5c18 | 2010-08-06 20:43:07 | [diff] [blame] | 86 | EXPECT_NSEQ(accessibility_, hitTestResult); |
[email protected] | 198d45d | 2010-07-27 00:20:56 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Test to ensure querying standard attributes works. |
[email protected] | 16a3356e | 2013-10-18 13:56:57 | [diff] [blame] | 90 | TEST_F(BrowserAccessibilityTest, BasicAttributeTest) { |
[email protected] | 020a50c | 2010-07-07 21:59:06 | [diff] [blame] | 91 | NSString* helpText = [accessibility_ |
| 92 | accessibilityAttributeValue:NSAccessibilityHelpAttribute]; |
[email protected] | 70d5c18 | 2010-08-06 20:43:07 | [diff] [blame] | 93 | EXPECT_NSEQ(@"HelpText", helpText); |
[email protected] | 020a50c | 2010-07-07 21:59:06 | [diff] [blame] | 94 | } |
[email protected] | 198d45d | 2010-07-27 00:20:56 | [diff] [blame] | 95 | |
| 96 | // Test querying for an invalid attribute to ensure it doesn't crash. |
| 97 | TEST_F(BrowserAccessibilityTest, InvalidAttributeTest) { |
| 98 | NSString* shouldBeNil = [accessibility_ |
| 99 | accessibilityAttributeValue:@"NSAnInvalidAttribute"]; |
| 100 | EXPECT_TRUE(shouldBeNil == nil); |
| 101 | } |
[email protected] | e6b3487 | 2012-10-24 20:51:32 | [diff] [blame] | 102 | |
[email protected] | 3911ab8 | 2013-06-21 05:15:26 | [diff] [blame] | 103 | TEST_F(BrowserAccessibilityTest, RetainedDetachedObjectsReturnNil) { |
| 104 | // Get the first child. |
| 105 | BrowserAccessibilityCocoa* retainedFirstChild = |
| 106 | [accessibility_ accessibilityHitTest:NSMakePoint(50, 50)]; |
| 107 | EXPECT_NSEQ(@"Child1", [retainedFirstChild |
| 108 | accessibilityAttributeValue:NSAccessibilityTitleAttribute]); |
| 109 | |
| 110 | // Retain it. This simulates what the system might do with an |
| 111 | // accessibility object. |
| 112 | [retainedFirstChild retain]; |
| 113 | |
| 114 | // Rebuild the accessibility tree, which should detach |retainedFirstChild|. |
| 115 | RebuildAccessibilityTree(); |
| 116 | |
| 117 | // Now any attributes we query should return nil. |
| 118 | EXPECT_EQ(nil, [retainedFirstChild |
| 119 | accessibilityAttributeValue:NSAccessibilityTitleAttribute]); |
| 120 | |
| 121 | // Don't leak memory in the test. |
| 122 | [retainedFirstChild release]; |
| 123 | } |
| 124 | |
[email protected] | e6b3487 | 2012-10-24 20:51:32 | [diff] [blame] | 125 | } // namespace content |