• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) 2011 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#import <Cocoa/Cocoa.h>
6
7#include "base/memory/scoped_ptr.h"
8#include "base/string_util.h"
9#include "base/utf_string_conversions.h"
10#include "chrome/browser/accessibility/browser_accessibility_cocoa.h"
11#include "chrome/browser/accessibility/browser_accessibility_manager.h"
12#include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
13#include "testing/gtest/include/gtest/gtest.h"
14#import "testing/gtest_mac.h"
15
16@interface MockAccessibilityDelegate :
17    NSView<BrowserAccessibilityDelegateCocoa>
18
19- (NSPoint)accessibilityPointInScreen:(BrowserAccessibilityCocoa*)accessibility;
20- (void)doDefaultAction:(int32)accessibilityObjectId;
21- (void)setAccessibilityFocus:(BOOL)focus
22              accessibilityId:(int32)accessibilityObjectId;
23- (NSWindow*)window;
24
25@end
26
27@implementation MockAccessibilityDelegate
28
29- (NSPoint)accessibilityPointInScreen:
30    (BrowserAccessibilityCocoa*)accessibility {
31  return NSZeroPoint;
32}
33- (void)doDefaultAction:(int32)accessibilityObjectId {
34}
35- (void)setAccessibilityFocus:(BOOL)focus
36              accessibilityId:(int32)accessibilityObjectId {
37}
38- (NSWindow*)window {
39  return nil;
40}
41
42@end
43
44
45class BrowserAccessibilityTest : public CocoaTest {
46 public:
47  virtual void SetUp() {
48    CocoaTest::SetUp();
49    WebAccessibility root;
50    root.location.set_width(500);
51    root.location.set_height(100);
52    root.role = WebAccessibility::ROLE_WEB_AREA;
53    root.attributes[WebAccessibility::ATTR_HELP] = ASCIIToUTF16("HelpText");
54
55    WebAccessibility child1;
56    child1.name = ASCIIToUTF16("Child1");
57    child1.location.set_width(250);
58    child1.location.set_height(100);
59    child1.role = WebAccessibility::ROLE_BUTTON;
60
61    WebAccessibility child2;
62    child2.location.set_x(250);
63    child2.location.set_width(250);
64    child2.location.set_height(100);
65    child2.role = WebAccessibility::ROLE_HEADING;
66
67    root.children.push_back(child1);
68    root.children.push_back(child2);
69
70    delegate_.reset([[MockAccessibilityDelegate alloc] init]);
71    manager_.reset(
72        BrowserAccessibilityManager::Create(delegate_, root, NULL));
73    accessibility_.reset([manager_->GetRoot()->toBrowserAccessibilityCocoa()
74        retain]);
75  }
76
77 protected:
78  scoped_nsobject<MockAccessibilityDelegate> delegate_;
79  scoped_nsobject<BrowserAccessibilityCocoa> accessibility_;
80  scoped_ptr<BrowserAccessibilityManager> manager_;
81};
82
83// Standard hit test.
84TEST_F(BrowserAccessibilityTest, HitTestTest) {
85  BrowserAccessibilityCocoa* firstChild =
86      [accessibility_ accessibilityHitTest:NSMakePoint(50, 50)];
87  EXPECT_NSEQ(@"Child1",
88      [firstChild accessibilityAttributeValue:NSAccessibilityTitleAttribute]);
89}
90
91// Test doing a hit test on the edge of a child.
92TEST_F(BrowserAccessibilityTest, EdgeHitTest) {
93  BrowserAccessibilityCocoa* firstChild =
94      [accessibility_ accessibilityHitTest:NSMakePoint(0, 0)];
95  EXPECT_NSEQ(@"Child1",
96      [firstChild accessibilityAttributeValue:NSAccessibilityTitleAttribute]);
97}
98
99// This will test a hit test with invalid coordinates.  It is assumed that
100// the hit test has been narrowed down to this object or one of its children
101// so it should return itself since it has no better hit result.
102TEST_F(BrowserAccessibilityTest, InvalidHitTestCoordsTest) {
103  BrowserAccessibilityCocoa* hitTestResult =
104      [accessibility_ accessibilityHitTest:NSMakePoint(-50, 50)];
105  EXPECT_NSEQ(accessibility_, hitTestResult);
106}
107
108// Test to ensure querying standard attributes works.
109TEST_F(BrowserAccessibilityTest, BasicAttributeTest) {
110  NSString* helpText = [accessibility_
111      accessibilityAttributeValue:NSAccessibilityHelpAttribute];
112  EXPECT_NSEQ(@"HelpText", helpText);
113}
114
115// Test querying for an invalid attribute to ensure it doesn't crash.
116TEST_F(BrowserAccessibilityTest, InvalidAttributeTest) {
117  NSString* shouldBeNil = [accessibility_
118      accessibilityAttributeValue:@"NSAnInvalidAttribute"];
119  EXPECT_TRUE(shouldBeNil == nil);
120}
121