[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / infobars / translate_infobar_unittest.mm
bloba0463a7b00e62cd2e1e0b9edd08916661fc5302a
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 #include "components/translate/core/browser/translate_manager.h"
21 #import "content/public/browser/web_contents.h"
22 #include "ipc/ipc_message.h"
23 #import "testing/gmock/include/gmock/gmock.h"
24 #import "testing/gtest/include/gtest/gtest.h"
25 #import "testing/platform_test.h"
27 using content::WebContents;
29 namespace {
31 // All states the translate toolbar can assume.
32 translate::TranslateStep kTranslateToolbarStates[] = {
33     translate::TRANSLATE_STEP_BEFORE_TRANSLATE,
34     translate::TRANSLATE_STEP_AFTER_TRANSLATE,
35     translate::TRANSLATE_STEP_TRANSLATING,
36     translate::TRANSLATE_STEP_TRANSLATE_ERROR};
38 class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate {
39  public:
40   MockTranslateInfoBarDelegate(content::WebContents* web_contents,
41                                translate::TranslateStep step,
42                                TranslateErrors::Type error)
43       : TranslateInfoBarDelegate(
44             TranslateTabHelper::GetManagerFromWebContents(
45                 web_contents)->GetWeakPtr(),
46             false,
47             step,
48             NULL,
49             "en",
50             "es",
51             error,
52             false) {}
54   MOCK_METHOD0(Translate, void());
55   MOCK_METHOD0(RevertTranslation, void());
57   MOCK_METHOD0(TranslationDeclined, void());
59   virtual bool IsTranslatableLanguageByPrefs() OVERRIDE { return true; }
60   MOCK_METHOD0(ToggleTranslatableLanguageByPrefs, void());
61   virtual bool IsSiteBlacklisted() OVERRIDE { return false; }
62   MOCK_METHOD0(ToggleSiteBlacklist, void());
63   virtual bool ShouldAlwaysTranslate() OVERRIDE { return false; }
64   MOCK_METHOD0(ToggleAlwaysTranslate, void());
67 }  // namespace
69 class TranslationInfoBarTest : public CocoaProfileTest {
70  public:
71   TranslationInfoBarTest() : CocoaProfileTest(), infobar_(NULL) {
72   }
74   // Each test gets a single Mock translate delegate for the lifetime of
75   // the test.
76   virtual void SetUp() OVERRIDE {
77     TranslateLanguageList::DisableUpdate();
78     CocoaProfileTest::SetUp();
79     web_contents_.reset(
80         WebContents::Create(WebContents::CreateParams(profile())));
81     InfoBarService::CreateForWebContents(web_contents_.get());
82     TranslateTabHelper::CreateForWebContents(web_contents_.get());
83   }
85   virtual void TearDown() OVERRIDE {
86     if (infobar_) {
87       infobar_->CloseSoon();
88       infobar_ = NULL;
89     }
90     CocoaProfileTest::TearDown();
91   }
93   void CreateInfoBar(translate::TranslateStep type) {
94     TranslateErrors::Type error = TranslateErrors::NONE;
95     if (type == translate::TRANSLATE_STEP_TRANSLATE_ERROR)
96       error = TranslateErrors::NETWORK;
97     [[infobar_controller_ view] removeFromSuperview];
99     scoped_ptr<TranslateInfoBarDelegate> delegate(
100         new MockTranslateInfoBarDelegate(web_contents_.get(), type, error));
101     scoped_ptr<infobars::InfoBar> infobar(
102         TranslateInfoBarDelegate::CreateInfoBar(delegate.Pass()));
103     if (infobar_)
104       infobar_->CloseSoon();
105     infobar_ = static_cast<InfoBarCocoa*>(infobar.release());
106     infobar_->SetOwner(InfoBarService::FromWebContents(web_contents_.get()));
108     infobar_controller_.reset([static_cast<TranslateInfoBarControllerBase*>(
109         infobar_->controller()) retain]);
111     // We need to set the window to be wide so that the options button
112     // doesn't overlap the other buttons.
113     [test_window() setContentSize:NSMakeSize(2000, 500)];
114     [[infobar_controller_ view] setFrame:NSMakeRect(0, 0, 2000, 500)];
115     [[test_window() contentView] addSubview:[infobar_controller_ view]];
116   }
118   MockTranslateInfoBarDelegate* infobar_delegate() const {
119     return static_cast<MockTranslateInfoBarDelegate*>(infobar_->delegate());
120   }
122   scoped_ptr<WebContents> web_contents_;
123   InfoBarCocoa* infobar_;  // weak, deletes itself
124   base::scoped_nsobject<TranslateInfoBarControllerBase> infobar_controller_;
127 // Check that we can instantiate a Translate Infobar correctly.
128 TEST_F(TranslationInfoBarTest, Instantiate) {
129   CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
130   ASSERT_TRUE(infobar_controller_.get());
133 // Check that clicking the Translate button calls Translate().
134 TEST_F(TranslationInfoBarTest, TranslateCalledOnButtonPress) {
135   CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
137   EXPECT_CALL(*infobar_delegate(), Translate()).Times(1);
138   [infobar_controller_ ok:nil];
141 // Check that clicking the "Retry" button calls Translate() when we're
142 // in the error mode - http://crbug.com/41315 .
143 TEST_F(TranslationInfoBarTest, TranslateCalledInErrorMode) {
144   CreateInfoBar(translate::TRANSLATE_STEP_TRANSLATE_ERROR);
146   EXPECT_CALL(*infobar_delegate(), Translate()).Times(1);
148   [infobar_controller_ ok:nil];
151 // Check that clicking the "Show Original button calls RevertTranslation().
152 TEST_F(TranslationInfoBarTest, RevertCalledOnButtonPress) {
153   CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
155   EXPECT_CALL(*infobar_delegate(), RevertTranslation()).Times(1);
156   [infobar_controller_ showOriginal:nil];
159 // Check that items in the options menu are hooked up correctly.
160 TEST_F(TranslationInfoBarTest, OptionsMenuItemsHookedUp) {
161   CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
162   EXPECT_CALL(*infobar_delegate(), Translate())
163     .Times(0);
165   [infobar_controller_ rebuildOptionsMenu:NO];
166   NSMenu* optionsMenu = [infobar_controller_ optionsMenu];
167   NSArray* optionsMenuItems = [optionsMenu itemArray];
169   EXPECT_EQ(7U, [optionsMenuItems count]);
171   // First item is the options menu button's title, so there's no need to test
172   // that the target on that is setup correctly.
173   for (NSUInteger i = 1; i < [optionsMenuItems count]; ++i) {
174     NSMenuItem* item = [optionsMenuItems objectAtIndex:i];
175     if (![item isSeparatorItem])
176       EXPECT_EQ([item target], infobar_controller_.get());
177   }
178   NSMenuItem* alwaysTranslateLanguateItem = [optionsMenuItems objectAtIndex:1];
179   NSMenuItem* neverTranslateLanguateItem = [optionsMenuItems objectAtIndex:2];
180   NSMenuItem* neverTranslateSiteItem = [optionsMenuItems objectAtIndex:3];
181   // Separator at 4.
182   NSMenuItem* reportBadLanguageItem = [optionsMenuItems objectAtIndex:5];
183   NSMenuItem* aboutTranslateItem = [optionsMenuItems objectAtIndex:6];
185   {
186     EXPECT_CALL(*infobar_delegate(), ToggleAlwaysTranslate())
187     .Times(1);
188     [infobar_controller_ optionsMenuChanged:alwaysTranslateLanguateItem];
189   }
191   {
192     EXPECT_CALL(*infobar_delegate(), ToggleTranslatableLanguageByPrefs())
193     .Times(1);
194     [infobar_controller_ optionsMenuChanged:neverTranslateLanguateItem];
195   }
197   {
198     EXPECT_CALL(*infobar_delegate(), ToggleSiteBlacklist())
199     .Times(1);
200     [infobar_controller_ optionsMenuChanged:neverTranslateSiteItem];
201   }
203   {
204     // Can't mock these effectively, so just check that the tag is set
205     // correctly.
206     EXPECT_EQ(IDC_TRANSLATE_REPORT_BAD_LANGUAGE_DETECTION,
207               [reportBadLanguageItem tag]);
208     EXPECT_EQ(IDC_TRANSLATE_OPTIONS_ABOUT, [aboutTranslateItem tag]);
209   }
212 // Check that selecting a new item from the "Source Language" popup in "before
213 // translate" mode doesn't trigger a translation or change state.
214 // http://crbug.com/36666
215 TEST_F(TranslationInfoBarTest, Bug36666) {
216   CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
217   EXPECT_CALL(*infobar_delegate(), Translate())
218     .Times(0);
220   int arbitrary_index = 2;
221   [infobar_controller_ sourceLanguageModified:arbitrary_index];
222   EXPECT_CALL(*infobar_delegate(), Translate())
223     .Times(0);
226 // Check that the infobar lays itself out correctly when instantiated in
227 // each of the states.
228 // http://crbug.com/36895
229 TEST_F(TranslationInfoBarTest, Bug36895) {
230   for (size_t i = 0; i < arraysize(kTranslateToolbarStates); ++i) {
231     CreateInfoBar(kTranslateToolbarStates[i]);
232     EXPECT_CALL(*infobar_delegate(), Translate())
233       .Times(0);
234     EXPECT_TRUE(
235         [infobar_controller_ verifyLayout]) << "Layout wrong, for state #" << i;
236   }
239 // Verify that the infobar shows the "Always translate this language" button
240 // after doing 3 translations.
241 TEST_F(TranslationInfoBarTest, TriggerShowAlwaysTranslateButton) {
242   scoped_ptr<TranslatePrefs> translate_prefs(
243       TranslateTabHelper::CreateTranslatePrefs(profile()->GetPrefs()));
244   translate_prefs->ResetTranslationAcceptedCount("en");
245   for (int i = 0; i < 4; ++i) {
246     translate_prefs->IncrementTranslationAcceptedCount("en");
247   }
248   CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
249   BeforeTranslateInfobarController* controller =
250       (BeforeTranslateInfobarController*)infobar_controller_.get();
251   EXPECT_TRUE([[controller alwaysTranslateButton] superview] !=  nil);
252   EXPECT_TRUE([[controller neverTranslateButton] superview] == nil);
255 // Verify that the infobar shows the "Never translate this language" button
256 // after denying 3 translations.
257 TEST_F(TranslationInfoBarTest, TriggerShowNeverTranslateButton) {
258   scoped_ptr<TranslatePrefs> translate_prefs(
259       TranslateTabHelper::CreateTranslatePrefs(profile()->GetPrefs()));
260   translate_prefs->ResetTranslationDeniedCount("en");
261   for (int i = 0; i < 4; ++i) {
262     translate_prefs->IncrementTranslationDeniedCount("en");
263   }
264   CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE);
265   BeforeTranslateInfobarController* controller =
266       (BeforeTranslateInfobarController*)infobar_controller_.get();
267   EXPECT_TRUE([[controller alwaysTranslateButton] superview] == nil);
268   EXPECT_TRUE([[controller neverTranslateButton] superview] != nil);