1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "components/translate/core/browser/language_state.h" 6 7 #include "base/memory/scoped_ptr.h" 8 #include "components/translate/core/browser/language_state.h" 9 #include "components/translate/core/browser/translate_driver.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "url/gurl.h" 12 13 namespace translate { 14 15 namespace { 16 17 const std::string kHtmlMimeType = "text/html"; 18 19 class MockTranslateDriver : public TranslateDriver { 20 public: 21 MockTranslateDriver() 22 : on_is_page_translated_changed_called_(false), 23 on_translate_enabled_changed_called_(false), 24 language_state_(this) { 25 } 26 27 void Reset() { 28 on_is_page_translated_changed_called_ = false; 29 on_translate_enabled_changed_called_ = false; 30 } 31 32 virtual ~MockTranslateDriver() {} 33 34 virtual void OnIsPageTranslatedChanged() OVERRIDE { 35 on_is_page_translated_changed_called_ = true; 36 } 37 38 virtual void OnTranslateEnabledChanged() OVERRIDE { 39 on_translate_enabled_changed_called_ = true; 40 } 41 42 virtual bool IsLinkNavigation() OVERRIDE { 43 return false; 44 } 45 46 virtual void TranslatePage(int page_seq_no, 47 const std::string& translate_script, 48 const std::string& source_lang, 49 const std::string& target_lang) OVERRIDE {} 50 51 virtual void RevertTranslation(int page_seq_no) OVERRIDE {} 52 53 virtual bool IsOffTheRecord() OVERRIDE { return false; } 54 55 virtual const std::string& GetContentsMimeType() OVERRIDE { 56 return kHtmlMimeType; 57 } 58 59 virtual const GURL& GetLastCommittedURL() OVERRIDE { 60 return GURL::EmptyGURL(); 61 } 62 63 virtual const GURL& GetActiveURL() OVERRIDE { return GURL::EmptyGURL(); } 64 65 virtual const GURL& GetVisibleURL() OVERRIDE { return GURL::EmptyGURL(); } 66 67 virtual bool HasCurrentPage() OVERRIDE { return true; } 68 69 virtual void OpenUrlInNewTab(const GURL& url) OVERRIDE {} 70 71 bool on_is_page_translated_changed_called() const { 72 return on_is_page_translated_changed_called_; 73 } 74 75 bool on_translate_enabled_changed_called() const { 76 return on_translate_enabled_changed_called_; 77 } 78 79 private: 80 bool on_is_page_translated_changed_called_; 81 bool on_translate_enabled_changed_called_; 82 LanguageState language_state_; 83 84 DISALLOW_COPY_AND_ASSIGN(MockTranslateDriver); 85 }; 86 87 } // namespace 88 89 TEST(LanguageStateTest, IsPageTranslated) { 90 scoped_ptr<MockTranslateDriver> driver( 91 new MockTranslateDriver); 92 LanguageState language_state(driver.get()); 93 EXPECT_FALSE(language_state.IsPageTranslated()); 94 95 // Navigate to a French page. 96 language_state.LanguageDetermined("fr", true); 97 EXPECT_EQ("fr", language_state.original_language()); 98 EXPECT_EQ("fr", language_state.current_language()); 99 EXPECT_FALSE(language_state.IsPageTranslated()); 100 101 // Translate the page into English. 102 language_state.SetCurrentLanguage("en"); 103 EXPECT_EQ("fr", language_state.original_language()); 104 EXPECT_EQ("en", language_state.current_language()); 105 EXPECT_TRUE(language_state.IsPageTranslated()); 106 107 // Move on another page in Japanese. 108 language_state.LanguageDetermined("ja", true); 109 EXPECT_EQ("ja", language_state.original_language()); 110 EXPECT_EQ("ja", language_state.current_language()); 111 EXPECT_FALSE(language_state.IsPageTranslated()); 112 } 113 114 TEST(LanguageStateTest, Driver) { 115 116 scoped_ptr<MockTranslateDriver> driver( 117 new MockTranslateDriver); 118 LanguageState language_state(driver.get()); 119 120 // Enable/Disable translate. 121 EXPECT_FALSE(language_state.translate_enabled()); 122 EXPECT_FALSE(driver->on_translate_enabled_changed_called()); 123 language_state.SetTranslateEnabled(true); 124 EXPECT_TRUE(language_state.translate_enabled()); 125 EXPECT_TRUE(driver->on_translate_enabled_changed_called()); 126 127 driver->Reset(); 128 language_state.SetTranslateEnabled(false); 129 EXPECT_FALSE(language_state.translate_enabled()); 130 EXPECT_TRUE(driver->on_translate_enabled_changed_called()); 131 132 // Navigate to a French page. 133 driver->Reset(); 134 language_state.LanguageDetermined("fr", true); 135 EXPECT_FALSE(language_state.translate_enabled()); 136 EXPECT_FALSE(driver->on_is_page_translated_changed_called()); 137 EXPECT_FALSE(driver->on_translate_enabled_changed_called()); 138 139 // Translate. 140 language_state.SetCurrentLanguage("en"); 141 EXPECT_TRUE(language_state.IsPageTranslated()); 142 EXPECT_TRUE(driver->on_is_page_translated_changed_called()); 143 144 // Translate feature must be enabled after an actual translation. 145 EXPECT_TRUE(language_state.translate_enabled()); 146 EXPECT_TRUE(driver->on_translate_enabled_changed_called()); 147 } 148 149 } // namespace translate 150