autofill_dialog_view_tester_cocoa.mm revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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.
4
5#include "chrome/browser/ui/cocoa/autofill/autofill_dialog_view_tester_cocoa.h"
6
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"
13
14
15// Mirrors the AutofillDialogViewTester API on the C++ side.
16@interface AutofillDialogWindowController (AutofillDialogViewTesterCocoa)
17
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;
26
27@end
28
29
30@implementation AutofillDialogWindowController (AutofillDialogViewTesterCocoa)
31
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))
37      continue;
38    // TODO(groby): Need to find the section for an input directly - wasteful.
39    [[mainContainer_ sectionForId:section] setFieldValue:text forType:type];
40  }
41}
42
43- (void)setTextContents:(NSString*)text
44 ofSuggestionForSection:(autofill::DialogSection)section {
45  [[mainContainer_ sectionForId:section] setSuggestionFieldValue:text];
46}
47
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))
52      continue;
53    [[mainContainer_ sectionForId:section] activateFieldForType:type];
54  }
55}
56
57- (content::WebContents*)getSignInWebContents {
58  return [signInContainer_ webContents];
59}
60
61- (BOOL)isShowingOverlay {
62  return ![[overlayController_ view] isHidden];
63}
64
65- (BOOL)isShowingSection:(autofill::DialogSection)section {
66  return ![[[mainContainer_ sectionForId:section] view] isHidden];
67}
68
69@end
70
71namespace autofill {
72
73scoped_ptr<AutofillDialogViewTester> AutofillDialogViewTester::For(
74    AutofillDialogView* dialog) {
75  return scoped_ptr<AutofillDialogViewTester>(
76      new AutofillDialogViewTesterCocoa(
77          static_cast<AutofillDialogCocoa*>(dialog)));
78}
79
80AutofillDialogViewTesterCocoa::AutofillDialogViewTesterCocoa(
81    AutofillDialogCocoa* dialog)
82    : dialog_(dialog) {}
83
84AutofillDialogViewTesterCocoa::~AutofillDialogViewTesterCocoa() {}
85
86void AutofillDialogViewTesterCocoa::SubmitForTesting() {
87  [controller() accept:nil];
88}
89
90void AutofillDialogViewTesterCocoa::CancelForTesting() {
91  [controller() cancel:nil];
92}
93
94base::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))
99      continue;
100    FieldValueMap contents;
101    [controller() getInputs:&contents forSection:section];
102    FieldValueMap::const_iterator it = contents.find(type);
103    if (it != contents.end())
104      return it->second;
105  }
106
107  NOTREACHED();
108  return base::string16();
109}
110
111void AutofillDialogViewTesterCocoa::SetTextContentsOfInput(
112    ServerFieldType type,
113    const base::string16& contents) {
114  [controller() setTextContents:base::SysUTF16ToNSString(contents)
115                        forType:type];
116}
117
118void AutofillDialogViewTesterCocoa::SetTextContentsOfSuggestionInput(
119    DialogSection section,
120    const base::string16& text) {
121  [controller() setTextContents:base::SysUTF16ToNSString(text)
122         ofSuggestionForSection:section];
123}
124
125void AutofillDialogViewTesterCocoa::ActivateInput(ServerFieldType type) {
126  [controller() activateFieldForType:type];
127}
128
129gfx::Size AutofillDialogViewTesterCocoa::GetSize() const {
130  return gfx::Size(NSSizeToCGSize([[controller() window] frame].size));
131}
132
133content::WebContents* AutofillDialogViewTesterCocoa::GetSignInWebContents() {
134  return [controller() getSignInWebContents];
135}
136
137bool AutofillDialogViewTesterCocoa::IsShowingOverlay() const {
138  return [controller() isShowingOverlay];
139}
140
141bool AutofillDialogViewTesterCocoa::IsShowingSection(
142    autofill::DialogSection section) const {
143  return [controller() isShowingSection:section];
144}
145
146AutofillDialogWindowController*
147    AutofillDialogViewTesterCocoa::controller() const {
148  return dialog_->sheet_delegate_;
149}
150
151}  // namespace autofill
152