Home | History | Annotate | Download | only in ime
      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 "base/logging.h"
      6 #include "chromeos/ime/component_extension_ime_manager.h"
      7 #include "chromeos/ime/extension_ime_util.h"
      8 #include "chromeos/ime/mock_component_extension_ime_manager_delegate.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 namespace chromeos {
     12 namespace input_method {
     13 
     14 namespace {
     15 
     16 class ComponentExtensionIMEManagerTest :
     17     public testing::Test,
     18     public ComponentExtensionIMEManager::Observer {
     19  public:
     20   ComponentExtensionIMEManagerTest()
     21       : mock_delegate_(NULL),
     22         on_initialized_callcount_(0) {
     23   }
     24 
     25   virtual void SetUp() {
     26     ime_list_.clear();
     27 
     28     ComponentExtensionIME ext1;
     29     ext1.id = "ext1_id";
     30     ext1.description = "ext1_description";
     31     ext1.options_page_url =
     32         GURL("chrome-extension://" + ext1.id + "/options.html");
     33     ext1.path = base::FilePath("ext1_file_path");
     34 
     35     ComponentExtensionEngine ext1_engine1;
     36     ext1_engine1.engine_id = "ext1_engine1_engine_id";
     37     ext1_engine1.display_name = "ext1_engine_1_display_name";
     38     ext1_engine1.language_codes.push_back("en");
     39     ext1_engine1.layouts.push_back("us");
     40     ext1.engines.push_back(ext1_engine1);
     41 
     42     ComponentExtensionEngine ext1_engine2;
     43     ext1_engine2.engine_id = "ext1_engine2_engine_id";
     44     ext1_engine2.display_name = "ext1_engine2_display_name";
     45     ext1_engine2.language_codes.push_back("en");
     46     ext1_engine2.layouts.push_back("us");
     47     ext1.engines.push_back(ext1_engine2);
     48 
     49     ComponentExtensionEngine ext1_engine3;
     50     ext1_engine3.engine_id = "ext1_engine3_engine_id";
     51     ext1_engine3.display_name = "ext1_engine3_display_name";
     52     ext1_engine3.language_codes.push_back("ja");
     53     ext1_engine3.layouts.push_back("us");
     54     ext1.engines.push_back(ext1_engine3);
     55 
     56     ime_list_.push_back(ext1);
     57 
     58     ComponentExtensionIME ext2;
     59     ext2.id = "ext2_id";
     60     ext2.description = "ext2_description";
     61     ext2.path = base::FilePath("ext2_file_path");
     62 
     63     ComponentExtensionEngine ext2_engine1;
     64     ext2_engine1.engine_id = "ext2_engine1_engine_id";
     65     ext2_engine1.display_name = "ext2_engine_1_display_name";
     66     ext2_engine1.language_codes.push_back("en");
     67     ext2_engine1.layouts.push_back("us");
     68     ext2.engines.push_back(ext2_engine1);
     69 
     70     ComponentExtensionEngine ext2_engine2;
     71     ext2_engine2.engine_id = "ext2_engine2_engine_id";
     72     ext2_engine2.display_name = "ext2_engine2_display_name";
     73     ext2_engine2.language_codes.push_back("hi");
     74     ext2_engine2.layouts.push_back("us");
     75     ext2.engines.push_back(ext2_engine2);
     76 
     77     ComponentExtensionEngine ext2_engine3;
     78     ext2_engine3.engine_id = "ext2_engine3_engine_id";
     79     ext2_engine3.display_name = "ext2_engine3_display_name";
     80     ext2_engine3.language_codes.push_back("ja");
     81     ext2_engine3.layouts.push_back("jp");
     82     ext2.engines.push_back(ext2_engine3);
     83 
     84     ime_list_.push_back(ext2);
     85 
     86     ComponentExtensionIME ext3;
     87     ext3.id = "ext3_id";
     88     ext3.description = "ext3_description";
     89     ext3.options_page_url =
     90     GURL("chrome-extension://" + ext3.id + "/options.html");
     91     ext3.path = base::FilePath("ext3_file_path");
     92 
     93     ComponentExtensionEngine ext3_engine1;
     94     ext3_engine1.engine_id = "ext3_engine1_engine_id";
     95     ext3_engine1.display_name = "ext3_engine_1_display_name";
     96     ext3_engine1.language_codes.push_back("hi");
     97     ext3_engine1.layouts.push_back("us");
     98     ext3.engines.push_back(ext3_engine1);
     99 
    100     ComponentExtensionEngine ext3_engine2;
    101     ext3_engine2.engine_id = "ext3_engine2_engine_id";
    102     ext3_engine2.display_name = "ext3_engine2_display_name";
    103     ext3_engine2.language_codes.push_back("en");
    104     ext3_engine2.layouts.push_back("us");
    105     ext3.engines.push_back(ext3_engine2);
    106 
    107     ComponentExtensionEngine ext3_engine3;
    108     ext3_engine3.engine_id = "ext3_engine3_engine_id";
    109     ext3_engine3.display_name = "ext3_engine3_display_name";
    110     ext3_engine3.language_codes.push_back("en");
    111     ext3_engine3.layouts.push_back("us");
    112     ext3.engines.push_back(ext3_engine3);
    113 
    114     ime_list_.push_back(ext3);
    115 
    116     mock_delegate_ = new MockComponentExtIMEManagerDelegate();
    117     mock_delegate_->set_ime_list(ime_list_);
    118     component_ext_mgr_.reset(new ComponentExtensionIMEManager());
    119     component_ext_mgr_->AddObserver(this);
    120     EXPECT_FALSE(component_ext_mgr_->IsInitialized());
    121     component_ext_mgr_->Initialize(
    122         scoped_ptr<ComponentExtensionIMEManagerDelegate>(
    123             mock_delegate_).Pass());
    124     EXPECT_TRUE(component_ext_mgr_->IsInitialized());
    125 
    126   }
    127 
    128   virtual void TearDown() {
    129     EXPECT_EQ(1, on_initialized_callcount_);
    130     component_ext_mgr_->RemoveObserver(this);
    131   }
    132 
    133  protected:
    134   MockComponentExtIMEManagerDelegate* mock_delegate_;
    135   scoped_ptr<ComponentExtensionIMEManager> component_ext_mgr_;
    136   std::vector<ComponentExtensionIME> ime_list_;
    137 
    138  private:
    139   virtual void OnInitialized() OVERRIDE {
    140     ++on_initialized_callcount_;
    141   }
    142 
    143   int on_initialized_callcount_;
    144 
    145   DISALLOW_COPY_AND_ASSIGN(ComponentExtensionIMEManagerTest);
    146 };
    147 
    148 TEST_F(ComponentExtensionIMEManagerTest, LoadComponentExtensionIMETest) {
    149   for (size_t i = 0; i < ime_list_.size(); ++i) {
    150     for (size_t j = 0; j < ime_list_[i].engines.size(); ++j) {
    151       const std::string input_method_id =
    152           extension_ime_util::GetComponentInputMethodID(
    153               ime_list_[i].id,
    154               ime_list_[i].engines[j].engine_id);
    155       component_ext_mgr_->LoadComponentExtensionIME(input_method_id);
    156       EXPECT_EQ(ime_list_[i].id, mock_delegate_->last_loaded_extension_id());
    157     }
    158   }
    159   EXPECT_EQ(9, mock_delegate_->load_call_count());
    160 }
    161 
    162 TEST_F(ComponentExtensionIMEManagerTest, UnloadComponentExtensionIMETest) {
    163   for (size_t i = 0; i < ime_list_.size(); ++i) {
    164     for (size_t j = 0; j < ime_list_[i].engines.size(); ++j) {
    165       const std::string input_method_id =
    166           extension_ime_util::GetComponentInputMethodID(
    167               ime_list_[i].id,
    168               ime_list_[i].engines[j].engine_id);
    169       component_ext_mgr_->UnloadComponentExtensionIME(input_method_id);
    170       EXPECT_EQ(ime_list_[i].id, mock_delegate_->last_unloaded_extension_id());
    171     }
    172   }
    173   EXPECT_EQ(9, mock_delegate_->unload_call_count());
    174 }
    175 
    176 TEST_F(ComponentExtensionIMEManagerTest, IsWhitelistedTest) {
    177   EXPECT_TRUE(component_ext_mgr_->IsWhitelisted(
    178       extension_ime_util::GetComponentInputMethodID(
    179           ime_list_[0].id,
    180           ime_list_[0].engines[0].engine_id)));
    181   EXPECT_FALSE(component_ext_mgr_->IsWhitelisted(
    182       extension_ime_util::GetInputMethodID(
    183           ime_list_[0].id,
    184           ime_list_[0].engines[0].engine_id)));
    185   EXPECT_FALSE(component_ext_mgr_->IsWhitelisted("mozc"));
    186   EXPECT_FALSE(component_ext_mgr_->IsWhitelisted(
    187       extension_ime_util::GetInputMethodID("AAAA", "012345")));
    188   EXPECT_FALSE(component_ext_mgr_->IsWhitelisted(
    189       extension_ime_util::GetComponentInputMethodID(
    190           "AAAA", "012345")));
    191 }
    192 
    193 TEST_F(ComponentExtensionIMEManagerTest, IsWhitelistedExtensionTest) {
    194   EXPECT_TRUE(component_ext_mgr_->IsWhitelistedExtension(ime_list_[0].id));
    195   EXPECT_TRUE(component_ext_mgr_->IsWhitelistedExtension(ime_list_[1].id));
    196   EXPECT_FALSE(component_ext_mgr_->IsWhitelistedExtension("dummy"));
    197   EXPECT_FALSE(component_ext_mgr_->IsWhitelistedExtension(""));
    198 }
    199 
    200 TEST_F(ComponentExtensionIMEManagerTest, GetNameDescriptionTest) {
    201   for (size_t i = 0; i < ime_list_.size(); ++i) {
    202     for (size_t j = 0; j < ime_list_[i].engines.size(); ++j) {
    203       const ComponentExtensionEngine& engine
    204           = ime_list_[i].engines[j];
    205 
    206       const std::string input_method_id =
    207           extension_ime_util::GetComponentInputMethodID(
    208               ime_list_[i].id,
    209               engine.engine_id);
    210 
    211       EXPECT_EQ(input_method_id,
    212                 component_ext_mgr_->GetId(ime_list_[i].id, engine.engine_id));
    213       EXPECT_EQ(engine.display_name,
    214                 component_ext_mgr_->GetName(input_method_id));
    215       EXPECT_EQ(engine.description,
    216                 component_ext_mgr_->GetDescription(input_method_id));
    217     }
    218   }
    219 }
    220 
    221 TEST_F(ComponentExtensionIMEManagerTest, ListIMEByLanguageTest) {
    222   const std::string hindi_layout1 =
    223       extension_ime_util::GetComponentInputMethodID(
    224           ime_list_[1].id, ime_list_[1].engines[1].engine_id);
    225   const std::string hindi_layout2 =
    226       extension_ime_util::GetComponentInputMethodID(
    227           ime_list_[2].id, ime_list_[2].engines[0].engine_id);
    228 
    229   std::vector<std::string> hindi_list
    230       = component_ext_mgr_->ListIMEByLanguage("hi");
    231   ASSERT_EQ(2UL, hindi_list.size());
    232   EXPECT_TRUE(hindi_list[0] == hindi_layout1 || hindi_list[0] == hindi_layout2);
    233   EXPECT_TRUE(hindi_list[1] == hindi_layout1 || hindi_list[1] == hindi_layout2);
    234 
    235   EXPECT_EQ(0UL, component_ext_mgr_->ListIMEByLanguage("ru").size());
    236   EXPECT_EQ(0UL, component_ext_mgr_->ListIMEByLanguage("").size());
    237   EXPECT_EQ(0UL, component_ext_mgr_->ListIMEByLanguage("invalid").size());
    238   EXPECT_EQ(5UL, component_ext_mgr_->ListIMEByLanguage("en").size());
    239   EXPECT_EQ(2UL, component_ext_mgr_->ListIMEByLanguage("ja").size());
    240 }
    241 
    242 TEST_F(ComponentExtensionIMEManagerTest, GetAllIMEAsInputMethodDescriptor) {
    243   input_method::InputMethodDescriptors descriptors =
    244       component_ext_mgr_->GetAllIMEAsInputMethodDescriptor();
    245   size_t total_ime_size = 0;
    246   for (size_t i = 0; i < ime_list_.size(); ++i) {
    247     total_ime_size += ime_list_[i].engines.size();
    248   }
    249   EXPECT_EQ(total_ime_size, descriptors.size());
    250 }
    251 
    252 }  // namespace
    253 
    254 }  // namespace input_method
    255 }  // namespace chromeos
    256