Home | History | Annotate | Download | only in tab_contents
      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.
      4 
      5 #include "chrome/browser/tab_contents/language_state.h"
      6 
      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"
     13 
     14 namespace {
     15 
     16 class MockLanguageStateObserver : public LanguageStateObserver {
     17  public:
     18   MockLanguageStateObserver()
     19       : on_is_page_translated_changed_called_(false),
     20         on_translate_enabled_changed_called_(false) {
     21   }
     22 
     23   virtual ~MockLanguageStateObserver() {}
     24 
     25   virtual void OnIsPageTranslatedChanged(
     26       content::WebContents* source) OVERRIDE {
     27     on_is_page_translated_changed_called_ = true;
     28   }
     29 
     30   virtual void OnTranslateEnabledChanged(
     31       content::WebContents* source) OVERRIDE {
     32     on_translate_enabled_changed_called_ = true;
     33   }
     34 
     35   bool on_is_page_translated_changed_called() const {
     36     return on_is_page_translated_changed_called_;
     37   }
     38 
     39   bool on_translate_enabled_changed_called() const {
     40     return on_translate_enabled_changed_called_;
     41   }
     42 
     43  private:
     44   bool on_is_page_translated_changed_called_;
     45   bool on_translate_enabled_changed_called_;
     46 
     47   DISALLOW_COPY_AND_ASSIGN(MockLanguageStateObserver);
     48 };
     49 
     50 }  // namespace
     51 
     52 typedef BrowserWithTestWindowTest LanguageStateTest;
     53 
     54 TEST_F(LanguageStateTest, IsPageTranslated) {
     55   LanguageState language_state(NULL);
     56   EXPECT_FALSE(language_state.IsPageTranslated());
     57 
     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());
     63 
     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());
     69 
     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());
     75 }
     76 
     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();
     82 
     83   scoped_ptr<MockLanguageStateObserver> observer(
     84       new MockLanguageStateObserver);
     85   LanguageState language_state(&navigation_controller);
     86   language_state.set_observer(observer.get());
     87 
     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());
     94 
     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());
    100 
    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());
    108 
    109   // Translate.
    110   language_state.SetCurrentLanguage("en");
    111   EXPECT_TRUE(language_state.IsPageTranslated());
    112   EXPECT_TRUE(observer->on_is_page_translated_changed_called());
    113 
    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());
    117 }
    118