summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_input_method_apitest.cc
blob: bce261cba4e7fc0a6e20f6622f29d0dfcf7d06e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/extension_apitest.h"

#include "base/stringprintf.h"
#include "chrome/browser/chromeos/extensions/input_method_event_router.h"
#include "chrome/browser/chromeos/input_method/input_method_manager.h"
#include "chrome/browser/extensions/extension_test_api.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/common/notification_observer.h"
#include "content/common/notification_registrar.h"
#include "content/common/notification_service.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

const char kNewInputMethod[] = "ru::rus";
const char kSetInputMethodMessage[] = "setInputMethod";
const char kSetInputMethodDone[] = "done";

// Class that listens for the JS message then changes input method and replies
// back.
class SetInputMethodListener : public NotificationObserver {
 public:
  // Creates listener, which should reply exactly |count_| times.
  explicit SetInputMethodListener(int count) : count_(count) {
    registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_TEST_MESSAGE,
                   NotificationService::AllSources());
  }

  virtual ~SetInputMethodListener() {
    EXPECT_EQ(0, count_);
  }

  // Implements the NotificationObserver interface.
  virtual void Observe(int type,
                       const NotificationSource& source,
                       const NotificationDetails& details) {
    const std::string& content = *Details<std::string>(details).ptr();
    const std::string expected_message = StringPrintf("%s:%s",
                                                      kSetInputMethodMessage,
                                                      kNewInputMethod);
    if (content == expected_message) {
      chromeos::input_method::InputMethodManager::GetInstance()->
          ChangeInputMethod(StringPrintf("xkb:%s", kNewInputMethod));

      ExtensionTestSendMessageFunction* function =
          Source<ExtensionTestSendMessageFunction>(source).ptr();
      EXPECT_GT(count_--, 0);
      function->Reply(kSetInputMethodDone);
    }
  }

 private:
  NotificationRegistrar registrar_;

  int count_;
};

}  // namespace

IN_PROC_BROWSER_TEST_F(ExtensionApiTest, InputMethodApiBasic) {
  // Two test, two calls. See JS code for more info.
  SetInputMethodListener listener(2);

  ASSERT_TRUE(RunExtensionTest("input_method")) << message_;
}