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"
12 #import "chrome/browser/ui/cocoa/autofill/autofill_sign_in_container.h"
15 // Mirrors the AutofillDialogViewTester API on the C++ side.
16 @interface AutofillDialogWindowController (AutofillDialogViewTesterCocoa)
18 - (void)setTextContents:(NSString*)text
19 forType:(autofill::ServerFieldType)type;
20 - (void)setTextContents:(NSString*)text
21 ofSuggestionForSection:(autofill::DialogSection)section;
22 - (void)activateFieldForType:(autofill::ServerFieldType)type;
23 - (content::WebContents*)getSignInWebContents;
24 - (BOOL)isShowingOverlay;
25 - (BOOL)isShowingSection:(autofill::DialogSection)section;
30 @implementation AutofillDialogWindowController (AutofillDialogViewTesterCocoa)
32 - (void)setTextContents:(NSString*)text
33 forType:(autofill::ServerFieldType)type {
34 for (size_t i = autofill::SECTION_MIN; i <= autofill::SECTION_MAX; ++i) {
35 autofill::DialogSection section = static_cast<autofill::DialogSection>(i);
36 if (!dialog_->delegate()->SectionIsActive(section))
38 // TODO(groby): Need to find the section for an input directly - wasteful.
39 [[mainContainer_ sectionForId:section] setFieldValue:text forType:type];
43 - (void)setTextContents:(NSString*)text
44 ofSuggestionForSection:(autofill::DialogSection)section {
45 [[mainContainer_ sectionForId:section] setSuggestionFieldValue:text];
48 - (void)activateFieldForType:(autofill::ServerFieldType)type {
49 for (size_t i = autofill::SECTION_MIN; i <= autofill::SECTION_MAX; ++i) {
50 autofill::DialogSection section = static_cast<autofill::DialogSection>(i);
51 if (!dialog_->delegate()->SectionIsActive(section))
53 [[mainContainer_ sectionForId:section] activateFieldForType:type];
57 - (content::WebContents*)getSignInWebContents {
58 return [signInContainer_ webContents];
61 - (BOOL)isShowingOverlay {
62 return ![[overlayController_ view] isHidden];
65 - (BOOL)isShowingSection:(autofill::DialogSection)section {
66 return ![[[mainContainer_ sectionForId:section] view] isHidden];
73 scoped_ptr<AutofillDialogViewTester> AutofillDialogViewTester::For(
74 AutofillDialogView* dialog) {
75 return scoped_ptr<AutofillDialogViewTester>(
76 new AutofillDialogViewTesterCocoa(
77 static_cast<AutofillDialogCocoa*>(dialog)));
80 AutofillDialogViewTesterCocoa::AutofillDialogViewTesterCocoa(
81 AutofillDialogCocoa* dialog)
84 AutofillDialogViewTesterCocoa::~AutofillDialogViewTesterCocoa() {}
86 void AutofillDialogViewTesterCocoa::SubmitForTesting() {
87 [controller() accept:nil];
90 void AutofillDialogViewTesterCocoa::CancelForTesting() {
91 [controller() cancel:nil];
94 base::string16 AutofillDialogViewTesterCocoa::GetTextContentsOfInput(
95 ServerFieldType type) {
96 for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
97 DialogSection section = static_cast<DialogSection>(i);
98 if (!dialog_->delegate()->SectionIsActive(section))
100 FieldValueMap contents;
101 [controller() getInputs:&contents forSection:section];
102 FieldValueMap::const_iterator it = contents.find(type);
103 if (it != contents.end())
108 return base::string16();
111 void AutofillDialogViewTesterCocoa::SetTextContentsOfInput(
112 ServerFieldType type,
113 const base::string16& contents) {
114 [controller() setTextContents:base::SysUTF16ToNSString(contents)
118 void AutofillDialogViewTesterCocoa::SetTextContentsOfSuggestionInput(
119 DialogSection section,
120 const base::string16& text) {
121 [controller() setTextContents:base::SysUTF16ToNSString(text)
122 ofSuggestionForSection:section];
125 void AutofillDialogViewTesterCocoa::ActivateInput(ServerFieldType type) {
126 [controller() activateFieldForType:type];
129 gfx::Size AutofillDialogViewTesterCocoa::GetSize() const {
130 return gfx::Size(NSSizeToCGSize([[controller() window] frame].size));
133 content::WebContents* AutofillDialogViewTesterCocoa::GetSignInWebContents() {
134 return [controller() getSignInWebContents];
137 bool AutofillDialogViewTesterCocoa::IsShowingOverlay() const {
138 return [controller() isShowingOverlay];
141 bool AutofillDialogViewTesterCocoa::IsShowingSection(
142 autofill::DialogSection section) const {
143 return [controller() isShowingSection:section];
146 AutofillDialogWindowController*
147 AutofillDialogViewTesterCocoa::controller() const {
148 return dialog_->sheet_delegate_;
151 } // namespace autofill