summaryrefslogtreecommitdiffstats
path: root/chrome/test/data/extensions/api_test/input_method/background.js
blob: 3ae7561494764cf64c47890def6210c730ec1306 (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.

var kOldInputMethod = "_comp_ime_fgoepimhcoialccpbmpnnblemnepkkaoxkb:us::eng";
var kNewInputMethod = "_comp_ime_fgoepimhcoialccpbmpnnblemnepkkaoxkb:fr::fra";
var kInvalidInputMethod = "xx::xxx";

// The tests needs to be executed in order.

function setTest() {
  console.log('setTest: Changing input method to: ' + kNewInputMethod);
  chrome.inputMethodPrivate.setCurrentInputMethod(kNewInputMethod,
    function() {
      chrome.test.assertTrue(!chrome.runtime.lastError);
      chrome.test.succeed();
    });
}

function getTest() {
  console.log('getTest: Getting current input method.');
  chrome.inputMethodPrivate.getCurrentInputMethod(function(inputMethod) {
    chrome.test.assertEq(kNewInputMethod, inputMethod);
    chrome.test.succeed();
  });
}

function observeTest() {
  console.log('observeTest: Adding input method event listener.');
  chrome.inputMethodPrivate.onChanged.addListener(function(subfix) {
    chrome.test.assertEq('us::eng', subfix);
    chrome.test.succeed();
  });
  console.log('observeTest: Changing input method to: ' + kOldInputMethod);
  chrome.inputMethodPrivate.setCurrentInputMethod(kOldInputMethod);
}


function setInvalidTest() {
  console.log(
      'setInvalidTest: Changing input method to: ' + kInvalidInputMethod);
  chrome.inputMethodPrivate.setCurrentInputMethod(kInvalidInputMethod,
    function() {
      chrome.test.assertTrue(!!chrome.runtime.lastError);
      chrome.test.succeed();
    });
}

function getListTest() {
  console.log('getListTest: Getting input method list.');
  chrome.inputMethodPrivate.getInputMethods(function(inputMethods) {
    chrome.test.assertEq(6, inputMethods.length);
    var foundOldInputMethod = false;
    var foundNewInputMethod = false;
    for (var i = 0; i < inputMethods.length; ++i) {
      if (inputMethods[i].id == kOldInputMethod)
        foundOldInputMethod = true;
      if (inputMethods[i].id == kNewInputMethod)
        foundNewInputMethod = true;
    }
    chrome.test.assertTrue(foundOldInputMethod);
    chrome.test.assertTrue(foundNewInputMethod);
    chrome.test.succeed();
  });
}

chrome.test.sendMessage('ready');
chrome.test.runTests(
    [setTest, getTest, observeTest, setInvalidTest, getListTest]);