Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(341)

Side by Side Diff: chrome/browser/chromeos/input_method/input_method_util_unittest.cc

Issue 11415266: Extract a delegate interface from c/b/input_method to permit decoupling from c/b. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Class comment. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/input_method/input_method_util.h" 5 #include "chrome/browser/chromeos/input_method/input_method_util.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/chromeos/input_method/input_method_manager.h" 11 #include "chrome/browser/chromeos/input_method/input_method_manager.h"
12 #include "chrome/browser/chromeos/input_method/input_method_whitelist.h" 12 #include "chrome/browser/chromeos/input_method/input_method_whitelist.h"
13 #include "chrome/browser/chromeos/input_method/mock_input_method_delegate.h"
13 #include "grit/generated_resources.h" 14 #include "grit/generated_resources.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/base/l10n/l10n_util.h" 16 #include "ui/base/l10n/l10n_util.h"
16 17
17 namespace chromeos { 18 namespace chromeos {
18 19
19 extern const char* kExtensionImePrefix; 20 extern const char* kExtensionImePrefix;
20 21
21 namespace input_method { 22 namespace input_method {
22 23
23 namespace { 24 namespace {
24 25
25 class TestableInputMethodUtil : public InputMethodUtil { 26 class TestableInputMethodUtil : public InputMethodUtil {
26 public: 27 public:
27 explicit TestableInputMethodUtil(scoped_ptr<InputMethodDescriptors> methods) 28 explicit TestableInputMethodUtil(InputMethodDelegate* delegate,
28 : InputMethodUtil(methods.Pass()) { 29 scoped_ptr<InputMethodDescriptors> methods)
30 : InputMethodUtil(delegate, methods.Pass()) {
29 } 31 }
30 // Change access rights. 32 // Change access rights.
31 using InputMethodUtil::StringIsSupported; 33 using InputMethodUtil::StringIsSupported;
32 using InputMethodUtil::GetInputMethodIdsFromLanguageCodeInternal; 34 using InputMethodUtil::GetInputMethodIdsFromLanguageCodeInternal;
33 using InputMethodUtil::ReloadInternalMaps; 35 using InputMethodUtil::ReloadInternalMaps;
34 using InputMethodUtil::SortLanguageCodesByNames; 36 using InputMethodUtil::SortLanguageCodesByNames;
35 using InputMethodUtil::supported_input_methods_; 37 using InputMethodUtil::supported_input_methods_;
36 }; 38 };
37 39
38 } // namespace 40 } // namespace
39 41
40 class InputMethodUtilTest : public testing::Test { 42 class InputMethodUtilTest : public testing::Test {
41 public: 43 public:
42 InputMethodUtilTest() : util_(whitelist_.GetSupportedInputMethods()) { 44 InputMethodUtilTest()
45 : util_(&delegate_, whitelist_.GetSupportedInputMethods()) {
43 } 46 }
44 47
45 InputMethodDescriptor GetDesc(const std::string& id, 48 InputMethodDescriptor GetDesc(const std::string& id,
46 const std::string& raw_layout, 49 const std::string& raw_layout,
47 const std::string& language_code) { 50 const std::string& language_code) {
48 return InputMethodDescriptor(id, 51 return InputMethodDescriptor(id,
49 "", 52 "",
50 raw_layout, 53 raw_layout,
51 language_code, 54 language_code,
52 false); 55 false);
53 } 56 }
54 57
58 MockInputMethodDelegate delegate_;
55 InputMethodWhitelist whitelist_; 59 InputMethodWhitelist whitelist_;
56 TestableInputMethodUtil util_; 60 TestableInputMethodUtil util_;
57 }; 61 };
58 62
59 TEST_F(InputMethodUtilTest, GetInputMethodShortNameTest) { 63 TEST_F(InputMethodUtilTest, GetInputMethodShortNameTest) {
60 // Test normal cases. Two-letter language code should be returned. 64 // Test normal cases. Two-letter language code should be returned.
61 { 65 {
62 InputMethodDescriptor desc = GetDesc("m17n:fa:isiri", // input method id 66 InputMethodDescriptor desc = GetDesc("m17n:fa:isiri", // input method id
63 "us", // keyboard layout name 67 "us", // keyboard layout name
64 "fa"); // language name 68 "fa"); // language name
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 } 370 }
367 371
368 TEST_F(InputMethodUtilTest, TestGetLanguageNativeDisplayNameFromCode) { 372 TEST_F(InputMethodUtilTest, TestGetLanguageNativeDisplayNameFromCode) {
369 EXPECT_EQ(UTF8ToUTF16("suomi"), 373 EXPECT_EQ(UTF8ToUTF16("suomi"),
370 InputMethodUtil::GetLanguageNativeDisplayNameFromCode("fi")); 374 InputMethodUtil::GetLanguageNativeDisplayNameFromCode("fi"));
371 } 375 }
372 376
373 TEST_F(InputMethodUtilTest, TestSortLanguageCodesByNames) { 377 TEST_F(InputMethodUtilTest, TestSortLanguageCodesByNames) {
374 std::vector<std::string> language_codes; 378 std::vector<std::string> language_codes;
375 // Check if this function can handle an empty list. 379 // Check if this function can handle an empty list.
376 TestableInputMethodUtil::SortLanguageCodesByNames(&language_codes); 380 util_.SortLanguageCodesByNames(&language_codes);
377 381
378 language_codes.push_back("ja"); 382 language_codes.push_back("ja");
379 language_codes.push_back("fr"); 383 language_codes.push_back("fr");
380 // For "t", see the comment in NormalizeLanguageCode test. 384 // For "t", see the comment in NormalizeLanguageCode test.
381 language_codes.push_back("t"); 385 language_codes.push_back("t");
382 TestableInputMethodUtil::SortLanguageCodesByNames(&language_codes); 386 util_.SortLanguageCodesByNames(&language_codes);
383 ASSERT_EQ(3U, language_codes.size()); 387 ASSERT_EQ(3U, language_codes.size());
384 ASSERT_EQ("fr", language_codes[0]); // French 388 ASSERT_EQ("fr", language_codes[0]); // French
385 ASSERT_EQ("ja", language_codes[1]); // Japanese 389 ASSERT_EQ("ja", language_codes[1]); // Japanese
386 ASSERT_EQ("t", language_codes[2]); // Others 390 ASSERT_EQ("t", language_codes[2]); // Others
387 391
388 // Add a duplicate entry and see if it works. 392 // Add a duplicate entry and see if it works.
389 language_codes.push_back("ja"); 393 language_codes.push_back("ja");
390 TestableInputMethodUtil::SortLanguageCodesByNames(&language_codes); 394 util_.SortLanguageCodesByNames(&language_codes);
391 ASSERT_EQ(4U, language_codes.size()); 395 ASSERT_EQ(4U, language_codes.size());
392 ASSERT_EQ("fr", language_codes[0]); // French 396 ASSERT_EQ("fr", language_codes[0]); // French
393 ASSERT_EQ("ja", language_codes[1]); // Japanese 397 ASSERT_EQ("ja", language_codes[1]); // Japanese
394 ASSERT_EQ("ja", language_codes[2]); // Japanese 398 ASSERT_EQ("ja", language_codes[2]); // Japanese
395 ASSERT_EQ("t", language_codes[3]); // Others 399 ASSERT_EQ("t", language_codes[3]); // Others
396 } 400 }
397 401
398 TEST_F(InputMethodUtilTest, TestGetInputMethodIdsForLanguageCode) { 402 TEST_F(InputMethodUtilTest, TestGetInputMethodIdsForLanguageCode) {
399 std::multimap<std::string, std::string> language_code_to_ids_map; 403 std::multimap<std::string, std::string> language_code_to_ids_map;
400 language_code_to_ids_map.insert(std::make_pair("ja", "mozc")); 404 language_code_to_ids_map.insert(std::make_pair("ja", "mozc"));
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
529 input_method_ids.push_back("mozc-jp"); // Japanese. 533 input_method_ids.push_back("mozc-jp"); // Japanese.
530 input_method_ids.push_back("xkb:fr::fra"); // French France. 534 input_method_ids.push_back("xkb:fr::fra"); // French France.
531 std::vector<std::string> language_codes; 535 std::vector<std::string> language_codes;
532 util_.GetLanguageCodesFromInputMethodIds(input_method_ids, &language_codes); 536 util_.GetLanguageCodesFromInputMethodIds(input_method_ids, &language_codes);
533 ASSERT_EQ(3U, language_codes.size()); 537 ASSERT_EQ(3U, language_codes.size());
534 EXPECT_EQ("en-US", language_codes[0]); 538 EXPECT_EQ("en-US", language_codes[0]);
535 EXPECT_EQ("ja", language_codes[1]); 539 EXPECT_EQ("ja", language_codes[1]);
536 EXPECT_EQ("fr", language_codes[2]); 540 EXPECT_EQ("fr", language_codes[2]);
537 } 541 }
538 542
539 TEST_F(InputMethodUtilTest, TestSetHardwareInputMethodId) {
540 util_.SetHardwareInputMethodIdForTesting("xkb:fr::fra");
541 EXPECT_EQ("xkb:fr::fra", util_.GetHardwareInputMethodId());
542 // Reset to the default behavior just in case.
543 util_.SetHardwareInputMethodIdForTesting("");
544 }
545
546 // Test all supported descriptors to detect a typo in ibus_input_methods.txt. 543 // Test all supported descriptors to detect a typo in ibus_input_methods.txt.
547 TEST_F(InputMethodUtilTest, TestIBusInputMethodText) { 544 TEST_F(InputMethodUtilTest, TestIBusInputMethodText) {
548 for (size_t i = 0; i < util_.supported_input_methods_->size(); ++i) { 545 for (size_t i = 0; i < util_.supported_input_methods_->size(); ++i) {
549 const std::string language_code = 546 const std::string language_code =
550 util_.supported_input_methods_->at(i).language_code(); 547 util_.supported_input_methods_->at(i).language_code();
551 const string16 display_name = 548 const string16 display_name =
552 l10n_util::GetDisplayNameForLocale(language_code, "en", false); 549 l10n_util::GetDisplayNameForLocale(language_code, "en", false);
553 // Only two formats, like "fr" (lower case) and "en-US" (lower-upper), are 550 // Only two formats, like "fr" (lower case) and "en-US" (lower-upper), are
554 // allowed. See the text file for details. 551 // allowed. See the text file for details.
555 EXPECT_TRUE(language_code.length() == 2 || 552 EXPECT_TRUE(language_code.length() == 2 ||
556 (language_code.length() == 5 && language_code[2] == '-')) 553 (language_code.length() == 5 && language_code[2] == '-'))
557 << "Invalid language code " << language_code; 554 << "Invalid language code " << language_code;
558 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax(language_code)) 555 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax(language_code))
559 << "Invalid language code " << language_code; 556 << "Invalid language code " << language_code;
560 EXPECT_FALSE(display_name.empty()) 557 EXPECT_FALSE(display_name.empty())
561 << "Invalid language code " << language_code; 558 << "Invalid language code " << language_code;
562 // On error, GetDisplayNameForLocale() returns the |language_code| as-is. 559 // On error, GetDisplayNameForLocale() returns the |language_code| as-is.
563 EXPECT_NE(language_code, UTF16ToUTF8(display_name)) 560 EXPECT_NE(language_code, UTF16ToUTF8(display_name))
564 << "Invalid language code " << language_code; 561 << "Invalid language code " << language_code;
565 } 562 }
566 } 563 }
567 564
568 } // namespace input_method 565 } // namespace input_method
569 } // namespace chromeos 566 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698