OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/basictypes.h" | 5 #include "base/basictypes.h" |
6 | 6 |
7 #include "base/shared_memory.h" | 7 #include "base/shared_memory.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "content/common/intents_messages.h" | 10 #include "content/common/intents_messages.h" |
11 #include "content/common/view_messages.h" | 11 #include "content/common/view_messages.h" |
12 #include "content/public/browser/native_web_keyboard_event.h" | 12 #include "content/public/browser/native_web_keyboard_event.h" |
13 #include "content/public/common/bindings_policy.h" | 13 #include "content/public/common/bindings_policy.h" |
| 14 #include "content/public/test/render_view_test.h" |
14 #include "content/renderer/render_view_impl.h" | 15 #include "content/renderer/render_view_impl.h" |
15 #include "content/test/render_view_test.h" | 16 #include "content/test/mock_keyboard.h" |
16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLError.
h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLError.
h" |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebIntentServiceInfo.
h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebIntentServiceInfo.
h" |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebWindowFeatures.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebWindowFeatures.h" |
23 #include "ui/base/keycodes/keyboard_codes.h" | 24 #include "ui/base/keycodes/keyboard_codes.h" |
24 #include "ui/base/range/range.h" | 25 #include "ui/base/range/range.h" |
25 #include "ui/gfx/codec/jpeg_codec.h" | 26 #include "ui/gfx/codec/jpeg_codec.h" |
26 #include "webkit/glue/web_io_operators.h" | 27 #include "webkit/glue/web_io_operators.h" |
27 | 28 |
| 29 #if defined(OS_LINUX) && !defined(USE_AURA) |
| 30 #include "ui/base/gtk/event_synthesis_gtk.h" |
| 31 #endif |
| 32 |
| 33 #if defined(USE_AURA) |
| 34 #include "ui/aura/event.h" |
| 35 #endif |
| 36 |
| 37 #if defined(USE_AURA) && defined(USE_X11) |
| 38 #include <X11/Xlib.h> |
| 39 #include "ui/base/events.h" |
| 40 #include "ui/base/keycodes/keyboard_code_conversion.h" |
| 41 #include "ui/base/x/x11_util.h" |
| 42 #endif |
| 43 |
28 using WebKit::WebFrame; | 44 using WebKit::WebFrame; |
29 using WebKit::WebInputEvent; | 45 using WebKit::WebInputEvent; |
30 using WebKit::WebMouseEvent; | 46 using WebKit::WebMouseEvent; |
31 using WebKit::WebString; | 47 using WebKit::WebString; |
32 using WebKit::WebTextDirection; | 48 using WebKit::WebTextDirection; |
33 using WebKit::WebURLError; | 49 using WebKit::WebURLError; |
| 50 using content::NativeWebKeyboardEvent; |
| 51 |
| 52 namespace { |
| 53 #if defined(USE_AURA) && defined(USE_X11) |
| 54 // Converts MockKeyboard::Modifiers to ui::EventFlags. |
| 55 int ConvertMockKeyboardModifier(MockKeyboard::Modifiers modifiers) { |
| 56 static struct ModifierMap { |
| 57 MockKeyboard::Modifiers src; |
| 58 int dst; |
| 59 } kModifierMap[] = { |
| 60 { MockKeyboard::LEFT_SHIFT, ui::EF_SHIFT_DOWN }, |
| 61 { MockKeyboard::RIGHT_SHIFT, ui::EF_SHIFT_DOWN }, |
| 62 { MockKeyboard::LEFT_CONTROL, ui::EF_CONTROL_DOWN }, |
| 63 { MockKeyboard::RIGHT_CONTROL, ui::EF_CONTROL_DOWN }, |
| 64 { MockKeyboard::LEFT_ALT, ui::EF_ALT_DOWN }, |
| 65 { MockKeyboard::RIGHT_ALT, ui::EF_ALT_DOWN }, |
| 66 }; |
| 67 int flags = 0; |
| 68 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kModifierMap); ++i) { |
| 69 if (kModifierMap[i].src & modifiers) { |
| 70 flags |= kModifierMap[i].dst; |
| 71 } |
| 72 } |
| 73 return flags; |
| 74 } |
| 75 #endif |
| 76 } |
34 | 77 |
35 class RenderViewImplTest : public content::RenderViewTest { | 78 class RenderViewImplTest : public content::RenderViewTest { |
36 public: | 79 public: |
| 80 RenderViewImplTest() { |
| 81 // Attach a pseudo keyboard device to this object. |
| 82 mock_keyboard_.reset(new MockKeyboard()); |
| 83 } |
| 84 |
37 RenderViewImpl* view() { | 85 RenderViewImpl* view() { |
38 return static_cast<RenderViewImpl*>(view_); | 86 return static_cast<RenderViewImpl*>(view_); |
39 } | 87 } |
| 88 |
| 89 // Sends IPC messages that emulates a key-press event. |
| 90 int SendKeyEvent(MockKeyboard::Layout layout, |
| 91 int key_code, |
| 92 MockKeyboard::Modifiers modifiers, |
| 93 string16* output) { |
| 94 #if defined(OS_WIN) |
| 95 // Retrieve the Unicode character for the given tuple (keyboard-layout, |
| 96 // key-code, and modifiers). |
| 97 // Exit when a keyboard-layout driver cannot assign a Unicode character to |
| 98 // the tuple to prevent sending an invalid key code to the RenderView object
. |
| 99 CHECK(mock_keyboard_.get()); |
| 100 CHECK(output); |
| 101 int length = mock_keyboard_->GetCharacters(layout, key_code, modifiers, |
| 102 output); |
| 103 if (length != 1) |
| 104 return -1; |
| 105 |
| 106 // Create IPC messages from Windows messages and send them to our |
| 107 // back-end. |
| 108 // A keyboard event of Windows consists of three Windows messages: |
| 109 // WM_KEYDOWN, WM_CHAR, and WM_KEYUP. |
| 110 // WM_KEYDOWN and WM_KEYUP sends virtual-key codes. On the other hand, |
| 111 // WM_CHAR sends a composed Unicode character. |
| 112 MSG msg1 = { NULL, WM_KEYDOWN, key_code, 0 }; |
| 113 #if defined(USE_AURA) |
| 114 aura::KeyEvent evt1(msg1, false); |
| 115 NativeWebKeyboardEvent keydown_event(&evt1); |
| 116 #else |
| 117 NativeWebKeyboardEvent keydown_event(msg1); |
| 118 #endif |
| 119 SendNativeKeyEvent(keydown_event); |
| 120 |
| 121 MSG msg2 = { NULL, WM_CHAR, (*output)[0], 0 }; |
| 122 #if defined(USE_AURA) |
| 123 aura::KeyEvent evt2(msg2, true); |
| 124 NativeWebKeyboardEvent char_event(&evt2); |
| 125 #else |
| 126 NativeWebKeyboardEvent char_event(msg2); |
| 127 #endif |
| 128 SendNativeKeyEvent(char_event); |
| 129 |
| 130 MSG msg3 = { NULL, WM_KEYUP, key_code, 0 }; |
| 131 #if defined(USE_AURA) |
| 132 aura::KeyEvent evt3(msg3, false); |
| 133 NativeWebKeyboardEvent keyup_event(&evt3); |
| 134 #else |
| 135 NativeWebKeyboardEvent keyup_event(msg3); |
| 136 #endif |
| 137 SendNativeKeyEvent(keyup_event); |
| 138 |
| 139 return length; |
| 140 #elif defined(USE_AURA) && defined(USE_X11) |
| 141 // We ignore |layout|, which means we are only testing the layout of the |
| 142 // current locale. TODO(mazda): fix this to respect |layout|. |
| 143 CHECK(output); |
| 144 const int flags = ConvertMockKeyboardModifier(modifiers); |
| 145 |
| 146 XEvent xevent1; |
| 147 InitXKeyEventForTesting(ui::ET_KEY_PRESSED, |
| 148 static_cast<ui::KeyboardCode>(key_code), |
| 149 flags, |
| 150 &xevent1); |
| 151 aura::KeyEvent event1(&xevent1, false); |
| 152 NativeWebKeyboardEvent keydown_event(&event1); |
| 153 SendNativeKeyEvent(keydown_event); |
| 154 |
| 155 XEvent xevent2; |
| 156 InitXKeyEventForTesting(ui::ET_KEY_PRESSED, |
| 157 static_cast<ui::KeyboardCode>(key_code), |
| 158 flags, |
| 159 &xevent2); |
| 160 aura::KeyEvent event2(&xevent2, true); |
| 161 NativeWebKeyboardEvent char_event(&event2); |
| 162 SendNativeKeyEvent(char_event); |
| 163 |
| 164 XEvent xevent3; |
| 165 InitXKeyEventForTesting(ui::ET_KEY_RELEASED, |
| 166 static_cast<ui::KeyboardCode>(key_code), |
| 167 flags, |
| 168 &xevent3); |
| 169 aura::KeyEvent event3(&xevent3, false); |
| 170 NativeWebKeyboardEvent keyup_event(&event3); |
| 171 SendNativeKeyEvent(keyup_event); |
| 172 |
| 173 long c = GetCharacterFromKeyCode(static_cast<ui::KeyboardCode>(key_code), |
| 174 flags); |
| 175 output->assign(1, static_cast<char16>(c)); |
| 176 return 1; |
| 177 #elif defined(OS_LINUX) |
| 178 // We ignore |layout|, which means we are only testing the layout of the |
| 179 // current locale. TODO(estade): fix this to respect |layout|. |
| 180 std::vector<GdkEvent*> events; |
| 181 ui::SynthesizeKeyPressEvents( |
| 182 NULL, static_cast<ui::KeyboardCode>(key_code), |
| 183 modifiers & (MockKeyboard::LEFT_CONTROL | MockKeyboard::RIGHT_CONTROL), |
| 184 modifiers & (MockKeyboard::LEFT_SHIFT | MockKeyboard::RIGHT_SHIFT), |
| 185 modifiers & (MockKeyboard::LEFT_ALT | MockKeyboard::RIGHT_ALT), |
| 186 &events); |
| 187 |
| 188 guint32 unicode_key = 0; |
| 189 for (size_t i = 0; i < events.size(); ++i) { |
| 190 // Only send the up/down events for key press itself (skip the up/down |
| 191 // events for the modifier keys). |
| 192 if ((i + 1) == (events.size() / 2) || i == (events.size() / 2)) { |
| 193 unicode_key = gdk_keyval_to_unicode(events[i]->key.keyval); |
| 194 NativeWebKeyboardEvent webkit_event(events[i]); |
| 195 SendNativeKeyEvent(webkit_event); |
| 196 |
| 197 // Need to add a char event after the key down. |
| 198 if (webkit_event.type == WebKit::WebInputEvent::RawKeyDown) { |
| 199 NativeWebKeyboardEvent char_event = webkit_event; |
| 200 char_event.type = WebKit::WebInputEvent::Char; |
| 201 char_event.skip_in_browser = true; |
| 202 SendNativeKeyEvent(char_event); |
| 203 } |
| 204 } |
| 205 gdk_event_free(events[i]); |
| 206 } |
| 207 |
| 208 output->assign(1, static_cast<char16>(unicode_key)); |
| 209 return 1; |
| 210 #else |
| 211 NOTIMPLEMENTED(); |
| 212 return L'\0'; |
| 213 #endif |
| 214 } |
| 215 |
| 216 private: |
| 217 scoped_ptr<MockKeyboard> mock_keyboard_; |
40 }; | 218 }; |
41 | 219 |
42 // Test that we get form state change notifications when input fields change. | 220 // Test that we get form state change notifications when input fields change. |
43 TEST_F(RenderViewImplTest, OnNavStateChanged) { | 221 TEST_F(RenderViewImplTest, OnNavStateChanged) { |
44 // Don't want any delay for form state sync changes. This will still post a | 222 // Don't want any delay for form state sync changes. This will still post a |
45 // message so updates will get coalesced, but as soon as we spin the message | 223 // message so updates will get coalesced, but as soon as we spin the message |
46 // loop, it will generate an update. | 224 // loop, it will generate an update. |
47 view()->set_send_content_state_immediately(true); | 225 view()->set_send_content_state_immediately(true); |
48 | 226 |
49 LoadHTML("<input type=\"text\" id=\"elt_text\"></input>"); | 227 LoadHTML("<input type=\"text\" id=\"elt_text\"></input>"); |
(...skipping 1279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1329 | 1507 |
1330 forward_item = GetMainFrame()->currentHistoryItem(); | 1508 forward_item = GetMainFrame()->currentHistoryItem(); |
1331 GoBack(page_a_item); | 1509 GoBack(page_a_item); |
1332 EXPECT_TRUE(ExecuteJavaScriptAndReturnIntValue(check_page_a, &was_page_a)); | 1510 EXPECT_TRUE(ExecuteJavaScriptAndReturnIntValue(check_page_a, &was_page_a)); |
1333 EXPECT_EQ(1, was_page_a); | 1511 EXPECT_EQ(1, was_page_a); |
1334 | 1512 |
1335 GoForward(forward_item); | 1513 GoForward(forward_item); |
1336 EXPECT_TRUE(ExecuteJavaScriptAndReturnIntValue(check_page_b, &was_page_b)); | 1514 EXPECT_TRUE(ExecuteJavaScriptAndReturnIntValue(check_page_b, &was_page_b)); |
1337 EXPECT_EQ(1, was_page_b); | 1515 EXPECT_EQ(1, was_page_b); |
1338 } | 1516 } |
OLD | NEW |