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"
17 const std::string kHtmlMimeType
= "text/html";
19 class MockTranslateDriver
: public TranslateDriver
{
22 : on_is_page_translated_changed_called_(false),
23 on_translate_enabled_changed_called_(false),
24 language_state_(this) {
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
& GetVisibleURL() override
{ return GURL::EmptyGURL(); }
59 bool HasCurrentPage() override
{ return true; }
61 void OpenUrlInNewTab(const GURL
& url
) override
{}
63 bool on_is_page_translated_changed_called() const {
64 return on_is_page_translated_changed_called_
;
67 bool on_translate_enabled_changed_called() const {
68 return on_translate_enabled_changed_called_
;
72 bool on_is_page_translated_changed_called_
;
73 bool on_translate_enabled_changed_called_
;
74 LanguageState language_state_
;
76 DISALLOW_COPY_AND_ASSIGN(MockTranslateDriver
);
81 TEST(LanguageStateTest
, IsPageTranslated
) {
82 scoped_ptr
<MockTranslateDriver
> driver(
83 new MockTranslateDriver
);
84 LanguageState
language_state(driver
.get());
85 EXPECT_FALSE(language_state
.IsPageTranslated());
87 // Navigate to a French page.
88 language_state
.LanguageDetermined("fr", true);
89 EXPECT_EQ("fr", language_state
.original_language());
90 EXPECT_EQ("fr", language_state
.current_language());
91 EXPECT_FALSE(language_state
.IsPageTranslated());
93 // Translate the page into English.
94 language_state
.SetCurrentLanguage("en");
95 EXPECT_EQ("fr", language_state
.original_language());
96 EXPECT_EQ("en", language_state
.current_language());
97 EXPECT_TRUE(language_state
.IsPageTranslated());
99 // Move on another page in Japanese.
100 language_state
.LanguageDetermined("ja", true);
101 EXPECT_EQ("ja", language_state
.original_language());
102 EXPECT_EQ("ja", language_state
.current_language());
103 EXPECT_FALSE(language_state
.IsPageTranslated());
106 TEST(LanguageStateTest
, Driver
) {
108 scoped_ptr
<MockTranslateDriver
> driver(
109 new MockTranslateDriver
);
110 LanguageState
language_state(driver
.get());
112 // Enable/Disable translate.
113 EXPECT_FALSE(language_state
.translate_enabled());
114 EXPECT_FALSE(driver
->on_translate_enabled_changed_called());
115 language_state
.SetTranslateEnabled(true);
116 EXPECT_TRUE(language_state
.translate_enabled());
117 EXPECT_TRUE(driver
->on_translate_enabled_changed_called());
120 language_state
.SetTranslateEnabled(false);
121 EXPECT_FALSE(language_state
.translate_enabled());
122 EXPECT_TRUE(driver
->on_translate_enabled_changed_called());
124 // Navigate to a French page.
126 language_state
.LanguageDetermined("fr", true);
127 EXPECT_FALSE(language_state
.translate_enabled());
128 EXPECT_FALSE(driver
->on_is_page_translated_changed_called());
129 EXPECT_FALSE(driver
->on_translate_enabled_changed_called());
132 language_state
.SetCurrentLanguage("en");
133 EXPECT_TRUE(language_state
.IsPageTranslated());
134 EXPECT_TRUE(driver
->on_is_page_translated_changed_called());
136 // Translate feature must be enabled after an actual translation.
137 EXPECT_TRUE(language_state
.translate_enabled());
138 EXPECT_TRUE(driver
->on_translate_enabled_changed_called());
141 } // namespace translate