OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "ios/chrome/browser/ui/omnibox/omnibox_text_field_ios.h" | 5 #import "ios/chrome/browser/ui/omnibox/omnibox_text_field_ios.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/mac/scoped_nsobject.h" | |
11 #include "base/path_service.h" | 10 #include "base/path_service.h" |
12 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
13 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
14 #include "ios/chrome/browser/chrome_paths.h" | 13 #include "ios/chrome/browser/chrome_paths.h" |
15 #include "ios/chrome/grit/ios_strings.h" | 14 #include "ios/chrome/grit/ios_strings.h" |
16 #include "testing/gtest_mac.h" | 15 #include "testing/gtest_mac.h" |
17 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
18 #include "ui/base/l10n/l10n_util_mac.h" | 17 #include "ui/base/l10n/l10n_util_mac.h" |
19 | 18 |
| 19 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 20 #error "This file requires ARC support." |
| 21 #endif |
| 22 |
20 // A category for making existing methods visible for use in these tests. | 23 // A category for making existing methods visible for use in these tests. |
21 @interface OmniboxTextFieldIOS (VisibleForTesting) | 24 @interface OmniboxTextFieldIOS (VisibleForTesting) |
22 - (CGRect)rectForDrawTextInRect:(CGRect)rect; | 25 - (CGRect)rectForDrawTextInRect:(CGRect)rect; |
23 @end | 26 @end |
24 | 27 |
25 namespace { | 28 namespace { |
26 | 29 |
27 class OmniboxTextFieldIOSTest : public PlatformTest { | 30 class OmniboxTextFieldIOSTest : public PlatformTest { |
28 protected: | 31 protected: |
29 void SetUp() override { | 32 void SetUp() override { |
30 PlatformTest::SetUp(); | 33 PlatformTest::SetUp(); |
31 // This rect is fairly arbitrary. The text field just needs a non-zero width | 34 // This rect is fairly arbitrary. The text field just needs a non-zero width |
32 // so that the pre-edit label's text alignment can be tested. | 35 // so that the pre-edit label's text alignment can be tested. |
33 CGRect rect = CGRectMake(0, 0, 100, 20); | 36 CGRect rect = CGRectMake(0, 0, 100, 20); |
34 textfield_.reset([[OmniboxTextFieldIOS alloc] initWithFrame:rect]); | 37 textfield_ = [[OmniboxTextFieldIOS alloc] initWithFrame:rect]; |
35 [[[UIApplication sharedApplication] keyWindow] addSubview:textfield_]; | 38 [[[UIApplication sharedApplication] keyWindow] addSubview:textfield_]; |
36 }; | 39 }; |
37 | 40 |
38 void TearDown() override { [textfield_ removeFromSuperview]; } | 41 void TearDown() override { [textfield_ removeFromSuperview]; } |
39 | 42 |
40 BOOL IsCopyUrlInMenu() { | 43 BOOL IsCopyUrlInMenu() { |
41 UIMenuController* menuController = [UIMenuController sharedMenuController]; | 44 UIMenuController* menuController = [UIMenuController sharedMenuController]; |
42 NSString* const kTitle = l10n_util::GetNSString(IDS_IOS_COPY_URL); | 45 NSString* const kTitle = l10n_util::GetNSString(IDS_IOS_COPY_URL); |
43 for (UIMenuItem* item in menuController.menuItems) { | 46 for (UIMenuItem* item in menuController.menuItems) { |
44 if ([item.title isEqual:kTitle]) | 47 if ([item.title isEqual:kTitle]) |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 // Increment |i| and |start| for the next iteration of the outer while | 112 // Increment |i| and |start| for the next iteration of the outer while |
110 // loop. This also requires |j| to be reset. | 113 // loop. This also requires |j| to be reset. |
111 ++i; | 114 ++i; |
112 j = i + 1; | 115 j = i + 1; |
113 start = [textfield_ positionFromPosition:beginning offset:i]; | 116 start = [textfield_ positionFromPosition:beginning offset:i]; |
114 } | 117 } |
115 | 118 |
116 [textfield_ resignFirstResponder]; | 119 [textfield_ resignFirstResponder]; |
117 } | 120 } |
118 | 121 |
119 base::scoped_nsobject<OmniboxTextFieldIOS> textfield_; | 122 OmniboxTextFieldIOS* textfield_; |
120 }; | 123 }; |
121 | 124 |
122 TEST_F(OmniboxTextFieldIOSTest, BecomeFirstResponderAddsCopyURLMenuItem) { | 125 TEST_F(OmniboxTextFieldIOSTest, BecomeFirstResponderAddsCopyURLMenuItem) { |
123 // The 'Copy URL' menu item should not be present before this test runs. | 126 // The 'Copy URL' menu item should not be present before this test runs. |
124 EXPECT_FALSE(IsCopyUrlInMenu()); | 127 EXPECT_FALSE(IsCopyUrlInMenu()); |
125 | 128 |
126 // Call |becomeFirstResponder| and verify the Copy URL menu item was added. | 129 // Call |becomeFirstResponder| and verify the Copy URL menu item was added. |
127 UIMenuController* menuController = [UIMenuController sharedMenuController]; | 130 UIMenuController* menuController = [UIMenuController sharedMenuController]; |
128 NSUInteger expectedItems = [menuController.menuItems count] + 1; | 131 NSUInteger expectedItems = [menuController.menuItems count] + 1; |
129 [textfield_ becomeFirstResponder]; | 132 [textfield_ becomeFirstResponder]; |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 } | 268 } |
266 | 269 |
267 TEST_F(OmniboxTextFieldIOSTest, SelectAllExitsPreEditState) { | 270 TEST_F(OmniboxTextFieldIOSTest, SelectAllExitsPreEditState) { |
268 [textfield_ enterPreEditState]; | 271 [textfield_ enterPreEditState]; |
269 EXPECT_TRUE([textfield_ isPreEditing]); | 272 EXPECT_TRUE([textfield_ isPreEditing]); |
270 [textfield_ selectAll:nil]; | 273 [textfield_ selectAll:nil]; |
271 EXPECT_FALSE([textfield_ isPreEditing]); | 274 EXPECT_FALSE([textfield_ isPreEditing]); |
272 } | 275 } |
273 | 276 |
274 } // namespace | 277 } // namespace |
OLD | NEW |