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