OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 | 6 |
7 #include "base/scoped_nsobject.h" | 7 #include "base/scoped_nsobject.h" |
8 #import "chrome/browser/cocoa/autocomplete_text_field.h" | 8 #import "chrome/browser/cocoa/autocomplete_text_field.h" |
9 #import "chrome/browser/cocoa/autocomplete_text_field_cell.h" | 9 #import "chrome/browser/cocoa/autocomplete_text_field_cell.h" |
| 10 #import "chrome/browser/cocoa/autocomplete_text_field_editor.h" |
10 #import "chrome/browser/cocoa/cocoa_test_helper.h" | 11 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 @interface AutocompleteTextFieldTestDelegate : NSObject { | 14 @interface AutocompleteTextFieldTestDelegate : NSObject { |
14 BOOL textShouldPaste_; | 15 BOOL textShouldPaste_; |
15 BOOL receivedTextShouldPaste_; | 16 BOOL receivedTextShouldPaste_; |
| 17 BOOL receivedFlagsChanged_; |
16 } | 18 } |
17 - initWithTextShouldPaste:(BOOL)flag; | 19 - initWithTextShouldPaste:(BOOL)flag; |
18 - (BOOL)receivedTextShouldPaste; | 20 - (BOOL)receivedTextShouldPaste; |
| 21 - (BOOL)receivedFlagsChanged; |
19 @end | 22 @end |
20 | 23 |
21 namespace { | 24 namespace { |
22 | 25 |
23 class AutocompleteTextFieldTest : public testing::Test { | 26 class AutocompleteTextFieldTest : public testing::Test { |
24 public: | 27 public: |
25 AutocompleteTextFieldTest() { | 28 AutocompleteTextFieldTest() { |
26 NSRect frame = NSMakeRect(0, 0, 50, 30); | 29 NSRect frame = NSMakeRect(0, 0, 50, 30); |
27 field_.reset([[AutocompleteTextField alloc] initWithFrame:frame]); | 30 field_.reset([[AutocompleteTextField alloc] initWithFrame:frame]); |
28 [field_ setStringValue:@"Testing"]; | 31 [field_ setStringValue:@"Testing"]; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 EXPECT_TRUE([shouldPaste receivedTextShouldPaste]); | 67 EXPECT_TRUE([shouldPaste receivedTextShouldPaste]); |
65 | 68 |
66 scoped_nsobject<AutocompleteTextFieldTestDelegate> shouldNotPaste( | 69 scoped_nsobject<AutocompleteTextFieldTestDelegate> shouldNotPaste( |
67 [[AutocompleteTextFieldTestDelegate alloc] initWithTextShouldPaste:NO]); | 70 [[AutocompleteTextFieldTestDelegate alloc] initWithTextShouldPaste:NO]); |
68 [field_ setDelegate:shouldNotPaste]; | 71 [field_ setDelegate:shouldNotPaste]; |
69 EXPECT_FALSE([shouldNotPaste receivedTextShouldPaste]); | 72 EXPECT_FALSE([shouldNotPaste receivedTextShouldPaste]); |
70 EXPECT_FALSE([field_ textShouldPaste:nil]); | 73 EXPECT_FALSE([field_ textShouldPaste:nil]); |
71 EXPECT_TRUE([shouldNotPaste receivedTextShouldPaste]); | 74 EXPECT_TRUE([shouldNotPaste receivedTextShouldPaste]); |
72 } | 75 } |
73 | 76 |
| 77 // Test that -control:flagsChanged: properly reaches the delegate. |
| 78 TEST_F(AutocompleteTextFieldTest, FlagsChanged) { |
| 79 EXPECT_TRUE(![field_ delegate]); |
| 80 |
| 81 // This shouldn't crash, at least. |
| 82 [field_ flagsChanged:nil]; |
| 83 |
| 84 scoped_nsobject<AutocompleteTextFieldTestDelegate> delegate( |
| 85 [[AutocompleteTextFieldTestDelegate alloc] initWithTextShouldPaste:NO]); |
| 86 [field_ setDelegate:delegate]; |
| 87 EXPECT_FALSE([delegate receivedFlagsChanged]); |
| 88 [field_ flagsChanged:nil]; |
| 89 EXPECT_TRUE([delegate receivedFlagsChanged]); |
| 90 } |
| 91 |
| 92 // Test that -control:flagsChanged: properly reaches the delegate when |
| 93 // the -flagsChanged: message goes to the editor. In that case it is |
| 94 // forwarded via the responder chain. |
| 95 TEST_F(AutocompleteTextFieldTest, FieldEditorFlagsChanged) { |
| 96 EXPECT_TRUE(![field_ delegate]); |
| 97 |
| 98 scoped_nsobject<AutocompleteTextFieldTestDelegate> delegate( |
| 99 [[AutocompleteTextFieldTestDelegate alloc] initWithTextShouldPaste:NO]); |
| 100 |
| 101 // Setup a field editor for |field_|. |
| 102 scoped_nsobject<AutocompleteTextFieldEditor> editor( |
| 103 [[AutocompleteTextFieldEditor alloc] init]); |
| 104 [field_ setDelegate:delegate]; |
| 105 |
| 106 [editor setFieldEditor:YES]; |
| 107 [[field_ cell] setUpFieldEditorAttributes:editor]; |
| 108 [[field_ cell] editWithFrame:[field_ bounds] |
| 109 inView:field_ |
| 110 editor:editor |
| 111 delegate:[field_ delegate] |
| 112 event:nil]; |
| 113 EXPECT_FALSE([delegate receivedFlagsChanged]); |
| 114 [editor flagsChanged:nil]; |
| 115 EXPECT_TRUE([delegate receivedFlagsChanged]); |
| 116 } |
| 117 |
74 } // namespace | 118 } // namespace |
75 | 119 |
76 @implementation AutocompleteTextFieldTestDelegate | 120 @implementation AutocompleteTextFieldTestDelegate |
77 | 121 |
78 - initWithTextShouldPaste:(BOOL)flag { | 122 - initWithTextShouldPaste:(BOOL)flag { |
79 self = [super init]; | 123 self = [super init]; |
80 if (self) { | 124 if (self) { |
81 textShouldPaste_ = flag; | 125 textShouldPaste_ = flag; |
82 receivedTextShouldPaste_ = NO; | 126 receivedTextShouldPaste_ = NO; |
| 127 receivedFlagsChanged_ = NO; |
83 } | 128 } |
84 return self; | 129 return self; |
85 } | 130 } |
86 | 131 |
87 - (BOOL)receivedTextShouldPaste { | 132 - (BOOL)receivedTextShouldPaste { |
88 return receivedTextShouldPaste_; | 133 return receivedTextShouldPaste_; |
89 } | 134 } |
90 | 135 |
| 136 - (BOOL)receivedFlagsChanged { |
| 137 return receivedFlagsChanged_; |
| 138 } |
| 139 |
91 - (BOOL)control:(NSControl*)control textShouldPaste:(NSText*)fieldEditor { | 140 - (BOOL)control:(NSControl*)control textShouldPaste:(NSText*)fieldEditor { |
92 receivedTextShouldPaste_ = YES; | 141 receivedTextShouldPaste_ = YES; |
93 return textShouldPaste_; | 142 return textShouldPaste_; |
94 } | 143 } |
95 | 144 |
| 145 - (void)control:(id)control flagsChanged:(NSEvent*)theEvent { |
| 146 receivedFlagsChanged_ = YES; |
| 147 } |
| 148 |
96 @end | 149 @end |
OLD | NEW |