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

Side by Side Diff: ui/views/widget/native_widget_mac_unittest.mm

Issue 2604793002: Allow Widget to pass accelerator handling to its parent.
Patch Set: Created 3 years, 11 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "ui/views/widget/native_widget_mac.h" 5 #import "ui/views/widget/native_widget_mac.h"
6 6
7 #import <Cocoa/Cocoa.h> 7 #import <Cocoa/Cocoa.h>
8 8
9 #import "base/mac/foundation_util.h" 9 #import "base/mac/foundation_util.h"
10 #include "base/mac/mac_util.h" 10 #include "base/mac/mac_util.h"
11 #import "base/mac/scoped_nsautorelease_pool.h" 11 #import "base/mac/scoped_nsautorelease_pool.h"
12 #import "base/mac/scoped_nsobject.h" 12 #import "base/mac/scoped_nsobject.h"
13 #import "base/mac/scoped_objc_class_swizzler.h" 13 #import "base/mac/scoped_objc_class_swizzler.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/optional.h"
15 #include "base/run_loop.h" 16 #include "base/run_loop.h"
16 #include "base/strings/sys_string_conversions.h" 17 #include "base/strings/sys_string_conversions.h"
17 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
18 #include "base/test/test_timeouts.h" 19 #include "base/test/test_timeouts.h"
19 #include "base/threading/thread_task_runner_handle.h" 20 #include "base/threading/thread_task_runner_handle.h"
20 #import "testing/gtest_mac.h" 21 #import "testing/gtest_mac.h"
21 #include "third_party/skia/include/core/SkBitmap.h" 22 #include "third_party/skia/include/core/SkBitmap.h"
22 #include "third_party/skia/include/core/SkCanvas.h" 23 #include "third_party/skia/include/core/SkCanvas.h"
23 #import "ui/base/cocoa/constrained_window/constrained_window_animation.h" 24 #import "ui/base/cocoa/constrained_window/constrained_window_animation.h"
25 #import "ui/base/cocoa/user_interface_item_command_handler.h"
24 #import "ui/base/cocoa/window_size_constants.h" 26 #import "ui/base/cocoa/window_size_constants.h"
25 #import "ui/base/test/scoped_fake_full_keyboard_access.h" 27 #import "ui/base/test/scoped_fake_full_keyboard_access.h"
26 #import "ui/events/test/cocoa_test_event_utils.h" 28 #import "ui/events/test/cocoa_test_event_utils.h"
27 #include "ui/events/test/event_generator.h" 29 #include "ui/events/test/event_generator.h"
28 #import "ui/gfx/mac/coordinate_conversion.h" 30 #import "ui/gfx/mac/coordinate_conversion.h"
29 #include "ui/views/bubble/bubble_dialog_delegate.h" 31 #include "ui/views/bubble/bubble_dialog_delegate.h"
30 #import "ui/views/cocoa/bridged_content_view.h" 32 #import "ui/views/cocoa/bridged_content_view.h"
31 #import "ui/views/cocoa/bridged_native_widget.h" 33 #import "ui/views/cocoa/bridged_native_widget.h"
32 #import "ui/views/cocoa/native_widget_mac_nswindow.h" 34 #import "ui/views/cocoa/native_widget_mac_nswindow.h"
33 #include "ui/views/controls/button/label_button.h" 35 #include "ui/views/controls/button/label_button.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 @property(assign, nonatomic) NSRect lastDirtyRect; 81 @property(assign, nonatomic) NSRect lastDirtyRect;
80 @end 82 @end
81 83
82 @interface FocusableTestNSView : NSView 84 @interface FocusableTestNSView : NSView
83 @end 85 @end
84 86
85 @interface TestNativeParentWindow : NSWindow 87 @interface TestNativeParentWindow : NSWindow
86 @property(assign, nonatomic) bool* deallocFlag; 88 @property(assign, nonatomic) bool* deallocFlag;
87 @end 89 @end
88 90
91 @interface MockValidatedUserInterfaceItem
92 : NSObject<NSValidatedUserInterfaceItem>
93 - (instancetype)initWithTagAndAction:(NSInteger)tag action:(SEL)action;
94 @property(assign, nonatomic) base::Optional<int> handledById;
95 @property(readonly) NSInteger tag;
96 @property(readonly) SEL action;
97 @end
98
99 @interface MockCommandHandler : NSObject<UserInterfaceItemCommandHandler>
100 - (instancetype)initWithId:(int)handlerId;
101 @end
102
89 namespace views { 103 namespace views {
90 namespace test { 104 namespace test {
91 105
92 // BridgedNativeWidget friend to access private members. 106 // BridgedNativeWidget friend to access private members.
93 class BridgedNativeWidgetTestApi { 107 class BridgedNativeWidgetTestApi {
94 public: 108 public:
95 explicit BridgedNativeWidgetTestApi(NSWindow* window) { 109 explicit BridgedNativeWidgetTestApi(NSWindow* window) {
96 bridge_ = NativeWidgetMac::GetBridgeForNativeWindow(window); 110 bridge_ = NativeWidgetMac::GetBridgeForNativeWindow(window);
97 } 111 }
98 112
(...skipping 1701 matching lines...) Expand 10 before | Expand all | Expand 10 after
1800 hosts_[2]->view(), child_view 1814 hosts_[2]->view(), child_view
1801 ]])); 1815 ]]));
1802 1816
1803 native_host_parent_->ReorderChildView(hosts_[2]->host(), 1); 1817 native_host_parent_->ReorderChildView(hosts_[2]->host(), 1);
1804 EXPECT_TRUE(([[GetContentNativeView() subviews] isEqualToArray:@[ 1818 EXPECT_TRUE(([[GetContentNativeView() subviews] isEqualToArray:@[
1805 compositor_view_, hosts_[0]->view(), hosts_[2]->view(), 1819 compositor_view_, hosts_[0]->view(), hosts_[2]->view(),
1806 hosts_[1]->view(), child_view 1820 hosts_[1]->view(), child_view
1807 ]])); 1821 ]]));
1808 } 1822 }
1809 1823
1824 namespace {
1825
1826 class TestNativeWidgetWithMockCommandHandler
1827 : public TestWindowNativeWidgetMac {
1828 public:
1829 TestNativeWidgetWithMockCommandHandler(Widget* widget, int id)
1830 : TestWindowNativeWidgetMac(widget), id_(id) {}
1831
1832 protected:
1833 // TestWindowNativeWidgetMac:
1834 NativeWidgetMacNSWindow* CreateNSWindow(
1835 const Widget::InitParams& params) override {
1836 NativeWidgetMacNSWindow* ns_window =
1837 TestWindowNativeWidgetMac::CreateNSWindow(params);
1838 [ns_window setCommandHandler:[[[MockCommandHandler alloc] initWithId:id_]
1839 autorelease]];
1840 return ns_window;
1841 }
1842
1843 private:
1844 int id_;
1845
1846 DISALLOW_COPY_AND_ASSIGN(TestNativeWidgetWithMockCommandHandler);
1847 };
1848
1849 } // namespace
1850
1851 class NativeWidgetMacCommandHandlerTest : public WidgetTest {
1852 public:
1853 NativeWidgetMacCommandHandlerTest() = default;
1854
1855 protected:
1856 void SetUp() override {
1857 WidgetTest::SetUp();
1858
1859 parent_ = base::MakeUnique<Widget>();
1860 Widget::InitParams params;
1861 params.native_widget =
1862 new TestNativeWidgetWithMockCommandHandler(parent_.get(), parent_id_);
1863 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1864 parent_->Init(params);
1865 parent_->Show();
1866 }
1867
1868 void TearDown() override {
1869 parent_.reset();
1870
1871 WidgetTest::TearDown();
1872 }
1873
1874 base::scoped_nsobject<MockValidatedUserInterfaceItem> CreateUserInterfaceItem(
1875 NSInteger tag) {
1876 base::scoped_nsobject<MockValidatedUserInterfaceItem> result(
1877 [[MockValidatedUserInterfaceItem alloc]
1878 initWithTagAndAction:tag
1879 action:@selector(commandDispatch:)]);
1880 return result;
1881 }
1882
1883 const int parent_id_ = 0;
1884 std::unique_ptr<Widget> parent_;
1885
1886 private:
1887 DISALLOW_COPY_AND_ASSIGN(NativeWidgetMacCommandHandlerTest);
1888 };
1889
1890 TEST_F(NativeWidgetMacCommandHandlerTest, CommandDispatch) {
1891 auto item = CreateUserInterfaceItem(1);
1892 id parent_native_window = parent_->GetNativeWindow();
1893 ASSERT_TRUE(
1894 [parent_native_window respondsToSelector:@selector(commandDispatch:)]);
1895 [parent_native_window commandDispatch:item.get()];
1896 EXPECT_EQ(parent_id_, [item.get() handledById]);
1897 }
1898
1899 TEST_F(NativeWidgetMacCommandHandlerTest, CommandDispatchNoForwarding) {
1900 std::unique_ptr<Widget> bubble(new Widget());
1901 Widget::InitParams params;
1902 params.native_widget = new TestWindowNativeWidgetMac(bubble.get());
1903 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1904 params.parent = parent_->GetNativeView();
1905 params.pass_accelerator_to_parent = false;
1906 bubble->Init(params);
1907 bubble->Show();
1908
1909 auto item = CreateUserInterfaceItem(1);
1910 id bubble_native_window = bubble->GetNativeWindow();
1911 ASSERT_TRUE(
1912 [bubble_native_window respondsToSelector:@selector(commandDispatch:)]);
1913 [bubble_native_window commandDispatch:item.get()];
1914 EXPECT_EQ(base::nullopt, [item.get() handledById]);
1915 }
1916
1917 TEST_F(NativeWidgetMacCommandHandlerTest, CommandDispatchForwarding) {
1918 std::unique_ptr<Widget> bubble(new Widget());
1919 Widget::InitParams params;
1920 params.native_widget = new TestWindowNativeWidgetMac(bubble.get());
1921 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1922 params.parent = parent_->GetNativeView();
1923 params.pass_accelerator_to_parent = true;
1924 bubble->Init(params);
1925 bubble->Show();
1926
1927 auto item = CreateUserInterfaceItem(1);
1928 id bubble_native_window = bubble->GetNativeWindow();
1929 ASSERT_TRUE(
1930 [bubble_native_window respondsToSelector:@selector(commandDispatch:)]);
1931 [bubble_native_window commandDispatch:item.get()];
1932 EXPECT_EQ(parent_id_, [item.get() handledById]);
1933 }
1934
1810 } // namespace test 1935 } // namespace test
1811 } // namespace views 1936 } // namespace views
1812 1937
1813 @implementation TestStopAnimationWaiter 1938 @implementation TestStopAnimationWaiter
1814 - (void)setWindowStateForEnd { 1939 - (void)setWindowStateForEnd {
1815 views::test::ScopedSwizzleWaiter::GetMethodAndMarkCalled()(self, _cmd); 1940 views::test::ScopedSwizzleWaiter::GetMethodAndMarkCalled()(self, _cmd);
1816 } 1941 }
1817 @end 1942 @end
1818 1943
1819 @implementation NativeWidgetMacTestWindow 1944 @implementation NativeWidgetMacTestWindow
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1862 1987
1863 - (void)dealloc { 1988 - (void)dealloc {
1864 if (deallocFlag_) { 1989 if (deallocFlag_) {
1865 DCHECK(!*deallocFlag_); 1990 DCHECK(!*deallocFlag_);
1866 *deallocFlag_ = true; 1991 *deallocFlag_ = true;
1867 } 1992 }
1868 [super dealloc]; 1993 [super dealloc];
1869 } 1994 }
1870 1995
1871 @end 1996 @end
1997
1998 @implementation MockValidatedUserInterfaceItem
1999
2000 @synthesize handledById = handledById_;
2001 @synthesize tag = tag_;
2002 @synthesize action = action_;
2003
2004 - (instancetype)initWithTagAndAction:(NSInteger)tag action:(SEL)action {
2005 if ((self = [super init])) {
2006 tag_ = tag;
2007 action_ = action;
2008 }
2009 return self;
2010 }
2011
2012 @end
2013
2014 @implementation MockCommandHandler {
2015 int id_;
2016 }
2017
2018 - (instancetype)initWithId:(int)id {
2019 if ((self = [super init])) {
2020 id_ = id;
2021 }
2022 return self;
2023 }
2024
2025 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item
2026 window:(NSWindow*)window {
2027 if (item.action == @selector(commandDispatch:))
2028 return YES;
2029 return NO;
2030 }
2031
2032 - (void)commandDispatch:(id)sender window:(NSWindow*)window {
2033 if ([sender respondsToSelector:@selector(setHandledById:)])
2034 [sender setHandledById:id_];
2035 }
2036
2037 - (void)commandDispatchUsingKeyModifiers:(id)sender window:(NSWindow*)window {
2038 NOTIMPLEMENTED();
2039 }
2040
2041 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698