Chromium Code Reviews| Index: ui/views/cocoa/text_input_unittest.mm |
| diff --git a/ui/views/cocoa/text_input_unittest.mm b/ui/views/cocoa/text_input_unittest.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..376cfcd95daf6e9bf4abfec26ef5320647f053b1 |
| --- /dev/null |
| +++ b/ui/views/cocoa/text_input_unittest.mm |
| @@ -0,0 +1,142 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#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.
|
| +#include "base/strings/sys_string_conversions.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "ui/gfx/test/ui_cocoa_test_helper.h" |
| +#include "ui/views/cocoa/bridged_content_view.h" |
| +#include "ui/views/controls/textfield/textfield.h" |
| + |
| +using base::ASCIIToUTF16; |
| +using base::SysNSStringToUTF8; |
| +using base::SysNSStringToUTF16; |
| + |
| +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.
|
| + |
| +#define EXPECT_EQ_RANGE(a, b) \ |
| + 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
|
| + EXPECT_EQ(a.length, b.length); |
| + |
| +namespace ui { |
| +namespace { |
| + |
| +class TextInputMacTest : public CocoaTest { |
| + public: |
| + virtual void SetUp() OVERRIDE { |
| + textfield_.reset(new views::Textfield()); |
| + bridge_.reset([[BridgedContentView alloc] initWithView:textfield_.get()]); |
|
tapted
2014/06/17 13:23:57
Somehting to consider - I've recently been finding
|
| + [bridge_ setTextInputClient:textfield_.get()]; |
| + } |
| + |
| + // Returns the current text as std::string. |
| + std::string GetText() { |
| + NSAttributedString* text = |
| + [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.
|
| + actualRange:NULL]; |
| + return SysNSStringToUTF8([text string]); |
| + } |
| + |
| + protected: |
| + base::scoped_nsobject<BridgedContentView> bridge_; |
| + scoped_ptr<views::Textfield> textfield_; |
| +}; |
|
tapted
2014/06/17 13:23:57
nit: private:
DISALLOW_COPY_AND_ASSIGN(..)
(you'
Andre
2014/06/18 21:48:35
Done.
|
| + |
| +} // namespace |
| + |
| +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.
|
| + 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
|
| + 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.
|
| + NSAttributedString* text; |
| + |
| + 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.
|
| + textfield_->SetText(ASCIIToUTF16(str)); |
| + |
| + // Get the entire string. |
| + range = NSMakeRange(0, 100); |
| + text = [bridge_ attributedSubstringForProposedRange:range |
| + actualRange:&actualRange]; |
| + 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
|
| + EXPECT_EQ(range.location, actualRange.location); |
| + EXPECT_EQ(strlen(str), actualRange.length); |
| + |
| + // Get the middle of the text. |
| + range = NSMakeRange(4, 3); |
| + text = [bridge_ attributedSubstringForProposedRange:range |
| + actualRange:&actualRange]; |
| + EXPECT_EQ("bar", SysNSStringToUTF8([text string])); |
| + EXPECT_EQ_RANGE(range, actualRange); |
| + |
| + // Get the end of the text. |
| + range = NSMakeRange(8, 100); |
| + text = [bridge_ attributedSubstringForProposedRange:range |
| + actualRange:&actualRange]; |
| + EXPECT_EQ("baz", SysNSStringToUTF8([text string])); |
| + EXPECT_EQ(range.location, actualRange.location); |
| + EXPECT_EQ(strlen("baz"), actualRange.length); |
| + |
| + // Test with empty range. |
| + range = EmptyRange; |
| + text = [bridge_ attributedSubstringForProposedRange:range |
| + actualRange:&actualRange]; |
| + EXPECT_EQ("", SysNSStringToUTF8([text string])); |
| + EXPECT_EQ_RANGE(range, actualRange); |
| +} |
| + |
| +TEST_F(TextInputMacTest, InsertText) { |
| + 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.
|
| + [bridge_ insertText:str replacementRange:EmptyRange]; |
| + gfx::Range range(0, [str length]); |
| + base::string16 text; |
| + EXPECT_TRUE(textfield_->GetTextFromRange(range, &text)); |
| + EXPECT_EQ(SysNSStringToUTF16(str), text); |
| +} |
| + |
| +TEST_F(TextInputMacTest, InsertTextWithReplacement) { |
| + const char* old_text = "foo bar"; |
| + textfield_->SetText(ASCIIToUTF16(old_text)); |
| + |
| + [bridge_ insertText:@"baz" replacementRange:NSMakeRange(4, 3)]; |
| + EXPECT_EQ("foo baz", GetText()); |
| +} |
| + |
| +TEST_F(TextInputMacTest, Composition) { |
| + const char* str = "foo bar baz "; |
| + textfield_->SetText(ASCIIToUTF16(str)); |
| + EXPECT_FALSE([bridge_ hasMarkedText]); |
| + EXPECT_EQ_RANGE(EmptyRange, [bridge_ markedRange]); |
| + |
| + // Start composition. |
| + NSString* compositionText = @"qux"; |
| + NSUInteger compositionLength = [compositionText length]; |
| + [bridge_ setMarkedText:compositionText |
| + selectedRange:NSMakeRange(0, 2) |
| + replacementRange:EmptyRange]; |
| + EXPECT_TRUE([bridge_ hasMarkedText]); |
| + EXPECT_EQ_RANGE(NSMakeRange(strlen(str), compositionLength), |
| + [bridge_ markedRange]); |
| + EXPECT_EQ_RANGE(NSMakeRange(strlen(str), 2), [bridge_ selectedRange]); |
| + |
| + // Confirm composition. |
| + [bridge_ unmarkText]; |
| + EXPECT_FALSE([bridge_ hasMarkedText]); |
| + EXPECT_EQ_RANGE(EmptyRange, [bridge_ markedRange]); |
| + EXPECT_EQ("foo bar baz qux", GetText()); |
| + EXPECT_EQ_RANGE(NSMakeRange(GetText().size(), 0), [bridge_ selectedRange]); |
| +} |
| + |
| +TEST_F(TextInputMacTest, MoveLeftRight) { |
| + textfield_->SetText(ASCIIToUTF16("foo")); |
| + EXPECT_EQ_RANGE(NSMakeRange(3, 0), [bridge_ selectedRange]); |
| + |
| + // Move left |
| + [bridge_ moveLeft:nil]; |
| + EXPECT_EQ_RANGE(NSMakeRange(2, 0), [bridge_ selectedRange]); |
| + |
| + // Move right |
| + [bridge_ moveRight:nil]; |
| + EXPECT_EQ_RANGE(NSMakeRange(3, 0), [bridge_ selectedRange]); |
| +} |
| + |
| +} // namespace ui |