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

Side by Side Diff: ui/views/cocoa/bridged_native_widget_unittest.mm

Issue 2180873002: MacViews: Change insertTextInternal to correctly handle IME character input. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix compile. Created 4 years, 4 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/cocoa/bridged_native_widget.h" 5 #import "ui/views/cocoa/bridged_native_widget.h"
6 6
7 #import <Cocoa/Cocoa.h> 7 #import <Cocoa/Cocoa.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #import "base/mac/foundation_util.h" 11 #import "base/mac/foundation_util.h"
12 #import "base/mac/mac_util.h" 12 #import "base/mac/mac_util.h"
13 #import "base/mac/sdk_forward_declarations.h" 13 #import "base/mac/sdk_forward_declarations.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "base/strings/sys_string_conversions.h" 17 #include "base/strings/sys_string_conversions.h"
18 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
19 #import "testing/gtest_mac.h" 19 #import "testing/gtest_mac.h"
20 #import "ui/base/cocoa/window_size_constants.h" 20 #import "ui/base/cocoa/window_size_constants.h"
21 #include "ui/base/ime/input_method.h" 21 #include "ui/base/ime/input_method.h"
22 #include "ui/events/test/cocoa_test_event_utils.h"
22 #import "ui/gfx/mac/coordinate_conversion.h" 23 #import "ui/gfx/mac/coordinate_conversion.h"
23 #import "ui/gfx/test/ui_cocoa_test_helper.h" 24 #import "ui/gfx/test/ui_cocoa_test_helper.h"
24 #import "ui/views/cocoa/bridged_content_view.h" 25 #import "ui/views/cocoa/bridged_content_view.h"
25 #import "ui/views/cocoa/native_widget_mac_nswindow.h" 26 #import "ui/views/cocoa/native_widget_mac_nswindow.h"
26 #import "ui/views/cocoa/views_nswindow_delegate.h" 27 #import "ui/views/cocoa/views_nswindow_delegate.h"
27 #include "ui/views/controls/textfield/textfield.h" 28 #include "ui/views/controls/textfield/textfield.h"
28 #include "ui/views/view.h" 29 #include "ui/views/view.h"
29 #include "ui/views/widget/native_widget_mac.h" 30 #include "ui/views/widget/native_widget_mac.h"
30 #include "ui/views/widget/root_view.h" 31 #include "ui/views/widget/root_view.h"
31 #include "ui/views/widget/widget.h" 32 #include "ui/views/widget/widget.h"
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 void SetSelectionRange(NSRange range); 298 void SetSelectionRange(NSRange range);
298 299
299 // Perform command |sel| on |ns_view_| and |dummy_text_view_|. 300 // Perform command |sel| on |ns_view_| and |dummy_text_view_|.
300 void PerformCommand(SEL sel); 301 void PerformCommand(SEL sel);
301 302
302 // Make selection from |start| to |end| on installed views::Textfield and 303 // Make selection from |start| to |end| on installed views::Textfield and
303 // |dummy_text_view_|. If |start| > |end|, extend selection to left from 304 // |dummy_text_view_|. If |start| > |end|, extend selection to left from
304 // |start|. 305 // |start|.
305 void MakeSelection(int start, int end); 306 void MakeSelection(int start, int end);
306 307
308 // Helper method to set the private |keyDownEvent_| field on |ns_view_|.
309 void SetKeyDownEvent(NSEvent* event);
310
307 // testing::Test: 311 // testing::Test:
308 void SetUp() override; 312 void SetUp() override;
309 void TearDown() override; 313 void TearDown() override;
310 314
311 protected: 315 protected:
312 // Test delete to beginning of line or paragraph based on |sel|. |sel| can be 316 // Test delete to beginning of line or paragraph based on |sel|. |sel| can be
313 // either deleteToBeginningOfLine: or deleteToBeginningOfParagraph:. 317 // either deleteToBeginningOfLine: or deleteToBeginningOfParagraph:.
314 void TestDeleteBeginning(SEL sel); 318 void TestDeleteBeginning(SEL sel);
315 319
316 // Test delete to end of line or paragraph based on |sel|. |sel| can be 320 // Test delete to end of line or paragraph based on |sel|. |sel| can be
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 //|start| to |end|. 423 //|start| to |end|.
420 [dummy_text_view_ setSelectedRange:NSMakeRange(start, 0)]; 424 [dummy_text_view_ setSelectedRange:NSMakeRange(start, 0)];
421 SEL sel = start > end ? @selector(moveBackwardAndModifySelection:) 425 SEL sel = start > end ? @selector(moveBackwardAndModifySelection:)
422 : @selector(moveForwardAndModifySelection:); 426 : @selector(moveForwardAndModifySelection:);
423 size_t delta = std::abs(end - start); 427 size_t delta = std::abs(end - start);
424 428
425 for (size_t i = 0; i < delta; i++) 429 for (size_t i = 0; i < delta; i++)
426 [dummy_text_view_ doCommandBySelector:sel]; 430 [dummy_text_view_ doCommandBySelector:sel];
427 } 431 }
428 432
433 void BridgedNativeWidgetTest::SetKeyDownEvent(NSEvent* event) {
434 [ns_view_ setValue:event forKey:@"keyDownEvent_"];
435 }
436
429 void BridgedNativeWidgetTest::SetUp() { 437 void BridgedNativeWidgetTest::SetUp() {
430 BridgedNativeWidgetTestBase::SetUp(); 438 BridgedNativeWidgetTestBase::SetUp();
431 439
432 view_.reset(new views::internal::RootView(widget_.get())); 440 view_.reset(new views::internal::RootView(widget_.get()));
433 base::scoped_nsobject<NSWindow> window([test_window() retain]); 441 base::scoped_nsobject<NSWindow> window([test_window() retain]);
434 442
435 // BridgedNativeWidget expects to be initialized with a hidden (deferred) 443 // BridgedNativeWidget expects to be initialized with a hidden (deferred)
436 // window. 444 // window.
437 [window orderOut:nil]; 445 [window orderOut:nil];
438 EXPECT_FALSE([window delegate]); 446 EXPECT_FALSE([window delegate]);
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
861 EXPECT_NSEQ_3(@"foo bar", GetExpectedText(), GetActualText()); 869 EXPECT_NSEQ_3(@"foo bar", GetExpectedText(), GetActualText());
862 EXPECT_EQ_RANGE_3(NSMakeRange([GetActualText() length], 0), 870 EXPECT_EQ_RANGE_3(NSMakeRange([GetActualText() length], 0),
863 GetExpectedSelectionRange(), GetActualSelectionRange()); 871 GetExpectedSelectionRange(), GetActualSelectionRange());
864 } 872 }
865 873
866 // Test IME composition for accented characters. 874 // Test IME composition for accented characters.
867 TEST_F(BridgedNativeWidgetTest, TextInput_AccentedCharacter) { 875 TEST_F(BridgedNativeWidgetTest, TextInput_AccentedCharacter) {
868 InstallTextField("abc"); 876 InstallTextField("abc");
869 877
870 // Simulate action messages generated when the key 'a' is pressed repeatedly 878 // Simulate action messages generated when the key 'a' is pressed repeatedly
871 // and leads to the showing of an IME candidate window. 879 // and leads to the showing of an IME candidate window. To simulate an event,
880 // set the private keyDownEvent field on the BridgedContentView.
872 881
873 // First an insertText: message with key 'a' is generated. 882 // First an insertText: message with key 'a' is generated.
883 SetKeyDownEvent(cocoa_test_event_utils::SynthesizeKeyEvent(
884 widget_->GetNativeWindow(), true, ui::VKEY_A, 0));
874 [ns_view_ insertText:@"a" replacementRange:EmptyRange()]; 885 [ns_view_ insertText:@"a" replacementRange:EmptyRange()];
875 [dummy_text_view_ insertText:@"a" replacementRange:EmptyRange()]; 886 [dummy_text_view_ insertText:@"a" replacementRange:EmptyRange()];
876 EXPECT_EQ_3(NO, [dummy_text_view_ hasMarkedText], [ns_view_ hasMarkedText]); 887 EXPECT_EQ_3(NO, [dummy_text_view_ hasMarkedText], [ns_view_ hasMarkedText]);
877 EXPECT_NSEQ_3(@"abca", GetExpectedText(), GetActualText()); 888 EXPECT_NSEQ_3(@"abca", GetExpectedText(), GetActualText());
878 889
879 // Next the IME popup appears. On selecting the accented character using arrow 890 // Next the IME popup appears. On selecting the accented character using arrow
880 // keys, setMarkedText action message is generated which replaces the earlier 891 // keys, setMarkedText action message is generated which replaces the earlier
881 // inserted 'a'. 892 // inserted 'a'.
893 SetKeyDownEvent(cocoa_test_event_utils::SynthesizeKeyEvent(
894 widget_->GetNativeWindow(), true, ui::VKEY_RIGHT, 0));
882 [ns_view_ setMarkedText:@"à" 895 [ns_view_ setMarkedText:@"à"
883 selectedRange:NSMakeRange(0, 1) 896 selectedRange:NSMakeRange(0, 1)
884 replacementRange:NSMakeRange(3, 1)]; 897 replacementRange:NSMakeRange(3, 1)];
885 [dummy_text_view_ setMarkedText:@"à" 898 [dummy_text_view_ setMarkedText:@"à"
886 selectedRange:NSMakeRange(0, 1) 899 selectedRange:NSMakeRange(0, 1)
887 replacementRange:NSMakeRange(3, 1)]; 900 replacementRange:NSMakeRange(3, 1)];
888 EXPECT_EQ_3(YES, [dummy_text_view_ hasMarkedText], [ns_view_ hasMarkedText]); 901 EXPECT_EQ_3(YES, [dummy_text_view_ hasMarkedText], [ns_view_ hasMarkedText]);
889 EXPECT_EQ_RANGE_3(NSMakeRange(3, 1), [dummy_text_view_ markedRange], 902 EXPECT_EQ_RANGE_3(NSMakeRange(3, 1), [dummy_text_view_ markedRange],
890 [ns_view_ markedRange]); 903 [ns_view_ markedRange]);
891 EXPECT_EQ_RANGE_3(NSMakeRange(3, 1), GetExpectedSelectionRange(), 904 EXPECT_EQ_RANGE_3(NSMakeRange(3, 1), GetExpectedSelectionRange(),
892 GetActualSelectionRange()); 905 GetActualSelectionRange());
893 EXPECT_NSEQ_3(@"abcà", GetExpectedText(), GetActualText()); 906 EXPECT_NSEQ_3(@"abcà", GetExpectedText(), GetActualText());
907
908 // On pressing enter, the marked text is confirmed.
909 SetKeyDownEvent(cocoa_test_event_utils::SynthesizeKeyEvent(
910 widget_->GetNativeWindow(), true, ui::VKEY_RETURN, 0));
911 [ns_view_ insertText:@"à" replacementRange:EmptyRange()];
912 [dummy_text_view_ insertText:@"à" replacementRange:EmptyRange()];
913 EXPECT_EQ_3(NO, [dummy_text_view_ hasMarkedText], [ns_view_ hasMarkedText]);
914 EXPECT_EQ_RANGE_3(NSMakeRange(4, 0), GetExpectedSelectionRange(),
915 GetActualSelectionRange());
916 EXPECT_NSEQ_3(@"abcà", GetExpectedText(), GetActualText());
894 } 917 }
895 918
896 // Test moving the caret left and right using text input protocol. 919 // Test moving the caret left and right using text input protocol.
897 TEST_F(BridgedNativeWidgetTest, TextInput_MoveLeftRight) { 920 TEST_F(BridgedNativeWidgetTest, TextInput_MoveLeftRight) {
898 InstallTextField("foo"); 921 InstallTextField("foo");
899 EXPECT_EQ_RANGE_3(NSMakeRange(3, 0), GetExpectedSelectionRange(), 922 EXPECT_EQ_RANGE_3(NSMakeRange(3, 0), GetExpectedSelectionRange(),
900 GetActualSelectionRange()); 923 GetActualSelectionRange());
901 924
902 // Move right not allowed, out of range. 925 // Move right not allowed, out of range.
903 PerformCommand(@selector(moveRight:)); 926 PerformCommand(@selector(moveRight:));
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
1226 [center postNotificationName:NSWindowDidExitFullScreenNotification 1249 [center postNotificationName:NSWindowDidExitFullScreenNotification
1227 object:window]; 1250 object:window];
1228 EXPECT_EQ(1, [window ignoredToggleFullScreenCount]); // No change. 1251 EXPECT_EQ(1, [window ignoredToggleFullScreenCount]); // No change.
1229 EXPECT_FALSE(bridge()->target_fullscreen_state()); 1252 EXPECT_FALSE(bridge()->target_fullscreen_state());
1230 1253
1231 widget_->CloseNow(); 1254 widget_->CloseNow();
1232 } 1255 }
1233 1256
1234 } // namespace test 1257 } // namespace test
1235 } // namespace views 1258 } // namespace views
OLDNEW
« ui/views/cocoa/bridged_content_view.mm ('K') | « ui/views/cocoa/bridged_content_view.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698