Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: chrome/browser/ui/cocoa/infobars/translate_infobar_unittest.mm

Issue 22694006: Infobar system refactor. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import <Cocoa/Cocoa.h> 5 #import <Cocoa/Cocoa.h>
6 6
7 #import "base/mac/scoped_nsobject.h" 7 #import "base/mac/scoped_nsobject.h"
8 #import "base/strings/string_util.h" 8 #import "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #import "chrome/app/chrome_command_ids.h" // For translate menu command ids. 10 #import "chrome/app/chrome_command_ids.h" // For translate menu command ids.
(...skipping 18 matching lines...) Expand all
29 // All states the translate toolbar can assume. 29 // All states the translate toolbar can assume.
30 TranslateInfoBarDelegate::Type kTranslateToolbarStates[] = { 30 TranslateInfoBarDelegate::Type kTranslateToolbarStates[] = {
31 TranslateInfoBarDelegate::BEFORE_TRANSLATE, 31 TranslateInfoBarDelegate::BEFORE_TRANSLATE,
32 TranslateInfoBarDelegate::AFTER_TRANSLATE, 32 TranslateInfoBarDelegate::AFTER_TRANSLATE,
33 TranslateInfoBarDelegate::TRANSLATING, 33 TranslateInfoBarDelegate::TRANSLATING,
34 TranslateInfoBarDelegate::TRANSLATION_ERROR 34 TranslateInfoBarDelegate::TRANSLATION_ERROR
35 }; 35 };
36 36
37 class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate { 37 class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate {
38 public: 38 public:
39 MockTranslateInfoBarDelegate(InfoBarService* infobar_service, 39 MockTranslateInfoBarDelegate(TranslateInfoBarDelegate::Type type,
40 TranslateInfoBarDelegate::Type type,
41 TranslateErrors::Type error, 40 TranslateErrors::Type error,
42 PrefService* prefs, 41 PrefService* prefs,
43 ShortcutConfiguration config) 42 ShortcutConfiguration config)
44 : TranslateInfoBarDelegate(infobar_service, type, NULL, "en", "es", error, 43 : TranslateInfoBarDelegate(type, NULL, "en", "es", error, prefs, config) {
45 prefs, config) {
46 } 44 }
47 45
48 MOCK_METHOD0(Translate, void()); 46 MOCK_METHOD0(Translate, void());
49 MOCK_METHOD0(RevertTranslation, void()); 47 MOCK_METHOD0(RevertTranslation, void());
50 48
51 MOCK_METHOD0(TranslationDeclined, void()); 49 MOCK_METHOD0(TranslationDeclined, void());
52 50
53 virtual bool IsTranslatableLanguageByPrefs() OVERRIDE { return true; } 51 virtual bool IsTranslatableLanguageByPrefs() OVERRIDE { return true; }
54 MOCK_METHOD0(ToggleTranslatableLanguageByPrefs, void()); 52 MOCK_METHOD0(ToggleTranslatableLanguageByPrefs, void());
55 virtual bool IsSiteBlacklisted() OVERRIDE { return false; } 53 virtual bool IsSiteBlacklisted() OVERRIDE { return false; }
56 MOCK_METHOD0(ToggleSiteBlacklist, void()); 54 MOCK_METHOD0(ToggleSiteBlacklist, void());
57 virtual bool ShouldAlwaysTranslate() OVERRIDE { return false; } 55 virtual bool ShouldAlwaysTranslate() OVERRIDE { return false; }
58 MOCK_METHOD0(ToggleAlwaysTranslate, void()); 56 MOCK_METHOD0(ToggleAlwaysTranslate, void());
59 }; 57 };
60 58
59 } // namespace
60
61 class TranslationInfoBarTest : public CocoaProfileTest { 61 class TranslationInfoBarTest : public CocoaProfileTest {
62 public: 62 public:
63 TranslationInfoBarTest() : CocoaProfileTest(), infobar_(NULL) {
64 }
65
63 // Each test gets a single Mock translate delegate for the lifetime of 66 // Each test gets a single Mock translate delegate for the lifetime of
64 // the test. 67 // the test.
65 virtual void SetUp() OVERRIDE { 68 virtual void SetUp() OVERRIDE {
66 TranslateLanguageList::DisableUpdate(); 69 TranslateLanguageList::DisableUpdate();
67 CocoaProfileTest::SetUp(); 70 CocoaProfileTest::SetUp();
68 web_contents_.reset( 71 web_contents_.reset(
69 WebContents::Create(WebContents::CreateParams(profile()))); 72 WebContents::Create(WebContents::CreateParams(profile())));
70 InfoBarService::CreateForWebContents(web_contents_.get()); 73 InfoBarService::CreateForWebContents(web_contents_.get());
71 CreateInfoBar();
72 } 74 }
73 75
74 void CreateInfoBar() { 76 virtual void TearDown() OVERRIDE {
75 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); 77 if (infobar_) {
78 infobar_->CloseSoon();
79 infobar_ = NULL;
80 }
81 CocoaProfileTest::TearDown();
76 } 82 }
77 83
78 void CreateInfoBar(TranslateInfoBarDelegate::Type type) { 84 void CreateInfoBar(TranslateInfoBarDelegate::Type type) {
79 TranslateErrors::Type error = TranslateErrors::NONE; 85 TranslateErrors::Type error = TranslateErrors::NONE;
80 if (type == TranslateInfoBarDelegate::TRANSLATION_ERROR) 86 if (type == TranslateInfoBarDelegate::TRANSLATION_ERROR)
81 error = TranslateErrors::NETWORK; 87 error = TranslateErrors::NETWORK;
82 InfoBarService* infobar_service =
83 InfoBarService::FromWebContents(web_contents_.get());
84 Profile* profile = 88 Profile* profile =
85 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); 89 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
86 ShortcutConfiguration config; 90 ShortcutConfiguration config;
87 config.never_translate_min_count = 3; 91 config.never_translate_min_count = 3;
88 config.always_translate_min_count = 3; 92 config.always_translate_min_count = 3;
89 infobar_delegate_.reset(new MockTranslateInfoBarDelegate(
90 infobar_service, type, error, profile->GetPrefs(), config));
91 [[infobar_controller_ view] removeFromSuperview]; 93 [[infobar_controller_ view] removeFromSuperview];
92 94
93 InfoBarDelegate* base = 95 scoped_ptr<TranslateInfoBarDelegate> delegate(
94 static_cast<InfoBarDelegate*>(infobar_delegate_.get()); 96 new MockTranslateInfoBarDelegate(type, error, profile->GetPrefs(),
95 infobar_.reset( 97 config));
96 static_cast<InfoBarCocoa*>(base->CreateInfoBar(infobar_service))); 98 scoped_ptr<InfoBar> infobar(
99 TranslateInfoBarDelegate::CreateInfoBar(delegate.Pass()));
100 if (infobar_)
101 infobar_->CloseSoon();
102 infobar_ = static_cast<InfoBarCocoa*>(infobar.release());
103 infobar_->SetOwner(InfoBarService::FromWebContents(web_contents_.get()));
104
97 infobar_controller_.reset([static_cast<TranslateInfoBarControllerBase*>( 105 infobar_controller_.reset([static_cast<TranslateInfoBarControllerBase*>(
98 infobar_->controller()) retain]); 106 infobar_->controller()) retain]);
99 107
100 // We need to set the window to be wide so that the options button 108 // We need to set the window to be wide so that the options button
101 // doesn't overlap the other buttons. 109 // doesn't overlap the other buttons.
102 [test_window() setContentSize:NSMakeSize(2000, 500)]; 110 [test_window() setContentSize:NSMakeSize(2000, 500)];
103 [[infobar_controller_ view] setFrame:NSMakeRect(0, 0, 2000, 500)]; 111 [[infobar_controller_ view] setFrame:NSMakeRect(0, 0, 2000, 500)];
104 [[test_window() contentView] addSubview:[infobar_controller_ view]]; 112 [[test_window() contentView] addSubview:[infobar_controller_ view]];
105 } 113 }
106 114
115 MockTranslateInfoBarDelegate* infobar_delegate() const {
116 return static_cast<MockTranslateInfoBarDelegate*>(infobar_->delegate());
117 }
118
107 scoped_ptr<WebContents> web_contents_; 119 scoped_ptr<WebContents> web_contents_;
108 scoped_ptr<MockTranslateInfoBarDelegate> infobar_delegate_; 120 InfoBarCocoa* infobar_; // weak, deletes itself
109 scoped_ptr<InfoBarCocoa> infobar_;
110 base::scoped_nsobject<TranslateInfoBarControllerBase> infobar_controller_; 121 base::scoped_nsobject<TranslateInfoBarControllerBase> infobar_controller_;
111 }; 122 };
112 123
113 // Check that we can instantiate a Translate Infobar correctly. 124 // Check that we can instantiate a Translate Infobar correctly.
114 TEST_F(TranslationInfoBarTest, Instantiate) { 125 TEST_F(TranslationInfoBarTest, Instantiate) {
115 CreateInfoBar(); 126 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE);
116 ASSERT_TRUE(infobar_controller_.get()); 127 ASSERT_TRUE(infobar_controller_.get());
117 } 128 }
118 129
119 // Check that clicking the Translate button calls Translate(). 130 // Check that clicking the Translate button calls Translate().
120 TEST_F(TranslationInfoBarTest, TranslateCalledOnButtonPress) { 131 TEST_F(TranslationInfoBarTest, TranslateCalledOnButtonPress) {
121 CreateInfoBar(); 132 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE);
122 133
123 EXPECT_CALL(*infobar_delegate_, Translate()).Times(1); 134 EXPECT_CALL(*infobar_delegate(), Translate()).Times(1);
124 [infobar_controller_ ok:nil]; 135 [infobar_controller_ ok:nil];
125 } 136 }
126 137
127 // Check that clicking the "Retry" button calls Translate() when we're 138 // Check that clicking the "Retry" button calls Translate() when we're
128 // in the error mode - http://crbug.com/41315 . 139 // in the error mode - http://crbug.com/41315 .
129 TEST_F(TranslationInfoBarTest, TranslateCalledInErrorMode) { 140 TEST_F(TranslationInfoBarTest, TranslateCalledInErrorMode) {
130 CreateInfoBar(TranslateInfoBarDelegate::TRANSLATION_ERROR); 141 CreateInfoBar(TranslateInfoBarDelegate::TRANSLATION_ERROR);
131 142
132 EXPECT_CALL(*infobar_delegate_, Translate()).Times(1); 143 EXPECT_CALL(*infobar_delegate(), Translate()).Times(1);
133 144
134 [infobar_controller_ ok:nil]; 145 [infobar_controller_ ok:nil];
135 } 146 }
136 147
137 // Check that clicking the "Show Original button calls RevertTranslation(). 148 // Check that clicking the "Show Original button calls RevertTranslation().
138 TEST_F(TranslationInfoBarTest, RevertCalledOnButtonPress) { 149 TEST_F(TranslationInfoBarTest, RevertCalledOnButtonPress) {
139 CreateInfoBar(); 150 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE);
140 151
141 EXPECT_CALL(*infobar_delegate_, RevertTranslation()).Times(1); 152 EXPECT_CALL(*infobar_delegate(), RevertTranslation()).Times(1);
142 [infobar_controller_ showOriginal:nil]; 153 [infobar_controller_ showOriginal:nil];
143 } 154 }
144 155
145 // Check that items in the options menu are hooked up correctly. 156 // Check that items in the options menu are hooked up correctly.
146 TEST_F(TranslationInfoBarTest, OptionsMenuItemsHookedUp) { 157 TEST_F(TranslationInfoBarTest, OptionsMenuItemsHookedUp) {
147 EXPECT_CALL(*infobar_delegate_, Translate()) 158 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE);
159 EXPECT_CALL(*infobar_delegate(), Translate())
148 .Times(0); 160 .Times(0);
149 161
150 [infobar_controller_ rebuildOptionsMenu:NO]; 162 [infobar_controller_ rebuildOptionsMenu:NO];
151 NSMenu* optionsMenu = [infobar_controller_ optionsMenu]; 163 NSMenu* optionsMenu = [infobar_controller_ optionsMenu];
152 NSArray* optionsMenuItems = [optionsMenu itemArray]; 164 NSArray* optionsMenuItems = [optionsMenu itemArray];
153 165
154 EXPECT_EQ(7U, [optionsMenuItems count]); 166 EXPECT_EQ(7U, [optionsMenuItems count]);
155 167
156 // First item is the options menu button's title, so there's no need to test 168 // First item is the options menu button's title, so there's no need to test
157 // that the target on that is setup correctly. 169 // that the target on that is setup correctly.
158 for (NSUInteger i = 1; i < [optionsMenuItems count]; ++i) { 170 for (NSUInteger i = 1; i < [optionsMenuItems count]; ++i) {
159 NSMenuItem* item = [optionsMenuItems objectAtIndex:i]; 171 NSMenuItem* item = [optionsMenuItems objectAtIndex:i];
160 if (![item isSeparatorItem]) 172 if (![item isSeparatorItem])
161 EXPECT_EQ([item target], infobar_controller_.get()); 173 EXPECT_EQ([item target], infobar_controller_.get());
162 } 174 }
163 NSMenuItem* alwaysTranslateLanguateItem = [optionsMenuItems objectAtIndex:1]; 175 NSMenuItem* alwaysTranslateLanguateItem = [optionsMenuItems objectAtIndex:1];
164 NSMenuItem* neverTranslateLanguateItem = [optionsMenuItems objectAtIndex:2]; 176 NSMenuItem* neverTranslateLanguateItem = [optionsMenuItems objectAtIndex:2];
165 NSMenuItem* neverTranslateSiteItem = [optionsMenuItems objectAtIndex:3]; 177 NSMenuItem* neverTranslateSiteItem = [optionsMenuItems objectAtIndex:3];
166 // Separator at 4. 178 // Separator at 4.
167 NSMenuItem* reportBadLanguageItem = [optionsMenuItems objectAtIndex:5]; 179 NSMenuItem* reportBadLanguageItem = [optionsMenuItems objectAtIndex:5];
168 NSMenuItem* aboutTranslateItem = [optionsMenuItems objectAtIndex:6]; 180 NSMenuItem* aboutTranslateItem = [optionsMenuItems objectAtIndex:6];
169 181
170 { 182 {
171 EXPECT_CALL(*infobar_delegate_, ToggleAlwaysTranslate()) 183 EXPECT_CALL(*infobar_delegate(), ToggleAlwaysTranslate())
172 .Times(1); 184 .Times(1);
173 [infobar_controller_ optionsMenuChanged:alwaysTranslateLanguateItem]; 185 [infobar_controller_ optionsMenuChanged:alwaysTranslateLanguateItem];
174 } 186 }
175 187
176 { 188 {
177 EXPECT_CALL(*infobar_delegate_, ToggleTranslatableLanguageByPrefs()) 189 EXPECT_CALL(*infobar_delegate(), ToggleTranslatableLanguageByPrefs())
178 .Times(1); 190 .Times(1);
179 [infobar_controller_ optionsMenuChanged:neverTranslateLanguateItem]; 191 [infobar_controller_ optionsMenuChanged:neverTranslateLanguateItem];
180 } 192 }
181 193
182 { 194 {
183 EXPECT_CALL(*infobar_delegate_, ToggleSiteBlacklist()) 195 EXPECT_CALL(*infobar_delegate(), ToggleSiteBlacklist())
184 .Times(1); 196 .Times(1);
185 [infobar_controller_ optionsMenuChanged:neverTranslateSiteItem]; 197 [infobar_controller_ optionsMenuChanged:neverTranslateSiteItem];
186 } 198 }
187 199
188 { 200 {
189 // Can't mock these effectively, so just check that the tag is set 201 // Can't mock these effectively, so just check that the tag is set
190 // correctly. 202 // correctly.
191 EXPECT_EQ(IDC_TRANSLATE_REPORT_BAD_LANGUAGE_DETECTION, 203 EXPECT_EQ(IDC_TRANSLATE_REPORT_BAD_LANGUAGE_DETECTION,
192 [reportBadLanguageItem tag]); 204 [reportBadLanguageItem tag]);
193 EXPECT_EQ(IDC_TRANSLATE_OPTIONS_ABOUT, [aboutTranslateItem tag]); 205 EXPECT_EQ(IDC_TRANSLATE_OPTIONS_ABOUT, [aboutTranslateItem tag]);
194 } 206 }
195 } 207 }
196 208
197 // Check that selecting a new item from the "Source Language" popup in "before 209 // Check that selecting a new item from the "Source Language" popup in "before
198 // translate" mode doesn't trigger a translation or change state. 210 // translate" mode doesn't trigger a translation or change state.
199 // http://crbug.com/36666 211 // http://crbug.com/36666
200 TEST_F(TranslationInfoBarTest, Bug36666) { 212 TEST_F(TranslationInfoBarTest, Bug36666) {
201 EXPECT_CALL(*infobar_delegate_, Translate()) 213 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE);
214 EXPECT_CALL(*infobar_delegate(), Translate())
202 .Times(0); 215 .Times(0);
203 216
204 CreateInfoBar();
205 int arbitrary_index = 2; 217 int arbitrary_index = 2;
206 [infobar_controller_ sourceLanguageModified:arbitrary_index]; 218 [infobar_controller_ sourceLanguageModified:arbitrary_index];
207 EXPECT_CALL(*infobar_delegate_, Translate()) 219 EXPECT_CALL(*infobar_delegate(), Translate())
208 .Times(0); 220 .Times(0);
209 } 221 }
210 222
211 // Check that the infobar lays itself out correctly when instantiated in 223 // Check that the infobar lays itself out correctly when instantiated in
212 // each of the states. 224 // each of the states.
213 // http://crbug.com/36895 225 // http://crbug.com/36895
214 TEST_F(TranslationInfoBarTest, Bug36895) { 226 TEST_F(TranslationInfoBarTest, Bug36895) {
215 EXPECT_CALL(*infobar_delegate_, Translate())
216 .Times(0);
217
218 for (size_t i = 0; i < arraysize(kTranslateToolbarStates); ++i) { 227 for (size_t i = 0; i < arraysize(kTranslateToolbarStates); ++i) {
219 CreateInfoBar(kTranslateToolbarStates[i]); 228 CreateInfoBar(kTranslateToolbarStates[i]);
229 EXPECT_CALL(*infobar_delegate(), Translate())
230 .Times(0);
220 EXPECT_TRUE( 231 EXPECT_TRUE(
221 [infobar_controller_ verifyLayout]) << "Layout wrong, for state #" << i; 232 [infobar_controller_ verifyLayout]) << "Layout wrong, for state #" << i;
222 } 233 }
223 } 234 }
224 235
225 // Verify that the infobar shows the "Always translate this language" button 236 // Verify that the infobar shows the "Always translate this language" button
226 // after doing 3 translations. 237 // after doing 3 translations.
227 TEST_F(TranslationInfoBarTest, TriggerShowAlwaysTranslateButton) { 238 TEST_F(TranslationInfoBarTest, TriggerShowAlwaysTranslateButton) {
228 TranslatePrefs translate_prefs(profile()->GetPrefs()); 239 TranslatePrefs translate_prefs(profile()->GetPrefs());
229 translate_prefs.ResetTranslationAcceptedCount("en"); 240 translate_prefs.ResetTranslationAcceptedCount("en");
(...skipping 14 matching lines...) Expand all
244 translate_prefs.ResetTranslationDeniedCount("en"); 255 translate_prefs.ResetTranslationDeniedCount("en");
245 for (int i = 0; i < 4; ++i) { 256 for (int i = 0; i < 4; ++i) {
246 translate_prefs.IncrementTranslationDeniedCount("en"); 257 translate_prefs.IncrementTranslationDeniedCount("en");
247 } 258 }
248 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); 259 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE);
249 BeforeTranslateInfobarController* controller = 260 BeforeTranslateInfobarController* controller =
250 (BeforeTranslateInfobarController*)infobar_controller_.get(); 261 (BeforeTranslateInfobarController*)infobar_controller_.get();
251 EXPECT_TRUE([[controller alwaysTranslateButton] superview] == nil); 262 EXPECT_TRUE([[controller alwaysTranslateButton] superview] == nil);
252 EXPECT_TRUE([[controller neverTranslateButton] superview] != nil); 263 EXPECT_TRUE([[controller neverTranslateButton] superview] != nil);
253 } 264 }
254
255 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/ui/cocoa/infobars/translate_infobar_base.mm ('k') | chrome/browser/ui/cocoa/keystone_infobar_delegate.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698