diff options
author | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-09 20:07:17 +0000 |
---|---|---|
committer | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-09 20:07:17 +0000 |
commit | be33ec5b288e33f16837321ca749b1a3e1cfed5e (patch) | |
tree | 3d5efd247d09b464b8725aff3d133ace136ddcc5 /ui/base | |
parent | 1e8187453cbfeab13f1d229c91b66b2b2201a578 (diff) | |
download | chromium_src-be33ec5b288e33f16837321ca749b1a3e1cfed5e.zip chromium_src-be33ec5b288e33f16837321ca749b1a3e1cfed5e.tar.gz chromium_src-be33ec5b288e33f16837321ca749b1a3e1cfed5e.tar.bz2 |
Move TestEventUtils from chrome to ui.
BUG=95573
TEST=no change
Review URL: http://codereview.chromium.org/7796008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@100474 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base')
-rw-r--r-- | ui/base/test/cocoa_test_event_utils.h | 48 | ||||
-rw-r--r-- | ui/base/test/cocoa_test_event_utils.mm | 86 | ||||
-rw-r--r-- | ui/base/test/ui_cocoa_test_helper.h | 6 |
3 files changed, 137 insertions, 3 deletions
diff --git a/ui/base/test/cocoa_test_event_utils.h b/ui/base/test/cocoa_test_event_utils.h new file mode 100644 index 0000000..2d6e7d0 --- /dev/null +++ b/ui/base/test/cocoa_test_event_utils.h @@ -0,0 +1,48 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef UI_BASE_TEST_COCOA_TEST_EVENT_UTILS_H_ +#define UI_BASE_TEST_COCOA_TEST_EVENT_UTILS_H_ +#pragma once + +#include <utility> + +#import <objc/objc-class.h> + +#include "base/basictypes.h" + +// Within a given scope, replace the selector |selector| on |target| with that +// from |source|. +class ScopedClassSwizzler { + public: + ScopedClassSwizzler(Class target, Class source, SEL selector); + ~ScopedClassSwizzler(); + + private: + Method old_selector_impl_; + Method new_selector_impl_; + + DISALLOW_COPY_AND_ASSIGN(ScopedClassSwizzler); +}; + +namespace cocoa_test_event_utils { + +// Create synthetic mouse events for testing. Currently these are very +// basic, flesh out as needed. Points are all in window coordinates; +// where the window is not specified, coordinate system is undefined +// (but will be repeated when the event is queried). +NSEvent* MakeMouseEvent(NSEventType type, NSUInteger modifiers); +NSEvent* MouseEventAtPoint(NSPoint point, NSEventType type, + NSUInteger modifiers); +NSEvent* LeftMouseDownAtPoint(NSPoint point); +NSEvent* LeftMouseDownAtPointInWindow(NSPoint point, NSWindow* window); + +// Return a mouse down and an up event with the given |clickCount| at +// |view|'s midpoint. +std::pair<NSEvent*, NSEvent*> MouseClickInView(NSView* view, + NSUInteger clickCount); + +} // namespace cocoa_test_event_utils + +#endif // UI_BASE_TEST_COCOA_TEST_EVENT_UTILS_H_ diff --git a/ui/base/test/cocoa_test_event_utils.mm b/ui/base/test/cocoa_test_event_utils.mm new file mode 100644 index 0000000..5fe5f99 --- /dev/null +++ b/ui/base/test/cocoa_test_event_utils.mm @@ -0,0 +1,86 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import <Cocoa/Cocoa.h> + +#include "ui/base/test/cocoa_test_event_utils.h" + +ScopedClassSwizzler::ScopedClassSwizzler(Class target, Class source, + SEL selector) { + old_selector_impl_ = class_getInstanceMethod(target, selector); + new_selector_impl_ = class_getInstanceMethod(source, selector); + method_exchangeImplementations(old_selector_impl_, new_selector_impl_); +} + +ScopedClassSwizzler::~ScopedClassSwizzler() { + method_exchangeImplementations(old_selector_impl_, new_selector_impl_); +} + +namespace cocoa_test_event_utils { + +NSEvent* MouseEventAtPoint(NSPoint point, NSEventType type, + NSUInteger modifiers) { + if (type == NSOtherMouseUp) { + // To synthesize middle clicks we need to create a CGEvent with the + // "center" button flags so that our resulting NSEvent will have the + // appropriate buttonNumber field. NSEvent provides no way to create a + // mouse event with a buttonNumber directly. + CGPoint location = { point.x, point.y }; + CGEventRef cg_event = CGEventCreateMouseEvent(NULL, kCGEventOtherMouseUp, + location, + kCGMouseButtonCenter); + NSEvent* event = [NSEvent eventWithCGEvent:cg_event]; + CFRelease(cg_event); + return event; + } + return [NSEvent mouseEventWithType:type + location:point + modifierFlags:modifiers + timestamp:0 + windowNumber:0 + context:nil + eventNumber:0 + clickCount:1 + pressure:1.0]; +} + +NSEvent* MakeMouseEvent(NSEventType type, NSUInteger modifiers) { + return MouseEventAtPoint(NSMakePoint(0, 0), type, modifiers); +} + +static NSEvent* MouseEventAtPointInWindow(NSPoint point, + NSEventType type, + NSWindow* window, + NSUInteger clickCount) { + return [NSEvent mouseEventWithType:type + location:point + modifierFlags:0 + timestamp:0 + windowNumber:[window windowNumber] + context:nil + eventNumber:0 + clickCount:clickCount + pressure:1.0]; +} + +NSEvent* LeftMouseDownAtPointInWindow(NSPoint point, NSWindow* window) { + return MouseEventAtPointInWindow(point, NSLeftMouseDown, window, 1); +} + +NSEvent* LeftMouseDownAtPoint(NSPoint point) { + return LeftMouseDownAtPointInWindow(point, nil); +} + +std::pair<NSEvent*,NSEvent*> MouseClickInView(NSView* view, + NSUInteger clickCount) { + const NSRect bounds = [view convertRect:[view bounds] toView:nil]; + const NSPoint mid_point = NSMakePoint(NSMidX(bounds), NSMidY(bounds)); + NSEvent* down = MouseEventAtPointInWindow(mid_point, NSLeftMouseDown, + [view window], clickCount); + NSEvent* up = MouseEventAtPointInWindow(mid_point, NSLeftMouseUp, + [view window], clickCount); + return std::make_pair(down, up); +} + +} // namespace cocoa_test_event_utils diff --git a/ui/base/test/ui_cocoa_test_helper.h b/ui/base/test/ui_cocoa_test_helper.h index 925a3a8..9b1c513 100644 --- a/ui/base/test/ui_cocoa_test_helper.h +++ b/ui/base/test/ui_cocoa_test_helper.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef UI_BASE_TEST_BASE_UI_COCOA_TEST_HELPER_H_ -#define UI_BASE_TEST_BASE_UI_COCOA_TEST_HELPER_H_ +#ifndef UI_BASE_TEST_UI_COCOA_TEST_HELPER_H_ +#define UI_BASE_TEST_UI_COCOA_TEST_HELPER_H_ #pragma once #include <set> @@ -146,4 +146,4 @@ class CocoaTest : public PlatformTest { [NSStringFromRect(expected) UTF8String] << \ " != " << [NSStringFromRect(actual) UTF8String] -#endif // UI_BASE_TEST_BASE_UI_COCOA_TEST_HELPER_H_ +#endif // UI_BASE_TEST_UI_COCOA_TEST_HELPER_H_ |