Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/common/render_messages.h" | 6 #include "chrome/common/render_messages.h" |
| 7 #include "chrome/renderer/page_click_listener.h" | 7 #include "chrome/renderer/page_click_listener.h" |
| 8 #include "chrome/renderer/page_click_tracker.h" | 8 #include "chrome/renderer/page_click_tracker.h" |
| 9 #include "chrome/test/base/chrome_render_view_test.h" | 9 #include "chrome/test/base/chrome_render_view_test.h" |
| 10 #include "content/public/renderer/render_view.h" | 10 #include "content/public/renderer/render_view.h" |
| 11 #include "content/test/mock_keyboard.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 17 #include "ui/base/keycodes/keyboard_codes.h" | |
| 16 | 18 |
| 17 class TestPageClickListener : public PageClickListener { | 19 class TestPageClickListener : public PageClickListener { |
| 18 public: | 20 public: |
| 19 TestPageClickListener() | 21 TestPageClickListener() |
| 20 : called_(false), | 22 : input_element_clicked_called_(false), |
| 23 input_element_lost_focus_called_(false), | |
| 21 was_focused_(false), | 24 was_focused_(false), |
| 22 is_focused_(false), | 25 is_focused_(false), |
| 23 notification_response_(false) { | 26 notification_response_(false) { |
| 24 } | 27 } |
| 25 | 28 |
| 26 virtual bool InputElementClicked(const WebKit::WebInputElement& element, | 29 virtual bool InputElementClicked(const WebKit::WebInputElement& element, |
| 27 bool was_focused, | 30 bool was_focused, |
| 28 bool is_focused) { | 31 bool is_focused) { |
| 29 called_ = true; | 32 input_element_clicked_called_ = true; |
| 30 element_clicked_ = element; | 33 element_clicked_ = element; |
| 31 was_focused_ = was_focused; | 34 was_focused_ = was_focused; |
| 32 is_focused_ = is_focused; | 35 is_focused_ = is_focused; |
| 33 return notification_response_; | 36 return notification_response_; |
| 34 } | 37 } |
| 35 | 38 |
| 39 virtual bool InputElementLostFocus() { | |
| 40 input_element_lost_focus_called_ = true; | |
| 41 return notification_response_; | |
| 42 } | |
| 43 | |
| 36 void ClearResults() { | 44 void ClearResults() { |
| 37 called_ = false; | 45 input_element_clicked_called_ = false; |
| 46 input_element_lost_focus_called_ = false; | |
| 38 element_clicked_.reset(); | 47 element_clicked_.reset(); |
| 39 was_focused_ = false; | 48 was_focused_ = false; |
| 40 is_focused_ = false; | 49 is_focused_ = false; |
| 41 } | 50 } |
| 42 | 51 |
| 43 bool called_; | 52 bool input_element_clicked_called_; |
| 53 bool input_element_lost_focus_called_; | |
| 44 WebKit::WebInputElement element_clicked_; | 54 WebKit::WebInputElement element_clicked_; |
| 45 bool was_focused_; | 55 bool was_focused_; |
| 46 bool is_focused_; | 56 bool is_focused_; |
| 47 bool notification_response_; | 57 bool notification_response_; |
| 48 }; | 58 }; |
| 49 | 59 |
| 60 class PageClickTrackerTest : public ChromeRenderViewTest { | |
| 61 protected: | |
| 62 virtual void SetUp() { | |
| 63 ChromeRenderViewTest::SetUp(); | |
| 64 | |
| 65 // RenderView creates PageClickTracker but it doesn't keep it around. | |
| 66 // Rather than make it do so for the test, we create a new object. | |
| 67 page_click_tracker_.reset(new PageClickTracker(view_)); | |
| 68 page_click_tracker_->AddListener(&test_listener1_); | |
| 69 page_click_tracker_->AddListener(&test_listener2_); | |
| 70 | |
| 71 LoadHTML("<form>" | |
| 72 " <input type='text' id='text_1'></input><br>" | |
| 73 " <input type='text' id='text_2'></input><br>" | |
| 74 " <input type='button' id='button'></input><br>" | |
| 75 "</form>"); | |
| 76 GetWebWidget()->resize(WebKit::WebSize(500, 500)); | |
| 77 GetWebWidget()->setFocus(true); | |
| 78 WebKit::WebDocument document = view_->GetWebView()->mainFrame()->document(); | |
| 79 text_ = document.getElementById("text_1"); | |
| 80 ASSERT_FALSE(text_.isNull()); | |
| 81 } | |
| 82 | |
| 83 scoped_ptr<PageClickTracker> page_click_tracker_; | |
| 84 TestPageClickListener test_listener1_; | |
| 85 TestPageClickListener test_listener2_; | |
| 86 WebKit::WebElement text_; | |
| 87 }; | |
| 88 | |
| 50 // Tests that PageClickTracker does notify correctly when a node is clicked. | 89 // Tests that PageClickTracker does notify correctly when a node is clicked. |
| 51 TEST_F(ChromeRenderViewTest, PageClickTracker) { | 90 TEST_F(PageClickTrackerTest, PageClickTrackerInputClicked) { |
| 52 // RenderView creates PageClickTracker but it doesn't keep it around. Rather | |
| 53 // than make it do so for the test, we create a new object. | |
| 54 PageClickTracker* page_click_tracker = new PageClickTracker(view_); | |
| 55 | |
| 56 TestPageClickListener test_listener1; | |
| 57 TestPageClickListener test_listener2; | |
| 58 page_click_tracker->AddListener(&test_listener1); | |
| 59 page_click_tracker->AddListener(&test_listener2); | |
| 60 | |
| 61 LoadHTML("<form>" | |
| 62 " <input type='text' id='text'></input><br>" | |
| 63 " <input type='button' id='button'></input><br>" | |
| 64 "</form>"); | |
| 65 GetWebWidget()->resize(WebKit::WebSize(500, 500)); | |
| 66 GetWebWidget()->setFocus(true); | |
| 67 WebKit::WebDocument document = view_->GetWebView()->mainFrame()->document(); | |
| 68 WebKit::WebElement text = document.getElementById("text"); | |
| 69 ASSERT_FALSE(text.isNull()); | |
| 70 WebKit::WebElement button = document.getElementById("button"); | |
| 71 ASSERT_FALSE(button.isNull()); | |
| 72 | |
| 73 // Click the text field once. | 91 // Click the text field once. |
| 74 EXPECT_TRUE(SimulateElementClick("text")); | 92 EXPECT_TRUE(SimulateElementClick("text_1")); |
| 75 EXPECT_TRUE(test_listener1.called_); | 93 EXPECT_TRUE(test_listener1_.input_element_clicked_called_); |
| 76 EXPECT_TRUE(test_listener2.called_); | 94 EXPECT_TRUE(test_listener2_.input_element_clicked_called_); |
| 77 EXPECT_FALSE(test_listener1.was_focused_); | 95 EXPECT_FALSE(test_listener1_.was_focused_); |
| 78 EXPECT_FALSE(test_listener2.was_focused_); | 96 EXPECT_FALSE(test_listener2_.was_focused_); |
| 79 EXPECT_TRUE(test_listener1.is_focused_); | 97 EXPECT_TRUE(test_listener1_.is_focused_); |
| 80 EXPECT_TRUE(test_listener2.is_focused_); | 98 EXPECT_TRUE(test_listener2_.is_focused_); |
| 81 EXPECT_TRUE(text == test_listener1.element_clicked_); | 99 EXPECT_TRUE(text_ == test_listener1_.element_clicked_); |
| 82 EXPECT_TRUE(text == test_listener2.element_clicked_); | 100 EXPECT_TRUE(text_ == test_listener2_.element_clicked_); |
| 83 test_listener1.ClearResults(); | 101 test_listener1_.ClearResults(); |
| 84 test_listener2.ClearResults(); | 102 test_listener2_.ClearResults(); |
| 85 | 103 |
| 86 // Click the text field again to test that was_focused_ is set correctly. | 104 // Click the text field again to test that was_focused_ is set correctly. |
| 87 EXPECT_TRUE(SimulateElementClick("text")); | 105 EXPECT_TRUE(SimulateElementClick("text_1")); |
| 88 EXPECT_TRUE(test_listener1.called_); | 106 EXPECT_TRUE(test_listener1_.input_element_clicked_called_); |
| 89 EXPECT_TRUE(test_listener2.called_); | 107 EXPECT_TRUE(test_listener2_.input_element_clicked_called_); |
| 90 EXPECT_TRUE(test_listener1.was_focused_); | 108 EXPECT_TRUE(test_listener1_.was_focused_); |
| 91 EXPECT_TRUE(test_listener2.was_focused_); | 109 EXPECT_TRUE(test_listener2_.was_focused_); |
| 92 EXPECT_TRUE(test_listener1.is_focused_); | 110 EXPECT_TRUE(test_listener1_.is_focused_); |
| 93 EXPECT_TRUE(test_listener2.is_focused_); | 111 EXPECT_TRUE(test_listener2_.is_focused_); |
| 94 EXPECT_TRUE(text == test_listener1.element_clicked_); | 112 EXPECT_TRUE(text_ == test_listener1_.element_clicked_); |
| 95 EXPECT_TRUE(text == test_listener2.element_clicked_); | 113 EXPECT_TRUE(text_ == test_listener2_.element_clicked_); |
| 96 test_listener1.ClearResults(); | 114 test_listener1_.ClearResults(); |
| 97 test_listener2.ClearResults(); | 115 test_listener2_.ClearResults(); |
| 98 | 116 |
| 99 // Click the button, no notification should happen (this is not a text-input). | 117 // Click the button, no notification should happen (this is not a text-input). |
| 100 EXPECT_TRUE(SimulateElementClick("button")); | 118 EXPECT_TRUE(SimulateElementClick("button")); |
| 101 EXPECT_FALSE(test_listener1.called_); | 119 EXPECT_FALSE(test_listener1_.input_element_clicked_called_); |
| 102 EXPECT_FALSE(test_listener2.called_); | 120 EXPECT_FALSE(test_listener2_.input_element_clicked_called_); |
| 103 | 121 |
| 104 // Make the first listener stop the event propagation, click the text field | 122 // Make the first listener stop the event propagation, click the text field |
| 105 // and make sure only the first listener is notified. | 123 // and make sure only the first listener is notified. |
| 106 test_listener1.notification_response_ = true; | 124 test_listener1_.notification_response_ = true; |
| 107 EXPECT_TRUE(SimulateElementClick("text")); | 125 EXPECT_TRUE(SimulateElementClick("text_1")); |
| 108 EXPECT_TRUE(test_listener1.called_); | 126 EXPECT_TRUE(test_listener1_.input_element_clicked_called_); |
| 109 EXPECT_FALSE(test_listener2.called_); | 127 EXPECT_FALSE(test_listener2_.input_element_clicked_called_); |
| 110 test_listener1.ClearResults(); | 128 test_listener1_.ClearResults(); |
| 111 | 129 |
| 112 // Make sure removing a listener work. | 130 // Make sure removing a listener work. |
| 113 page_click_tracker->RemoveListener(&test_listener1); | 131 page_click_tracker_->RemoveListener(&test_listener1_); |
| 114 EXPECT_TRUE(SimulateElementClick("text")); | 132 EXPECT_TRUE(SimulateElementClick("text_1")); |
| 115 EXPECT_FALSE(test_listener1.called_); | 133 EXPECT_FALSE(test_listener1_.input_element_clicked_called_); |
| 116 EXPECT_TRUE(test_listener2.called_); | 134 EXPECT_TRUE(test_listener2_.input_element_clicked_called_); |
| 117 test_listener2.ClearResults(); | 135 test_listener2_.ClearResults(); |
| 118 | 136 |
| 119 // Make sure we don't choke when no listeners are registered. | 137 // Make sure we don't choke when no listeners are registered. |
| 120 page_click_tracker->RemoveListener(&test_listener2); | 138 page_click_tracker_->RemoveListener(&test_listener2_); |
| 121 EXPECT_TRUE(SimulateElementClick("text")); | 139 EXPECT_TRUE(SimulateElementClick("text_1")); |
| 122 EXPECT_FALSE(test_listener1.called_); | 140 EXPECT_FALSE(test_listener1_.input_element_clicked_called_); |
| 123 EXPECT_FALSE(test_listener2.called_); | 141 EXPECT_FALSE(test_listener2_.input_element_clicked_called_); |
| 124 } | 142 } |
| 143 | |
| 144 TEST_F(PageClickTrackerTest, PageClickTrackerInputFocusLost) { | |
| 145 // Gain focus on the text field by using tab. | |
| 146 std::wstring char_code; | |
| 147 EXPECT_EQ(1, SendKeyEvent(MockKeyboard::LAYOUT_UNITED_STATES, | |
|
Ilya Sherman
2011/10/27 22:04:17
It looks like you should not use this, as it is on
csharp
2011/10/31 15:24:25
I couldn't figure out how to properly pass in a We
Ilya Sherman
2011/10/31 21:23:12
You can do something like this: http://pastebin.co
Ilya Sherman
2011/10/31 21:25:21
The other option is to implement SendKeyEvent on M
| |
| 148 ui::VKEY_TAB, | |
| 149 MockKeyboard::NONE, | |
| 150 &char_code)); | |
| 151 EXPECT_EQ(text_, text_.document().focusedNode()); | |
| 152 EXPECT_FALSE(test_listener1_.input_element_lost_focus_called_); | |
| 153 EXPECT_FALSE(test_listener2_.input_element_lost_focus_called_); | |
| 154 | |
| 155 // Click a button and ensure that the lost focus notification was sent, | |
| 156 // even though focus was gained without the mouse. | |
| 157 EXPECT_TRUE(SimulateElementClick("button")); | |
| 158 EXPECT_TRUE(test_listener1_.input_element_lost_focus_called_); | |
| 159 EXPECT_TRUE(test_listener2_.input_element_lost_focus_called_); | |
| 160 test_listener1_.ClearResults(); | |
| 161 test_listener2_.ClearResults(); | |
| 162 | |
| 163 // Click a text field and test that no lost focus notifications are sent. | |
| 164 EXPECT_TRUE(SimulateElementClick("text_1")); | |
| 165 EXPECT_FALSE(test_listener1_.input_element_lost_focus_called_); | |
| 166 EXPECT_FALSE(test_listener2_.input_element_lost_focus_called_); | |
| 167 test_listener1_.ClearResults(); | |
| 168 test_listener2_.ClearResults(); | |
| 169 | |
| 170 // Select another text field to test that the notifcation for the | |
| 171 // first text field losing focus is sent. | |
| 172 EXPECT_TRUE(SimulateElementClick("text_2")); | |
| 173 EXPECT_TRUE(test_listener1_.input_element_lost_focus_called_); | |
| 174 EXPECT_TRUE(test_listener2_.input_element_lost_focus_called_); | |
| 175 test_listener1_.ClearResults(); | |
| 176 test_listener2_.ClearResults(); | |
| 177 | |
| 178 // Click the button, a notification should happen since a text field has | |
| 179 // lost focus. | |
| 180 EXPECT_TRUE(SimulateElementClick("button")); | |
| 181 EXPECT_TRUE(test_listener1_.input_element_lost_focus_called_); | |
| 182 EXPECT_TRUE(test_listener2_.input_element_lost_focus_called_); | |
| 183 test_listener1_.ClearResults(); | |
| 184 test_listener2_.ClearResults(); | |
| 185 | |
| 186 // Click on a text field while the button has focus and ensure no lost focus | |
| 187 // notification is sent. | |
| 188 EXPECT_TRUE(SimulateElementClick("text_1")); | |
| 189 EXPECT_FALSE(test_listener1_.input_element_lost_focus_called_); | |
| 190 EXPECT_FALSE(test_listener2_.input_element_lost_focus_called_); | |
| 191 | |
| 192 // Make the first listener stop the event propagation, then click a button | |
| 193 // and make sure only the first listener is notified. | |
| 194 test_listener1_.notification_response_ = true; | |
| 195 EXPECT_TRUE(SimulateElementClick("button")); | |
| 196 EXPECT_TRUE(test_listener1_.input_element_lost_focus_called_); | |
| 197 EXPECT_FALSE(test_listener2_.input_element_lost_focus_called_); | |
| 198 test_listener1_.ClearResults(); | |
| 199 | |
| 200 // Make sure removing a listener work. | |
| 201 page_click_tracker_->RemoveListener(&test_listener1_); | |
| 202 EXPECT_TRUE(SimulateElementClick("text_1")); | |
| 203 EXPECT_TRUE(SimulateElementClick("button")); | |
| 204 EXPECT_FALSE(test_listener1_.input_element_lost_focus_called_); | |
| 205 EXPECT_TRUE(test_listener2_.input_element_lost_focus_called_); | |
| 206 test_listener2_.ClearResults(); | |
| 207 | |
| 208 // Make sure we don't choke when no listeners are registered. | |
| 209 page_click_tracker_->RemoveListener(&test_listener2_); | |
| 210 EXPECT_TRUE(SimulateElementClick("text_1")); | |
| 211 EXPECT_TRUE(SimulateElementClick("button")); | |
| 212 EXPECT_FALSE(test_listener1_.input_element_lost_focus_called_); | |
| 213 EXPECT_FALSE(test_listener2_.input_element_lost_focus_called_); | |
| 214 } | |
| OLD | NEW |