| 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 #include "chrome/browser/infobars/infobar_service.h" | 10 #include "chrome/browser/infobars/infobar_service.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 // All states the translate toolbar can assume. | 31 // All states the translate toolbar can assume. |
| 32 translate::TranslateStep kTranslateToolbarStates[] = { | 32 translate::TranslateStep kTranslateToolbarStates[] = { |
| 33 translate::TRANSLATE_STEP_BEFORE_TRANSLATE, | 33 translate::TRANSLATE_STEP_BEFORE_TRANSLATE, |
| 34 translate::TRANSLATE_STEP_AFTER_TRANSLATE, | 34 translate::TRANSLATE_STEP_AFTER_TRANSLATE, |
| 35 translate::TRANSLATE_STEP_TRANSLATING, | 35 translate::TRANSLATE_STEP_TRANSLATING, |
| 36 translate::TRANSLATE_STEP_TRANSLATE_ERROR}; | 36 translate::TRANSLATE_STEP_TRANSLATE_ERROR}; |
| 37 | 37 |
| 38 class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate { | 38 class MockTranslateInfoBarDelegate |
| 39 : public translate::TranslateInfoBarDelegate { |
| 39 public: | 40 public: |
| 40 MockTranslateInfoBarDelegate(content::WebContents* web_contents, | 41 MockTranslateInfoBarDelegate(content::WebContents* web_contents, |
| 41 translate::TranslateStep step, | 42 translate::TranslateStep step, |
| 42 TranslateErrors::Type error) | 43 translate::TranslateErrors::Type error) |
| 43 : TranslateInfoBarDelegate( | 44 : translate::TranslateInfoBarDelegate( |
| 44 ChromeTranslateClient::GetManagerFromWebContents(web_contents) | 45 ChromeTranslateClient::GetManagerFromWebContents(web_contents) |
| 45 ->GetWeakPtr(), | 46 ->GetWeakPtr(), |
| 46 false, | 47 false, |
| 47 step, | 48 step, |
| 48 NULL, | 49 NULL, |
| 49 "en", | 50 "en", |
| 50 "es", | 51 "es", |
| 51 error, | 52 error, |
| 52 false) {} | 53 false) {} |
| 53 | 54 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 67 } // namespace | 68 } // namespace |
| 68 | 69 |
| 69 class TranslationInfoBarTest : public CocoaProfileTest { | 70 class TranslationInfoBarTest : public CocoaProfileTest { |
| 70 public: | 71 public: |
| 71 TranslationInfoBarTest() : CocoaProfileTest(), infobar_(NULL) { | 72 TranslationInfoBarTest() : CocoaProfileTest(), infobar_(NULL) { |
| 72 } | 73 } |
| 73 | 74 |
| 74 // Each test gets a single Mock translate delegate for the lifetime of | 75 // Each test gets a single Mock translate delegate for the lifetime of |
| 75 // the test. | 76 // the test. |
| 76 virtual void SetUp() OVERRIDE { | 77 virtual void SetUp() OVERRIDE { |
| 77 TranslateLanguageList::DisableUpdate(); | 78 translate::TranslateLanguageList::DisableUpdate(); |
| 78 CocoaProfileTest::SetUp(); | 79 CocoaProfileTest::SetUp(); |
| 79 web_contents_.reset( | 80 web_contents_.reset( |
| 80 WebContents::Create(WebContents::CreateParams(profile()))); | 81 WebContents::Create(WebContents::CreateParams(profile()))); |
| 81 InfoBarService::CreateForWebContents(web_contents_.get()); | 82 InfoBarService::CreateForWebContents(web_contents_.get()); |
| 82 ChromeTranslateClient::CreateForWebContents(web_contents_.get()); | 83 ChromeTranslateClient::CreateForWebContents(web_contents_.get()); |
| 83 } | 84 } |
| 84 | 85 |
| 85 virtual void TearDown() OVERRIDE { | 86 virtual void TearDown() OVERRIDE { |
| 86 if (infobar_) { | 87 if (infobar_) { |
| 87 infobar_->CloseSoon(); | 88 infobar_->CloseSoon(); |
| 88 infobar_ = NULL; | 89 infobar_ = NULL; |
| 89 } | 90 } |
| 90 CocoaProfileTest::TearDown(); | 91 CocoaProfileTest::TearDown(); |
| 91 } | 92 } |
| 92 | 93 |
| 93 void CreateInfoBar(translate::TranslateStep type) { | 94 void CreateInfoBar(translate::TranslateStep type) { |
| 94 TranslateErrors::Type error = TranslateErrors::NONE; | 95 translate::TranslateErrors::Type error = translate::TranslateErrors::NONE; |
| 95 if (type == translate::TRANSLATE_STEP_TRANSLATE_ERROR) | 96 if (type == translate::TRANSLATE_STEP_TRANSLATE_ERROR) |
| 96 error = TranslateErrors::NETWORK; | 97 error = translate::TranslateErrors::NETWORK; |
| 97 [[infobar_controller_ view] removeFromSuperview]; | 98 [[infobar_controller_ view] removeFromSuperview]; |
| 98 | 99 |
| 99 ChromeTranslateClient* chrome_translate_client = | 100 ChromeTranslateClient* chrome_translate_client = |
| 100 ChromeTranslateClient::FromWebContents(web_contents_.get()); | 101 ChromeTranslateClient::FromWebContents(web_contents_.get()); |
| 101 scoped_ptr<TranslateInfoBarDelegate> delegate( | 102 scoped_ptr<translate::TranslateInfoBarDelegate> delegate( |
| 102 new MockTranslateInfoBarDelegate(web_contents_.get(), type, error)); | 103 new MockTranslateInfoBarDelegate(web_contents_.get(), type, error)); |
| 103 scoped_ptr<infobars::InfoBar> infobar( | 104 scoped_ptr<infobars::InfoBar> infobar( |
| 104 chrome_translate_client->CreateInfoBar(delegate.Pass())); | 105 chrome_translate_client->CreateInfoBar(delegate.Pass())); |
| 105 if (infobar_) | 106 if (infobar_) |
| 106 infobar_->CloseSoon(); | 107 infobar_->CloseSoon(); |
| 107 infobar_ = static_cast<InfoBarCocoa*>(infobar.release()); | 108 infobar_ = static_cast<InfoBarCocoa*>(infobar.release()); |
| 108 infobar_->SetOwner(InfoBarService::FromWebContents(web_contents_.get())); | 109 infobar_->SetOwner(InfoBarService::FromWebContents(web_contents_.get())); |
| 109 | 110 |
| 110 infobar_controller_.reset([static_cast<TranslateInfoBarControllerBase*>( | 111 infobar_controller_.reset([static_cast<TranslateInfoBarControllerBase*>( |
| 111 infobar_->controller()) retain]); | 112 infobar_->controller()) retain]); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 | 199 |
| 199 { | 200 { |
| 200 EXPECT_CALL(*infobar_delegate(), ToggleSiteBlacklist()) | 201 EXPECT_CALL(*infobar_delegate(), ToggleSiteBlacklist()) |
| 201 .Times(1); | 202 .Times(1); |
| 202 [infobar_controller_ optionsMenuChanged:neverTranslateSiteItem]; | 203 [infobar_controller_ optionsMenuChanged:neverTranslateSiteItem]; |
| 203 } | 204 } |
| 204 | 205 |
| 205 { | 206 { |
| 206 // Can't mock these effectively, so just check that the tag is set | 207 // Can't mock these effectively, so just check that the tag is set |
| 207 // correctly. | 208 // correctly. |
| 208 EXPECT_EQ(OptionsMenuModel::REPORT_BAD_DETECTION, | 209 EXPECT_EQ(translate::OptionsMenuModel::REPORT_BAD_DETECTION, |
| 209 [reportBadLanguageItem tag]); | 210 [reportBadLanguageItem tag]); |
| 210 EXPECT_EQ(OptionsMenuModel::ABOUT_TRANSLATE, [aboutTranslateItem tag]); | 211 EXPECT_EQ(translate::OptionsMenuModel::ABOUT_TRANSLATE, |
| 212 [aboutTranslateItem tag]); |
| 211 } | 213 } |
| 212 } | 214 } |
| 213 | 215 |
| 214 // Check that selecting a new item from the "Source Language" popup in "before | 216 // Check that selecting a new item from the "Source Language" popup in "before |
| 215 // translate" mode doesn't trigger a translation or change state. | 217 // translate" mode doesn't trigger a translation or change state. |
| 216 // http://crbug.com/36666 | 218 // http://crbug.com/36666 |
| 217 TEST_F(TranslationInfoBarTest, Bug36666) { | 219 TEST_F(TranslationInfoBarTest, Bug36666) { |
| 218 CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE); | 220 CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE); |
| 219 EXPECT_CALL(*infobar_delegate(), Translate()) | 221 EXPECT_CALL(*infobar_delegate(), Translate()) |
| 220 .Times(0); | 222 .Times(0); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 234 EXPECT_CALL(*infobar_delegate(), Translate()) | 236 EXPECT_CALL(*infobar_delegate(), Translate()) |
| 235 .Times(0); | 237 .Times(0); |
| 236 EXPECT_TRUE( | 238 EXPECT_TRUE( |
| 237 [infobar_controller_ verifyLayout]) << "Layout wrong, for state #" << i; | 239 [infobar_controller_ verifyLayout]) << "Layout wrong, for state #" << i; |
| 238 } | 240 } |
| 239 } | 241 } |
| 240 | 242 |
| 241 // Verify that the infobar shows the "Always translate this language" button | 243 // Verify that the infobar shows the "Always translate this language" button |
| 242 // after doing 3 translations. | 244 // after doing 3 translations. |
| 243 TEST_F(TranslationInfoBarTest, TriggerShowAlwaysTranslateButton) { | 245 TEST_F(TranslationInfoBarTest, TriggerShowAlwaysTranslateButton) { |
| 244 scoped_ptr<TranslatePrefs> translate_prefs( | 246 scoped_ptr<translate::TranslatePrefs> translate_prefs( |
| 245 ChromeTranslateClient::CreateTranslatePrefs(profile()->GetPrefs())); | 247 ChromeTranslateClient::CreateTranslatePrefs(profile()->GetPrefs())); |
| 246 translate_prefs->ResetTranslationAcceptedCount("en"); | 248 translate_prefs->ResetTranslationAcceptedCount("en"); |
| 247 for (int i = 0; i < 4; ++i) { | 249 for (int i = 0; i < 4; ++i) { |
| 248 translate_prefs->IncrementTranslationAcceptedCount("en"); | 250 translate_prefs->IncrementTranslationAcceptedCount("en"); |
| 249 } | 251 } |
| 250 CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE); | 252 CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE); |
| 251 BeforeTranslateInfobarController* controller = | 253 BeforeTranslateInfobarController* controller = |
| 252 (BeforeTranslateInfobarController*)infobar_controller_.get(); | 254 (BeforeTranslateInfobarController*)infobar_controller_.get(); |
| 253 EXPECT_TRUE([[controller alwaysTranslateButton] superview] != nil); | 255 EXPECT_TRUE([[controller alwaysTranslateButton] superview] != nil); |
| 254 EXPECT_TRUE([[controller neverTranslateButton] superview] == nil); | 256 EXPECT_TRUE([[controller neverTranslateButton] superview] == nil); |
| 255 } | 257 } |
| 256 | 258 |
| 257 // Verify that the infobar shows the "Never translate this language" button | 259 // Verify that the infobar shows the "Never translate this language" button |
| 258 // after denying 3 translations. | 260 // after denying 3 translations. |
| 259 TEST_F(TranslationInfoBarTest, TriggerShowNeverTranslateButton) { | 261 TEST_F(TranslationInfoBarTest, TriggerShowNeverTranslateButton) { |
| 260 scoped_ptr<TranslatePrefs> translate_prefs( | 262 scoped_ptr<translate::TranslatePrefs> translate_prefs( |
| 261 ChromeTranslateClient::CreateTranslatePrefs(profile()->GetPrefs())); | 263 ChromeTranslateClient::CreateTranslatePrefs(profile()->GetPrefs())); |
| 262 translate_prefs->ResetTranslationDeniedCount("en"); | 264 translate_prefs->ResetTranslationDeniedCount("en"); |
| 263 for (int i = 0; i < 4; ++i) { | 265 for (int i = 0; i < 4; ++i) { |
| 264 translate_prefs->IncrementTranslationDeniedCount("en"); | 266 translate_prefs->IncrementTranslationDeniedCount("en"); |
| 265 } | 267 } |
| 266 CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE); | 268 CreateInfoBar(translate::TRANSLATE_STEP_BEFORE_TRANSLATE); |
| 267 BeforeTranslateInfobarController* controller = | 269 BeforeTranslateInfobarController* controller = |
| 268 (BeforeTranslateInfobarController*)infobar_controller_.get(); | 270 (BeforeTranslateInfobarController*)infobar_controller_.get(); |
| 269 EXPECT_TRUE([[controller alwaysTranslateButton] superview] == nil); | 271 EXPECT_TRUE([[controller alwaysTranslateButton] superview] == nil); |
| 270 EXPECT_TRUE([[controller neverTranslateButton] superview] != nil); | 272 EXPECT_TRUE([[controller neverTranslateButton] superview] != nil); |
| 271 } | 273 } |
| OLD | NEW |