diff options
author | tapted@chromium.org <tapted@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-12 12:03:48 +0000 |
---|---|---|
committer | tapted@chromium.org <tapted@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-12 12:05:27 +0000 |
commit | 0b7f85234f13b9e644cb63be4b560a26e0c09c2e (patch) | |
tree | f2e0695554513feb1c32f1d9410c59b8bc97da72 /ui | |
parent | 5f01d48039db9d83409290e3f2ebd5c159e97a9e (diff) | |
download | chromium_src-0b7f85234f13b9e644cb63be4b560a26e0c09c2e.zip chromium_src-0b7f85234f13b9e644cb63be4b560a26e0c09c2e.tar.gz chromium_src-0b7f85234f13b9e644cb63be4b560a26e0c09c2e.tar.bz2 |
Add ScopedObjCClassSwizzler in base/mac, absorbs objc_method_swizzle and ScopedClassSwizzler
ScopedClassSwizzler from ui/test is wanted for new tests where it can't
currently be accessed. It also re-implements a concept in
chrome/common/mac/objc_method_swizzle.*
This change adds base::mac::ScopedObjCClassSwizzler, merges concepts
from objc_method_swizzle, and adjusts chrome_browser_application_mac.mm
to use the new swizzler.
The test from objc_method_swizzle is adapted and extended for the scoped
swizzler.
BUG=378134
TEST=base_unittests
Review URL: https://codereview.chromium.org/345243007
Cr-Commit-Position: refs/heads/master@{#288943}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288943 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/base/cocoa/cocoa_base_utils_unittest.mm | 5 | ||||
-rw-r--r-- | ui/events/cocoa/events_mac_unittest.mm | 13 | ||||
-rw-r--r-- | ui/events/test/cocoa_test_event_utils.h | 14 | ||||
-rw-r--r-- | ui/events/test/cocoa_test_event_utils.mm | 16 | ||||
-rw-r--r-- | ui/views/test/event_generator_delegate_mac.mm | 6 |
5 files changed, 12 insertions, 42 deletions
diff --git a/ui/base/cocoa/cocoa_base_utils_unittest.mm b/ui/base/cocoa/cocoa_base_utils_unittest.mm index 5749e2d..f379120 100644 --- a/ui/base/cocoa/cocoa_base_utils_unittest.mm +++ b/ui/base/cocoa/cocoa_base_utils_unittest.mm @@ -6,6 +6,7 @@ #import <objc/objc-class.h> +#import "base/mac/scoped_objc_class_swizzler.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/platform_test.h" #include "ui/events/event_constants.h" @@ -39,8 +40,8 @@ TEST_F(CocoaBaseUtilsTest, WindowOpenDispositionFromNSEvent) { // Shift+Middle Click = new foreground tab. { - ScopedClassSwizzler swizzler([NSEvent class], [TestEvent class], - @selector(modifierFlags)); + base::mac::ScopedObjCClassSwizzler swizzler( + [NSEvent class], [TestEvent class], @selector(modifierFlags)); me = cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp, NSShiftKeyMask); EXPECT_EQ(NEW_FOREGROUND_TAB, WindowOpenDispositionFromNSEvent(me)); diff --git a/ui/events/cocoa/events_mac_unittest.mm b/ui/events/cocoa/events_mac_unittest.mm index 77f8549..f58cfd0 100644 --- a/ui/events/cocoa/events_mac_unittest.mm +++ b/ui/events/cocoa/events_mac_unittest.mm @@ -7,6 +7,7 @@ #import <Cocoa/Cocoa.h> #include "base/mac/scoped_cftyperef.h" +#import "base/mac/scoped_objc_class_swizzler.h" #include "base/memory/scoped_ptr.h" #include "testing/gtest/include/gtest/gtest.h" #include "ui/events/event_constants.h" @@ -32,7 +33,7 @@ NSWindow* g_test_window = nil; @end @implementation MiddleMouseButtonNumberDonor -- (NSUInteger)buttonNumber { return 2; } +- (NSInteger)buttonNumber { return 2; } @end @implementation TestWindowDonor @@ -57,7 +58,7 @@ class EventsMacTest : public CocoaTest { void SwizzleMiddleMouseButton() { DCHECK(!swizzler_); - swizzler_.reset(new ScopedClassSwizzler( + swizzler_.reset(new base::mac::ScopedObjCClassSwizzler( [NSEvent class], [MiddleMouseButtonNumberDonor class], @selector(buttonNumber))); @@ -67,10 +68,8 @@ class EventsMacTest : public CocoaTest { DCHECK(!g_test_window); DCHECK(!swizzler_); g_test_window = test_window(); - swizzler_.reset(new ScopedClassSwizzler( - [NSEvent class], - [TestWindowDonor class], - @selector(window))); + swizzler_.reset(new base::mac::ScopedObjCClassSwizzler( + [NSEvent class], [TestWindowDonor class], @selector(window))); } void ClearSwizzle() { @@ -117,7 +116,7 @@ class EventsMacTest : public CocoaTest { } private: - scoped_ptr<ScopedClassSwizzler> swizzler_; + scoped_ptr<base::mac::ScopedObjCClassSwizzler> swizzler_; DISALLOW_COPY_AND_ASSIGN(EventsMacTest); }; diff --git a/ui/events/test/cocoa_test_event_utils.h b/ui/events/test/cocoa_test_event_utils.h index 6c4994d..76c6a32 100644 --- a/ui/events/test/cocoa_test_event_utils.h +++ b/ui/events/test/cocoa_test_event_utils.h @@ -11,20 +11,6 @@ #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 diff --git a/ui/events/test/cocoa_test_event_utils.mm b/ui/events/test/cocoa_test_event_utils.mm index 4f0cb67..2694b84e 100644 --- a/ui/events/test/cocoa_test_event_utils.mm +++ b/ui/events/test/cocoa_test_event_utils.mm @@ -6,22 +6,6 @@ #include "ui/events/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); - if (!old_selector_impl_ && !new_selector_impl_) { - // Try class methods. - old_selector_impl_ = class_getClassMethod(target, selector); - new_selector_impl_ = class_getClassMethod(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, diff --git a/ui/views/test/event_generator_delegate_mac.mm b/ui/views/test/event_generator_delegate_mac.mm index d0e351a..db94d34 100644 --- a/ui/views/test/event_generator_delegate_mac.mm +++ b/ui/views/test/event_generator_delegate_mac.mm @@ -4,13 +4,13 @@ #import <Cocoa/Cocoa.h> +#import "base/mac/scoped_objc_class_swizzler.h" #include "base/memory/singleton.h" #include "ui/events/event_processor.h" #include "ui/events/event_target.h" #include "ui/events/event_target_iterator.h" #include "ui/events/event_targeter.h" #include "ui/events/test/event_generator.h" -#import "ui/events/test/cocoa_test_event_utils.h" #include "ui/gfx/mac/coordinate_conversion.h" namespace { @@ -293,7 +293,7 @@ class EventGeneratorDelegateMac : public ui::EventTarget, ui::test::EventGenerator* owner_; NSWindow* window_; - scoped_ptr<ScopedClassSwizzler> swizzle_pressed_; + scoped_ptr<base::mac::ScopedObjCClassSwizzler> swizzle_pressed_; DISALLOW_COPY_AND_ASSIGN(EventGeneratorDelegateMac); }; @@ -332,7 +332,7 @@ void EventGeneratorDelegateMac::SetContext(ui::test::EventGenerator* owner, owner_ = owner; window_ = window; if (owner_) { - swizzle_pressed_.reset(new ScopedClassSwizzler( + swizzle_pressed_.reset(new base::mac::ScopedObjCClassSwizzler( [NSEvent class], [NSEventDonor class], @selector(pressedMouseButtons))); |