1 // Copyright 2013 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 "chrome/browser/tab_contents/language_state.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/tab_contents/language_state_observer.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/test/base/browser_with_test_window_test.h"
11 #include "content/public/browser/web_contents.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 class MockLanguageStateObserver
: public LanguageStateObserver
{
18 MockLanguageStateObserver()
19 : on_is_page_translated_changed_called_(false),
20 on_translate_enabled_changed_called_(false) {
23 virtual ~MockLanguageStateObserver() {}
25 virtual void OnIsPageTranslatedChanged(
26 content::WebContents
* source
) OVERRIDE
{
27 on_is_page_translated_changed_called_
= true;
30 virtual void OnTranslateEnabledChanged(
31 content::WebContents
* source
) OVERRIDE
{
32 on_translate_enabled_changed_called_
= true;
35 bool on_is_page_translated_changed_called() const {
36 return on_is_page_translated_changed_called_
;
39 bool on_translate_enabled_changed_called() const {
40 return on_translate_enabled_changed_called_
;
44 bool on_is_page_translated_changed_called_
;
45 bool on_translate_enabled_changed_called_
;
47 DISALLOW_COPY_AND_ASSIGN(MockLanguageStateObserver
);
52 typedef BrowserWithTestWindowTest LanguageStateTest
;
54 TEST_F(LanguageStateTest
, IsPageTranslated
) {
55 LanguageState
language_state(NULL
);
56 EXPECT_FALSE(language_state
.IsPageTranslated());
58 // Navigate to a French page.
59 language_state
.LanguageDetermined("fr", true);
60 EXPECT_EQ("fr", language_state
.original_language());
61 EXPECT_EQ("fr", language_state
.current_language());
62 EXPECT_FALSE(language_state
.IsPageTranslated());
64 // Translate the page into English.
65 language_state
.SetCurrentLanguage("en");
66 EXPECT_EQ("fr", language_state
.original_language());
67 EXPECT_EQ("en", language_state
.current_language());
68 EXPECT_TRUE(language_state
.IsPageTranslated());
70 // Move on another page in Japanese.
71 language_state
.LanguageDetermined("ja", true);
72 EXPECT_EQ("ja", language_state
.original_language());
73 EXPECT_EQ("ja", language_state
.current_language());
74 EXPECT_FALSE(language_state
.IsPageTranslated());
77 TEST_F(LanguageStateTest
, Observer
) {
78 GURL
url("http://foo/");
79 AddTab(browser(), url
);
80 content::NavigationController
& navigation_controller
=
81 browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
83 scoped_ptr
<MockLanguageStateObserver
> observer(
84 new MockLanguageStateObserver
);
85 LanguageState
language_state(&navigation_controller
);
86 language_state
.set_observer(observer
.get());
88 // Enable/Disable translate.
89 EXPECT_FALSE(language_state
.translate_enabled());
90 EXPECT_FALSE(observer
->on_translate_enabled_changed_called());
91 language_state
.SetTranslateEnabled(true);
92 EXPECT_TRUE(language_state
.translate_enabled());
93 EXPECT_TRUE(observer
->on_translate_enabled_changed_called());
95 observer
.reset(new MockLanguageStateObserver
);
96 language_state
.set_observer(observer
.get());
97 language_state
.SetTranslateEnabled(false);
98 EXPECT_FALSE(language_state
.translate_enabled());
99 EXPECT_TRUE(observer
->on_translate_enabled_changed_called());
101 // Navigate to a French page.
102 observer
.reset(new MockLanguageStateObserver
);
103 language_state
.set_observer(observer
.get());
104 language_state
.LanguageDetermined("fr", true);
105 EXPECT_FALSE(language_state
.translate_enabled());
106 EXPECT_FALSE(observer
->on_is_page_translated_changed_called());
107 EXPECT_FALSE(observer
->on_translate_enabled_changed_called());
110 language_state
.SetCurrentLanguage("en");
111 EXPECT_TRUE(language_state
.IsPageTranslated());
112 EXPECT_TRUE(observer
->on_is_page_translated_changed_called());
114 // Translate feature must be enabled after an actual translation.
115 EXPECT_TRUE(language_state
.translate_enabled());
116 EXPECT_TRUE(observer
->on_translate_enabled_changed_called());