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