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
3 // found in the LICENSE file.
5 #import <Cocoa/Cocoa.h>
7 #import "base/mac/scoped_nsobject.h"
8 #import "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #import "chrome/app/chrome_command_ids.h" // For translate menu command ids.
11 #include "chrome/browser/infobars/infobar_service.h"
12 #import "chrome/browser/translate/translate_infobar_delegate.h"
13 #import "chrome/browser/translate/translate_tab_helper.h"
14 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
15 #import "chrome/browser/ui/cocoa/infobars/before_translate_infobar_controller.h"
16 #import "chrome/browser/ui/cocoa/infobars/infobar_cocoa.h"
17 #import "chrome/browser/ui/cocoa/infobars/translate_infobar_base.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/translate/core/browser/translate_language_list.h"
20 #import "content/public/browser/web_contents.h"
21 #include "ipc/ipc_message.h"
22 #import "testing/gmock/include/gmock/gmock.h"
23 #import "testing/gtest/include/gtest/gtest.h"
24 #import "testing/platform_test.h"
26 using content::WebContents;
30 // All states the translate toolbar can assume.
31 translate::TranslateStep kTranslateToolbarStates[] = {
32 translate::TRANSLATE_STEP_BEFORE_TRANSLATE,
33 translate::TRANSLATE_STEP_AFTER_TRANSLATE,
34 translate::TRANSLATE_STEP_TRANSLATING,
35 translate::TRANSLATE_STEP_TRANSLATE_ERROR};
37 class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate {
39 MockTranslateInfoBarDelegate(content::WebContents* web_contents,
40 translate::TranslateStep step,
41 TranslateErrors::Type error,
43 : TranslateInfoBarDelegate(web_contents,
52 MOCK_METHOD0(Translate, void());
53 MOCK_METHOD0(RevertTranslation, void());
55 MOCK_METHOD0(TranslationDeclined, void());
57 virtual bool IsTranslatableLanguageByPrefs() OVERRIDE { return true; }
58 MOCK_METHOD0(ToggleTranslatableLanguageByPrefs, void());
59 virtual bool IsSiteBlacklisted() OVERRIDE { return false; }
60 MOCK_METHOD0(ToggleSiteBlacklist, void());
61 virtual bool ShouldAlwaysTranslate() OVERRIDE { return false; }
62 MOCK_METHOD0(ToggleAlwaysTranslate, void());
67 class TranslationInfoBarTest : public CocoaProfileTest {
69 TranslationInfoBarTest() : CocoaProfileTest(), infobar_(NULL) {
72 // Each test gets a single Mock translate delegate for the lifetime of
74 virtual void SetUp() OVERRIDE {
75 TranslateLanguageList::DisableUpdate();
76 CocoaProfileTest::SetUp();
78 WebContents::Create(WebContents::CreateParams(profile())));
79 InfoBarService::CreateForWebContents(web_contents_.get());
80 TranslateTabHelper::CreateForWebContents(web_contents_.get());
83 virtual void TearDown() OVERRIDE {
85 infobar_->CloseSoon();
88 CocoaProfileTest::TearDown();
91 void CreateInfoBar(translate::TranslateStep type) {
92 TranslateErrors::Type error = TranslateErrors::NONE;
93 if (type == translate::TRANSLATE_STEP_TRANSLATE_ERROR)
94 error = TranslateErrors::NETWORK;
96 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
97 [[infobar_controller_ view] removeFromSuperview];
99 scoped_ptr<TranslateInfoBarDelegate> delegate(
100 new MockTranslateInfoBarDelegate(web_contents_.get(), type, error,
101 profile->GetPrefs()));
102 scoped_ptr<infobars::InfoBar> infobar(
103 TranslateInfoBarDelegate::CreateInfoBar(delegate.Pass()));
105 infobar_->CloseSoon();
106 infobar_ = static_cast<InfoBarCocoa*>(infobar.release());
107 infobar_->SetOwner(InfoBarService::FromWebContents(web_contents_.get()));
109 infobar_controller_.reset([static_cast<TranslateInfoBarControllerBase*>(
110 infobar_->controller()) retain]);
112 // We need to set the window to be wide so that the options button
113 // doesn't overlap the other buttons.
114 [test_window() setContentSize:NSMakeSize(2000, 500)];
115 [[infobar_controller_ view] setFrame:NSMakeRect(0, 0, 2000, 500)];
116 [[test_window() contentView] addSubview:[infobar_controller_ view]];
119 MockTranslateInfoBarDelegate* infobar_delegate() const {
120 return static_cast<MockTranslateInfoBarDelegate*>(infobar_->delegate());
123 scoped_ptr<WebContents> web_contents_;
124 InfoBarCocoa* infobar_; // weak, deletes itself
125 base::scoped_nsobject<TranslateInfoBarControllerBase> infobar_controller_;
128 // Check that we can instantiate a Translate Infobar correctly.
129 TEST_F(TranslationInfoBarTest, Instantiate) {
130 CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
131 ASSERT_TRUE(infobar_controller_.get());
134 // Check that clicking the Translate button calls Translate().
135 TEST_F(TranslationInfoBarTest, TranslateCalledOnButtonPress) {
136 CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
138 EXPECT_CALL(*infobar_delegate(), Translate()).Times(1);
139 [infobar_controller_ ok:nil];
142 // Check that clicking the "Retry" button calls Translate() when we're
143 // in the error mode - http://crbug.com/41315 .
144 TEST_F(TranslationInfoBarTest, TranslateCalledInErrorMode) {
145 CreateInfoBar(translate::TRANSLATE_STEP_TRANSLATE_ERROR);
147 EXPECT_CALL(*infobar_delegate(), Translate()).Times(1);
149 [infobar_controller_ ok:nil];
152 // Check that clicking the "Show Original button calls RevertTranslation().
153 TEST_F(TranslationInfoBarTest, RevertCalledOnButtonPress) {
154 CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
156 EXPECT_CALL(*infobar_delegate(), RevertTranslation()).Times(1);
157 [infobar_controller_ showOriginal:nil];
160 // Check that items in the options menu are hooked up correctly.
161 TEST_F(TranslationInfoBarTest, OptionsMenuItemsHookedUp) {
162 CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
163 EXPECT_CALL(*infobar_delegate(), Translate())
166 [infobar_controller_ rebuildOptionsMenu:NO];
167 NSMenu* optionsMenu = [infobar_controller_ optionsMenu];
168 NSArray* optionsMenuItems = [optionsMenu itemArray];
170 EXPECT_EQ(7U, [optionsMenuItems count]);
172 // First item is the options menu button's title, so there's no need to test
173 // that the target on that is setup correctly.
174 for (NSUInteger i = 1; i < [optionsMenuItems count]; ++i) {
175 NSMenuItem* item = [optionsMenuItems objectAtIndex:i];
176 if (![item isSeparatorItem])
177 EXPECT_EQ([item target], infobar_controller_.get());
179 NSMenuItem* alwaysTranslateLanguateItem = [optionsMenuItems objectAtIndex:1];
180 NSMenuItem* neverTranslateLanguateItem = [optionsMenuItems objectAtIndex:2];
181 NSMenuItem* neverTranslateSiteItem = [optionsMenuItems objectAtIndex:3];
183 NSMenuItem* reportBadLanguageItem = [optionsMenuItems objectAtIndex:5];
184 NSMenuItem* aboutTranslateItem = [optionsMenuItems objectAtIndex:6];
187 EXPECT_CALL(*infobar_delegate(), ToggleAlwaysTranslate())
189 [infobar_controller_ optionsMenuChanged:alwaysTranslateLanguateItem];
193 EXPECT_CALL(*infobar_delegate(), ToggleTranslatableLanguageByPrefs())
195 [infobar_controller_ optionsMenuChanged:neverTranslateLanguateItem];
199 EXPECT_CALL(*infobar_delegate(), ToggleSiteBlacklist())
201 [infobar_controller_ optionsMenuChanged:neverTranslateSiteItem];
205 // Can't mock these effectively, so just check that the tag is set
207 EXPECT_EQ(IDC_TRANSLATE_REPORT_BAD_LANGUAGE_DETECTION,
208 [reportBadLanguageItem tag]);
209 EXPECT_EQ(IDC_TRANSLATE_OPTIONS_ABOUT, [aboutTranslateItem tag]);
213 // Check that selecting a new item from the "Source Language" popup in "before
214 // translate" mode doesn't trigger a translation or change state.
215 // http://crbug.com/36666
216 TEST_F(TranslationInfoBarTest, Bug36666) {
217 CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
218 EXPECT_CALL(*infobar_delegate(), Translate())
221 int arbitrary_index = 2;
222 [infobar_controller_ sourceLanguageModified:arbitrary_index];
223 EXPECT_CALL(*infobar_delegate(), Translate())
227 // Check that the infobar lays itself out correctly when instantiated in
228 // each of the states.
229 // http://crbug.com/36895
230 TEST_F(TranslationInfoBarTest, Bug36895) {
231 for (size_t i = 0; i < arraysize(kTranslateToolbarStates); ++i) {
232 CreateInfoBar(kTranslateToolbarStates[i]);
233 EXPECT_CALL(*infobar_delegate(), Translate())
236 [infobar_controller_ verifyLayout]) << "Layout wrong, for state #" << i;
240 // Verify that the infobar shows the "Always translate this language" button
241 // after doing 3 translations.
242 TEST_F(TranslationInfoBarTest, TriggerShowAlwaysTranslateButton) {
243 scoped_ptr<TranslatePrefs> translate_prefs(
244 TranslateTabHelper::CreateTranslatePrefs(profile()->GetPrefs()));
245 translate_prefs->ResetTranslationAcceptedCount("en");
246 for (int i = 0; i < 4; ++i) {
247 translate_prefs->IncrementTranslationAcceptedCount("en");
249 CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
250 BeforeTranslateInfobarController* controller =
251 (BeforeTranslateInfobarController*)infobar_controller_.get();
252 EXPECT_TRUE([[controller alwaysTranslateButton] superview] != nil);
253 EXPECT_TRUE([[controller neverTranslateButton] superview] == nil);
256 // Verify that the infobar shows the "Never translate this language" button
257 // after denying 3 translations.
258 TEST_F(TranslationInfoBarTest, TriggerShowNeverTranslateButton) {
259 scoped_ptr<TranslatePrefs> translate_prefs(
260 TranslateTabHelper::CreateTranslatePrefs(profile()->GetPrefs()));
261 translate_prefs->ResetTranslationDeniedCount("en");
262 for (int i = 0; i < 4; ++i) {
263 translate_prefs->IncrementTranslationDeniedCount("en");
265 CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
266 BeforeTranslateInfobarController* controller =
267 (BeforeTranslateInfobarController*)infobar_controller_.get();
268 EXPECT_TRUE([[controller alwaysTranslateButton] superview] == nil);
269 EXPECT_TRUE([[controller neverTranslateButton] superview] != nil);