| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/scoped_nsobject.h" | 7 #import "base/scoped_nsobject.h" |
| 8 #import "base/string_util.h" | 8 #import "base/string_util.h" |
| 9 #import "chrome/app/chrome_dll_resource.h" // For translate menu command ids. | 9 #import "chrome/app/chrome_dll_resource.h" // For translate menu command ids. |
| 10 #import "chrome/browser/cocoa/browser_test_helper.h" | 10 #import "chrome/browser/cocoa/browser_test_helper.h" |
| 11 #import "chrome/browser/cocoa/cocoa_test_helper.h" | 11 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
| 12 #import "chrome/browser/cocoa/infobar.h" | 12 #import "chrome/browser/cocoa/infobar.h" |
| 13 #import "chrome/browser/cocoa/translate/translate_infobar_base.h" | 13 #import "chrome/browser/cocoa/translate/translate_infobar_base.h" |
| 14 #import "chrome/browser/cocoa/translate/before_translate_infobar_controller.h" | 14 #import "chrome/browser/cocoa/translate/before_translate_infobar_controller.h" |
| 15 #import "chrome/browser/renderer_host/site_instance.h" | 15 #import "chrome/browser/renderer_host/site_instance.h" |
| 16 #import "chrome/browser/tab_contents/tab_contents.h" | 16 #import "chrome/browser/tab_contents/tab_contents.h" |
| 17 #import "chrome/browser/translate/translate_infobar_delegate.h" | 17 #import "chrome/browser/translate/translate_infobar_delegate.h" |
| 18 #import "testing/gmock/include/gmock/gmock.h" | 18 #import "testing/gmock/include/gmock/gmock.h" |
| 19 #import "testing/gtest/include/gtest/gtest.h" | 19 #import "testing/gtest/include/gtest/gtest.h" |
| 20 #import "testing/platform_test.h" | 20 #import "testing/platform_test.h" |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // All states the translate toolbar can assume. | 24 // All states the translate toolbar can assume. |
| 25 TranslateInfoBarDelegate::Type kTranslateToolbarStates[] = { | 25 TranslateInfoBarDelegate::Type kTranslateToolbarStates[] = { |
| 26 TranslateInfoBarDelegate::kBeforeTranslate, | 26 TranslateInfoBarDelegate::BEFORE_TRANSLATE, |
| 27 TranslateInfoBarDelegate::kAfterTranslate, | 27 TranslateInfoBarDelegate::AFTER_TRANSLATE, |
| 28 TranslateInfoBarDelegate::kTranslating, | 28 TranslateInfoBarDelegate::TRANSLATING, |
| 29 TranslateInfoBarDelegate::kTranslationError | 29 TranslateInfoBarDelegate::TRANSLATION_ERROR |
| 30 }; | 30 }; |
| 31 | 31 |
| 32 class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate { | 32 class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate { |
| 33 public: | 33 public: |
| 34 MockTranslateInfoBarDelegate(TranslateInfoBarDelegate::Type type, | 34 MockTranslateInfoBarDelegate(TranslateInfoBarDelegate::Type type, |
| 35 TranslateErrors::Type error, | 35 TranslateErrors::Type error, |
| 36 TabContents* contents) | 36 TabContents* contents) |
| 37 : TranslateInfoBarDelegate(type, error, contents, "en", "es"){ | 37 : TranslateInfoBarDelegate(type, error, contents, "en", "es"){ |
| 38 // Start out in the "Before Translate" state. | 38 // Start out in the "Before Translate" state. |
| 39 type_ = type; | 39 type_ = type; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 | 72 |
| 73 public: | 73 public: |
| 74 // Each test gets a single Mock translate delegate for the lifetime of | 74 // Each test gets a single Mock translate delegate for the lifetime of |
| 75 // the test. | 75 // the test. |
| 76 virtual void SetUp() { | 76 virtual void SetUp() { |
| 77 CocoaTest::SetUp(); | 77 CocoaTest::SetUp(); |
| 78 CreateInfoBar(); | 78 CreateInfoBar(); |
| 79 } | 79 } |
| 80 | 80 |
| 81 void CreateInfoBar() { | 81 void CreateInfoBar() { |
| 82 CreateInfoBar(TranslateInfoBarDelegate::kBeforeTranslate); | 82 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void CreateInfoBar(TranslateInfoBarDelegate::Type type) { | 85 void CreateInfoBar(TranslateInfoBarDelegate::Type type) { |
| 86 SiteInstance* instance = | 86 SiteInstance* instance = |
| 87 SiteInstance::CreateSiteInstance(browser_helper_.profile()); | 87 SiteInstance::CreateSiteInstance(browser_helper_.profile()); |
| 88 scoped_ptr<TabContents> tab_contents( | 88 scoped_ptr<TabContents> tab_contents( |
| 89 new TabContents(browser_helper_.profile(), | 89 new TabContents(browser_helper_.profile(), |
| 90 instance, | 90 instance, |
| 91 MSG_ROUTING_NONE, | 91 MSG_ROUTING_NONE, |
| 92 NULL)); | 92 NULL)); |
| 93 TranslateErrors::Type error = TranslateErrors::NONE; | 93 TranslateErrors::Type error = TranslateErrors::NONE; |
| 94 if (type == TranslateInfoBarDelegate::kTranslationError) | 94 if (type == TranslateInfoBarDelegate::TRANSLATION_ERROR) |
| 95 error = TranslateErrors::NETWORK; | 95 error = TranslateErrors::NETWORK; |
| 96 infobar_delegate.reset( | 96 infobar_delegate.reset( |
| 97 new MockTranslateInfoBarDelegate(type, error, tab_contents.get())); | 97 new MockTranslateInfoBarDelegate(type, error, tab_contents.get())); |
| 98 [[infobar_controller view] removeFromSuperview]; | 98 [[infobar_controller view] removeFromSuperview]; |
| 99 scoped_ptr<InfoBar> infobar(infobar_delegate->CreateInfoBar()); | 99 scoped_ptr<InfoBar> infobar(infobar_delegate->CreateInfoBar()); |
| 100 infobar_controller.reset( | 100 infobar_controller.reset( |
| 101 reinterpret_cast<TranslateInfoBarControllerBase*>( | 101 reinterpret_cast<TranslateInfoBarControllerBase*>( |
| 102 infobar->controller())); | 102 infobar->controller())); |
| 103 // We need to set the window to be wide so that the options button | 103 // We need to set the window to be wide so that the options button |
| 104 // doesn't overlap the other buttons. | 104 // doesn't overlap the other buttons. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 118 TEST_F(TranslationInfoBarTest, TranslateCalledOnButtonPress) { | 118 TEST_F(TranslationInfoBarTest, TranslateCalledOnButtonPress) { |
| 119 CreateInfoBar(); | 119 CreateInfoBar(); |
| 120 | 120 |
| 121 EXPECT_CALL(*infobar_delegate, Translate()).Times(1); | 121 EXPECT_CALL(*infobar_delegate, Translate()).Times(1); |
| 122 [infobar_controller ok:nil]; | 122 [infobar_controller ok:nil]; |
| 123 } | 123 } |
| 124 | 124 |
| 125 // Check that clicking the "Retry" button calls Translate() when we're | 125 // Check that clicking the "Retry" button calls Translate() when we're |
| 126 // in the error mode - http://crbug.com/41315 . | 126 // in the error mode - http://crbug.com/41315 . |
| 127 TEST_F(TranslationInfoBarTest, TranslateCalledInErrorMode) { | 127 TEST_F(TranslationInfoBarTest, TranslateCalledInErrorMode) { |
| 128 CreateInfoBar(TranslateInfoBarDelegate::kTranslationError); | 128 CreateInfoBar(TranslateInfoBarDelegate::TRANSLATION_ERROR); |
| 129 | 129 |
| 130 EXPECT_CALL(*infobar_delegate, Translate()).Times(1); | 130 EXPECT_CALL(*infobar_delegate, Translate()).Times(1); |
| 131 | 131 |
| 132 [infobar_controller ok:nil]; | 132 [infobar_controller ok:nil]; |
| 133 } | 133 } |
| 134 | 134 |
| 135 // Check that clicking the "Show Original button calls RevertTranslation(). | 135 // Check that clicking the "Show Original button calls RevertTranslation(). |
| 136 TEST_F(TranslationInfoBarTest, RevertCalledOnButtonPress) { | 136 TEST_F(TranslationInfoBarTest, RevertCalledOnButtonPress) { |
| 137 CreateInfoBar(); | 137 CreateInfoBar(); |
| 138 | 138 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 } | 220 } |
| 221 | 221 |
| 222 // Verify that the infobar shows the "Always translate this language" button | 222 // Verify that the infobar shows the "Always translate this language" button |
| 223 // after doing 3 translations. | 223 // after doing 3 translations. |
| 224 TEST_F(TranslationInfoBarTest, TriggerShowAlwaysTranslateButton) { | 224 TEST_F(TranslationInfoBarTest, TriggerShowAlwaysTranslateButton) { |
| 225 TranslatePrefs translate_prefs(browser_helper_.profile()->GetPrefs()); | 225 TranslatePrefs translate_prefs(browser_helper_.profile()->GetPrefs()); |
| 226 translate_prefs.ResetTranslationAcceptedCount("en"); | 226 translate_prefs.ResetTranslationAcceptedCount("en"); |
| 227 for (int i = 0; i < 4; ++i) { | 227 for (int i = 0; i < 4; ++i) { |
| 228 translate_prefs.IncrementTranslationAcceptedCount("en"); | 228 translate_prefs.IncrementTranslationAcceptedCount("en"); |
| 229 } | 229 } |
| 230 CreateInfoBar(TranslateInfoBarDelegate::kBeforeTranslate); | 230 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); |
| 231 BeforeTranslateInfobarController* controller = | 231 BeforeTranslateInfobarController* controller = |
| 232 (BeforeTranslateInfobarController*)infobar_controller.get(); | 232 (BeforeTranslateInfobarController*)infobar_controller.get(); |
| 233 EXPECT_TRUE([[controller alwaysTranslateButton] superview] != nil); | 233 EXPECT_TRUE([[controller alwaysTranslateButton] superview] != nil); |
| 234 EXPECT_TRUE([[controller neverTranslateButton] superview] == nil); | 234 EXPECT_TRUE([[controller neverTranslateButton] superview] == nil); |
| 235 } | 235 } |
| 236 | 236 |
| 237 // Verify that the infobar shows the "Never translate this language" button | 237 // Verify that the infobar shows the "Never translate this language" button |
| 238 // after denying 3 translations. | 238 // after denying 3 translations. |
| 239 TEST_F(TranslationInfoBarTest, TriggerShowNeverTranslateButton) { | 239 TEST_F(TranslationInfoBarTest, TriggerShowNeverTranslateButton) { |
| 240 TranslatePrefs translate_prefs(browser_helper_.profile()->GetPrefs()); | 240 TranslatePrefs translate_prefs(browser_helper_.profile()->GetPrefs()); |
| 241 translate_prefs.ResetTranslationDeniedCount("en"); | 241 translate_prefs.ResetTranslationDeniedCount("en"); |
| 242 for (int i = 0; i < 4; ++i) { | 242 for (int i = 0; i < 4; ++i) { |
| 243 translate_prefs.IncrementTranslationDeniedCount("en"); | 243 translate_prefs.IncrementTranslationDeniedCount("en"); |
| 244 } | 244 } |
| 245 CreateInfoBar(TranslateInfoBarDelegate::kBeforeTranslate); | 245 CreateInfoBar(TranslateInfoBarDelegate::BEFORE_TRANSLATE); |
| 246 BeforeTranslateInfobarController* controller = | 246 BeforeTranslateInfobarController* controller = |
| 247 (BeforeTranslateInfobarController*)infobar_controller.get(); | 247 (BeforeTranslateInfobarController*)infobar_controller.get(); |
| 248 EXPECT_TRUE([[controller alwaysTranslateButton] superview] == nil); | 248 EXPECT_TRUE([[controller alwaysTranslateButton] superview] == nil); |
| 249 EXPECT_TRUE([[controller neverTranslateButton] superview] != nil); | 249 EXPECT_TRUE([[controller neverTranslateButton] superview] != nil); |
| 250 } | 250 } |
| 251 | 251 |
| 252 } // namespace | 252 } // namespace |
| OLD | NEW |