Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(349)

Unified Diff: ui/views/widget/native_widget_mac_unittest.mm

Issue 2604793002: Allow Widget to pass accelerator handling to its parent.
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/views/widget/native_widget_mac_unittest.mm
diff --git a/ui/views/widget/native_widget_mac_unittest.mm b/ui/views/widget/native_widget_mac_unittest.mm
index 96f7f7bd425edd1affcf41b79e1fad202e3a66fa..dba47c25b9b0cc8361eda6c28dfdbcf596edfc3f 100644
--- a/ui/views/widget/native_widget_mac_unittest.mm
+++ b/ui/views/widget/native_widget_mac_unittest.mm
@@ -12,6 +12,7 @@
#import "base/mac/scoped_nsobject.h"
#import "base/mac/scoped_objc_class_swizzler.h"
#include "base/macros.h"
+#include "base/optional.h"
#include "base/run_loop.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
@@ -21,6 +22,7 @@
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#import "ui/base/cocoa/constrained_window/constrained_window_animation.h"
+#import "ui/base/cocoa/user_interface_item_command_handler.h"
#import "ui/base/cocoa/window_size_constants.h"
#import "ui/base/test/scoped_fake_full_keyboard_access.h"
#import "ui/events/test/cocoa_test_event_utils.h"
@@ -86,6 +88,18 @@
@property(assign, nonatomic) bool* deallocFlag;
@end
+@interface MockValidatedUserInterfaceItem
+ : NSObject<NSValidatedUserInterfaceItem>
+- (instancetype)initWithTagAndAction:(NSInteger)tag action:(SEL)action;
+@property(assign, nonatomic) base::Optional<int> handledById;
+@property(readonly) NSInteger tag;
+@property(readonly) SEL action;
+@end
+
+@interface MockCommandHandler : NSObject<UserInterfaceItemCommandHandler>
+- (instancetype)initWithId:(int)handlerId;
+@end
+
namespace views {
namespace test {
@@ -1807,6 +1821,117 @@ TEST_F(NativeWidgetMacViewsOrderTest, UnassociatedViewsIsAbove) {
]]));
}
+namespace {
+
+class TestNativeWidgetWithMockCommandHandler
+ : public TestWindowNativeWidgetMac {
+ public:
+ TestNativeWidgetWithMockCommandHandler(Widget* widget, int id)
+ : TestWindowNativeWidgetMac(widget), id_(id) {}
+
+ protected:
+ // TestWindowNativeWidgetMac:
+ NativeWidgetMacNSWindow* CreateNSWindow(
+ const Widget::InitParams& params) override {
+ NativeWidgetMacNSWindow* ns_window =
+ TestWindowNativeWidgetMac::CreateNSWindow(params);
+ [ns_window setCommandHandler:[[[MockCommandHandler alloc] initWithId:id_]
+ autorelease]];
+ return ns_window;
+ }
+
+ private:
+ int id_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestNativeWidgetWithMockCommandHandler);
+};
+
+} // namespace
+
+class NativeWidgetMacCommandHandlerTest : public WidgetTest {
+ public:
+ NativeWidgetMacCommandHandlerTest() = default;
+
+ protected:
+ void SetUp() override {
+ WidgetTest::SetUp();
+
+ parent_ = base::MakeUnique<Widget>();
+ Widget::InitParams params;
+ params.native_widget =
+ new TestNativeWidgetWithMockCommandHandler(parent_.get(), parent_id_);
+ params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
+ parent_->Init(params);
+ parent_->Show();
+ }
+
+ void TearDown() override {
+ parent_.reset();
+
+ WidgetTest::TearDown();
+ }
+
+ base::scoped_nsobject<MockValidatedUserInterfaceItem> CreateUserInterfaceItem(
+ NSInteger tag) {
+ base::scoped_nsobject<MockValidatedUserInterfaceItem> result(
+ [[MockValidatedUserInterfaceItem alloc]
+ initWithTagAndAction:tag
+ action:@selector(commandDispatch:)]);
+ return result;
+ }
+
+ const int parent_id_ = 0;
+ std::unique_ptr<Widget> parent_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(NativeWidgetMacCommandHandlerTest);
+};
+
+TEST_F(NativeWidgetMacCommandHandlerTest, CommandDispatch) {
+ auto item = CreateUserInterfaceItem(1);
+ id parent_native_window = parent_->GetNativeWindow();
+ ASSERT_TRUE(
+ [parent_native_window respondsToSelector:@selector(commandDispatch:)]);
+ [parent_native_window commandDispatch:item.get()];
+ EXPECT_EQ(parent_id_, [item.get() handledById]);
+}
+
+TEST_F(NativeWidgetMacCommandHandlerTest, CommandDispatchNoForwarding) {
+ std::unique_ptr<Widget> bubble(new Widget());
+ Widget::InitParams params;
+ params.native_widget = new TestWindowNativeWidgetMac(bubble.get());
+ params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
+ params.parent = parent_->GetNativeView();
+ params.pass_accelerator_to_parent = false;
+ bubble->Init(params);
+ bubble->Show();
+
+ auto item = CreateUserInterfaceItem(1);
+ id bubble_native_window = bubble->GetNativeWindow();
+ ASSERT_TRUE(
+ [bubble_native_window respondsToSelector:@selector(commandDispatch:)]);
+ [bubble_native_window commandDispatch:item.get()];
+ EXPECT_EQ(base::nullopt, [item.get() handledById]);
+}
+
+TEST_F(NativeWidgetMacCommandHandlerTest, CommandDispatchForwarding) {
+ std::unique_ptr<Widget> bubble(new Widget());
+ Widget::InitParams params;
+ params.native_widget = new TestWindowNativeWidgetMac(bubble.get());
+ params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
+ params.parent = parent_->GetNativeView();
+ params.pass_accelerator_to_parent = true;
+ bubble->Init(params);
+ bubble->Show();
+
+ auto item = CreateUserInterfaceItem(1);
+ id bubble_native_window = bubble->GetNativeWindow();
+ ASSERT_TRUE(
+ [bubble_native_window respondsToSelector:@selector(commandDispatch:)]);
+ [bubble_native_window commandDispatch:item.get()];
+ EXPECT_EQ(parent_id_, [item.get() handledById]);
+}
+
} // namespace test
} // namespace views
@@ -1869,3 +1994,48 @@ TEST_F(NativeWidgetMacViewsOrderTest, UnassociatedViewsIsAbove) {
}
@end
+
+@implementation MockValidatedUserInterfaceItem
+
+@synthesize handledById = handledById_;
+@synthesize tag = tag_;
+@synthesize action = action_;
+
+- (instancetype)initWithTagAndAction:(NSInteger)tag action:(SEL)action {
+ if ((self = [super init])) {
+ tag_ = tag;
+ action_ = action;
+ }
+ return self;
+}
+
+@end
+
+@implementation MockCommandHandler {
+ int id_;
+}
+
+- (instancetype)initWithId:(int)id {
+ if ((self = [super init])) {
+ id_ = id;
+ }
+ return self;
+}
+
+- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item
+ window:(NSWindow*)window {
+ if (item.action == @selector(commandDispatch:))
+ return YES;
+ return NO;
+}
+
+- (void)commandDispatch:(id)sender window:(NSWindow*)window {
+ if ([sender respondsToSelector:@selector(setHandledById:)])
+ [sender setHandledById:id_];
+}
+
+- (void)commandDispatchUsingKeyModifiers:(id)sender window:(NSWindow*)window {
+ NOTIMPLEMENTED();
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698