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.
5 #include "chrome/browser/ui/cocoa/autofill/autofill_dialog_view_tester_cocoa.h"
7 #include "base/strings/sys_string_conversions.h"
8 #include "chrome/browser/ui/cocoa/autofill/autofill_dialog_cocoa.h"
9 #import "chrome/browser/ui/cocoa/autofill/autofill_dialog_window_controller.h"
10 #import "chrome/browser/ui/cocoa/autofill/autofill_main_container.h"
11 #import "chrome/browser/ui/cocoa/autofill/autofill_section_container.h"
14 // Mirrors the AutofillDialogViewTester API on the C++ side.
15 @interface AutofillDialogWindowController (AutofillDialogViewTesterCocoa)
17 - (void)setTextContents:(NSString*)text
18 forType:(autofill::ServerFieldType)type;
19 - (void)setTextContents:(NSString*)text
20 ofSuggestionForSection:(autofill::DialogSection)section;
21 - (void)activateFieldForType:(autofill::ServerFieldType)type;
22 - (BOOL)isShowingSection:(autofill::DialogSection)section;
27 @implementation AutofillDialogWindowController (AutofillDialogViewTesterCocoa)
29 - (void)setTextContents:(NSString*)text
30 forType:(autofill::ServerFieldType)type {
31 for (size_t i = autofill::SECTION_MIN; i <= autofill::SECTION_MAX; ++i) {
32 autofill::DialogSection section = static_cast<autofill::DialogSection>(i);
33 if (!dialog_->delegate()->SectionIsActive(section))
35 // TODO(groby): Need to find the section for an input directly - wasteful.
36 [[mainContainer_ sectionForId:section] setFieldValue:text forType:type];
40 - (void)setTextContents:(NSString*)text
41 ofSuggestionForSection:(autofill::DialogSection)section {
42 [[mainContainer_ sectionForId:section] setSuggestionFieldValue:text];
45 - (void)activateFieldForType:(autofill::ServerFieldType)type {
46 for (size_t i = autofill::SECTION_MIN; i <= autofill::SECTION_MAX; ++i) {
47 autofill::DialogSection section = static_cast<autofill::DialogSection>(i);
48 if (!dialog_->delegate()->SectionIsActive(section))
50 [[mainContainer_ sectionForId:section] activateFieldForType:type];
54 - (BOOL)isShowingSection:(autofill::DialogSection)section {
55 return ![[[mainContainer_ sectionForId:section] view] isHidden];
62 scoped_ptr<AutofillDialogViewTester> AutofillDialogViewTester::For(
63 AutofillDialogView* dialog) {
64 return scoped_ptr<AutofillDialogViewTester>(
65 new AutofillDialogViewTesterCocoa(
66 static_cast<AutofillDialogCocoa*>(dialog)));
69 AutofillDialogViewTesterCocoa::AutofillDialogViewTesterCocoa(
70 AutofillDialogCocoa* dialog)
73 AutofillDialogViewTesterCocoa::~AutofillDialogViewTesterCocoa() {}
75 void AutofillDialogViewTesterCocoa::SubmitForTesting() {
76 [controller() accept:nil];
79 void AutofillDialogViewTesterCocoa::CancelForTesting() {
80 [controller() cancel:nil];
83 base::string16 AutofillDialogViewTesterCocoa::GetTextContentsOfInput(
84 ServerFieldType type) {
85 for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
86 DialogSection section = static_cast<DialogSection>(i);
87 if (!dialog_->delegate()->SectionIsActive(section))
89 FieldValueMap contents;
90 [controller() getInputs:&contents forSection:section];
91 FieldValueMap::const_iterator it = contents.find(type);
92 if (it != contents.end())
97 return base::string16();
100 void AutofillDialogViewTesterCocoa::SetTextContentsOfInput(
101 ServerFieldType type,
102 const base::string16& contents) {
103 [controller() setTextContents:base::SysUTF16ToNSString(contents)
107 void AutofillDialogViewTesterCocoa::SetTextContentsOfSuggestionInput(
108 DialogSection section,
109 const base::string16& text) {
110 [controller() setTextContents:base::SysUTF16ToNSString(text)
111 ofSuggestionForSection:section];
114 void AutofillDialogViewTesterCocoa::ActivateInput(ServerFieldType type) {
115 [controller() activateFieldForType:type];
118 gfx::Size AutofillDialogViewTesterCocoa::GetSize() const {
119 return gfx::Size(NSSizeToCGSize([[controller() window] frame].size));
122 bool AutofillDialogViewTesterCocoa::IsShowingSection(
123 autofill::DialogSection section) const {
124 return [controller() isShowingSection:section];
127 AutofillDialogWindowController*
128 AutofillDialogViewTesterCocoa::controller() const {
129 return dialog_->sheet_delegate_;
132 } // namespace autofill