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