Index: trunk/src/chrome/browser/ui/cocoa/infobars/translate_infobar_unittest.mm |
=================================================================== |
--- trunk/src/chrome/browser/ui/cocoa/infobars/translate_infobar_unittest.mm (revision 238401) |
+++ trunk/src/chrome/browser/ui/cocoa/infobars/translate_infobar_unittest.mm (working copy) |
@@ -36,11 +36,13 @@ |
class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate { |
public: |
- MockTranslateInfoBarDelegate(TranslateInfoBarDelegate::Type type, |
+ MockTranslateInfoBarDelegate(InfoBarService* infobar_service, |
+ TranslateInfoBarDelegate::Type type, |
TranslateErrors::Type error, |
PrefService* prefs, |
ShortcutConfiguration config) |
- : TranslateInfoBarDelegate(type, NULL, "en", "es", error, prefs, config) { |
+ : TranslateInfoBarDelegate(infobar_service, type, NULL, "en", "es", error, |
+ prefs, config) { |
} |
MOCK_METHOD0(Translate, void()); |
@@ -56,13 +58,8 @@ |
MOCK_METHOD0(ToggleAlwaysTranslate, void()); |
}; |
-} // namespace |
- |
class TranslationInfoBarTest : public CocoaProfileTest { |
public: |
- TranslationInfoBarTest() : CocoaProfileTest(), infobar_(NULL) { |
- } |
- |
// Each test gets a single Mock translate delegate for the lifetime of |
// the test. |
virtual void SetUp() OVERRIDE { |
@@ -71,37 +68,32 @@ |
web_contents_.reset( |
WebContents::Create(WebContents::CreateParams(profile()))); |
InfoBarService::CreateForWebContents(web_contents_.get()); |
+ CreateInfoBar(); |
} |
- virtual void TearDown() OVERRIDE { |
- if (infobar_) { |
- infobar_->CloseSoon(); |
- infobar_ = NULL; |
- } |
- CocoaProfileTest::TearDown(); |
+ void CreateInfoBar() { |
+ CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); |
} |
void CreateInfoBar(TranslateInfoBarDelegate::Type type) { |
TranslateErrors::Type error = TranslateErrors::NONE; |
if (type == TranslateInfoBarDelegate::TRANSLATION_ERROR) |
error = TranslateErrors::NETWORK; |
+ InfoBarService* infobar_service = |
+ InfoBarService::FromWebContents(web_contents_.get()); |
Profile* profile = |
Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
ShortcutConfiguration config; |
config.never_translate_min_count = 3; |
config.always_translate_min_count = 3; |
+ infobar_delegate_.reset(new MockTranslateInfoBarDelegate( |
+ infobar_service, type, error, profile->GetPrefs(), config)); |
[[infobar_controller_ view] removeFromSuperview]; |
- scoped_ptr<TranslateInfoBarDelegate> delegate( |
- new MockTranslateInfoBarDelegate(type, error, profile->GetPrefs(), |
- config)); |
- scoped_ptr<InfoBar> infobar( |
- TranslateInfoBarDelegate::CreateInfoBar(delegate.Pass())); |
- if (infobar_) |
- infobar_->CloseSoon(); |
- infobar_ = static_cast<InfoBarCocoa*>(infobar.release()); |
- infobar_->SetOwner(InfoBarService::FromWebContents(web_contents_.get())); |
- |
+ InfoBarDelegate* base = |
+ static_cast<InfoBarDelegate*>(infobar_delegate_.get()); |
+ infobar_.reset( |
+ static_cast<InfoBarCocoa*>(base->CreateInfoBar(infobar_service))); |
infobar_controller_.reset([static_cast<TranslateInfoBarControllerBase*>( |
infobar_->controller()) retain]); |
@@ -112,26 +104,23 @@ |
[[test_window() contentView] addSubview:[infobar_controller_ view]]; |
} |
- MockTranslateInfoBarDelegate* infobar_delegate() const { |
- return static_cast<MockTranslateInfoBarDelegate*>(infobar_->delegate()); |
- } |
- |
scoped_ptr<WebContents> web_contents_; |
- InfoBarCocoa* infobar_; // weak, deletes itself |
+ scoped_ptr<MockTranslateInfoBarDelegate> infobar_delegate_; |
+ scoped_ptr<InfoBarCocoa> infobar_; |
base::scoped_nsobject<TranslateInfoBarControllerBase> infobar_controller_; |
}; |
// Check that we can instantiate a Translate Infobar correctly. |
TEST_F(TranslationInfoBarTest, Instantiate) { |
- CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); |
+ CreateInfoBar(); |
ASSERT_TRUE(infobar_controller_.get()); |
} |
// Check that clicking the Translate button calls Translate(). |
TEST_F(TranslationInfoBarTest, TranslateCalledOnButtonPress) { |
- CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); |
+ CreateInfoBar(); |
- EXPECT_CALL(*infobar_delegate(), Translate()).Times(1); |
+ EXPECT_CALL(*infobar_delegate_, Translate()).Times(1); |
[infobar_controller_ ok:nil]; |
} |
@@ -140,23 +129,22 @@ |
TEST_F(TranslationInfoBarTest, TranslateCalledInErrorMode) { |
CreateInfoBar(TranslateInfoBarDelegate::TRANSLATION_ERROR); |
- EXPECT_CALL(*infobar_delegate(), Translate()).Times(1); |
+ EXPECT_CALL(*infobar_delegate_, Translate()).Times(1); |
[infobar_controller_ ok:nil]; |
} |
// Check that clicking the "Show Original button calls RevertTranslation(). |
TEST_F(TranslationInfoBarTest, RevertCalledOnButtonPress) { |
- CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); |
+ CreateInfoBar(); |
- EXPECT_CALL(*infobar_delegate(), RevertTranslation()).Times(1); |
+ EXPECT_CALL(*infobar_delegate_, RevertTranslation()).Times(1); |
[infobar_controller_ showOriginal:nil]; |
} |
// Check that items in the options menu are hooked up correctly. |
TEST_F(TranslationInfoBarTest, OptionsMenuItemsHookedUp) { |
- CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); |
- EXPECT_CALL(*infobar_delegate(), Translate()) |
+ EXPECT_CALL(*infobar_delegate_, Translate()) |
.Times(0); |
[infobar_controller_ rebuildOptionsMenu:NO]; |
@@ -180,19 +168,19 @@ |
NSMenuItem* aboutTranslateItem = [optionsMenuItems objectAtIndex:6]; |
{ |
- EXPECT_CALL(*infobar_delegate(), ToggleAlwaysTranslate()) |
+ EXPECT_CALL(*infobar_delegate_, ToggleAlwaysTranslate()) |
.Times(1); |
[infobar_controller_ optionsMenuChanged:alwaysTranslateLanguateItem]; |
} |
{ |
- EXPECT_CALL(*infobar_delegate(), ToggleTranslatableLanguageByPrefs()) |
+ EXPECT_CALL(*infobar_delegate_, ToggleTranslatableLanguageByPrefs()) |
.Times(1); |
[infobar_controller_ optionsMenuChanged:neverTranslateLanguateItem]; |
} |
{ |
- EXPECT_CALL(*infobar_delegate(), ToggleSiteBlacklist()) |
+ EXPECT_CALL(*infobar_delegate_, ToggleSiteBlacklist()) |
.Times(1); |
[infobar_controller_ optionsMenuChanged:neverTranslateSiteItem]; |
} |
@@ -210,13 +198,13 @@ |
// translate" mode doesn't trigger a translation or change state. |
// http://crbug.com/36666 |
TEST_F(TranslationInfoBarTest, Bug36666) { |
- CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); |
- EXPECT_CALL(*infobar_delegate(), Translate()) |
+ EXPECT_CALL(*infobar_delegate_, Translate()) |
.Times(0); |
+ CreateInfoBar(); |
int arbitrary_index = 2; |
[infobar_controller_ sourceLanguageModified:arbitrary_index]; |
- EXPECT_CALL(*infobar_delegate(), Translate()) |
+ EXPECT_CALL(*infobar_delegate_, Translate()) |
.Times(0); |
} |
@@ -224,10 +212,11 @@ |
// each of the states. |
// http://crbug.com/36895 |
TEST_F(TranslationInfoBarTest, Bug36895) { |
+ EXPECT_CALL(*infobar_delegate_, Translate()) |
+ .Times(0); |
+ |
for (size_t i = 0; i < arraysize(kTranslateToolbarStates); ++i) { |
CreateInfoBar(kTranslateToolbarStates[i]); |
- EXPECT_CALL(*infobar_delegate(), Translate()) |
- .Times(0); |
EXPECT_TRUE( |
[infobar_controller_ verifyLayout]) << "Layout wrong, for state #" << i; |
} |
@@ -262,3 +251,5 @@ |
EXPECT_TRUE([[controller alwaysTranslateButton] superview] == nil); |
EXPECT_TRUE([[controller neverTranslateButton] superview] != nil); |
} |
+ |
+} // namespace |