Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / infobars / translate_infobar_unittest.mm
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.
4
5 #import <Cocoa/Cocoa.h>
6
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"
25
26 using content::WebContents;
27
28 namespace {
29
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};
36
37 class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate {
38  public:
39   MockTranslateInfoBarDelegate(content::WebContents* web_contents,
40                                translate::TranslateStep step,
41                                TranslateErrors::Type error,
42                                PrefService* prefs)
43       : TranslateInfoBarDelegate(web_contents,
44                                  step,
45                                  NULL,
46                                  "en",
47                                  "es",
48                                  error,
49                                  prefs,
50                                  false) {}
51
52   MOCK_METHOD0(Translate, void());
53   MOCK_METHOD0(RevertTranslation, void());
54
55   MOCK_METHOD0(TranslationDeclined, void());
56
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());
63 };
64
65 }  // namespace
66
67 class TranslationInfoBarTest : public CocoaProfileTest {
68  public:
69   TranslationInfoBarTest() : CocoaProfileTest(), infobar_(NULL) {
70   }
71
72   // Each test gets a single Mock translate delegate for the lifetime of
73   // the test.
74   virtual void SetUp() OVERRIDE {
75     TranslateLanguageList::DisableUpdate();
76     CocoaProfileTest::SetUp();
77     web_contents_.reset(
78         WebContents::Create(WebContents::CreateParams(profile())));
79     InfoBarService::CreateForWebContents(web_contents_.get());
80     TranslateTabHelper::CreateForWebContents(web_contents_.get());
81   }
82
83   virtual void TearDown() OVERRIDE {
84     if (infobar_) {
85       infobar_->CloseSoon();
86       infobar_ = NULL;
87     }
88     CocoaProfileTest::TearDown();
89   }
90
91   void CreateInfoBar(translate::TranslateStep type) {
92     TranslateErrors::Type error = TranslateErrors::NONE;
93     if (type == translate::TRANSLATE_STEP_TRANSLATE_ERROR)
94       error = TranslateErrors::NETWORK;
95     Profile* profile =
96         Profile::FromBrowserContext(web_contents_->GetBrowserContext());
97     [[infobar_controller_ view] removeFromSuperview];
98
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()));
104     if (infobar_)
105       infobar_->CloseSoon();
106     infobar_ = static_cast<InfoBarCocoa*>(infobar.release());
107     infobar_->SetOwner(InfoBarService::FromWebContents(web_contents_.get()));
108
109     infobar_controller_.reset([static_cast<TranslateInfoBarControllerBase*>(
110         infobar_->controller()) retain]);
111
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]];
117   }
118
119   MockTranslateInfoBarDelegate* infobar_delegate() const {
120     return static_cast<MockTranslateInfoBarDelegate*>(infobar_->delegate());
121   }
122
123   scoped_ptr<WebContents> web_contents_;
124   InfoBarCocoa* infobar_;  // weak, deletes itself
125   base::scoped_nsobject<TranslateInfoBarControllerBase> infobar_controller_;
126 };
127
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());
132 }
133
134 // Check that clicking the Translate button calls Translate().
135 TEST_F(TranslationInfoBarTest, TranslateCalledOnButtonPress) {
136   CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
137
138   EXPECT_CALL(*infobar_delegate(), Translate()).Times(1);
139   [infobar_controller_ ok:nil];
140 }
141
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);
146
147   EXPECT_CALL(*infobar_delegate(), Translate()).Times(1);
148
149   [infobar_controller_ ok:nil];
150 }
151
152 // Check that clicking the "Show Original button calls RevertTranslation().
153 TEST_F(TranslationInfoBarTest, RevertCalledOnButtonPress) {
154   CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
155
156   EXPECT_CALL(*infobar_delegate(), RevertTranslation()).Times(1);
157   [infobar_controller_ showOriginal:nil];
158 }
159
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())
164     .Times(0);
165
166   [infobar_controller_ rebuildOptionsMenu:NO];
167   NSMenu* optionsMenu = [infobar_controller_ optionsMenu];
168   NSArray* optionsMenuItems = [optionsMenu itemArray];
169
170   EXPECT_EQ(7U, [optionsMenuItems count]);
171
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());
178   }
179   NSMenuItem* alwaysTranslateLanguateItem = [optionsMenuItems objectAtIndex:1];
180   NSMenuItem* neverTranslateLanguateItem = [optionsMenuItems objectAtIndex:2];
181   NSMenuItem* neverTranslateSiteItem = [optionsMenuItems objectAtIndex:3];
182   // Separator at 4.
183   NSMenuItem* reportBadLanguageItem = [optionsMenuItems objectAtIndex:5];
184   NSMenuItem* aboutTranslateItem = [optionsMenuItems objectAtIndex:6];
185
186   {
187     EXPECT_CALL(*infobar_delegate(), ToggleAlwaysTranslate())
188     .Times(1);
189     [infobar_controller_ optionsMenuChanged:alwaysTranslateLanguateItem];
190   }
191
192   {
193     EXPECT_CALL(*infobar_delegate(), ToggleTranslatableLanguageByPrefs())
194     .Times(1);
195     [infobar_controller_ optionsMenuChanged:neverTranslateLanguateItem];
196   }
197
198   {
199     EXPECT_CALL(*infobar_delegate(), ToggleSiteBlacklist())
200     .Times(1);
201     [infobar_controller_ optionsMenuChanged:neverTranslateSiteItem];
202   }
203
204   {
205     // Can't mock these effectively, so just check that the tag is set
206     // correctly.
207     EXPECT_EQ(IDC_TRANSLATE_REPORT_BAD_LANGUAGE_DETECTION,
208               [reportBadLanguageItem tag]);
209     EXPECT_EQ(IDC_TRANSLATE_OPTIONS_ABOUT, [aboutTranslateItem tag]);
210   }
211 }
212
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())
219     .Times(0);
220
221   int arbitrary_index = 2;
222   [infobar_controller_ sourceLanguageModified:arbitrary_index];
223   EXPECT_CALL(*infobar_delegate(), Translate())
224     .Times(0);
225 }
226
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())
234       .Times(0);
235     EXPECT_TRUE(
236         [infobar_controller_ verifyLayout]) << "Layout wrong, for state #" << i;
237   }
238 }
239
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");
248   }
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);
254 }
255
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");
264   }
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);
270 }