Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 1 | // Copyright (c) 2013 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 | #import "chrome/browser/ui/cocoa/autofill/autofill_section_container.h" |
| 6 | |
| 7 | #include "base/mac/foundation_util.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 8 | #include "base/mac/scoped_nsobject.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 10 | #include "chrome/browser/ui/autofill/autofill_dialog_models.h" |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 11 | #include "chrome/browser/ui/autofill/mock_autofill_dialog_view_delegate.h" |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 12 | #import "chrome/browser/ui/cocoa/autofill/autofill_input_field.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 13 | #import "chrome/browser/ui/cocoa/autofill/layout_view.h" |
| 14 | #import "chrome/browser/ui/cocoa/menu_button.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 16 | #include "testing/gtest_mac.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 17 | #include "testing/platform_test.h" |
| 18 | #include "ui/base/models/combobox_model.h" |
| 19 | #include "ui/base/models/simple_menu_model.h" |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 20 | #import "ui/gfx/test/ui_cocoa_test_helper.h" |
| 21 | |
| 22 | using base::ASCIIToUTF16; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | |
| 26 | class AutofillSectionContainerTest : public ui::CocoaTest { |
| 27 | public: |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 28 | AutofillSectionContainerTest() : section_(autofill::SECTION_BILLING) {} |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 29 | virtual void SetUp() { |
| 30 | CocoaTest::SetUp(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 31 | ResetContainer(); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void ResetContainer() { |
| 35 | container_.reset( |
| 36 | [[AutofillSectionContainer alloc] |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 37 | initWithDelegate:&delegate_ |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 38 | forSection:section_]); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 39 | [[test_window() contentView] setSubviews:@[ [container_ view] ]]; |
| 40 | } |
| 41 | |
| 42 | protected: |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 43 | base::scoped_nsobject<AutofillSectionContainer> container_; |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 44 | testing::NiceMock<autofill::MockAutofillDialogViewDelegate> delegate_; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 45 | autofill::DialogSection section_; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | } // namespace |
| 49 | |
| 50 | TEST_VIEW(AutofillSectionContainerTest, [container_ view]) |
| 51 | |
| 52 | TEST_F(AutofillSectionContainerTest, HasSubviews) { |
| 53 | bool hasLayoutView = false; |
| 54 | bool hasTextField = false; |
| 55 | bool hasSuggestButton = false; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 56 | bool hasSuggestionView = false; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 57 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 58 | ASSERT_EQ(4U, [[[container_ view] subviews] count]); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 59 | for (NSView* view in [[container_ view] subviews]) { |
| 60 | if ([view isKindOfClass:[NSTextField class]]) { |
| 61 | hasTextField = true; |
| 62 | } else if ([view isKindOfClass:[LayoutView class]]) { |
| 63 | hasLayoutView = true; |
| 64 | } else if ([view isKindOfClass:[MenuButton class]]) { |
| 65 | hasSuggestButton = true; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 66 | } else if ([view isKindOfClass:[NSView class]]) { |
| 67 | hasSuggestionView = true; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | EXPECT_TRUE(hasSuggestButton); |
| 72 | EXPECT_TRUE(hasLayoutView); |
| 73 | EXPECT_TRUE(hasTextField); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 74 | EXPECT_TRUE(hasSuggestionView); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST_F(AutofillSectionContainerTest, ModelsPopulateComboboxes) { |
| 78 | using namespace autofill; |
| 79 | using namespace testing; |
| 80 | |
| 81 | const DetailInput kTestInputs[] = { |
Torne (Richard Coles) | a3f6a49 | 2013-12-18 16:25:09 +0000 | [diff] [blame] | 82 | { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH }, |
| 83 | { DetailInput::SHORT, CREDIT_CARD_EXP_4_DIGIT_YEAR }, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | DetailInputs inputs; |
| 87 | inputs.push_back(kTestInputs[0]); |
Ben Murdoch | ba5b9a6 | 2013-08-12 14:20:17 +0100 | [diff] [blame] | 88 | inputs.push_back(kTestInputs[1]); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 89 | |
Ben Murdoch | ba5b9a6 | 2013-08-12 14:20:17 +0100 | [diff] [blame] | 90 | autofill::MonthComboboxModel monthComboModel; |
| 91 | autofill::YearComboboxModel yearComboModel; |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 92 | EXPECT_CALL(delegate_, RequestedFieldsForSection(section_)) |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 93 | .WillOnce(ReturnRef(inputs)); |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 94 | EXPECT_CALL(delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH)) |
Ben Murdoch | ba5b9a6 | 2013-08-12 14:20:17 +0100 | [diff] [blame] | 95 | .WillRepeatedly(Return(&monthComboModel)); |
| 96 | EXPECT_CALL( |
| 97 | delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR)) |
| 98 | .WillRepeatedly(Return(&yearComboModel)); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 99 | ResetContainer(); |
| 100 | |
| 101 | NSPopUpButton* popup = base::mac::ObjCCastStrict<NSPopUpButton>( |
| 102 | [container_ getField:CREDIT_CARD_EXP_MONTH]); |
| 103 | EXPECT_EQ(13, [popup numberOfItems]); |
Ben Murdoch | ba5b9a6 | 2013-08-12 14:20:17 +0100 | [diff] [blame] | 104 | EXPECT_NSEQ(@"Month", [popup itemTitleAtIndex:0]); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 105 | EXPECT_NSEQ(@"01", [popup itemTitleAtIndex:1]); |
| 106 | EXPECT_NSEQ(@"02", [popup itemTitleAtIndex:2]); |
| 107 | EXPECT_NSEQ(@"03", [popup itemTitleAtIndex:3]); |
| 108 | EXPECT_NSEQ(@"12", [popup itemTitleAtIndex:12]); |
Ben Murdoch | ba5b9a6 | 2013-08-12 14:20:17 +0100 | [diff] [blame] | 109 | |
| 110 | NSPopUpButton* yearPopup = base::mac::ObjCCastStrict<NSPopUpButton>( |
| 111 | [container_ getField:CREDIT_CARD_EXP_4_DIGIT_YEAR]); |
| 112 | EXPECT_EQ(11, [yearPopup numberOfItems]); |
| 113 | EXPECT_NSEQ(@"Year", [yearPopup itemTitleAtIndex:0]); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | TEST_F(AutofillSectionContainerTest, OutputMatchesDefinition) { |
| 117 | using namespace autofill; |
| 118 | using namespace testing; |
| 119 | |
| 120 | const DetailInput kTestInputs[] = { |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 121 | { DetailInput::LONG, EMAIL_ADDRESS }, |
Torne (Richard Coles) | a3f6a49 | 2013-12-18 16:25:09 +0000 | [diff] [blame] | 122 | { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH }, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 123 | }; |
| 124 | autofill::MonthComboboxModel comboModel; |
| 125 | DetailInputs inputs; |
| 126 | inputs.push_back(kTestInputs[0]); |
| 127 | inputs.push_back(kTestInputs[1]); |
| 128 | |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 129 | EXPECT_CALL(delegate_, RequestedFieldsForSection(section_)) |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 130 | .WillOnce(ReturnRef(inputs)); |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 131 | EXPECT_CALL(delegate_, ComboboxModelForAutofillType(EMAIL_ADDRESS)) |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 132 | .WillRepeatedly(ReturnNull()); |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 133 | EXPECT_CALL(delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH)) |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 134 | .WillRepeatedly(Return(&comboModel)); |
| 135 | |
| 136 | ResetContainer(); |
| 137 | |
| 138 | NSPopUpButton* popup = base::mac::ObjCCastStrict<NSPopUpButton>( |
| 139 | [container_ getField:CREDIT_CARD_EXP_MONTH]); |
| 140 | [popup selectItemWithTitle:@"02"]; |
| 141 | [[container_ getField:EMAIL_ADDRESS] setStringValue:@"magic@example.org"]; |
| 142 | |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 143 | autofill::FieldValueMap output; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 144 | [container_ getInputs:&output]; |
| 145 | |
| 146 | ASSERT_EQ(inputs.size(), output.size()); |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 147 | EXPECT_EQ(ASCIIToUTF16("magic@example.org"), output[inputs[0].type]); |
| 148 | EXPECT_EQ(ASCIIToUTF16("02"), output[inputs[1].type]); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | TEST_F(AutofillSectionContainerTest, SuggestionsPopulatedByController) { |
| 152 | ui::SimpleMenuModel model(NULL); |
| 153 | model.AddItem(10, ASCIIToUTF16("a")); |
| 154 | model.AddItem(11, ASCIIToUTF16("b")); |
| 155 | |
| 156 | using namespace autofill; |
| 157 | using namespace testing; |
| 158 | |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 159 | EXPECT_CALL(delegate_, MenuModelForSection(section_)) |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 160 | .WillOnce(Return(&model)); |
| 161 | |
| 162 | ResetContainer(); |
| 163 | MenuButton* button = nil; |
| 164 | for (id item in [[container_ view] subviews]) { |
| 165 | if ([item isKindOfClass:[MenuButton class]]) { |
| 166 | button = item; |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | NSMenu* menu = [button attachedMenu]; |
| 172 | // Expect _three_ items - popup menus need an empty first item. |
| 173 | ASSERT_EQ(3, [menu numberOfItems]); |
| 174 | |
| 175 | EXPECT_NSEQ(@"a", [[menu itemAtIndex:1] title]); |
| 176 | EXPECT_NSEQ(@"b", [[menu itemAtIndex:2] title]); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 177 | } |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 178 | |
| 179 | TEST_F(AutofillSectionContainerTest, FieldsAreInitiallyValid) { |
| 180 | using namespace autofill; |
| 181 | using namespace testing; |
| 182 | |
| 183 | const DetailInput kTestInputs[] = { |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 184 | { DetailInput::LONG, EMAIL_ADDRESS }, |
Torne (Richard Coles) | a3f6a49 | 2013-12-18 16:25:09 +0000 | [diff] [blame] | 185 | { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH }, |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | MonthComboboxModel comboModel; |
| 189 | DetailInputs inputs; |
| 190 | inputs.push_back(kTestInputs[0]); |
| 191 | inputs.push_back(kTestInputs[1]); |
| 192 | |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 193 | EXPECT_CALL(delegate_, RequestedFieldsForSection(section_)) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 194 | .WillOnce(ReturnRef(inputs)); |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 195 | EXPECT_CALL(delegate_, ComboboxModelForAutofillType(EMAIL_ADDRESS)) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 196 | .WillRepeatedly(ReturnNull()); |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 197 | EXPECT_CALL(delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH)) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 198 | .WillRepeatedly(Return(&comboModel)); |
| 199 | |
| 200 | ResetContainer(); |
| 201 | NSControl<AutofillInputField>* field = [container_ getField:EMAIL_ADDRESS]; |
| 202 | EXPECT_FALSE([field invalid]); |
| 203 | field = [container_ getField:CREDIT_CARD_EXP_MONTH]; |
| 204 | EXPECT_FALSE([field invalid]); |
| 205 | } |
| 206 | |
| 207 | TEST_F(AutofillSectionContainerTest, ControllerInformsValidity) { |
| 208 | using namespace autofill; |
| 209 | using namespace testing; |
| 210 | |
| 211 | const DetailInput kTestInputs[] = { |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 212 | { DetailInput::LONG, EMAIL_ADDRESS }, |
| 213 | { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH }, |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | MonthComboboxModel comboModel; |
| 217 | DetailInputs inputs; |
| 218 | inputs.push_back(kTestInputs[0]); |
| 219 | inputs.push_back(kTestInputs[1]); |
| 220 | |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 221 | ValidityMessages validity, validity2; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 222 | |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 223 | validity.Set(EMAIL_ADDRESS, |
| 224 | ValidityMessage(ASCIIToUTF16("Some error message"), false)); |
| 225 | validity2.Set(CREDIT_CARD_EXP_MONTH, |
| 226 | ValidityMessage(ASCIIToUTF16("Some other error message"), false)); |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 227 | EXPECT_CALL(delegate_, RequestedFieldsForSection(section_)) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 228 | .WillOnce(ReturnRef(inputs)); |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 229 | EXPECT_CALL(delegate_, ComboboxModelForAutofillType(EMAIL_ADDRESS)) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 230 | .WillRepeatedly(ReturnNull()); |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 231 | EXPECT_CALL(delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH)) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 232 | .WillRepeatedly(Return(&comboModel)); |
| 233 | |
| 234 | ResetContainer(); |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 235 | autofill::FieldValueMap output; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 236 | [container_ getInputs:&output]; |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 237 | EXPECT_CALL(delegate_, InputsAreValid(section_, output)) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 238 | .WillOnce(Return(validity)) |
| 239 | .WillOnce(Return(validity2)); |
| 240 | |
| 241 | [container_ validateFor:VALIDATE_FINAL]; |
| 242 | NSControl<AutofillInputField>* field = [container_ getField:EMAIL_ADDRESS]; |
| 243 | EXPECT_TRUE([field invalid]); |
| 244 | field = [container_ getField:CREDIT_CARD_EXP_MONTH]; |
| 245 | EXPECT_FALSE([field invalid]); |
| 246 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 247 | [container_ validateFor:VALIDATE_FINAL]; |
| 248 | field = [container_ getField:EMAIL_ADDRESS]; |
| 249 | EXPECT_FALSE([field invalid]); |
| 250 | field = [container_ getField:CREDIT_CARD_EXP_MONTH]; |
| 251 | EXPECT_TRUE([field invalid]); |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 252 | } |