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/test/base/chrome_render_view_test.h" | 6 #include "chrome/test/base/chrome_render_view_test.h" |
7 #include "components/autofill/content/renderer/page_click_listener.h" | 7 #include "components/autofill/content/renderer/page_click_listener.h" |
8 #include "components/autofill/content/renderer/page_click_tracker.h" | 8 #include "components/autofill/content/renderer/page_click_tracker.h" |
9 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "third_party/WebKit/public/platform/WebFloatPoint.h" |
| 12 #include "third_party/WebKit/public/platform/WebSize.h" |
11 #include "third_party/WebKit/public/web/WebDocument.h" | 13 #include "third_party/WebKit/public/web/WebDocument.h" |
12 #include "third_party/WebKit/public/web/WebInputElement.h" | 14 #include "third_party/WebKit/public/web/WebInputElement.h" |
| 15 #include "third_party/WebKit/public/web/WebSettings.h" |
13 #include "third_party/WebKit/public/web/WebTextAreaElement.h" | 16 #include "third_party/WebKit/public/web/WebTextAreaElement.h" |
14 #include "third_party/WebKit/public/web/WebView.h" | 17 #include "third_party/WebKit/public/web/WebView.h" |
15 #include "third_party/WebKit/public/platform/WebSize.h" | |
16 #include "ui/events/keycodes/keyboard_codes.h" | 18 #include "ui/events/keycodes/keyboard_codes.h" |
17 | 19 |
18 namespace autofill { | 20 namespace autofill { |
19 | 21 |
20 class TestPageClickListener : public PageClickListener { | 22 class TestPageClickListener : public PageClickListener { |
21 public: | 23 public: |
22 TestPageClickListener() | 24 TestPageClickListener() |
23 : form_control_element_clicked_called_(false), | 25 : form_control_element_clicked_called_(false), |
24 was_focused_(false) {} | 26 was_focused_(false) {} |
25 | 27 |
(...skipping 17 matching lines...) Expand all Loading... |
43 | 45 |
44 class PageClickTrackerTest : public ChromeRenderViewTest { | 46 class PageClickTrackerTest : public ChromeRenderViewTest { |
45 protected: | 47 protected: |
46 void SetUp() override { | 48 void SetUp() override { |
47 ChromeRenderViewTest::SetUp(); | 49 ChromeRenderViewTest::SetUp(); |
48 | 50 |
49 // RenderView creates PageClickTracker but it doesn't keep it around. | 51 // RenderView creates PageClickTracker but it doesn't keep it around. |
50 // Rather than make it do so for the test, we create a new object. | 52 // Rather than make it do so for the test, we create a new object. |
51 page_click_tracker_.reset(new PageClickTracker(view_, &test_listener_)); | 53 page_click_tracker_.reset(new PageClickTracker(view_, &test_listener_)); |
52 | 54 |
| 55 // Must be set before loading HTML. |
| 56 view_->GetWebView()->setDefaultPageScaleLimits(1, 4); |
| 57 view_->GetWebView()->settings()->setPinchVirtualViewportEnabled(true); |
| 58 |
53 LoadHTML("<form>" | 59 LoadHTML("<form>" |
54 " <input type='text' id='text_1'></input><br>" | 60 " <input type='text' id='text_1'></input><br>" |
55 " <input type='text' id='text_2'></input><br>" | 61 " <input type='text' id='text_2'></input><br>" |
56 " <textarea id='textarea_1'></textarea><br>" | 62 " <textarea id='textarea_1'></textarea><br>" |
57 " <textarea id='textarea_2'></textarea><br>" | 63 " <textarea id='textarea_2'></textarea><br>" |
58 " <input type='button' id='button'></input><br>" | 64 " <input type='button' id='button'></input><br>" |
| 65 " <input type='button' id='button_2' disabled></input><br>" |
59 "</form>"); | 66 "</form>"); |
60 GetWebWidget()->resize(blink::WebSize(500, 500)); | 67 GetWebWidget()->resize(blink::WebSize(500, 500)); |
61 GetWebWidget()->setFocus(true); | 68 GetWebWidget()->setFocus(true); |
62 blink::WebDocument document = view_->GetWebView()->mainFrame()->document(); | 69 blink::WebDocument document = view_->GetWebView()->mainFrame()->document(); |
63 text_ = document.getElementById("text_1"); | 70 text_ = document.getElementById("text_1"); |
64 textarea_ = document.getElementById("textarea_1"); | 71 textarea_ = document.getElementById("textarea_1"); |
65 ASSERT_FALSE(text_.isNull()); | 72 ASSERT_FALSE(text_.isNull()); |
66 ASSERT_FALSE(textarea_.isNull()); | 73 ASSERT_FALSE(textarea_.isNull()); |
| 74 |
| 75 // Enable show-ime event when element is focused by indicating that a user |
| 76 // gesture has been processed since load. |
| 77 EXPECT_TRUE(SimulateElementClick("button")); |
67 } | 78 } |
68 | 79 |
69 void TearDown() override { | 80 void TearDown() override { |
70 text_.reset(); | 81 text_.reset(); |
71 textarea_.reset(); | 82 textarea_.reset(); |
72 test_listener_.ClearResults(); | 83 test_listener_.ClearResults(); |
73 page_click_tracker_.reset(); | 84 page_click_tracker_.reset(); |
74 ChromeRenderViewTest::TearDown(); | 85 ChromeRenderViewTest::TearDown(); |
75 } | 86 } |
76 | 87 |
77 // Simulates a click on the given element and then waits for the stack | |
78 // to unwind. | |
79 void SendElementClick(const std::string& element_id) { | |
80 EXPECT_TRUE(SimulateElementClick(element_id)); | |
81 ProcessPendingMessages(); | |
82 } | |
83 | |
84 // Send all the messages required for a complete key press. | |
85 void SendKeyPress(int key_code) { | |
86 blink::WebKeyboardEvent keyboard_event; | |
87 keyboard_event.windowsKeyCode = key_code; | |
88 keyboard_event.setKeyIdentifierFromWindowsKeyCode(); | |
89 | |
90 keyboard_event.type = blink::WebInputEvent::RawKeyDown; | |
91 SendWebKeyboardEvent(keyboard_event); | |
92 | |
93 keyboard_event.type = blink::WebInputEvent::Char; | |
94 SendWebKeyboardEvent(keyboard_event); | |
95 | |
96 keyboard_event.type = blink::WebInputEvent::KeyUp; | |
97 SendWebKeyboardEvent(keyboard_event); | |
98 } | |
99 | |
100 scoped_ptr<PageClickTracker> page_click_tracker_; | 88 scoped_ptr<PageClickTracker> page_click_tracker_; |
101 TestPageClickListener test_listener_; | 89 TestPageClickListener test_listener_; |
102 blink::WebElement text_; | 90 blink::WebElement text_; |
103 blink::WebElement textarea_; | 91 blink::WebElement textarea_; |
104 }; | 92 }; |
105 | 93 |
106 // Tests that PageClickTracker does notify correctly when an input | 94 // Tests that PageClickTracker does notify correctly when an input |
107 // node is clicked. | 95 // node is clicked. |
108 TEST_F(PageClickTrackerTest, PageClickTrackerInputClicked) { | 96 TEST_F(PageClickTrackerTest, PageClickTrackerInputClicked) { |
109 EXPECT_NE(text_, text_.document().focusedElement()); | 97 EXPECT_NE(text_, text_.document().focusedElement()); |
110 // Click the text field once. | 98 // Click the text field once. |
111 SendElementClick("text_1"); | 99 EXPECT_TRUE(SimulateElementClick("text_1")); |
112 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_); | 100 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_); |
113 EXPECT_FALSE(test_listener_.was_focused_); | 101 EXPECT_FALSE(test_listener_.was_focused_); |
114 EXPECT_TRUE(text_ == test_listener_.form_control_element_clicked_); | 102 EXPECT_TRUE(text_ == test_listener_.form_control_element_clicked_); |
115 test_listener_.ClearResults(); | 103 test_listener_.ClearResults(); |
116 | 104 |
117 // Click the text field again to test that was_focused_ is set correctly. | 105 // Click the text field again to test that was_focused_ is set correctly. |
118 SendElementClick("text_1"); | 106 EXPECT_TRUE(SimulateElementClick("text_1")); |
119 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_); | 107 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_); |
120 EXPECT_TRUE(test_listener_.was_focused_); | 108 EXPECT_TRUE(test_listener_.was_focused_); |
121 EXPECT_TRUE(text_ == test_listener_.form_control_element_clicked_); | 109 EXPECT_TRUE(text_ == test_listener_.form_control_element_clicked_); |
122 test_listener_.ClearResults(); | 110 test_listener_.ClearResults(); |
123 | 111 |
124 // Click the button, no notification should happen (this is not a text-input). | 112 // Click the button, no notification should happen (this is not a text-input). |
125 SendElementClick("button"); | 113 EXPECT_TRUE(SimulateElementClick("button")); |
126 EXPECT_FALSE(test_listener_.form_control_element_clicked_called_); | 114 EXPECT_FALSE(test_listener_.form_control_element_clicked_called_); |
127 } | 115 } |
128 | 116 |
| 117 TEST_F(PageClickTrackerTest, PageClickTrackerInputFocusedAndClicked) { |
| 118 // Focus the text field without a click. |
| 119 ExecuteJavaScript("document.getElementById('text_1').focus();"); |
| 120 EXPECT_FALSE(test_listener_.form_control_element_clicked_called_); |
| 121 test_listener_.ClearResults(); |
| 122 |
| 123 // Click the focused text field to test that was_focused_ is set correctly. |
| 124 EXPECT_TRUE(SimulateElementClick("text_1")); |
| 125 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_); |
| 126 EXPECT_TRUE(test_listener_.was_focused_); |
| 127 EXPECT_TRUE(text_ == test_listener_.form_control_element_clicked_); |
| 128 } |
| 129 |
129 // Tests that PageClickTracker does notify correctly when a textarea | 130 // Tests that PageClickTracker does notify correctly when a textarea |
130 // node is clicked. | 131 // node is clicked. |
131 TEST_F(PageClickTrackerTest, PageClickTrackerTextAreaClicked) { | 132 TEST_F(PageClickTrackerTest, PageClickTrackerTextAreaClicked) { |
132 // Click the textarea field once. | 133 // Click the textarea field once. |
133 SendElementClick("textarea_1"); | 134 EXPECT_TRUE(SimulateElementClick("textarea_1")); |
134 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_); | 135 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_); |
135 EXPECT_FALSE(test_listener_.was_focused_); | 136 EXPECT_FALSE(test_listener_.was_focused_); |
136 EXPECT_TRUE(textarea_ == test_listener_.form_control_element_clicked_); | 137 EXPECT_TRUE(textarea_ == test_listener_.form_control_element_clicked_); |
137 test_listener_.ClearResults(); | 138 test_listener_.ClearResults(); |
138 | 139 |
139 // Click the textarea field again to test that was_focused_ is set correctly. | 140 // Click the textarea field again to test that was_focused_ is set correctly. |
140 SendElementClick("textarea_1"); | 141 EXPECT_TRUE(SimulateElementClick("textarea_1")); |
141 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_); | 142 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_); |
142 EXPECT_TRUE(test_listener_.was_focused_); | 143 EXPECT_TRUE(test_listener_.was_focused_); |
143 EXPECT_TRUE(textarea_ == test_listener_.form_control_element_clicked_); | 144 EXPECT_TRUE(textarea_ == test_listener_.form_control_element_clicked_); |
144 test_listener_.ClearResults(); | 145 test_listener_.ClearResults(); |
145 | 146 |
146 // Click the button, no notification should happen (this is not a text-input). | 147 // Click the button, no notification should happen (this is not a text-input). |
147 SendElementClick("button"); | 148 EXPECT_TRUE(SimulateElementClick("button")); |
148 EXPECT_FALSE(test_listener_.form_control_element_clicked_called_); | 149 EXPECT_FALSE(test_listener_.form_control_element_clicked_called_); |
149 } | 150 } |
150 | 151 |
| 152 TEST_F(PageClickTrackerTest, PageClickTrackerTextAreaFocusedAndClicked) { |
| 153 // Focus the textarea without a click. |
| 154 ExecuteJavaScript("document.getElementById('textarea_1').focus();"); |
| 155 EXPECT_FALSE(test_listener_.form_control_element_clicked_called_); |
| 156 test_listener_.ClearResults(); |
| 157 |
| 158 // Click the focused text field to test that was_focused_ is set correctly. |
| 159 EXPECT_TRUE(SimulateElementClick("textarea_1")); |
| 160 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_); |
| 161 EXPECT_TRUE(test_listener_.was_focused_); |
| 162 EXPECT_TRUE(textarea_ == test_listener_.form_control_element_clicked_); |
| 163 test_listener_.ClearResults(); |
| 164 } |
| 165 |
| 166 TEST_F(PageClickTrackerTest, PageClickTrackerScaledTextareaClicked) { |
| 167 EXPECT_NE(text_, text_.document().focusedElement()); |
| 168 view_->GetWebView()->setPageScaleFactor(3); |
| 169 view_->GetWebView()->setPinchViewportOffset(blink::WebFloatPoint(50, 50)); |
| 170 |
| 171 // Click textarea_1. |
| 172 SimulatePointClick(gfx::Point(30, 30)); |
| 173 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_); |
| 174 EXPECT_FALSE(test_listener_.was_focused_); |
| 175 EXPECT_TRUE(textarea_ == test_listener_.form_control_element_clicked_); |
| 176 } |
| 177 |
| 178 TEST_F(PageClickTrackerTest, PageClickTrackerDisabledInputClickedNoEvent) { |
| 179 EXPECT_NE(text_, text_.document().focusedElement()); |
| 180 // Click the text field once. |
| 181 EXPECT_TRUE(SimulateElementClick("text_1")); |
| 182 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_); |
| 183 EXPECT_FALSE(test_listener_.was_focused_); |
| 184 EXPECT_TRUE(text_ == test_listener_.form_control_element_clicked_); |
| 185 test_listener_.ClearResults(); |
| 186 |
| 187 // Click the disabled element. |
| 188 EXPECT_TRUE(SimulateElementClick("button_2")); |
| 189 EXPECT_FALSE(test_listener_.form_control_element_clicked_called_); |
| 190 } |
| 191 |
151 } // namespace autofill | 192 } // namespace autofill |
OLD | NEW |