OLD | NEW |
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/extensions/extension_apitest.h" | 5 #include "chrome/browser/extensions/extension_apitest.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
13 #include "chrome/browser/chromeos/extensions/input_method_event_router.h" | 13 #include "chrome/browser/chromeos/extensions/input_method_event_router.h" |
| 14 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
14 #include "chrome/browser/extensions/api/test/test_api.h" | 15 #include "chrome/browser/extensions/api/test/test_api.h" |
15 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
16 #include "chromeos/ime/extension_ime_util.h" | 17 #include "chromeos/ime/extension_ime_util.h" |
17 #include "chromeos/ime/input_method_manager.h" | 18 #include "chromeos/ime/input_method_manager.h" |
18 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
19 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
20 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 const char kLoginScreenUILanguage[] = "fr"; | 26 const char kLoginScreenUILanguage[] = "fr"; |
26 const char kInitialInputMethodOnLoginScreen[] = "xkb:us::eng"; | 27 const char kInitialInputMethodOnLoginScreen[] = "xkb:us::eng"; |
27 const char kNewInputMethod[] = "fr::fra"; | 28 const char kNewInputMethod[] = "fr::fra"; |
28 const char kSetInputMethodMessage[] = "setInputMethod"; | 29 const char kSetInputMethodMessage[] = "setInputMethod"; |
29 const char kSetInputMethodDone[] = "done"; | 30 const char kSetInputMethodDone[] = "done"; |
| 31 const char kBackgroundReady[] = "ready"; |
30 | 32 |
31 // Class that listens for the JS message then changes input method and replies | 33 // Class that listens for the JS message then changes input method and replies |
32 // back. | 34 // back. |
33 class SetInputMethodListener : public content::NotificationObserver { | 35 class SetInputMethodListener : public content::NotificationObserver { |
34 public: | 36 public: |
35 // Creates listener, which should reply exactly |count_| times. | 37 // Creates listener, which should reply exactly |count_| times. |
36 explicit SetInputMethodListener(int count) : count_(count) { | 38 explicit SetInputMethodListener(int count) : count_(count) { |
37 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_TEST_MESSAGE, | 39 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_TEST_MESSAGE, |
38 content::NotificationService::AllSources()); | 40 content::NotificationService::AllSources()); |
39 std::vector<std::string> keyboard_layouts; | |
40 keyboard_layouts.push_back(kInitialInputMethodOnLoginScreen); | |
41 chromeos::input_method::InputMethodManager::Get()->EnableLoginLayouts( | |
42 kLoginScreenUILanguage, keyboard_layouts); | |
43 } | 41 } |
44 | 42 |
45 virtual ~SetInputMethodListener() { | 43 virtual ~SetInputMethodListener() { |
46 EXPECT_EQ(0, count_); | 44 EXPECT_EQ(0, count_); |
47 } | 45 } |
48 | 46 |
49 // Implements the content::NotificationObserver interface. | 47 // Implements the content::NotificationObserver interface. |
50 virtual void Observe(int type, | 48 virtual void Observe(int type, |
51 const content::NotificationSource& source, | 49 const content::NotificationSource& source, |
52 const content::NotificationDetails& details) OVERRIDE { | 50 const content::NotificationDetails& details) OVERRIDE { |
53 const std::string& content = *content::Details<std::string>(details).ptr(); | 51 const std::string& content = *content::Details<std::string>(details).ptr(); |
| 52 if (content == kBackgroundReady) { |
| 53 // Initializes IMF for testing when receives ready message from |
| 54 // background. |
| 55 chromeos::input_method::InputMethodManager* manager = |
| 56 chromeos::input_method::InputMethodManager::Get(); |
| 57 manager->GetInputMethodUtil()->InitXkbInputMethodsForTesting(); |
| 58 |
| 59 std::vector<std::string> keyboard_layouts; |
| 60 keyboard_layouts.push_back( |
| 61 chromeos::extension_ime_util::GetInputMethodIDByKeyboardLayout( |
| 62 kInitialInputMethodOnLoginScreen)); |
| 63 manager->EnableLoginLayouts(kLoginScreenUILanguage, keyboard_layouts); |
| 64 return; |
| 65 } |
| 66 |
54 const std::string expected_message = | 67 const std::string expected_message = |
55 base::StringPrintf("%s:%s", kSetInputMethodMessage, kNewInputMethod); | 68 base::StringPrintf("%s:%s", kSetInputMethodMessage, kNewInputMethod); |
56 if (content == expected_message) { | 69 if (content == expected_message) { |
57 chromeos::input_method::InputMethodManager::Get()-> | 70 chromeos::input_method::InputMethodManager::Get()->ChangeInputMethod( |
58 ChangeInputMethod(base::StringPrintf("xkb:%s", kNewInputMethod)); | 71 chromeos::extension_ime_util::GetInputMethodIDByKeyboardLayout( |
| 72 base::StringPrintf("xkb:%s", kNewInputMethod))); |
59 | 73 |
60 scoped_refptr<extensions::TestSendMessageFunction> function = | 74 scoped_refptr<extensions::TestSendMessageFunction> function = |
61 content::Source<extensions::TestSendMessageFunction>( | 75 content::Source<extensions::TestSendMessageFunction>( |
62 source).ptr(); | 76 source).ptr(); |
63 EXPECT_GT(count_--, 0); | 77 EXPECT_GT(count_--, 0); |
64 function->Reply(kSetInputMethodDone); | 78 function->Reply(kSetInputMethodDone); |
65 } | 79 } |
66 } | 80 } |
67 | 81 |
68 private: | 82 private: |
69 content::NotificationRegistrar registrar_; | 83 content::NotificationRegistrar registrar_; |
70 | 84 |
71 int count_; | 85 int count_; |
72 }; | 86 }; |
73 | 87 |
74 class ExtensionInputMethodApiTest : public ExtensionApiTest { | 88 class ExtensionInputMethodApiTest : public ExtensionApiTest { |
75 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 89 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
76 ExtensionApiTest::SetUpCommandLine(command_line); | 90 ExtensionApiTest::SetUpCommandLine(command_line); |
77 command_line->AppendSwitchASCII( | 91 command_line->AppendSwitchASCII( |
78 switches::kWhitelistedExtensionID, "ilanclmaeigfpnmdlgelmhkpkegdioip"); | 92 switches::kWhitelistedExtensionID, "ilanclmaeigfpnmdlgelmhkpkegdioip"); |
79 } | 93 } |
80 }; | 94 }; |
81 | 95 |
82 } // namespace | 96 } // namespace |
83 | 97 |
84 IN_PROC_BROWSER_TEST_F(ExtensionInputMethodApiTest, Basic) { | 98 IN_PROC_BROWSER_TEST_F(ExtensionInputMethodApiTest, Basic) { |
85 chromeos::extension_ime_util::ScopedUseExtensionKeyboardFlagForTesting | |
86 scoped_flag(false); | |
87 | |
88 // Two test, two calls. See JS code for more info. | 99 // Two test, two calls. See JS code for more info. |
89 SetInputMethodListener listener(2); | 100 SetInputMethodListener listener(2); |
90 | 101 |
91 ASSERT_TRUE(RunExtensionTest("input_method")) << message_; | 102 ASSERT_TRUE(RunExtensionTest("input_method")) << message_; |
92 } | 103 } |
OLD | NEW |