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;
29 @implementation AutofillDialogWindowController (AutofillDialogViewTesterCocoa)
31 - (void)setTextContents:(NSString*)text
32 forType:(autofill::ServerFieldType)type {
33 for (size_t i = autofill::SECTION_MIN; i <= autofill::SECTION_MAX; ++i) {
34 autofill::DialogSection section = static_cast<autofill::DialogSection>(i);
35 if (!dialog_->delegate()->SectionIsActive(section))
37 // TODO(groby): Need to find the section for an input directly - wasteful.
38 [[mainContainer_ sectionForId:section] setFieldValue:text forType:type];
42 - (void)setTextContents:(NSString*)text
43 ofSuggestionForSection:(autofill::DialogSection)section {
44 [[mainContainer_ sectionForId:section] setSuggestionFieldValue:text];
47 - (void)activateFieldForType:(autofill::ServerFieldType)type {
48 for (size_t i = autofill::SECTION_MIN; i <= autofill::SECTION_MAX; ++i) {
49 autofill::DialogSection section = static_cast<autofill::DialogSection>(i);
50 if (!dialog_->delegate()->SectionIsActive(section))
52 [[mainContainer_ sectionForId:section] activateFieldForType:type];
56 - (content::WebContents*)getSignInWebContents {
57 return [signInContainer_ webContents];
60 - (BOOL)isShowingOverlay {
61 return ![[overlayController_ view] isHidden];
68 scoped_ptr<AutofillDialogViewTester> AutofillDialogViewTester::For(
69 AutofillDialogView* dialog) {
70 return scoped_ptr<AutofillDialogViewTester>(
71 new AutofillDialogViewTesterCocoa(
72 static_cast<AutofillDialogCocoa*>(dialog)));
75 AutofillDialogViewTesterCocoa::AutofillDialogViewTesterCocoa(
76 AutofillDialogCocoa* dialog)
79 AutofillDialogViewTesterCocoa::~AutofillDialogViewTesterCocoa() {}
81 void AutofillDialogViewTesterCocoa::SubmitForTesting() {
82 [controller() accept:nil];
85 void AutofillDialogViewTesterCocoa::CancelForTesting() {
86 [controller() cancel:nil];
89 base::string16 AutofillDialogViewTesterCocoa::GetTextContentsOfInput(
90 ServerFieldType type) {
91 for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
92 DialogSection section = static_cast<DialogSection>(i);
93 if (!dialog_->delegate()->SectionIsActive(section))
95 FieldValueMap contents;
96 [controller() getInputs:&contents forSection:section];
97 FieldValueMap::const_iterator it = contents.find(type);
98 if (it != contents.end())
103 return base::string16();
106 void AutofillDialogViewTesterCocoa::SetTextContentsOfInput(
107 ServerFieldType type,
108 const base::string16& contents) {
109 [controller() setTextContents:base::SysUTF16ToNSString(contents)
113 void AutofillDialogViewTesterCocoa::SetTextContentsOfSuggestionInput(
114 DialogSection section,
115 const base::string16& text) {
116 [controller() setTextContents:base::SysUTF16ToNSString(text)
117 ofSuggestionForSection:section];
120 void AutofillDialogViewTesterCocoa::ActivateInput(ServerFieldType type) {
121 [controller() activateFieldForType:type];
124 gfx::Size AutofillDialogViewTesterCocoa::GetSize() const {
125 return gfx::Size(NSSizeToCGSize([[controller() window] frame].size));
128 content::WebContents* AutofillDialogViewTesterCocoa::GetSignInWebContents() {
129 return [controller() getSignInWebContents];
132 bool AutofillDialogViewTesterCocoa::IsShowingOverlay() const {
133 return [controller() isShowingOverlay];
136 AutofillDialogWindowController*
137 AutofillDialogViewTesterCocoa::controller() const {
138 return dialog_->sheet_delegate_;
141 } // namespace autofill