summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorjeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-15 12:16:30 +0000
committerjeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-15 12:16:30 +0000
commit6a6ce8b3a868dd1f94f8731ebd05abd39da46fec (patch)
tree1d13b28d170d29bd1ef503ba167eae1500afb705 /chrome
parent4757aecb7691429748791c315b17b7f402869197 (diff)
downloadchromium_src-6a6ce8b3a868dd1f94f8731ebd05abd39da46fec.zip
chromium_src-6a6ce8b3a868dd1f94f8731ebd05abd39da46fec.tar.gz
chromium_src-6a6ce8b3a868dd1f94f8731ebd05abd39da46fec.tar.bz2
Reland translate toolbar unit tests (originally landed as r41274, rolled back at r41284).
BUG=None TEST=New unit tests, tree should stay green after this checkin. Revert "Reland translate toolbar unit tests (originally landed as r41274, rolled back at r41284)." Revert r41569 - 10.6 unit tests not passing. Review URL: http://codereview.chromium.org/962001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@41579 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/cocoa/translate_infobar.h26
-rw-r--r--chrome/browser/cocoa/translate_infobar.mm84
-rw-r--r--chrome/browser/cocoa/translate_infobar_unittest.mm187
-rw-r--r--chrome/browser/gtk/translate_infobars.cc10
-rw-r--r--chrome/browser/translate/languages_menu_model.cc2
-rw-r--r--chrome/browser/translate/options_menu_model.cc4
-rw-r--r--chrome/browser/translate/translate_infobars_delegates.cc32
-rw-r--r--chrome/browser/translate/translate_infobars_delegates.h32
-rw-r--r--chrome/browser/views/infobars/translate_infobars.cc6
-rw-r--r--chrome/chrome_tests.gypi1
10 files changed, 341 insertions, 43 deletions
diff --git a/chrome/browser/cocoa/translate_infobar.h b/chrome/browser/cocoa/translate_infobar.h
index 410c60a..3df507b 100644
--- a/chrome/browser/cocoa/translate_infobar.h
+++ b/chrome/browser/cocoa/translate_infobar.h
@@ -9,6 +9,7 @@
#include "base/scoped_ptr.h"
#include "chrome/browser/translate/languages_menu_model.h"
#include "chrome/browser/translate/options_menu_model.h"
+#include "chrome/browser/translate/translate_infobars_delegates.h"
#include "chrome/common/notification_registrar.h"
class TranslateInfoBarMenuModel;
@@ -56,3 +57,28 @@ class TranslateNotificationObserverBridge;
}
@end
+
+@interface TranslateInfoBarController (TestingAPI)
+
+// Main function to update the toolbar graphic state and data model after
+// the state has changed.
+// Controls are moved around as needed and visibility changed to match the
+// current state.
+- (void)updateState;
+
+// Called when the source or target language selection changes in a menu.
+// |newLanguageIdx| is the index of the newly selected item in the appropriate
+// menu.
+- (void)sourceLanguageModified:(NSInteger)newLanguageIdx;
+- (void)targetLanguageModified:(NSInteger)newLanguageIdx;
+
+// Called when an item in one of the toolbar's menus is selected.
+- (void)menuItemSelected:(id)item;
+
+// Returns the underlying options menu.
+- (NSMenu*)optionsMenu;
+
+// Verifies that the layout of the infobar is correct for |state|.
+- (bool)verifyLayout:(TranslateInfoBarDelegate::TranslateState)state;
+
+@end
diff --git a/chrome/browser/cocoa/translate_infobar.mm b/chrome/browser/cocoa/translate_infobar.mm
index f727401..bb277db 100644
--- a/chrome/browser/cocoa/translate_infobar.mm
+++ b/chrome/browser/cocoa/translate_infobar.mm
@@ -15,7 +15,6 @@
#import "chrome/browser/cocoa/infobar_controller.h"
#import "chrome/browser/cocoa/infobar_gradient_view.h"
#include "chrome/browser/tab_contents/tab_contents.h"
-#include "chrome/browser/translate/translate_infobars_delegates.h"
#include "chrome/common/notification_service.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
@@ -58,6 +57,21 @@ void VerticallyCenterView(NSView *toMove) {
[toMove setFrame:viewFrame];
}
+// Check that the control |before| is ordered visually before the |after|
+// control.
+// Also, check that there is space between them.
+bool VerifyControlOrderAndSpacing(id before, id after) {
+ NSRect beforeFrame = [before frame];
+ NSRect afterFrame = [after frame];
+ NSUInteger spaceBetweenControls = -1;
+
+ spaceBetweenControls = NSMaxX(beforeFrame) - NSMinX(afterFrame);
+ // RTL case to be used when we have an RTL version of this UI.
+ // spaceBetweenControls = NSMaxX(afterFrame) - NSMinX(beforeFrame);
+
+ return (spaceBetweenControls >= 0);
+}
+
// Creates a label control in the style we need for the translate infobar's
// labels within |bounds|.
NSTextField* CreateLabel(NSRect bounds) {
@@ -134,12 +148,6 @@ class TranslateNotificationObserverBridge :
// Returns the main translate delegate.
- (TranslateInfoBarDelegate*)delegate;
-// Main function to update the toolbar graphic state and data model after
-// the state has changed.
-// Controls are moved around as needed and visibility changed to match the
-// current state.
-- (void)updateState;
-
// Make the infobar blue.
- (void)setInfoBarGradientColor;
@@ -540,6 +548,68 @@ class TranslateNotificationObserverBridge :
}
}
+#pragma mark TestingAPI
+- (NSMenu*)optionsMenu {
+ return [optionsPopUp_ menu];
+}
+
+- (bool)verifyLayout:(TranslateInfoBarDelegate::TranslateState)state {
+ NSArray* allControls = [NSArray arrayWithObjects:label_, label2_.get(),
+ label3_.get(), translatingLabel_.get(), fromLanguagePopUp_.get(),
+ toLanguagePopUp_.get(), optionsPopUp_.get(), closeButton_, nil];
+
+ // Array of all visible controls ordered from start -> end.
+ NSArray* visibleControls = nil;
+
+ switch (state) {
+ case TranslateInfoBarDelegate::kBeforeTranslate:
+ visibleControls = [NSArray arrayWithObjects:label_,
+ fromLanguagePopUp_.get(), label2_.get(), optionsPopUp_.get(),
+ closeButton_, nil];
+ break;
+ case TranslateInfoBarDelegate::kTranslating:
+ visibleControls = [NSArray arrayWithObjects:label_,
+ fromLanguagePopUp_.get(), label2_.get(), translatingLabel_.get(),
+ optionsPopUp_.get(), closeButton_, nil];
+ break;
+ case TranslateInfoBarDelegate::kAfterTranslate:
+ visibleControls = [NSArray arrayWithObjects:label_,
+ fromLanguagePopUp_.get(), label2_.get(), toLanguagePopUp_.get(),
+ optionsPopUp_.get(), closeButton_, nil];
+ break;
+ default:
+ NOTREACHED() << "Unknown state";
+ return false;
+ }
+
+ // Step 1: Make sure control visibility is what we expect.
+ for (NSUInteger i = 0; i < [allControls count]; ++i) {
+ id control = [allControls objectAtIndex:i];
+ bool hasSuperView = [control superview];
+ bool expectedVisibility = [visibleControls containsObject:control];
+ if (expectedVisibility != hasSuperView) {
+ LOG(ERROR) <<
+ "Control @" << i << (hasSuperView ? " has" : " doesn't have") <<
+ "a superview" << base::SysNSStringToUTF8([control description]);
+ return false;
+ }
+ }
+
+ // Step 2: Check that controls are ordered correctly with no overlap.
+ id previousControl = nil;
+ for (NSUInteger i = 0; i < [allControls count]; ++i) {
+ id control = [allControls objectAtIndex:i];
+ if (!VerifyControlOrderAndSpacing(previousControl, control)) {
+ LOG(ERROR) <<
+ "Control @" << i << " not ordered correctly: " <<
+ base::SysNSStringToUTF8([control description]);
+ return false;
+ }
+ previousControl = control;
+ }
+ return true;
+}
+
@end
#pragma mark CreateInfoBar implementation.
diff --git a/chrome/browser/cocoa/translate_infobar_unittest.mm b/chrome/browser/cocoa/translate_infobar_unittest.mm
new file mode 100644
index 0000000..9a7042a
--- /dev/null
+++ b/chrome/browser/cocoa/translate_infobar_unittest.mm
@@ -0,0 +1,187 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import <Cocoa/Cocoa.h>
+#import "chrome/browser/cocoa/translate_infobar.h"
+
+#include "base/scoped_nsobject.h"
+#include "base/string_util.h"
+#include "chrome/app/chrome_dll_resource.h" // For translate menu command ids.
+#import "chrome/browser/cocoa/cocoa_test_helper.h"
+#include "chrome/browser/translate/translate_infobars_delegates.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/platform_test.h"
+
+namespace {
+
+// All states the translate toolbar can assume.
+TranslateInfoBarDelegate::TranslateState kTranslateToolbarStates[] = {
+ TranslateInfoBarDelegate::kBeforeTranslate,
+ TranslateInfoBarDelegate::kTranslating,
+ TranslateInfoBarDelegate::kAfterTranslate};
+
+class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate {
+ public:
+ MockTranslateInfoBarDelegate() {
+ // Start out in the "Before Translate" state.
+ UpdateState(kBeforeTranslate);
+ }
+
+ virtual string16 GetDisplayNameForLocale(const std::string& language_code) {
+ return ASCIIToUTF16("Foo");
+ }
+
+ virtual bool IsLanguageBlacklisted() {
+ return false;
+ }
+
+ virtual bool IsSiteBlacklisted() {
+ return false;
+ }
+
+ virtual bool ShouldAlwaysTranslate() {
+ return false;
+ }
+
+ MOCK_METHOD0(Translate, void());
+ MOCK_METHOD0(TranslationDeclined, void());
+ MOCK_METHOD0(ToggleLanguageBlacklist, void());
+ MOCK_METHOD0(ToggleSiteBlacklist, void());
+ MOCK_METHOD0(ToggleAlwaysTranslate, void());
+
+};
+
+class TranslationBarInfoTest : public CocoaTest {
+ public:
+ scoped_ptr<MockTranslateInfoBarDelegate> infobar_delegate;
+ scoped_nsobject<TranslateInfoBarController> infobar_controller;
+
+ public:
+ // Each test gets a single Mock translate delegate for the lifetime of
+ // the test.
+ virtual void SetUp() {
+ CocoaTest::SetUp();
+ infobar_delegate.reset(new MockTranslateInfoBarDelegate);
+ }
+
+ void CreateInfoBar() {
+ CreateInfoBar(TranslateInfoBarDelegate::kBeforeTranslate);
+ }
+
+ void CreateInfoBar(TranslateInfoBarDelegate::TranslateState initial_state) {
+ infobar_delegate->UpdateState(initial_state);
+ [[infobar_controller view] removeFromSuperview];
+ infobar_controller.reset(
+ [[TranslateInfoBarController alloc]
+ initWithDelegate:infobar_delegate.get()]);
+ // Need to call this to get the view to load from nib.
+ [[test_window() contentView] addSubview:[infobar_controller view]];
+ }
+};
+
+// Check that we can instantiate a Translate Infobar correctly.
+TEST_F(TranslationBarInfoTest, Instantiate) {
+ CreateInfoBar();
+ ASSERT_TRUE(infobar_controller.get());
+}
+
+// Check that clicking the Translate button calls Translate().
+TEST_F(TranslationBarInfoTest, TranslateCalledOnButtonPress) {
+ CreateInfoBar();
+
+ EXPECT_CALL(*infobar_delegate, Translate())
+ .Times(1);
+ [infobar_controller ok:nil];
+}
+
+// Check that UI is layed out correctly as we transition synchronously through
+// toolbar states.
+TEST_F(TranslationBarInfoTest, StateTransitions) {
+ EXPECT_CALL(*infobar_delegate, Translate())
+ .Times(0);
+ CreateInfoBar();
+
+ for (size_t i = 0; i < arraysize(kTranslateToolbarStates); ++i) {
+ infobar_delegate->UpdateState(kTranslateToolbarStates[i]);
+
+ // First time around, the toolbar should already be layed out.
+ if (i != 0)
+ [infobar_controller updateState];
+
+ bool result =
+ [infobar_controller verifyLayout:kTranslateToolbarStates[i]];
+ EXPECT_TRUE(result) << "Layout wrong, for state #" << i;
+ }
+}
+
+// Check that items in the options menu are hooked up correctly.
+TEST_F(TranslationBarInfoTest, OptionsMenuItemsHookedUp) {
+ EXPECT_CALL(*infobar_delegate, Translate())
+ .Times(0);
+ CreateInfoBar();
+
+ NSMenu* optionsMenu = [infobar_controller optionsMenu];
+ NSArray* optionsMenuItems = [optionsMenu itemArray];
+
+ EXPECT_EQ([optionsMenuItems count], 4U);
+
+ // First item is the options menu button's title, so there's no need to test
+ // that the target on that is setup correctly.
+ for (NSUInteger i = 1; i < [optionsMenuItems count]; ++i) {
+ NSMenuItem* item = [optionsMenuItems objectAtIndex:i];
+ EXPECT_EQ([item target], infobar_controller.get());
+ }
+ NSMenuItem* neverTranslateLanguateItem = [optionsMenuItems objectAtIndex:1];
+ NSMenuItem* neverTranslateSiteItem = [optionsMenuItems objectAtIndex:2];
+ NSMenuItem* aboutTranslateItem = [optionsMenuItems objectAtIndex:3];
+
+ {
+ EXPECT_CALL(*infobar_delegate, ToggleLanguageBlacklist())
+ .Times(1);
+ [infobar_controller menuItemSelected:neverTranslateLanguateItem];
+ }
+
+ {
+ EXPECT_CALL(*infobar_delegate, ToggleSiteBlacklist())
+ .Times(1);
+ [infobar_controller menuItemSelected:neverTranslateSiteItem];
+ }
+
+ {
+ // Can't mock this effectively, so just check that the tag is set correctly.
+ EXPECT_EQ([aboutTranslateItem tag], IDC_TRANSLATE_OPTIONS_ABOUT);
+ }
+}
+
+// Check that selecting a new item from the "Source Language" popup in "before
+// translate" mode doesn't trigger a translation or change state.
+// http://crbug.com/36666
+TEST_F(TranslationBarInfoTest, Bug36666) {
+ EXPECT_CALL(*infobar_delegate, Translate())
+ .Times(0);
+
+ CreateInfoBar();
+ int arbitrary_index = 2;
+ [infobar_controller sourceLanguageModified:arbitrary_index];
+ EXPECT_EQ(infobar_delegate->state(),
+ TranslateInfoBarDelegate::kBeforeTranslate);
+}
+
+// Check that the infobar lays itself out correctly when instantiated in
+// each of the states.
+// http://crbug.com/36895
+TEST_F(TranslationBarInfoTest, Bug36895) {
+ EXPECT_CALL(*infobar_delegate, Translate())
+ .Times(0);
+
+ for (size_t i = 0; i < arraysize(kTranslateToolbarStates); ++i) {
+ CreateInfoBar(kTranslateToolbarStates[i]);
+ EXPECT_TRUE(
+ [infobar_controller verifyLayout:kTranslateToolbarStates[i]]) <<
+ "Layout wrong, for state #" << i;
+ }
+}
+
+} // namespace
diff --git a/chrome/browser/gtk/translate_infobars.cc b/chrome/browser/gtk/translate_infobars.cc
index 5f85985..4cffe6c0 100644
--- a/chrome/browser/gtk/translate_infobars.cc
+++ b/chrome/browser/gtk/translate_infobars.cc
@@ -35,13 +35,13 @@ void ReorderWidgetsTo(GtkWidget* box, GtkWidget** widgets) {
// Creates a combobox set up to display text from a list of language codes
// (translating the codes into the display string).
GtkWidget* BuildLanguageComboboxFrom(
+ TranslateInfoBarDelegate* delegate,
const std::vector<std::string>& languages) {
GtkListStore* model = gtk_list_store_new(1, G_TYPE_STRING);
for (std::vector<std::string>::const_iterator iter = languages.begin();
iter != languages.end(); ++iter) {
GtkTreeIter tree_iter;
- std::string name = UTF16ToUTF8(
- TranslateInfoBarDelegate::GetDisplayNameForLocale(*iter));
+ std::string name = UTF16ToUTF8(delegate->GetDisplayNameForLocale(*iter));
gtk_list_store_append(model, &tree_iter);
gtk_list_store_set(model, &tree_iter, 0, name.c_str(), -1);
}
@@ -202,7 +202,8 @@ void TranslateInfoBar::BuildWidgets() {
std::vector<std::string> orig_languages;
GetDelegate()->GetAvailableOriginalLanguages(&orig_languages);
- original_language_combobox_ = BuildLanguageComboboxFrom(orig_languages);
+ original_language_combobox_ = BuildLanguageComboboxFrom(GetDelegate(),
+ orig_languages);
g_signal_connect(original_language_combobox_, "changed",
G_CALLBACK(&OnOriginalModifiedThunk), this);
original_language_combobox_vbox_ = gtk_util::CenterWidgetInHBox(
@@ -210,7 +211,8 @@ void TranslateInfoBar::BuildWidgets() {
std::vector<std::string> target_languages;
GetDelegate()->GetAvailableTargetLanguages(&target_languages);
- target_language_combobox_ = BuildLanguageComboboxFrom(target_languages);
+ target_language_combobox_ = BuildLanguageComboboxFrom(GetDelegate(),
+ target_languages);
g_signal_connect(target_language_combobox_, "changed",
G_CALLBACK(&OnTargetModifiedThunk), this);
target_language_combobox_vbox_ = gtk_util::CenterWidgetInHBox(
diff --git a/chrome/browser/translate/languages_menu_model.cc b/chrome/browser/translate/languages_menu_model.cc
index ba749df..5231ce0 100644
--- a/chrome/browser/translate/languages_menu_model.cc
+++ b/chrome/browser/translate/languages_menu_model.cc
@@ -24,7 +24,7 @@ LanguagesMenuModel::LanguagesMenuModel(
}
std::vector<std::string>::const_iterator iter = languages.begin();
for (int i = base_command_id; iter != languages.end(); ++i, ++iter) {
- AddItem(i, TranslateInfoBarDelegate::GetDisplayNameForLocale(*iter));
+ AddItem(i, translate_delegate->GetDisplayNameForLocale(*iter));
}
}
diff --git a/chrome/browser/translate/options_menu_model.cc b/chrome/browser/translate/options_menu_model.cc
index 376e2cb..4364e07 100644
--- a/chrome/browser/translate/options_menu_model.cc
+++ b/chrome/browser/translate/options_menu_model.cc
@@ -13,7 +13,7 @@ OptionsMenuModel::OptionsMenuModel(menus::SimpleMenuModel::Delegate* delegate,
TranslateInfoBarDelegate* translate_delegate)
: menus::SimpleMenuModel(delegate) {
string16 original_language =
- TranslateInfoBarDelegate::GetDisplayNameForLocale(
+ translate_delegate->GetDisplayNameForLocale(
translate_delegate->original_lang_code());
TranslateInfoBarDelegate::TranslateState state = translate_delegate->state();
if (state == TranslateInfoBarDelegate::kBeforeTranslate) {
@@ -26,7 +26,7 @@ OptionsMenuModel::OptionsMenuModel(menus::SimpleMenuModel::Delegate* delegate,
IDS_TRANSLATE_INFOBAR_OPTIONS_NEVER_TRANSLATE_SITE));
} else if (state == TranslateInfoBarDelegate::kAfterTranslate) {
string16 target_language =
- TranslateInfoBarDelegate::GetDisplayNameForLocale(
+ translate_delegate->GetDisplayNameForLocale(
translate_delegate->target_lang_code());
AddCheckItem(IDC_TRANSLATE_OPTIONS_ALWAYS,
l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS_ALWAYS,
diff --git a/chrome/browser/translate/translate_infobars_delegates.cc b/chrome/browser/translate/translate_infobars_delegates.cc
index 948e337..062eba8 100644
--- a/chrome/browser/translate/translate_infobars_delegates.cc
+++ b/chrome/browser/translate/translate_infobars_delegates.cc
@@ -41,6 +41,12 @@ void TranslateInfoBarDelegate::InfoBarClosed() {
// TranslateInfoBarDelegate: public: -------------------------------------------
+string16 TranslateInfoBarDelegate::GetDisplayNameForLocale(
+ const std::string& language_code) {
+ return l10n_util::GetDisplayNameForLocale(
+ language_code, g_browser_process->GetApplicationLocale(), true);
+}
+
void TranslateInfoBarDelegate::UpdateState(TranslateState new_state) {
if (state_ != new_state)
state_ = new_state;
@@ -84,7 +90,7 @@ void TranslateInfoBarDelegate::TranslationDeclined() {
bool TranslateInfoBarDelegate::IsLanguageBlacklisted() {
if (state_ == kBeforeTranslate) {
never_translate_language_ =
- prefs_.IsLanguageBlacklisted(original_lang_code());
+ prefs_->IsLanguageBlacklisted(original_lang_code());
return never_translate_language_;
}
NOTREACHED() << "Invalid mehod called for translate state";
@@ -93,7 +99,7 @@ bool TranslateInfoBarDelegate::IsLanguageBlacklisted() {
bool TranslateInfoBarDelegate::IsSiteBlacklisted() {
if (state_ == kBeforeTranslate) {
- never_translate_site_ = prefs_.IsSiteBlacklisted(site_);
+ never_translate_site_ = prefs_->IsSiteBlacklisted(site_);
return never_translate_site_;
}
NOTREACHED() << "Invalid mehod called for translate state";
@@ -102,7 +108,7 @@ bool TranslateInfoBarDelegate::IsSiteBlacklisted() {
bool TranslateInfoBarDelegate::ShouldAlwaysTranslate() {
if (state_ == kAfterTranslate) {
- always_translate_ = prefs_.IsLanguagePairWhitelisted(original_lang_code(),
+ always_translate_ = prefs_->IsLanguagePairWhitelisted(original_lang_code(),
target_lang_code());
return always_translate_;
}
@@ -114,9 +120,9 @@ void TranslateInfoBarDelegate::ToggleLanguageBlacklist() {
if (state_ == kBeforeTranslate) {
never_translate_language_ = !never_translate_language_;
if (never_translate_language_)
- prefs_.BlacklistLanguage(original_lang_code());
+ prefs_->BlacklistLanguage(original_lang_code());
else
- prefs_.RemoveLanguageFromBlacklist(original_lang_code());
+ prefs_->RemoveLanguageFromBlacklist(original_lang_code());
} else {
NOTREACHED() << "Invalid method called for translate state";
}
@@ -126,9 +132,9 @@ void TranslateInfoBarDelegate::ToggleSiteBlacklist() {
if (state_ == kBeforeTranslate) {
never_translate_site_ = !never_translate_site_;
if (never_translate_site_)
- prefs_.BlacklistSite(site_);
+ prefs_->BlacklistSite(site_);
else
- prefs_.RemoveSiteFromBlacklist(site_);
+ prefs_->RemoveSiteFromBlacklist(site_);
} else {
NOTREACHED() << "Invalid mehod called for translate state";
}
@@ -138,9 +144,9 @@ void TranslateInfoBarDelegate::ToggleAlwaysTranslate() {
if (state_ == kAfterTranslate) {
always_translate_ = !always_translate_;
if (always_translate_)
- prefs_.WhitelistLanguagePair(original_lang_code(), target_lang_code());
+ prefs_->WhitelistLanguagePair(original_lang_code(), target_lang_code());
else
- prefs_.RemoveLanguagePairFromWhitelist(original_lang_code(),
+ prefs_->RemoveLanguagePairFromWhitelist(original_lang_code(),
target_lang_code());
} else {
NOTREACHED() << "Invalid mehod called for translate state";
@@ -209,12 +215,6 @@ TranslateInfoBarDelegate* TranslateInfoBarDelegate::Create(
original_lang_index, target_lang_index);
}
-string16 TranslateInfoBarDelegate::GetDisplayNameForLocale(
- const std::string& language_code) {
- return l10n_util::GetDisplayNameForLocale(
- language_code, g_browser_process->GetApplicationLocale(), true);
-}
-
// TranslateInfoBarDelegate: private: ------------------------------------------
TranslateInfoBarDelegate::TranslateInfoBarDelegate(TabContents* tab_contents,
@@ -222,7 +222,7 @@ TranslateInfoBarDelegate::TranslateInfoBarDelegate(TabContents* tab_contents,
int original_lang_index, int target_lang_index)
: InfoBarDelegate(tab_contents),
tab_contents_(tab_contents),
- prefs_(user_prefs),
+ prefs_(new TranslatePrefs(user_prefs)),
state_(state),
site_(url.HostNoBrackets()),
original_lang_index_(original_lang_index),
diff --git a/chrome/browser/translate/translate_infobars_delegates.h b/chrome/browser/translate/translate_infobars_delegates.h
index ce8cede..74753a8 100644
--- a/chrome/browser/translate/translate_infobars_delegates.h
+++ b/chrome/browser/translate/translate_infobars_delegates.h
@@ -35,14 +35,14 @@ class TranslateInfoBarDelegate : public InfoBarDelegate {
void GetAvailableTargetLanguages(std::vector<std::string>* languages);
void ModifyOriginalLanguage(int lang_index);
void ModifyTargetLanguage(int lang_index);
- void Translate();
- void TranslationDeclined();
- bool IsLanguageBlacklisted();
- void ToggleLanguageBlacklist();
- bool IsSiteBlacklisted();
- void ToggleSiteBlacklist();
- bool ShouldAlwaysTranslate();
- void ToggleAlwaysTranslate();
+ virtual void Translate();
+ virtual void TranslationDeclined();
+ virtual bool IsLanguageBlacklisted();
+ virtual void ToggleLanguageBlacklist();
+ virtual bool IsSiteBlacklisted();
+ virtual void ToggleSiteBlacklist();
+ virtual bool ShouldAlwaysTranslate();
+ virtual void ToggleAlwaysTranslate();
int original_lang_index() const {
return original_lang_index_;
@@ -94,11 +94,23 @@ class TranslateInfoBarDelegate : public InfoBarDelegate {
virtual void InfoBarClosed();
// Returns the printable version of the language code |language_code|.
- static string16 GetDisplayNameForLocale(const std::string& language_code);
+ virtual string16 GetDisplayNameForLocale(const std::string& language_code);
// Overridden from InfoBarDelegate:
virtual InfoBar* CreateInfoBar();
+ protected:
+ // For testing.
+ TranslateInfoBarDelegate() :
+ InfoBarDelegate(NULL),
+ tab_contents_(NULL),
+ state_(kBeforeTranslate),
+ original_lang_index_(0),
+ target_lang_index_(0),
+ never_translate_language_(false),
+ never_translate_site_(false),
+ always_translate_(false) {}
+
private:
TranslateInfoBarDelegate(TabContents* contents,
PrefService* user_prefs,
@@ -108,7 +120,7 @@ class TranslateInfoBarDelegate : public InfoBarDelegate {
int target_language_index);
TabContents* tab_contents_; // Weak.
- TranslatePrefs prefs_;
+ scoped_ptr<TranslatePrefs> prefs_;
TranslateState state_;
std::string site_;
int original_lang_index_;
diff --git a/chrome/browser/views/infobars/translate_infobars.cc b/chrome/browser/views/infobars/translate_infobars.cc
index 1caedec..89a8325 100644
--- a/chrome/browser/views/infobars/translate_infobars.cc
+++ b/chrome/browser/views/infobars/translate_infobars.cc
@@ -239,7 +239,7 @@ TranslateInfoBar::TranslateInfoBar(TranslateInfoBarDelegate* delegate)
AddChildView(icon_);
// Create original language menu button.
- string16 language_name = TranslateInfoBarDelegate::GetDisplayNameForLocale(
+ string16 language_name = delegate->GetDisplayNameForLocale(
GetDelegate()->original_lang_code());
original_language_menu_button_ = CreateMenuButton(kMenuIDOriginalLanguage,
UTF16ToWideHack(language_name));
@@ -269,7 +269,7 @@ void TranslateInfoBar::UpdateState(
CreateLabels();
if (!target_language_menu_button_) {
string16 language_name =
- TranslateInfoBarDelegate::GetDisplayNameForLocale(
+ GetDelegate()->GetDisplayNameForLocale(
GetDelegate()->target_lang_code());
target_language_menu_button_ = CreateMenuButton(kMenuIDTargetLanguage,
UTF16ToWideHack(language_name));
@@ -708,7 +708,7 @@ void TranslateInfoBar::OnLanguageModified(views::MenuButton* menu_button,
GetDelegate()->ModifyTargetLanguage(new_language_index);
}
- string16 new_language = TranslateInfoBarDelegate::GetDisplayNameForLocale(
+ string16 new_language = GetDelegate()->GetDisplayNameForLocale(
GetDelegate()->GetLocaleFromIndex(new_language_index));
menu_button->SetText(UTF16ToWideHack(new_language));
menu_button->ClearMaxTextSize();
diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi
index 818cc2b..2721f70 100644
--- a/chrome/chrome_tests.gypi
+++ b/chrome/chrome_tests.gypi
@@ -706,6 +706,7 @@
'browser/cocoa/test_event_utils.h',
'browser/cocoa/test_event_utils.mm',
'browser/cocoa/throbber_view_unittest.mm',
+ 'browser/cocoa/translate_infobar_unittest.mm',
'browser/cocoa/toolbar_button_cell_unittest.mm',
'browser/cocoa/toolbar_controller_unittest.mm',
'browser/cocoa/toolbar_view_unittest.mm',