OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "base/keyboard_codes.h" |
6 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
7 #include "base/shared_memory.h" | 8 #include "base/shared_memory.h" |
8 #include "build/build_config.h" | 9 #include "build/build_config.h" |
9 #include "chrome/browser/renderer_host/backing_store.h" | 10 #include "chrome/browser/renderer_host/backing_store.h" |
10 #include "chrome/browser/renderer_host/test_render_view_host.h" | 11 #include "chrome/browser/renderer_host/test_render_view_host.h" |
11 #include "chrome/common/render_messages.h" | 12 #include "chrome/common/render_messages.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 | 113 |
113 BackingStore* AllocBackingStore(const gfx::Size& size) { | 114 BackingStore* AllocBackingStore(const gfx::Size& size) { |
114 return new BackingStore(size); | 115 return new BackingStore(size); |
115 } | 116 } |
116 | 117 |
117 protected: | 118 protected: |
118 gfx::Rect bounds_; | 119 gfx::Rect bounds_; |
119 DISALLOW_COPY_AND_ASSIGN(TestView); | 120 DISALLOW_COPY_AND_ASSIGN(TestView); |
120 }; | 121 }; |
121 | 122 |
| 123 // MockRenderWidgetHostTest ---------------------------------------------------- |
| 124 |
| 125 class MockRenderWidgetHost : public RenderWidgetHost { |
| 126 public: |
| 127 MockRenderWidgetHost(RenderProcessHost* process, int routing_id) |
| 128 : RenderWidgetHost(process, routing_id), |
| 129 unhandled_keyboard_event_called_(false) { |
| 130 } |
| 131 |
| 132 // Tests that make sure we ignore keyboard event acknowledgments to events we |
| 133 // didn't send work by making sure we didn't call UnhandledKeyboardEvent(). |
| 134 bool unhandled_keyboard_event_called() const { |
| 135 return unhandled_keyboard_event_called_; |
| 136 } |
| 137 |
| 138 protected: |
| 139 virtual void UnhandledKeyboardEvent(const WebKeyboardEvent& event) { |
| 140 unhandled_keyboard_event_called_ = true; |
| 141 } |
| 142 |
| 143 private: |
| 144 bool unhandled_keyboard_event_called_; |
| 145 }; |
| 146 |
122 // RenderWidgetHostTest -------------------------------------------------------- | 147 // RenderWidgetHostTest -------------------------------------------------------- |
123 | 148 |
124 class RenderWidgetHostTest : public testing::Test { | 149 class RenderWidgetHostTest : public testing::Test { |
125 public: | 150 public: |
126 RenderWidgetHostTest() : process_(NULL) { | 151 RenderWidgetHostTest() : process_(NULL) { |
127 } | 152 } |
128 ~RenderWidgetHostTest() { | 153 ~RenderWidgetHostTest() { |
129 } | 154 } |
130 | 155 |
131 protected: | 156 protected: |
132 // testing::Test | 157 // testing::Test |
133 void SetUp() { | 158 void SetUp() { |
134 profile_.reset(new TestingProfile()); | 159 profile_.reset(new TestingProfile()); |
135 process_ = new RenderWidgetHostProcess(profile_.get()); | 160 process_ = new RenderWidgetHostProcess(profile_.get()); |
136 host_.reset(new RenderWidgetHost(process_, 1)); | 161 host_.reset(new MockRenderWidgetHost(process_, 1)); |
137 view_.reset(new TestView); | 162 view_.reset(new TestView); |
138 host_->set_view(view_.get()); | 163 host_->set_view(view_.get()); |
139 } | 164 } |
140 void TearDown() { | 165 void TearDown() { |
141 view_.reset(); | 166 view_.reset(); |
142 host_.reset(); | 167 host_.reset(); |
143 process_ = NULL; | 168 process_ = NULL; |
144 profile_.reset(); | 169 profile_.reset(); |
145 | 170 |
146 // Process all pending tasks to avoid leaks. | 171 // Process all pending tasks to avoid leaks. |
147 MessageLoop::current()->RunAllPending(); | 172 MessageLoop::current()->RunAllPending(); |
148 } | 173 } |
149 | 174 |
150 MessageLoopForUI message_loop_; | 175 MessageLoopForUI message_loop_; |
151 | 176 |
152 scoped_ptr<TestingProfile> profile_; | 177 scoped_ptr<TestingProfile> profile_; |
153 RenderWidgetHostProcess* process_; // Deleted automatically by the widget. | 178 RenderWidgetHostProcess* process_; // Deleted automatically by the widget. |
154 scoped_ptr<RenderWidgetHost> host_; | 179 scoped_ptr<MockRenderWidgetHost> host_; |
155 scoped_ptr<TestView> view_; | 180 scoped_ptr<TestView> view_; |
156 | 181 |
157 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostTest); | 182 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostTest); |
158 }; | 183 }; |
159 | 184 |
160 } // namespace | 185 } // namespace |
161 | 186 |
162 // ----------------------------------------------------------------------------- | 187 // ----------------------------------------------------------------------------- |
163 | 188 |
164 TEST_F(RenderWidgetHostTest, Resize) { | 189 TEST_F(RenderWidgetHostTest, Resize) { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 EXPECT_FALSE(host_->is_hidden_); | 322 EXPECT_FALSE(host_->is_hidden_); |
298 | 323 |
299 // It should have sent out a restored message with a request to paint. | 324 // It should have sent out a restored message with a request to paint. |
300 const IPC::Message* restored = process_->sink().GetUniqueMessageMatching( | 325 const IPC::Message* restored = process_->sink().GetUniqueMessageMatching( |
301 ViewMsg_WasRestored::ID); | 326 ViewMsg_WasRestored::ID); |
302 ASSERT_TRUE(restored); | 327 ASSERT_TRUE(restored); |
303 bool needs_repaint; | 328 bool needs_repaint; |
304 ViewMsg_WasRestored::Read(restored, &needs_repaint); | 329 ViewMsg_WasRestored::Read(restored, &needs_repaint); |
305 EXPECT_TRUE(needs_repaint); | 330 EXPECT_TRUE(needs_repaint); |
306 } | 331 } |
| 332 |
| 333 TEST_F(RenderWidgetHostTest, HandleKeyEventsWeSent) { |
| 334 WebKeyboardEvent key_event; |
| 335 key_event.type = WebInputEvent::KEY_DOWN; |
| 336 key_event.modifiers = WebInputEvent::CTRL_KEY; |
| 337 key_event.key_code = base::VKEY_L; // non-null made up value. |
| 338 |
| 339 host_->ForwardKeyboardEvent(key_event); |
| 340 |
| 341 // Make sure we sent the input event to the renderer. |
| 342 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( |
| 343 ViewMsg_HandleInputEvent::ID)); |
| 344 process_->sink().ClearMessages(); |
| 345 |
| 346 // Send the simulated response from the renderer back. |
| 347 scoped_ptr<IPC::Message> response( |
| 348 new ViewHostMsg_HandleInputEvent_ACK(0)); |
| 349 response->WriteInt(key_event.type); |
| 350 response->WriteBool(false); |
| 351 host_->OnMessageReceived(*response); |
| 352 |
| 353 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
| 354 } |
| 355 |
| 356 TEST_F(RenderWidgetHostTest, IgnoreKeyEventsWeDidntSend) { |
| 357 // Send a simulated, unrequested key response. We should ignore this. |
| 358 scoped_ptr<IPC::Message> response( |
| 359 new ViewHostMsg_HandleInputEvent_ACK(0)); |
| 360 response->WriteInt(WebInputEvent::KEY_DOWN); |
| 361 response->WriteBool(false); |
| 362 host_->OnMessageReceived(*response); |
| 363 |
| 364 EXPECT_FALSE(host_->unhandled_keyboard_event_called()); |
| 365 } |
| 366 |
OLD | NEW |