| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 | 6 |
| 7 #import "base/mac/scoped_nsobject.h" | 7 #import "base/mac/scoped_nsobject.h" |
| 8 #import "base/strings/string_util.h" | 8 #import "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #import "chrome/app/chrome_command_ids.h" // For translate menu command ids. | 10 #import "chrome/app/chrome_command_ids.h" // For translate menu command ids. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 // All states the translate toolbar can assume. | 29 // All states the translate toolbar can assume. |
| 30 TranslateInfoBarDelegate::Type kTranslateToolbarStates[] = { | 30 TranslateInfoBarDelegate::Type kTranslateToolbarStates[] = { |
| 31 TranslateInfoBarDelegate::BEFORE_TRANSLATE, | 31 TranslateInfoBarDelegate::BEFORE_TRANSLATE, |
| 32 TranslateInfoBarDelegate::AFTER_TRANSLATE, | 32 TranslateInfoBarDelegate::AFTER_TRANSLATE, |
| 33 TranslateInfoBarDelegate::TRANSLATING, | 33 TranslateInfoBarDelegate::TRANSLATING, |
| 34 TranslateInfoBarDelegate::TRANSLATION_ERROR | 34 TranslateInfoBarDelegate::TRANSLATION_ERROR |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate { | 37 class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate { |
| 38 public: | 38 public: |
| 39 MockTranslateInfoBarDelegate(TranslateInfoBarDelegate::Type type, | 39 MockTranslateInfoBarDelegate(InfoBarService* infobar_service, |
| 40 TranslateInfoBarDelegate::Type type, |
| 40 TranslateErrors::Type error, | 41 TranslateErrors::Type error, |
| 41 PrefService* prefs, | 42 PrefService* prefs, |
| 42 ShortcutConfiguration config) | 43 ShortcutConfiguration config) |
| 43 : TranslateInfoBarDelegate(type, NULL, "en", "es", error, prefs, config) { | 44 : TranslateInfoBarDelegate(infobar_service, type, NULL, "en", "es", error, |
| 45 prefs, config) { |
| 44 } | 46 } |
| 45 | 47 |
| 46 MOCK_METHOD0(Translate, void()); | 48 MOCK_METHOD0(Translate, void()); |
| 47 MOCK_METHOD0(RevertTranslation, void()); | 49 MOCK_METHOD0(RevertTranslation, void()); |
| 48 | 50 |
| 49 MOCK_METHOD0(TranslationDeclined, void()); | 51 MOCK_METHOD0(TranslationDeclined, void()); |
| 50 | 52 |
| 51 virtual bool IsTranslatableLanguageByPrefs() OVERRIDE { return true; } | 53 virtual bool IsTranslatableLanguageByPrefs() OVERRIDE { return true; } |
| 52 MOCK_METHOD0(ToggleTranslatableLanguageByPrefs, void()); | 54 MOCK_METHOD0(ToggleTranslatableLanguageByPrefs, void()); |
| 53 virtual bool IsSiteBlacklisted() OVERRIDE { return false; } | 55 virtual bool IsSiteBlacklisted() OVERRIDE { return false; } |
| 54 MOCK_METHOD0(ToggleSiteBlacklist, void()); | 56 MOCK_METHOD0(ToggleSiteBlacklist, void()); |
| 55 virtual bool ShouldAlwaysTranslate() OVERRIDE { return false; } | 57 virtual bool ShouldAlwaysTranslate() OVERRIDE { return false; } |
| 56 MOCK_METHOD0(ToggleAlwaysTranslate, void()); | 58 MOCK_METHOD0(ToggleAlwaysTranslate, void()); |
| 57 }; | 59 }; |
| 58 | 60 |
| 59 } // namespace | |
| 60 | |
| 61 class TranslationInfoBarTest : public CocoaProfileTest { | 61 class TranslationInfoBarTest : public CocoaProfileTest { |
| 62 public: | 62 public: |
| 63 TranslationInfoBarTest() : CocoaProfileTest(), infobar_(NULL) { | |
| 64 } | |
| 65 | |
| 66 // Each test gets a single Mock translate delegate for the lifetime of | 63 // Each test gets a single Mock translate delegate for the lifetime of |
| 67 // the test. | 64 // the test. |
| 68 virtual void SetUp() OVERRIDE { | 65 virtual void SetUp() OVERRIDE { |
| 69 TranslateLanguageList::DisableUpdate(); | 66 TranslateLanguageList::DisableUpdate(); |
| 70 CocoaProfileTest::SetUp(); | 67 CocoaProfileTest::SetUp(); |
| 71 web_contents_.reset( | 68 web_contents_.reset( |
| 72 WebContents::Create(WebContents::CreateParams(profile()))); | 69 WebContents::Create(WebContents::CreateParams(profile()))); |
| 73 InfoBarService::CreateForWebContents(web_contents_.get()); | 70 InfoBarService::CreateForWebContents(web_contents_.get()); |
| 71 CreateInfoBar(); |
| 74 } | 72 } |
| 75 | 73 |
| 76 virtual void TearDown() OVERRIDE { | 74 void CreateInfoBar() { |
| 77 if (infobar_) { | 75 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); |
| 78 infobar_->CloseSoon(); | |
| 79 infobar_ = NULL; | |
| 80 } | |
| 81 CocoaProfileTest::TearDown(); | |
| 82 } | 76 } |
| 83 | 77 |
| 84 void CreateInfoBar(TranslateInfoBarDelegate::Type type) { | 78 void CreateInfoBar(TranslateInfoBarDelegate::Type type) { |
| 85 TranslateErrors::Type error = TranslateErrors::NONE; | 79 TranslateErrors::Type error = TranslateErrors::NONE; |
| 86 if (type == TranslateInfoBarDelegate::TRANSLATION_ERROR) | 80 if (type == TranslateInfoBarDelegate::TRANSLATION_ERROR) |
| 87 error = TranslateErrors::NETWORK; | 81 error = TranslateErrors::NETWORK; |
| 82 InfoBarService* infobar_service = |
| 83 InfoBarService::FromWebContents(web_contents_.get()); |
| 88 Profile* profile = | 84 Profile* profile = |
| 89 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | 85 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 90 ShortcutConfiguration config; | 86 ShortcutConfiguration config; |
| 91 config.never_translate_min_count = 3; | 87 config.never_translate_min_count = 3; |
| 92 config.always_translate_min_count = 3; | 88 config.always_translate_min_count = 3; |
| 89 infobar_delegate_.reset(new MockTranslateInfoBarDelegate( |
| 90 infobar_service, type, error, profile->GetPrefs(), config)); |
| 93 [[infobar_controller_ view] removeFromSuperview]; | 91 [[infobar_controller_ view] removeFromSuperview]; |
| 94 | 92 |
| 95 scoped_ptr<TranslateInfoBarDelegate> delegate( | 93 InfoBarDelegate* base = |
| 96 new MockTranslateInfoBarDelegate(type, error, profile->GetPrefs(), | 94 static_cast<InfoBarDelegate*>(infobar_delegate_.get()); |
| 97 config)); | 95 infobar_.reset( |
| 98 scoped_ptr<InfoBar> infobar( | 96 static_cast<InfoBarCocoa*>(base->CreateInfoBar(infobar_service))); |
| 99 TranslateInfoBarDelegate::CreateInfoBar(delegate.Pass())); | |
| 100 if (infobar_) | |
| 101 infobar_->CloseSoon(); | |
| 102 infobar_ = static_cast<InfoBarCocoa*>(infobar.release()); | |
| 103 infobar_->SetOwner(InfoBarService::FromWebContents(web_contents_.get())); | |
| 104 | |
| 105 infobar_controller_.reset([static_cast<TranslateInfoBarControllerBase*>( | 97 infobar_controller_.reset([static_cast<TranslateInfoBarControllerBase*>( |
| 106 infobar_->controller()) retain]); | 98 infobar_->controller()) retain]); |
| 107 | 99 |
| 108 // We need to set the window to be wide so that the options button | 100 // We need to set the window to be wide so that the options button |
| 109 // doesn't overlap the other buttons. | 101 // doesn't overlap the other buttons. |
| 110 [test_window() setContentSize:NSMakeSize(2000, 500)]; | 102 [test_window() setContentSize:NSMakeSize(2000, 500)]; |
| 111 [[infobar_controller_ view] setFrame:NSMakeRect(0, 0, 2000, 500)]; | 103 [[infobar_controller_ view] setFrame:NSMakeRect(0, 0, 2000, 500)]; |
| 112 [[test_window() contentView] addSubview:[infobar_controller_ view]]; | 104 [[test_window() contentView] addSubview:[infobar_controller_ view]]; |
| 113 } | 105 } |
| 114 | 106 |
| 115 MockTranslateInfoBarDelegate* infobar_delegate() const { | |
| 116 return static_cast<MockTranslateInfoBarDelegate*>(infobar_->delegate()); | |
| 117 } | |
| 118 | |
| 119 scoped_ptr<WebContents> web_contents_; | 107 scoped_ptr<WebContents> web_contents_; |
| 120 InfoBarCocoa* infobar_; // weak, deletes itself | 108 scoped_ptr<MockTranslateInfoBarDelegate> infobar_delegate_; |
| 109 scoped_ptr<InfoBarCocoa> infobar_; |
| 121 base::scoped_nsobject<TranslateInfoBarControllerBase> infobar_controller_; | 110 base::scoped_nsobject<TranslateInfoBarControllerBase> infobar_controller_; |
| 122 }; | 111 }; |
| 123 | 112 |
| 124 // Check that we can instantiate a Translate Infobar correctly. | 113 // Check that we can instantiate a Translate Infobar correctly. |
| 125 TEST_F(TranslationInfoBarTest, Instantiate) { | 114 TEST_F(TranslationInfoBarTest, Instantiate) { |
| 126 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); | 115 CreateInfoBar(); |
| 127 ASSERT_TRUE(infobar_controller_.get()); | 116 ASSERT_TRUE(infobar_controller_.get()); |
| 128 } | 117 } |
| 129 | 118 |
| 130 // Check that clicking the Translate button calls Translate(). | 119 // Check that clicking the Translate button calls Translate(). |
| 131 TEST_F(TranslationInfoBarTest, TranslateCalledOnButtonPress) { | 120 TEST_F(TranslationInfoBarTest, TranslateCalledOnButtonPress) { |
| 132 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); | 121 CreateInfoBar(); |
| 133 | 122 |
| 134 EXPECT_CALL(*infobar_delegate(), Translate()).Times(1); | 123 EXPECT_CALL(*infobar_delegate_, Translate()).Times(1); |
| 135 [infobar_controller_ ok:nil]; | 124 [infobar_controller_ ok:nil]; |
| 136 } | 125 } |
| 137 | 126 |
| 138 // Check that clicking the "Retry" button calls Translate() when we're | 127 // Check that clicking the "Retry" button calls Translate() when we're |
| 139 // in the error mode - http://crbug.com/41315 . | 128 // in the error mode - http://crbug.com/41315 . |
| 140 TEST_F(TranslationInfoBarTest, TranslateCalledInErrorMode) { | 129 TEST_F(TranslationInfoBarTest, TranslateCalledInErrorMode) { |
| 141 CreateInfoBar(TranslateInfoBarDelegate::TRANSLATION_ERROR); | 130 CreateInfoBar(TranslateInfoBarDelegate::TRANSLATION_ERROR); |
| 142 | 131 |
| 143 EXPECT_CALL(*infobar_delegate(), Translate()).Times(1); | 132 EXPECT_CALL(*infobar_delegate_, Translate()).Times(1); |
| 144 | 133 |
| 145 [infobar_controller_ ok:nil]; | 134 [infobar_controller_ ok:nil]; |
| 146 } | 135 } |
| 147 | 136 |
| 148 // Check that clicking the "Show Original button calls RevertTranslation(). | 137 // Check that clicking the "Show Original button calls RevertTranslation(). |
| 149 TEST_F(TranslationInfoBarTest, RevertCalledOnButtonPress) { | 138 TEST_F(TranslationInfoBarTest, RevertCalledOnButtonPress) { |
| 150 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); | 139 CreateInfoBar(); |
| 151 | 140 |
| 152 EXPECT_CALL(*infobar_delegate(), RevertTranslation()).Times(1); | 141 EXPECT_CALL(*infobar_delegate_, RevertTranslation()).Times(1); |
| 153 [infobar_controller_ showOriginal:nil]; | 142 [infobar_controller_ showOriginal:nil]; |
| 154 } | 143 } |
| 155 | 144 |
| 156 // Check that items in the options menu are hooked up correctly. | 145 // Check that items in the options menu are hooked up correctly. |
| 157 TEST_F(TranslationInfoBarTest, OptionsMenuItemsHookedUp) { | 146 TEST_F(TranslationInfoBarTest, OptionsMenuItemsHookedUp) { |
| 158 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); | 147 EXPECT_CALL(*infobar_delegate_, Translate()) |
| 159 EXPECT_CALL(*infobar_delegate(), Translate()) | |
| 160 .Times(0); | 148 .Times(0); |
| 161 | 149 |
| 162 [infobar_controller_ rebuildOptionsMenu:NO]; | 150 [infobar_controller_ rebuildOptionsMenu:NO]; |
| 163 NSMenu* optionsMenu = [infobar_controller_ optionsMenu]; | 151 NSMenu* optionsMenu = [infobar_controller_ optionsMenu]; |
| 164 NSArray* optionsMenuItems = [optionsMenu itemArray]; | 152 NSArray* optionsMenuItems = [optionsMenu itemArray]; |
| 165 | 153 |
| 166 EXPECT_EQ(7U, [optionsMenuItems count]); | 154 EXPECT_EQ(7U, [optionsMenuItems count]); |
| 167 | 155 |
| 168 // First item is the options menu button's title, so there's no need to test | 156 // First item is the options menu button's title, so there's no need to test |
| 169 // that the target on that is setup correctly. | 157 // that the target on that is setup correctly. |
| 170 for (NSUInteger i = 1; i < [optionsMenuItems count]; ++i) { | 158 for (NSUInteger i = 1; i < [optionsMenuItems count]; ++i) { |
| 171 NSMenuItem* item = [optionsMenuItems objectAtIndex:i]; | 159 NSMenuItem* item = [optionsMenuItems objectAtIndex:i]; |
| 172 if (![item isSeparatorItem]) | 160 if (![item isSeparatorItem]) |
| 173 EXPECT_EQ([item target], infobar_controller_.get()); | 161 EXPECT_EQ([item target], infobar_controller_.get()); |
| 174 } | 162 } |
| 175 NSMenuItem* alwaysTranslateLanguateItem = [optionsMenuItems objectAtIndex:1]; | 163 NSMenuItem* alwaysTranslateLanguateItem = [optionsMenuItems objectAtIndex:1]; |
| 176 NSMenuItem* neverTranslateLanguateItem = [optionsMenuItems objectAtIndex:2]; | 164 NSMenuItem* neverTranslateLanguateItem = [optionsMenuItems objectAtIndex:2]; |
| 177 NSMenuItem* neverTranslateSiteItem = [optionsMenuItems objectAtIndex:3]; | 165 NSMenuItem* neverTranslateSiteItem = [optionsMenuItems objectAtIndex:3]; |
| 178 // Separator at 4. | 166 // Separator at 4. |
| 179 NSMenuItem* reportBadLanguageItem = [optionsMenuItems objectAtIndex:5]; | 167 NSMenuItem* reportBadLanguageItem = [optionsMenuItems objectAtIndex:5]; |
| 180 NSMenuItem* aboutTranslateItem = [optionsMenuItems objectAtIndex:6]; | 168 NSMenuItem* aboutTranslateItem = [optionsMenuItems objectAtIndex:6]; |
| 181 | 169 |
| 182 { | 170 { |
| 183 EXPECT_CALL(*infobar_delegate(), ToggleAlwaysTranslate()) | 171 EXPECT_CALL(*infobar_delegate_, ToggleAlwaysTranslate()) |
| 184 .Times(1); | 172 .Times(1); |
| 185 [infobar_controller_ optionsMenuChanged:alwaysTranslateLanguateItem]; | 173 [infobar_controller_ optionsMenuChanged:alwaysTranslateLanguateItem]; |
| 186 } | 174 } |
| 187 | 175 |
| 188 { | 176 { |
| 189 EXPECT_CALL(*infobar_delegate(), ToggleTranslatableLanguageByPrefs()) | 177 EXPECT_CALL(*infobar_delegate_, ToggleTranslatableLanguageByPrefs()) |
| 190 .Times(1); | 178 .Times(1); |
| 191 [infobar_controller_ optionsMenuChanged:neverTranslateLanguateItem]; | 179 [infobar_controller_ optionsMenuChanged:neverTranslateLanguateItem]; |
| 192 } | 180 } |
| 193 | 181 |
| 194 { | 182 { |
| 195 EXPECT_CALL(*infobar_delegate(), ToggleSiteBlacklist()) | 183 EXPECT_CALL(*infobar_delegate_, ToggleSiteBlacklist()) |
| 196 .Times(1); | 184 .Times(1); |
| 197 [infobar_controller_ optionsMenuChanged:neverTranslateSiteItem]; | 185 [infobar_controller_ optionsMenuChanged:neverTranslateSiteItem]; |
| 198 } | 186 } |
| 199 | 187 |
| 200 { | 188 { |
| 201 // Can't mock these effectively, so just check that the tag is set | 189 // Can't mock these effectively, so just check that the tag is set |
| 202 // correctly. | 190 // correctly. |
| 203 EXPECT_EQ(IDC_TRANSLATE_REPORT_BAD_LANGUAGE_DETECTION, | 191 EXPECT_EQ(IDC_TRANSLATE_REPORT_BAD_LANGUAGE_DETECTION, |
| 204 [reportBadLanguageItem tag]); | 192 [reportBadLanguageItem tag]); |
| 205 EXPECT_EQ(IDC_TRANSLATE_OPTIONS_ABOUT, [aboutTranslateItem tag]); | 193 EXPECT_EQ(IDC_TRANSLATE_OPTIONS_ABOUT, [aboutTranslateItem tag]); |
| 206 } | 194 } |
| 207 } | 195 } |
| 208 | 196 |
| 209 // Check that selecting a new item from the "Source Language" popup in "before | 197 // Check that selecting a new item from the "Source Language" popup in "before |
| 210 // translate" mode doesn't trigger a translation or change state. | 198 // translate" mode doesn't trigger a translation or change state. |
| 211 // http://crbug.com/36666 | 199 // http://crbug.com/36666 |
| 212 TEST_F(TranslationInfoBarTest, Bug36666) { | 200 TEST_F(TranslationInfoBarTest, Bug36666) { |
| 213 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); | 201 EXPECT_CALL(*infobar_delegate_, Translate()) |
| 214 EXPECT_CALL(*infobar_delegate(), Translate()) | |
| 215 .Times(0); | 202 .Times(0); |
| 216 | 203 |
| 204 CreateInfoBar(); |
| 217 int arbitrary_index = 2; | 205 int arbitrary_index = 2; |
| 218 [infobar_controller_ sourceLanguageModified:arbitrary_index]; | 206 [infobar_controller_ sourceLanguageModified:arbitrary_index]; |
| 219 EXPECT_CALL(*infobar_delegate(), Translate()) | 207 EXPECT_CALL(*infobar_delegate_, Translate()) |
| 220 .Times(0); | 208 .Times(0); |
| 221 } | 209 } |
| 222 | 210 |
| 223 // Check that the infobar lays itself out correctly when instantiated in | 211 // Check that the infobar lays itself out correctly when instantiated in |
| 224 // each of the states. | 212 // each of the states. |
| 225 // http://crbug.com/36895 | 213 // http://crbug.com/36895 |
| 226 TEST_F(TranslationInfoBarTest, Bug36895) { | 214 TEST_F(TranslationInfoBarTest, Bug36895) { |
| 215 EXPECT_CALL(*infobar_delegate_, Translate()) |
| 216 .Times(0); |
| 217 |
| 227 for (size_t i = 0; i < arraysize(kTranslateToolbarStates); ++i) { | 218 for (size_t i = 0; i < arraysize(kTranslateToolbarStates); ++i) { |
| 228 CreateInfoBar(kTranslateToolbarStates[i]); | 219 CreateInfoBar(kTranslateToolbarStates[i]); |
| 229 EXPECT_CALL(*infobar_delegate(), Translate()) | |
| 230 .Times(0); | |
| 231 EXPECT_TRUE( | 220 EXPECT_TRUE( |
| 232 [infobar_controller_ verifyLayout]) << "Layout wrong, for state #" << i; | 221 [infobar_controller_ verifyLayout]) << "Layout wrong, for state #" << i; |
| 233 } | 222 } |
| 234 } | 223 } |
| 235 | 224 |
| 236 // Verify that the infobar shows the "Always translate this language" button | 225 // Verify that the infobar shows the "Always translate this language" button |
| 237 // after doing 3 translations. | 226 // after doing 3 translations. |
| 238 TEST_F(TranslationInfoBarTest, TriggerShowAlwaysTranslateButton) { | 227 TEST_F(TranslationInfoBarTest, TriggerShowAlwaysTranslateButton) { |
| 239 TranslatePrefs translate_prefs(profile()->GetPrefs()); | 228 TranslatePrefs translate_prefs(profile()->GetPrefs()); |
| 240 translate_prefs.ResetTranslationAcceptedCount("en"); | 229 translate_prefs.ResetTranslationAcceptedCount("en"); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 255 translate_prefs.ResetTranslationDeniedCount("en"); | 244 translate_prefs.ResetTranslationDeniedCount("en"); |
| 256 for (int i = 0; i < 4; ++i) { | 245 for (int i = 0; i < 4; ++i) { |
| 257 translate_prefs.IncrementTranslationDeniedCount("en"); | 246 translate_prefs.IncrementTranslationDeniedCount("en"); |
| 258 } | 247 } |
| 259 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); | 248 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); |
| 260 BeforeTranslateInfobarController* controller = | 249 BeforeTranslateInfobarController* controller = |
| 261 (BeforeTranslateInfobarController*)infobar_controller_.get(); | 250 (BeforeTranslateInfobarController*)infobar_controller_.get(); |
| 262 EXPECT_TRUE([[controller alwaysTranslateButton] superview] == nil); | 251 EXPECT_TRUE([[controller alwaysTranslateButton] superview] == nil); |
| 263 EXPECT_TRUE([[controller neverTranslateButton] superview] != nil); | 252 EXPECT_TRUE([[controller neverTranslateButton] superview] != nil); |
| 264 } | 253 } |
| 254 |
| 255 } // namespace |
| OLD | NEW |