Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / translate / core / browser / language_state_unittest.cc
blob12bab06ef9e1fd5234a0f19283be80908da310c2
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.
5 #include "components/translate/core/browser/language_state.h"
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"
13 namespace translate {
15 namespace {
17 const std::string kHtmlMimeType = "text/html";
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) {
27 void Reset() {
28 on_is_page_translated_changed_called_ = false;
29 on_translate_enabled_changed_called_ = false;
32 virtual ~MockTranslateDriver() {}
34 void OnIsPageTranslatedChanged() override {
35 on_is_page_translated_changed_called_ = true;
38 void OnTranslateEnabledChanged() override {
39 on_translate_enabled_changed_called_ = true;
42 bool IsLinkNavigation() override { return false; }
44 void TranslatePage(int page_seq_no,
45 const std::string& translate_script,
46 const std::string& source_lang,
47 const std::string& target_lang) override {}
49 void RevertTranslation(int page_seq_no) override {}
51 bool IsOffTheRecord() override { return false; }
53 const std::string& GetContentsMimeType() override { return kHtmlMimeType; }
55 const GURL& GetLastCommittedURL() override { return GURL::EmptyGURL(); }
57 const GURL& GetActiveURL() override { return GURL::EmptyGURL(); }
59 const GURL& GetVisibleURL() override { return GURL::EmptyGURL(); }
61 bool HasCurrentPage() override { return true; }
63 void OpenUrlInNewTab(const GURL& url) override {}
65 bool on_is_page_translated_changed_called() const {
66 return on_is_page_translated_changed_called_;
69 bool on_translate_enabled_changed_called() const {
70 return on_translate_enabled_changed_called_;
73 private:
74 bool on_is_page_translated_changed_called_;
75 bool on_translate_enabled_changed_called_;
76 LanguageState language_state_;
78 DISALLOW_COPY_AND_ASSIGN(MockTranslateDriver);
81 } // namespace
83 TEST(LanguageStateTest, IsPageTranslated) {
84 scoped_ptr<MockTranslateDriver> driver(
85 new MockTranslateDriver);
86 LanguageState language_state(driver.get());
87 EXPECT_FALSE(language_state.IsPageTranslated());
89 // Navigate to a French page.
90 language_state.LanguageDetermined("fr", true);
91 EXPECT_EQ("fr", language_state.original_language());
92 EXPECT_EQ("fr", language_state.current_language());
93 EXPECT_FALSE(language_state.IsPageTranslated());
95 // Translate the page into English.
96 language_state.SetCurrentLanguage("en");
97 EXPECT_EQ("fr", language_state.original_language());
98 EXPECT_EQ("en", language_state.current_language());
99 EXPECT_TRUE(language_state.IsPageTranslated());
101 // Move on another page in Japanese.
102 language_state.LanguageDetermined("ja", true);
103 EXPECT_EQ("ja", language_state.original_language());
104 EXPECT_EQ("ja", language_state.current_language());
105 EXPECT_FALSE(language_state.IsPageTranslated());
108 TEST(LanguageStateTest, Driver) {
110 scoped_ptr<MockTranslateDriver> driver(
111 new MockTranslateDriver);
112 LanguageState language_state(driver.get());
114 // Enable/Disable translate.
115 EXPECT_FALSE(language_state.translate_enabled());
116 EXPECT_FALSE(driver->on_translate_enabled_changed_called());
117 language_state.SetTranslateEnabled(true);
118 EXPECT_TRUE(language_state.translate_enabled());
119 EXPECT_TRUE(driver->on_translate_enabled_changed_called());
121 driver->Reset();
122 language_state.SetTranslateEnabled(false);
123 EXPECT_FALSE(language_state.translate_enabled());
124 EXPECT_TRUE(driver->on_translate_enabled_changed_called());
126 // Navigate to a French page.
127 driver->Reset();
128 language_state.LanguageDetermined("fr", true);
129 EXPECT_FALSE(language_state.translate_enabled());
130 EXPECT_FALSE(driver->on_is_page_translated_changed_called());
131 EXPECT_FALSE(driver->on_translate_enabled_changed_called());
133 // Translate.
134 language_state.SetCurrentLanguage("en");
135 EXPECT_TRUE(language_state.IsPageTranslated());
136 EXPECT_TRUE(driver->on_is_page_translated_changed_called());
138 // Translate feature must be enabled after an actual translation.
139 EXPECT_TRUE(language_state.translate_enabled());
140 EXPECT_TRUE(driver->on_translate_enabled_changed_called());
143 } // namespace translate