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

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

Issue 329463002: MacViews: Implement text input. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 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 | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "base/mac/scoped_nsobject.h"
tapted 2014/06/17 13:23:56 nit: import this and the other objc-only headers
Andre 2014/06/18 21:48:35 Done.
6 #include "base/strings/sys_string_conversions.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/gfx/test/ui_cocoa_test_helper.h"
9 #include "ui/views/cocoa/bridged_content_view.h"
10 #include "ui/views/controls/textfield/textfield.h"
11
12 using base::ASCIIToUTF16;
13 using base::SysNSStringToUTF8;
14 using base::SysNSStringToUTF16;
15
16 static NSRange EmptyRange = NSMakeRange(NSNotFound, 0);
tapted 2014/06/17 13:23:57 non-primitive statics aren't usually allowed - htt
Andre 2014/06/18 21:48:35 Done.
17
18 #define EXPECT_EQ_RANGE(a, b) \
19 EXPECT_EQ(a.location, b.location); \
tapted 2014/06/17 13:23:57 I've seen EXPECT_TRUE(NSEqualRanges(a, b)) for stu
Andre 2014/06/18 21:48:35 I had it like that, but the failure output is much
20 EXPECT_EQ(a.length, b.length);
21
22 namespace ui {
23 namespace {
24
25 class TextInputMacTest : public CocoaTest {
26 public:
27 virtual void SetUp() OVERRIDE {
28 textfield_.reset(new views::Textfield());
29 bridge_.reset([[BridgedContentView alloc] initWithView:textfield_.get()]);
tapted 2014/06/17 13:23:57 Somehting to consider - I've recently been finding
30 [bridge_ setTextInputClient:textfield_.get()];
31 }
32
33 // Returns the current text as std::string.
34 std::string GetText() {
35 NSAttributedString* text =
36 [bridge_ attributedSubstringForProposedRange:NSMakeRange(0, 1000)
tapted 2014/06/17 13:23:57 this 1000 should be a constant somewhere
Andre 2014/06/18 21:48:35 Done.
37 actualRange:NULL];
38 return SysNSStringToUTF8([text string]);
39 }
40
41 protected:
42 base::scoped_nsobject<BridgedContentView> bridge_;
43 scoped_ptr<views::Textfield> textfield_;
44 };
tapted 2014/06/17 13:23:57 nit: private: DISALLOW_COPY_AND_ASSIGN(..) (you'
Andre 2014/06/18 21:48:35 Done.
45
46 } // namespace
47
48 TEST_F(TextInputMacTest, AttributedSubstring) {
tapted 2014/06/17 13:23:57 nit: it's sometimes nice to have a comment before
Andre 2014/06/18 21:48:35 Done.
49 NSRange range;
tapted 2014/06/17 13:23:57 Unless it's a constant, declarations are usually d
Andre 2014/06/18 21:48:35 Yeah, I did this way because the first use became
50 NSRange actualRange;
tapted 2014/06/17 13:23:57 actualRange -> actual_range, since it's not an obj
Andre 2014/06/18 21:48:35 Done.
51 NSAttributedString* text;
52
53 const char* str = "foo bar baz";
tapted 2014/06/17 13:23:57 I'd usually see `const char kTestString[] = "foo b
Andre 2014/06/18 21:48:35 Done.
54 textfield_->SetText(ASCIIToUTF16(str));
55
56 // Get the entire string.
57 range = NSMakeRange(0, 100);
58 text = [bridge_ attributedSubstringForProposedRange:range
59 actualRange:&actualRange];
60 EXPECT_EQ(str, SysNSStringToUTF8([text string]));
tapted 2014/06/17 13:23:57 There's also EXPECT_NSEQ from testing/gtest_mac.h
Andre 2014/06/18 21:48:35 Thanks, I don't think it will reduce the number of
61 EXPECT_EQ(range.location, actualRange.location);
62 EXPECT_EQ(strlen(str), actualRange.length);
63
64 // Get the middle of the text.
65 range = NSMakeRange(4, 3);
66 text = [bridge_ attributedSubstringForProposedRange:range
67 actualRange:&actualRange];
68 EXPECT_EQ("bar", SysNSStringToUTF8([text string]));
69 EXPECT_EQ_RANGE(range, actualRange);
70
71 // Get the end of the text.
72 range = NSMakeRange(8, 100);
73 text = [bridge_ attributedSubstringForProposedRange:range
74 actualRange:&actualRange];
75 EXPECT_EQ("baz", SysNSStringToUTF8([text string]));
76 EXPECT_EQ(range.location, actualRange.location);
77 EXPECT_EQ(strlen("baz"), actualRange.length);
78
79 // Test with empty range.
80 range = EmptyRange;
81 text = [bridge_ attributedSubstringForProposedRange:range
82 actualRange:&actualRange];
83 EXPECT_EQ("", SysNSStringToUTF8([text string]));
84 EXPECT_EQ_RANGE(range, actualRange);
85 }
86
87 TEST_F(TextInputMacTest, InsertText) {
88 NSString* str = @"foo";
tapted 2014/06/17 13:23:57 `str` probably counts as an abbreviation, so not a
Andre 2014/06/18 21:48:35 Done.
89 [bridge_ insertText:str replacementRange:EmptyRange];
90 gfx::Range range(0, [str length]);
91 base::string16 text;
92 EXPECT_TRUE(textfield_->GetTextFromRange(range, &text));
93 EXPECT_EQ(SysNSStringToUTF16(str), text);
94 }
95
96 TEST_F(TextInputMacTest, InsertTextWithReplacement) {
97 const char* old_text = "foo bar";
98 textfield_->SetText(ASCIIToUTF16(old_text));
99
100 [bridge_ insertText:@"baz" replacementRange:NSMakeRange(4, 3)];
101 EXPECT_EQ("foo baz", GetText());
102 }
103
104 TEST_F(TextInputMacTest, Composition) {
105 const char* str = "foo bar baz ";
106 textfield_->SetText(ASCIIToUTF16(str));
107 EXPECT_FALSE([bridge_ hasMarkedText]);
108 EXPECT_EQ_RANGE(EmptyRange, [bridge_ markedRange]);
109
110 // Start composition.
111 NSString* compositionText = @"qux";
112 NSUInteger compositionLength = [compositionText length];
113 [bridge_ setMarkedText:compositionText
114 selectedRange:NSMakeRange(0, 2)
115 replacementRange:EmptyRange];
116 EXPECT_TRUE([bridge_ hasMarkedText]);
117 EXPECT_EQ_RANGE(NSMakeRange(strlen(str), compositionLength),
118 [bridge_ markedRange]);
119 EXPECT_EQ_RANGE(NSMakeRange(strlen(str), 2), [bridge_ selectedRange]);
120
121 // Confirm composition.
122 [bridge_ unmarkText];
123 EXPECT_FALSE([bridge_ hasMarkedText]);
124 EXPECT_EQ_RANGE(EmptyRange, [bridge_ markedRange]);
125 EXPECT_EQ("foo bar baz qux", GetText());
126 EXPECT_EQ_RANGE(NSMakeRange(GetText().size(), 0), [bridge_ selectedRange]);
127 }
128
129 TEST_F(TextInputMacTest, MoveLeftRight) {
130 textfield_->SetText(ASCIIToUTF16("foo"));
131 EXPECT_EQ_RANGE(NSMakeRange(3, 0), [bridge_ selectedRange]);
132
133 // Move left
134 [bridge_ moveLeft:nil];
135 EXPECT_EQ_RANGE(NSMakeRange(2, 0), [bridge_ selectedRange]);
136
137 // Move right
138 [bridge_ moveRight:nil];
139 EXPECT_EQ_RANGE(NSMakeRange(3, 0), [bridge_ selectedRange]);
140 }
141
142 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698