Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / extensions / input_method_apitest_chromeos.cc
blob12b482ba0f7935efebba510fb6209a565ab8e57e
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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/extension_apitest.h"
7 #include "base/command_line.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/strings/stringprintf.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/chromeos/extensions/input_method_event_router.h"
12 #include "chrome/browser/extensions/api/test/test_api.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chromeos/ime/input_method_manager.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "content/public/browser/notification_service.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 namespace {
22 const char kLoginScreenUILanguage[] = "fr";
23 const char kInitialInputMethodOnLoginScreen[] = "xkb:us::eng";
24 const char kNewInputMethod[] = "fr::fra";
25 const char kSetInputMethodMessage[] = "setInputMethod";
26 const char kSetInputMethodDone[] = "done";
28 // Class that listens for the JS message then changes input method and replies
29 // back.
30 class SetInputMethodListener : public content::NotificationObserver {
31 public:
32 // Creates listener, which should reply exactly |count_| times.
33 explicit SetInputMethodListener(int count) : count_(count) {
34 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_TEST_MESSAGE,
35 content::NotificationService::AllSources());
36 chromeos::input_method::InputMethodManager::Get()->
37 EnableLayouts(kLoginScreenUILanguage, kInitialInputMethodOnLoginScreen);
40 virtual ~SetInputMethodListener() {
41 EXPECT_EQ(0, count_);
44 // Implements the content::NotificationObserver interface.
45 virtual void Observe(int type,
46 const content::NotificationSource& source,
47 const content::NotificationDetails& details) OVERRIDE {
48 const std::string& content = *content::Details<std::string>(details).ptr();
49 const std::string expected_message =
50 base::StringPrintf("%s:%s", kSetInputMethodMessage, kNewInputMethod);
51 if (content == expected_message) {
52 chromeos::input_method::InputMethodManager::Get()->
53 ChangeInputMethod(base::StringPrintf("xkb:%s", kNewInputMethod));
55 scoped_refptr<extensions::TestSendMessageFunction> function =
56 content::Source<extensions::TestSendMessageFunction>(
57 source).ptr();
58 EXPECT_GT(count_--, 0);
59 function->Reply(kSetInputMethodDone);
63 private:
64 content::NotificationRegistrar registrar_;
66 int count_;
69 class ExtensionInputMethodApiTest : public ExtensionApiTest {
70 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
71 ExtensionApiTest::SetUpCommandLine(command_line);
72 command_line->AppendSwitchASCII(
73 switches::kWhitelistedExtensionID, "ilanclmaeigfpnmdlgelmhkpkegdioip");
77 } // namespace
79 IN_PROC_BROWSER_TEST_F(ExtensionInputMethodApiTest, Basic) {
80 // Two test, two calls. See JS code for more info.
81 SetInputMethodListener listener(2);
83 ASSERT_TRUE(RunExtensionTest("input_method")) << message_;