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
3 // found in the LICENSE file.
5 #import "ui/views/cocoa/bridged_native_widget.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #import "testing/gtest_mac.h"
13 #import "ui/gfx/test/ui_cocoa_test_helper.h"
14 #import "ui/views/cocoa/bridged_content_view.h"
15 #include "ui/views/controls/textfield/textfield.h"
16 #include "ui/views/ime/input_method.h"
17 #include "ui/views/view.h"
18 #include "ui/views/widget/native_widget_mac.h"
19 #include "ui/views/widget/widget.h"
20 #include "ui/views/widget/widget_observer.h"
22 using base::ASCIIToUTF16;
23 using base::SysNSStringToUTF8;
24 using base::SysNSStringToUTF16;
25 using base::SysUTF8ToNSString;
27 #define EXPECT_EQ_RANGE(a, b) \
28 EXPECT_EQ(a.location, b.location); \
29 EXPECT_EQ(a.length, b.length);
33 // Empty range shortcut for readibility.
34 NSRange EmptyRange() {
35 return NSMakeRange(NSNotFound, 0);
43 // Provides the |parent| argument to construct a BridgedNativeWidget.
44 class MockNativeWidgetMac : public NativeWidgetMac {
46 MockNativeWidgetMac(Widget* delegate) : NativeWidgetMac(delegate) {}
48 // Expose a reference, so that it can be reset() independently.
49 scoped_ptr<BridgedNativeWidget>& bridge() {
53 // internal::NativeWidgetPrivate:
54 virtual void InitNativeWidget(const Widget::InitParams& params) OVERRIDE {
55 ownership_ = params.ownership;
57 // Usually the bridge gets initialized here. It is skipped to run extra
58 // checks in tests, and so that a second window isn't created.
59 delegate()->OnNativeWidgetCreated(true);
62 virtual void ReorderNativeViews() OVERRIDE {
63 // Called via Widget::Init to set the content view. No-op in these tests.
67 DISALLOW_COPY_AND_ASSIGN(MockNativeWidgetMac);
70 // Helper test base to construct a BridgedNativeWidget with a valid parent.
71 class BridgedNativeWidgetTestBase : public ui::CocoaTest {
73 BridgedNativeWidgetTestBase()
74 : widget_(new Widget),
75 native_widget_mac_(new MockNativeWidgetMac(widget_.get())) {
78 scoped_ptr<BridgedNativeWidget>& bridge() {
79 return native_widget_mac_->bridge();
82 // Overridden from testing::Test:
83 virtual void SetUp() OVERRIDE {
84 ui::CocoaTest::SetUp();
86 Widget::InitParams params;
87 params.native_widget = native_widget_mac_;
88 // To control the lifetime without an actual window that must be closed,
89 // tests in this file need to use WIDGET_OWNS_NATIVE_WIDGET.
90 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
91 native_widget_mac_->GetWidget()->Init(params);
95 scoped_ptr<Widget> widget_;
96 MockNativeWidgetMac* native_widget_mac_; // Weak. Owned by |widget_|.
99 class BridgedNativeWidgetTest : public BridgedNativeWidgetTestBase {
101 BridgedNativeWidgetTest();
102 virtual ~BridgedNativeWidgetTest();
104 // Install a textfield in the view hierarchy and make it the text input
106 void InstallTextField(const std::string& text);
108 // Returns the current text as std::string.
109 std::string GetText();
112 virtual void SetUp() OVERRIDE;
113 virtual void TearDown() OVERRIDE;
116 // TODO(tapted): Make this a EventCountView from widget_unittest.cc.
117 scoped_ptr<views::View> view_;
118 scoped_ptr<BridgedNativeWidget> bridge_;
119 BridgedContentView* ns_view_; // Weak. Owned by bridge_.
122 DISALLOW_COPY_AND_ASSIGN(BridgedNativeWidgetTest);
125 BridgedNativeWidgetTest::BridgedNativeWidgetTest() {
128 BridgedNativeWidgetTest::~BridgedNativeWidgetTest() {
131 void BridgedNativeWidgetTest::InstallTextField(const std::string& text) {
132 Textfield* textfield = new Textfield();
133 textfield->SetText(ASCIIToUTF16(text));
134 view_->AddChildView(textfield);
135 [ns_view_ setTextInputClient:textfield];
138 std::string BridgedNativeWidgetTest::GetText() {
139 NSRange range = NSMakeRange(0, NSUIntegerMax);
140 NSAttributedString* text =
141 [ns_view_ attributedSubstringForProposedRange:range actualRange:NULL];
142 return SysNSStringToUTF8([text string]);
145 void BridgedNativeWidgetTest::SetUp() {
146 BridgedNativeWidgetTestBase::SetUp();
148 view_.reset(new views::View);
149 base::scoped_nsobject<NSWindow> window([test_window() retain]);
151 EXPECT_FALSE([window delegate]);
152 bridge()->Init(window, Widget::InitParams());
154 // The delegate should exist before setting the root view.
155 EXPECT_TRUE([window delegate]);
156 bridge()->SetRootView(view_.get());
157 ns_view_ = bridge()->ns_view();
159 [test_window() makePretendKeyWindowAndSetFirstResponder:bridge()->ns_view()];
162 void BridgedNativeWidgetTest::TearDown() {
164 BridgedNativeWidgetTestBase::TearDown();
167 // The TEST_VIEW macro expects the view it's testing to have a superview. In
168 // these tests, the NSView bridge is a contentView, at the root. These mimic
169 // what TEST_VIEW usually does.
170 TEST_F(BridgedNativeWidgetTest, BridgedNativeWidgetTest_TestViewAddRemove) {
171 base::scoped_nsobject<BridgedContentView> view([bridge()->ns_view() retain]);
172 EXPECT_NSEQ([test_window() contentView], view);
173 EXPECT_NSEQ(test_window(), [view window]);
175 // The superview of a contentView is an NSNextStepFrame.
176 EXPECT_TRUE([view superview]);
177 EXPECT_TRUE([view hostedView]);
179 // Destroying the C++ bridge should remove references to any C++ objects in
180 // the ObjectiveC object, and remove it from the hierarchy.
182 EXPECT_FALSE([view hostedView]);
183 EXPECT_FALSE([view superview]);
184 EXPECT_FALSE([view window]);
185 EXPECT_FALSE([test_window() contentView]);
186 EXPECT_FALSE([test_window() delegate]);
189 TEST_F(BridgedNativeWidgetTest, BridgedNativeWidgetTest_TestViewDisplay) {
190 [bridge()->ns_view() display];
193 // Test that resizing the window resizes the root view appropriately.
194 TEST_F(BridgedNativeWidgetTest, ViewSizeTracksWindow) {
195 const int kTestNewWidth = 400;
196 const int kTestNewHeight = 300;
198 // |test_window()| is borderless, so these should align.
199 NSSize window_size = [test_window() frame].size;
200 EXPECT_EQ(view_->width(), static_cast<int>(window_size.width));
201 EXPECT_EQ(view_->height(), static_cast<int>(window_size.height));
203 // Make sure a resize actually occurs.
204 EXPECT_NE(kTestNewWidth, view_->width());
205 EXPECT_NE(kTestNewHeight, view_->height());
207 [test_window() setFrame:NSMakeRect(0, 0, kTestNewWidth, kTestNewHeight)
209 EXPECT_EQ(kTestNewWidth, view_->width());
210 EXPECT_EQ(kTestNewHeight, view_->height());
213 TEST_F(BridgedNativeWidgetTest, CreateInputMethodShouldNotReturnNull) {
214 scoped_ptr<views::InputMethod> input_method(bridge()->CreateInputMethod());
215 EXPECT_TRUE(input_method);
218 TEST_F(BridgedNativeWidgetTest, GetHostInputMethodShouldNotReturnNull) {
219 EXPECT_TRUE(bridge()->GetHostInputMethod());
222 // A simpler test harness for testing initialization flows.
223 typedef BridgedNativeWidgetTestBase BridgedNativeWidgetInitTest;
225 // Test that BridgedNativeWidget remains sane if Init() is never called.
226 TEST_F(BridgedNativeWidgetInitTest, InitNotCalled) {
227 EXPECT_FALSE(bridge()->ns_view());
228 EXPECT_FALSE(bridge()->ns_window());
232 // Test attaching to a parent window that is not a NativeWidgetMac. When the
233 // parent is a NativeWidgetMac, that is covered in widget_unittest.cc by
234 // WidgetOwnershipTest.Ownership_ViewsNativeWidgetOwnsWidget*.
235 TEST_F(BridgedNativeWidgetInitTest, ParentWindowNotNativeWidgetMac) {
236 Widget::InitParams params;
237 params.parent = [test_window() contentView];
238 EXPECT_EQ(0u, [[test_window() childWindows] count]);
240 base::scoped_nsobject<NSWindow> child_window(
241 [[NSWindow alloc] initWithContentRect:NSMakeRect(50, 50, 400, 300)
242 styleMask:NSBorderlessWindowMask
243 backing:NSBackingStoreBuffered
245 [child_window setReleasedWhenClosed:NO]; // Owned by scoped_nsobject.
247 EXPECT_FALSE([child_window parentWindow]);
248 bridge()->Init(child_window, params);
250 EXPECT_EQ(1u, [[test_window() childWindows] count]);
251 EXPECT_EQ(test_window(), [bridge()->ns_window() parentWindow]);
253 EXPECT_EQ(0u, [[test_window() childWindows] count]);
256 // Test getting complete string using text input protocol.
257 TEST_F(BridgedNativeWidgetTest, TextInput_GetCompleteString) {
258 const std::string kTestString = "foo bar baz";
259 InstallTextField(kTestString);
261 NSRange range = NSMakeRange(0, kTestString.size());
262 NSRange actual_range;
263 NSAttributedString* text =
264 [ns_view_ attributedSubstringForProposedRange:range
265 actualRange:&actual_range];
266 EXPECT_EQ(kTestString, SysNSStringToUTF8([text string]));
267 EXPECT_EQ_RANGE(range, actual_range);
270 // Test getting middle substring using text input protocol.
271 TEST_F(BridgedNativeWidgetTest, TextInput_GetMiddleSubstring) {
272 const std::string kTestString = "foo bar baz";
273 InstallTextField(kTestString);
275 NSRange range = NSMakeRange(4, 3);
276 NSRange actual_range;
277 NSAttributedString* text =
278 [ns_view_ attributedSubstringForProposedRange:range
279 actualRange:&actual_range];
280 EXPECT_EQ("bar", SysNSStringToUTF8([text string]));
281 EXPECT_EQ_RANGE(range, actual_range);
284 // Test getting ending substring using text input protocol.
285 TEST_F(BridgedNativeWidgetTest, TextInput_GetEndingSubstring) {
286 const std::string kTestString = "foo bar baz";
287 InstallTextField(kTestString);
289 NSRange range = NSMakeRange(8, 100);
290 NSRange actual_range;
291 NSAttributedString* text =
292 [ns_view_ attributedSubstringForProposedRange:range
293 actualRange:&actual_range];
294 EXPECT_EQ("baz", SysNSStringToUTF8([text string]));
295 EXPECT_EQ(range.location, actual_range.location);
296 EXPECT_EQ(3U, actual_range.length);
299 // Test getting empty substring using text input protocol.
300 TEST_F(BridgedNativeWidgetTest, TextInput_GetEmptySubstring) {
301 const std::string kTestString = "foo bar baz";
302 InstallTextField(kTestString);
304 NSRange range = EmptyRange();
305 NSRange actual_range;
306 NSAttributedString* text =
307 [ns_view_ attributedSubstringForProposedRange:range
308 actualRange:&actual_range];
309 EXPECT_EQ("", SysNSStringToUTF8([text string]));
310 EXPECT_EQ_RANGE(range, actual_range);
313 // Test inserting text using text input protocol.
314 TEST_F(BridgedNativeWidgetTest, TextInput_InsertText) {
315 const std::string kTestString = "foo";
316 InstallTextField(kTestString);
318 [ns_view_ insertText:SysUTF8ToNSString(kTestString)
319 replacementRange:EmptyRange()];
320 gfx::Range range(0, kTestString.size());
322 EXPECT_TRUE([ns_view_ textInputClient]->GetTextFromRange(range, &text));
323 EXPECT_EQ(ASCIIToUTF16(kTestString), text);
326 // Test replacing text using text input protocol.
327 TEST_F(BridgedNativeWidgetTest, TextInput_ReplaceText) {
328 const std::string kTestString = "foo bar";
329 InstallTextField(kTestString);
331 [ns_view_ insertText:@"baz" replacementRange:NSMakeRange(4, 3)];
332 EXPECT_EQ("foo baz", GetText());
335 // Test IME composition using text input protocol.
336 TEST_F(BridgedNativeWidgetTest, TextInput_Compose) {
337 const std::string kTestString = "foo ";
338 InstallTextField(kTestString);
340 EXPECT_FALSE([ns_view_ hasMarkedText]);
341 EXPECT_EQ_RANGE(EmptyRange(), [ns_view_ markedRange]);
343 // Start composition.
344 NSString* compositionText = @"bar";
345 NSUInteger compositionLength = [compositionText length];
346 [ns_view_ setMarkedText:compositionText
347 selectedRange:NSMakeRange(0, 2)
348 replacementRange:EmptyRange()];
349 EXPECT_TRUE([ns_view_ hasMarkedText]);
350 EXPECT_EQ_RANGE(NSMakeRange(kTestString.size(), compositionLength),
351 [ns_view_ markedRange]);
352 EXPECT_EQ_RANGE(NSMakeRange(kTestString.size(), 2), [ns_view_ selectedRange]);
354 // Confirm composition.
355 [ns_view_ unmarkText];
356 EXPECT_FALSE([ns_view_ hasMarkedText]);
357 EXPECT_EQ_RANGE(EmptyRange(), [ns_view_ markedRange]);
358 EXPECT_EQ("foo bar", GetText());
359 EXPECT_EQ_RANGE(NSMakeRange(GetText().size(), 0), [ns_view_ selectedRange]);
362 // Test moving the caret left and right using text input protocol.
363 TEST_F(BridgedNativeWidgetTest, TextInput_MoveLeftRight) {
364 InstallTextField("foo");
365 EXPECT_EQ_RANGE(NSMakeRange(3, 0), [ns_view_ selectedRange]);
367 // Move right not allowed, out of range.
368 [ns_view_ doCommandBySelector:@selector(moveRight:)];
369 EXPECT_EQ_RANGE(NSMakeRange(3, 0), [ns_view_ selectedRange]);
372 [ns_view_ doCommandBySelector:@selector(moveLeft:)];
373 EXPECT_EQ_RANGE(NSMakeRange(2, 0), [ns_view_ selectedRange]);
376 [ns_view_ doCommandBySelector:@selector(moveRight:)];
377 EXPECT_EQ_RANGE(NSMakeRange(3, 0), [ns_view_ selectedRange]);
380 // Test backward delete using text input protocol.
381 TEST_F(BridgedNativeWidgetTest, TextInput_DeleteBackward) {
382 InstallTextField("a");
383 EXPECT_EQ_RANGE(NSMakeRange(1, 0), [ns_view_ selectedRange]);
385 // Delete one character.
386 [ns_view_ doCommandBySelector:@selector(deleteBackward:)];
387 EXPECT_EQ("", GetText());
388 EXPECT_EQ_RANGE(NSMakeRange(0, 0), [ns_view_ selectedRange]);
390 // Try to delete again on an empty string.
391 [ns_view_ doCommandBySelector:@selector(deleteBackward:)];
392 EXPECT_EQ("", GetText());
393 EXPECT_EQ_RANGE(NSMakeRange(0, 0), [ns_view_ selectedRange]);
396 // Test forward delete using text input protocol.
397 TEST_F(BridgedNativeWidgetTest, TextInput_DeleteForward) {
398 InstallTextField("a");
399 EXPECT_EQ_RANGE(NSMakeRange(1, 0), [ns_view_ selectedRange]);
401 // At the end of the string, can't delete forward.
402 [ns_view_ doCommandBySelector:@selector(deleteForward:)];
403 EXPECT_EQ("a", GetText());
404 EXPECT_EQ_RANGE(NSMakeRange(1, 0), [ns_view_ selectedRange]);
406 // Should succeed after moving left first.
407 [ns_view_ doCommandBySelector:@selector(moveLeft:)];
408 [ns_view_ doCommandBySelector:@selector(deleteForward:)];
409 EXPECT_EQ("", GetText());
410 EXPECT_EQ_RANGE(NSMakeRange(0, 0), [ns_view_ selectedRange]);