ProjectingObserverChromeos: Drop DBusThreadManager dependency for better testing.
[chromium-blink-merge.git] / components / translate / core / browser / language_state_unittest.cc
blob3137369861504110b2a1cb08aadf49f3fb4455ef
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 virtual void OnIsPageTranslatedChanged() override {
35 on_is_page_translated_changed_called_ = true;
38 virtual void OnTranslateEnabledChanged() override {
39 on_translate_enabled_changed_called_ = true;
42 virtual bool IsLinkNavigation() override {
43 return false;
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 {}
51 virtual void RevertTranslation(int page_seq_no) override {}
53 virtual bool IsOffTheRecord() override { return false; }
55 virtual const std::string& GetContentsMimeType() override {
56 return kHtmlMimeType;
59 virtual const GURL& GetLastCommittedURL() override {
60 return GURL::EmptyGURL();
63 virtual const GURL& GetActiveURL() override { return GURL::EmptyGURL(); }
65 virtual const GURL& GetVisibleURL() override { return GURL::EmptyGURL(); }
67 virtual bool HasCurrentPage() override { return true; }
69 virtual void OpenUrlInNewTab(const GURL& url) override {}
71 bool on_is_page_translated_changed_called() const {
72 return on_is_page_translated_changed_called_;
75 bool on_translate_enabled_changed_called() const {
76 return on_translate_enabled_changed_called_;
79 private:
80 bool on_is_page_translated_changed_called_;
81 bool on_translate_enabled_changed_called_;
82 LanguageState language_state_;
84 DISALLOW_COPY_AND_ASSIGN(MockTranslateDriver);
87 } // namespace
89 TEST(LanguageStateTest, IsPageTranslated) {
90 scoped_ptr<MockTranslateDriver> driver(
91 new MockTranslateDriver);
92 LanguageState language_state(driver.get());
93 EXPECT_FALSE(language_state.IsPageTranslated());
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());
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());
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());
114 TEST(LanguageStateTest, Driver) {
116 scoped_ptr<MockTranslateDriver> driver(
117 new MockTranslateDriver);
118 LanguageState language_state(driver.get());
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());
127 driver->Reset();
128 language_state.SetTranslateEnabled(false);
129 EXPECT_FALSE(language_state.translate_enabled());
130 EXPECT_TRUE(driver->on_translate_enabled_changed_called());
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());
139 // Translate.
140 language_state.SetCurrentLanguage("en");
141 EXPECT_TRUE(language_state.IsPageTranslated());
142 EXPECT_TRUE(driver->on_is_page_translated_changed_called());
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());
149 } // namespace translate