OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/auto_reset.h" |
5 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
6 #include "chrome/browser/extensions/api/input_ime/input_ime_api_nonchromeos.h" | 7 #include "chrome/browser/extensions/api/input_ime/input_ime_api_nonchromeos.h" |
7 #include "chrome/browser/extensions/extension_apitest.h" | 8 #include "chrome/browser/extensions/extension_apitest.h" |
8 #include "chrome/browser/ui/views/frame/browser_view.h" | 9 #include "chrome/browser/ui/browser_window.h" |
9 #include "chrome/common/chrome_switches.h" | 10 #include "chrome/common/chrome_switches.h" |
| 11 #include "chrome/test/base/ui_test_utils.h" |
10 #include "ui/aura/window.h" | 12 #include "ui/aura/window.h" |
11 #include "ui/aura/window_tree_host.h" | 13 #include "ui/aura/window_tree_host.h" |
12 #include "ui/base/ime/composition_text.h" | 14 #include "ui/base/ime/composition_text.h" |
13 #include "ui/base/ime/dummy_text_input_client.h" | 15 #include "ui/base/ime/dummy_text_input_client.h" |
14 #include "ui/base/ime/input_method.h" | 16 #include "ui/base/ime/input_method.h" |
15 | 17 |
16 namespace extensions { | 18 namespace extensions { |
17 | 19 |
18 class InputImeApiTest : public ExtensionApiTest { | 20 class InputImeApiTest : public ExtensionApiTest { |
19 public: | 21 public: |
20 InputImeApiTest() {} | 22 InputImeApiTest() {} |
21 | 23 |
22 protected: | 24 protected: |
23 void SetUpCommandLine(base::CommandLine* command_line) override { | 25 void SetUpCommandLine(base::CommandLine* command_line) override { |
24 ExtensionApiTest::SetUpCommandLine(command_line); | 26 ExtensionApiTest::SetUpCommandLine(command_line); |
25 command_line->AppendSwitch(switches::kEnableInputImeAPI); | 27 command_line->AppendSwitch(switches::kEnableInputImeAPI); |
26 } | 28 } |
27 | 29 |
| 30 // Sets the private flag of |track_key_events_for_testing_| in InputMethod. |
| 31 void SetTrackKeyEvents(ui::InputMethod* input_method, bool track) { |
| 32 input_method->track_key_events_for_testing_ = track; |
| 33 } |
| 34 |
| 35 // Returns true if the key events get from input method equals to the expected |
| 36 // key events. |
| 37 bool CompareKeyEvents( |
| 38 const std::vector<std::unique_ptr<ui::KeyEvent>>& expected_key_events, |
| 39 ui::InputMethod* input_method) { |
| 40 if (expected_key_events.size() != GetKeyEvents(input_method).size()) |
| 41 return false; |
| 42 for (size_t i = 0; i < expected_key_events.size(); i++) { |
| 43 ui::KeyEvent* event1 = expected_key_events[i].get(); |
| 44 ui::KeyEvent* event2 = GetKeyEvents(input_method)[i].get(); |
| 45 if (event1->type() != event2->type() || |
| 46 event1->key_code() != event2->key_code() || |
| 47 event1->flags() != event2->flags()) { |
| 48 return false; |
| 49 } |
| 50 } |
| 51 return true; |
| 52 } |
| 53 |
28 private: | 54 private: |
| 55 // Returns the tracked key events of using input.ime.sendKeyEvents API. |
| 56 const std::vector<std::unique_ptr<ui::KeyEvent>>& GetKeyEvents( |
| 57 ui::InputMethod* input_method) { |
| 58 return input_method->GetKeyEventsForTesting(); |
| 59 } |
| 60 |
29 DISALLOW_COPY_AND_ASSIGN(InputImeApiTest); | 61 DISALLOW_COPY_AND_ASSIGN(InputImeApiTest); |
30 }; | 62 }; |
31 | 63 |
32 IN_PROC_BROWSER_TEST_F(InputImeApiTest, BasicApiTest) { | 64 IN_PROC_BROWSER_TEST_F(InputImeApiTest, BasicApiTest) { |
33 // Manipulates the focused text input client because the follow cursor | 65 // Manipulates the focused text input client because the follow cursor |
34 // window requires the text input focus. | 66 // window requires the text input focus. |
35 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser()); | |
36 ui::InputMethod* input_method = | 67 ui::InputMethod* input_method = |
37 browser_view->GetNativeWindow()->GetHost()->GetInputMethod(); | 68 browser()->window()->GetNativeWindow()->GetHost()->GetInputMethod(); |
38 std::unique_ptr<ui::DummyTextInputClient> client( | 69 std::unique_ptr<ui::DummyTextInputClient> client( |
39 new ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT)); | 70 new ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT)); |
40 input_method->SetFocusedTextInputClient(client.get()); | 71 input_method->SetFocusedTextInputClient(client.get()); |
41 ExtensionFunction::ScopedUserGestureForTests scoped_user_gesture; | 72 ExtensionFunction::ScopedUserGestureForTests scoped_user_gesture; |
42 InputImeActivateFunction::disable_bubble_for_testing_ = true; | 73 base::AutoReset<bool> auto_reset_disable_bubble( |
| 74 &InputImeActivateFunction::disable_bubble_for_testing_, true); |
| 75 SetTrackKeyEvents(input_method, true); |
43 | 76 |
44 ASSERT_TRUE(RunExtensionTest("input_ime_nonchromeos")) << message_; | 77 ASSERT_TRUE(RunExtensionTest("input_ime_nonchromeos")) << message_; |
45 | 78 |
46 // Test the input.ime.sendKeyEvents API. | 79 // Test the input.ime.sendKeyEvents API. |
47 ASSERT_EQ(1, client->insert_char_count()); | 80 ASSERT_EQ(1, client->insert_char_count()); |
48 EXPECT_EQ(L'a', client->last_insert_char()); | 81 EXPECT_EQ(L'a', client->last_insert_char()); |
49 | 82 |
50 // Test the input.ime.commitText API. | 83 // Test the input.ime.commitText API. |
51 ASSERT_EQ(1, client->insert_text_count()); | 84 ASSERT_EQ(1, client->insert_text_count()); |
52 EXPECT_EQ(base::UTF8ToUTF16("test_commit_text"), client->last_insert_text()); | 85 EXPECT_EQ(base::UTF8ToUTF16("test_commit_text"), client->last_insert_text()); |
53 | 86 |
54 // Test the input.ime.setComposition API. | 87 // Test the input.ime.setComposition API. |
55 ui::CompositionText composition; | 88 ui::CompositionText composition; |
56 composition.text = base::UTF8ToUTF16("test_set_composition"); | 89 composition.text = base::UTF8ToUTF16("test_set_composition"); |
57 composition.underlines.push_back( | 90 composition.underlines.push_back( |
58 ui::CompositionUnderline(0, composition.text.length(), SK_ColorBLACK, | 91 ui::CompositionUnderline(0, composition.text.length(), SK_ColorBLACK, |
59 false /* thick */, SK_ColorTRANSPARENT)); | 92 false /* thick */, SK_ColorTRANSPARENT)); |
60 composition.selection = gfx::Range(2, 2); | 93 composition.selection = gfx::Range(2, 2); |
61 ASSERT_EQ(1, client->set_composition_count()); | 94 ASSERT_EQ(1, client->set_composition_count()); |
62 ASSERT_EQ(composition, client->last_composition()); | 95 ASSERT_EQ(composition, client->last_composition()); |
63 | 96 |
| 97 std::vector<std::unique_ptr<ui::KeyEvent>> key_events; |
| 98 key_events.push_back(std::unique_ptr<ui::KeyEvent>( |
| 99 new ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE))); |
| 100 key_events.push_back(std::unique_ptr<ui::KeyEvent>( |
| 101 new ui::KeyEvent(ui::ET_KEY_RELEASED, ui::VKEY_A, ui::EF_NONE))); |
| 102 key_events.push_back(std::unique_ptr<ui::KeyEvent>( |
| 103 new ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_CONTROL_DOWN))); |
| 104 key_events.push_back(std::unique_ptr<ui::KeyEvent>( |
| 105 new ui::KeyEvent(ui::ET_KEY_RELEASED, ui::VKEY_A, ui::EF_CONTROL_DOWN))); |
| 106 key_events.push_back(std::unique_ptr<ui::KeyEvent>( |
| 107 new ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_TAB, ui::EF_NONE))); |
| 108 |
| 109 EXPECT_TRUE(CompareKeyEvents(key_events, input_method)); |
| 110 |
64 input_method->DetachTextInputClient(client.get()); | 111 input_method->DetachTextInputClient(client.get()); |
65 } | 112 } |
66 | 113 |
| 114 IN_PROC_BROWSER_TEST_F(InputImeApiTest, SendKeyEventsOnSpecialPages) { |
| 115 // Navigates to special page that sendKeyEvents API has limition with. |
| 116 ui_test_utils::NavigateToURL(browser(), GURL("chrome://flags")); |
| 117 |
| 118 ui::InputMethod* input_method = |
| 119 browser()->window()->GetNativeWindow()->GetHost()->GetInputMethod(); |
| 120 std::unique_ptr<ui::DummyTextInputClient> client( |
| 121 new ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT)); |
| 122 input_method->SetFocusedTextInputClient(client.get()); |
| 123 ExtensionFunction::ScopedUserGestureForTests scoped_user_gesture; |
| 124 base::AutoReset<bool> auto_reset_disable_bubble( |
| 125 &InputImeActivateFunction::disable_bubble_for_testing_, true); |
| 126 SetTrackKeyEvents(input_method, true); |
| 127 |
| 128 ASSERT_TRUE(RunExtensionTest("input_ime_nonchromeos")) << message_; |
| 129 |
| 130 ASSERT_EQ(1, client->insert_char_count()); |
| 131 EXPECT_EQ(L'a', client->last_insert_char()); |
| 132 |
| 133 std::vector<std::unique_ptr<ui::KeyEvent>> key_events; |
| 134 key_events.push_back(std::unique_ptr<ui::KeyEvent>( |
| 135 new ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE))); |
| 136 key_events.push_back(std::unique_ptr<ui::KeyEvent>( |
| 137 new ui::KeyEvent(ui::ET_KEY_RELEASED, ui::VKEY_A, ui::EF_NONE))); |
| 138 |
| 139 EXPECT_TRUE(CompareKeyEvents(key_events, input_method)); |
| 140 input_method->DetachTextInputClient(client.get()); |
| 141 } |
| 142 |
67 } // namespace extensions | 143 } // namespace extensions |
OLD | NEW |