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

Unified Diff: ui/views/cocoa/bridged_native_widget_interactive_uitest.mm

Issue 1146873002: [MacViews] Enable dragging a window by its caption/draggable areas. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Change to single repost and [NSWindow setMovableByWindowBackground]. Created 5 years, 7 months 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/cocoa/bridged_native_widget_interactive_uitest.mm
diff --git a/ui/views/cocoa/bridged_native_widget_interactive_uitest.mm b/ui/views/cocoa/bridged_native_widget_interactive_uitest.mm
index b0962fb9a27116e7ee142f1f5933445a873b23ff..aabc8c0cbf5bd919ddafe38cf1c8ac9317e95bcc 100644
--- a/ui/views/cocoa/bridged_native_widget_interactive_uitest.mm
+++ b/ui/views/cocoa/bridged_native_widget_interactive_uitest.mm
@@ -9,7 +9,51 @@
#import "base/mac/mac_util.h"
#import "base/mac/sdk_forward_declarations.h"
#import "ui/base/test/nswindow_fullscreen_notification_waiter.h"
+#include "ui/base/hit_test.h"
+#include "ui/events/test/cocoa_test_event_utils.h"
#include "ui/views/test/widget_test.h"
+#include "ui/views/window/native_frame_view.h"
+
+// Watches for NSNotifications from the default notification center.
+@interface WindowedNSNotificationObserver : NSObject {
jackhou1 2015/06/02 06:33:09 This can probably be merged with the one in app_sh
tapted 2015/06/03 00:28:37 are they both called WindowedNSNotificationObserve
jackhou1 2015/06/03 04:04:44 Nope, this one is in macviews_interactive_ui_tests
tapted 2015/06/03 07:04:48 oh whoops - nope. #wirescrossed
+ @private
+ BOOL notificationReceived_;
+ scoped_ptr<base::RunLoop> runLoop_;
+}
+
+- (id)initForNotification:(NSString*)name;
+- (void)observe:(NSNotification*)notification;
+- (void)wait;
+@end
+
+@implementation WindowedNSNotificationObserver
+
+- (id)initForNotification:(NSString*)name {
+ if (self = [super init]) {
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(observe:)
+ name:name
+ object:nil];
+ }
+ return self;
+}
+
+- (void)observe:(NSNotification*)notification {
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
+ notificationReceived_ = YES;
+ if (runLoop_.get())
+ runLoop_->Quit();
+}
+
+- (void)wait {
+ if (notificationReceived_)
+ return;
+
+ runLoop_.reset(new base::RunLoop);
+ runLoop_->Run();
+}
+
+@end
namespace views {
tapted 2015/06/03 00:28:37 add namespace test { after this (i.e. whole file s
jackhou1 2015/06/03 04:04:44 Done.
@@ -144,4 +188,144 @@ TEST_F(BridgedNativeWidgetUITest, FullscreenEnterAndExit) {
EXPECT_EQ(restored_bounds, widget_->GetRestoredBounds());
}
+namespace {
+
+// This is used to wait for reposted events to be seen. We can't just use
+// RunPendingMessages() because CGEventPost might not be synchronous.
+class HitTestBridgedNativeWidget : public BridgedNativeWidget {
+ public:
+ HitTestBridgedNativeWidget(NativeWidgetMac* widget)
+ : BridgedNativeWidget(widget) {}
+
+ bool ShouldRepostPendingLeftMouseDown(NSPoint location_in_window) override {
+ bool draggable_before = [ns_view() mouseDownCanMoveWindow];
+ bool should_repost = BridgedNativeWidget::ShouldRepostPendingLeftMouseDown(
+ location_in_window);
+ bool draggable_after = [ns_view() mouseDownCanMoveWindow];
+
+ if (run_loop_.get() && draggable_before && !draggable_after)
+ run_loop_->Quit();
+
+ return should_repost;
+ }
+
+ void WaitForRepost() {
+ run_loop_.reset(new base::RunLoop);
+ run_loop_->Run();
+ }
+
+ private:
+ scoped_ptr<base::RunLoop> run_loop_;
+};
+
+// This is used to return a customized result to NonClientHitTest.
+class HitTestNonClientFrameView : public NativeFrameView {
+ public:
+ HitTestNonClientFrameView(Widget* widget)
+ : NativeFrameView(widget), hit_test_result_(HTNOWHERE) {}
+
+ // NonClientFrameView overrides:
+ int NonClientHitTest(const gfx::Point& point) override {
+ return hit_test_result_;
+ }
+
+ void set_hit_test_result(int component) { hit_test_result_ = component; }
+
+ private:
+ int hit_test_result_;
+};
+
+} // namespace
+
+// This is used to inject test versions of NativeFrameView and
+// BridgedNativeWidget.
+// Outside anonymous namespace so NativeWidgetMac can friend this.
+class HitTestNativeWidgetMac : public NativeWidgetMac {
+ public:
+ HitTestNativeWidgetMac(internal::NativeWidgetDelegate* delegate,
+ NativeFrameView* native_frame_view)
+ : NativeWidgetMac(delegate), native_frame_view_(native_frame_view) {
+ NativeWidgetMac::bridge_.reset(new HitTestBridgedNativeWidget(this));
+ }
+
+ HitTestBridgedNativeWidget* bridge() {
+ return static_cast<HitTestBridgedNativeWidget*>(
+ NativeWidgetMac::bridge_.get());
+ }
+
+ // internal::NativeWidgetPrivate:
+ NonClientFrameView* CreateNonClientFrameView() override {
+ return native_frame_view_;
+ }
+
+ private:
+ // Owned by Widget.
+ NativeFrameView* native_frame_view_;
+};
+
+TEST_F(BridgedNativeWidgetUITest, HitTest) {
+ Widget widget;
+ HitTestNonClientFrameView* frame_view =
+ new HitTestNonClientFrameView(&widget);
+ HitTestNativeWidgetMac* native_widget =
+ new HitTestNativeWidgetMac(&widget, frame_view);
+ Widget::InitParams init_params =
+ CreateParams(Widget::InitParams::TYPE_WINDOW);
+ init_params.native_widget = native_widget;
+ init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
+ init_params.bounds = gfx::Rect(100, 100, 400, 300);
+ widget.Init(init_params);
+ widget.Show();
+ RunPendingMessages();
+
+ frame_view->set_hit_test_result(HTCAPTION);
+ {
+ EXPECT_EQ(100, [widget.GetNativeWindow() frame].origin.x);
+
+ NSEvent* mouse_down = cocoa_test_event_utils::LeftMouseDownAtPointInWindow(
+ NSMakePoint(10, 10), widget.GetNativeWindow());
+ CGEventPost(kCGSessionEventTap, [mouse_down CGEvent]);
+ native_widget->bridge()->WaitForRepost();
+
+ base::scoped_nsobject<WindowedNSNotificationObserver> ns_observer(
+ [[WindowedNSNotificationObserver alloc]
+ initForNotification:NSWindowDidMoveNotification]);
+ NSEvent* mouse_drag = cocoa_test_event_utils::MouseEventAtPointInWindow(
+ NSMakePoint(110, 110), NSLeftMouseDragged, widget.GetNativeWindow(),
+ 1u);
+ CGEventPost(kCGSessionEventTap, [mouse_drag CGEvent]);
+ [ns_observer wait];
+ EXPECT_EQ(200, [widget.GetNativeWindow() frame].origin.x);
+
+ NSEvent* mouse_up = cocoa_test_event_utils::MouseEventAtPointInWindow(
+ NSMakePoint(110, 110), NSLeftMouseUp, widget.GetNativeWindow(), 2u);
+ CGEventPost(kCGSessionEventTap, [mouse_up CGEvent]);
+ RunPendingMessages();
+ EXPECT_EQ(200, [widget.GetNativeWindow() frame].origin.x);
+ }
+
+ frame_view->set_hit_test_result(HTCLIENT);
+ {
+ EXPECT_EQ(200, [widget.GetNativeWindow() frame].origin.x);
+
+ NSEvent* mouse_down = cocoa_test_event_utils::LeftMouseDownAtPointInWindow(
+ NSMakePoint(10, 10), widget.GetNativeWindow());
+ CGEventPost(kCGSessionEventTap, [mouse_down CGEvent]);
+ RunPendingMessages();
jackhou1 2015/06/02 06:33:09 I suspect these are not actually waiting long enou
tapted 2015/06/03 00:28:37 hm.... for these kind of events, maybe not without
jackhou1 2015/06/03 04:04:44 Just ended up using a local event monitor. I had
+
+ NSEvent* mouse_drag = cocoa_test_event_utils::MouseEventAtPointInWindow(
+ NSMakePoint(110, 110), NSLeftMouseDragged, widget.GetNativeWindow(),
+ 3u);
+ CGEventPost(kCGSessionEventTap, [mouse_drag CGEvent]);
+ RunPendingMessages();
+ EXPECT_EQ(200, [widget.GetNativeWindow() frame].origin.x);
+
+ NSEvent* mouse_up = cocoa_test_event_utils::MouseEventAtPointInWindow(
+ NSMakePoint(110, 110), NSLeftMouseUp, widget.GetNativeWindow(), 4u);
+ CGEventPost(kCGSessionEventTap, [mouse_up CGEvent]);
+ RunPendingMessages();
+ EXPECT_EQ(200, [widget.GetNativeWindow() frame].origin.x);
+ }
+}
+
} // namespace views

Powered by Google App Engine
This is Rietveld 408576698