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