blob: dcba0bbe5d19e1f98f3ff224bd3355cbfceb3da0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/basictypes.h"
#include "chrome/common/render_messages.h"
#include "chrome/renderer/page_click_listener.h"
#include "chrome/renderer/page_click_tracker.h"
#include "chrome/test/render_view_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/WebKit/chromium/public/WebInputElement.h"
#include "third_party/WebKit/WebKit/chromium/public/WebSize.h"
#include "third_party/WebKit/WebKit/chromium/public/WebView.h"
class TestPageClickListener : public PageClickListener {
public:
TestPageClickListener()
: called_(false),
was_focused_(false),
is_focused_(false),
notification_response_(false) {
}
virtual bool InputElementClicked(const WebKit::WebInputElement& element,
bool was_focused,
bool is_focused) {
called_ = true;
element_clicked_ = element;
was_focused_ = was_focused;
is_focused_ = is_focused;
return notification_response_;
}
void ClearResults() {
called_ = false;
element_clicked_.reset();
was_focused_ = false;
is_focused_ = false;
}
bool called_;
WebKit::WebInputElement element_clicked_;
bool was_focused_;
bool is_focused_;
bool notification_response_;
};
// Tests that PageClickTracker does notify correctly when a node is clicked.
TEST_F(RenderViewTest, PageClickTracker) {
TestPageClickListener test_listener1;
TestPageClickListener test_listener2;
view_->page_click_tracker()->AddListener(&test_listener1);
view_->page_click_tracker()->AddListener(&test_listener2);
LoadHTML("<form>"
" <input type='text' id='text'></input><br>"
" <input type='button' id='button'></input><br>"
"</form>");
view_->webwidget()->resize(WebKit::WebSize(500, 500));
view_->webwidget()->setFocus(true);
WebKit::WebDocument document = view_->webview()->mainFrame()->document();
WebKit::WebElement text = document.getElementById("text");
ASSERT_FALSE(text.isNull());
WebKit::WebElement button = document.getElementById("button");
ASSERT_FALSE(button.isNull());
// Click the text field once.
EXPECT_TRUE(SimulateElementClick("text"));
EXPECT_TRUE(test_listener1.called_);
EXPECT_TRUE(test_listener2.called_);
EXPECT_FALSE(test_listener1.was_focused_);
EXPECT_FALSE(test_listener2.was_focused_);
EXPECT_TRUE(test_listener1.is_focused_);
EXPECT_TRUE(test_listener2.is_focused_);
EXPECT_TRUE(text == test_listener1.element_clicked_);
EXPECT_TRUE(text == test_listener2.element_clicked_);
test_listener1.ClearResults();
test_listener2.ClearResults();
// Click the text field again to test that was_focused_ is set correctly.
EXPECT_TRUE(SimulateElementClick("text"));
EXPECT_TRUE(test_listener1.called_);
EXPECT_TRUE(test_listener2.called_);
EXPECT_TRUE(test_listener1.was_focused_);
EXPECT_TRUE(test_listener2.was_focused_);
EXPECT_TRUE(test_listener1.is_focused_);
EXPECT_TRUE(test_listener2.is_focused_);
EXPECT_TRUE(text == test_listener1.element_clicked_);
EXPECT_TRUE(text == test_listener2.element_clicked_);
test_listener1.ClearResults();
test_listener2.ClearResults();
// Click the button, no notification should happen (this is not a text-input).
EXPECT_TRUE(SimulateElementClick("button"));
EXPECT_FALSE(test_listener1.called_);
EXPECT_FALSE(test_listener2.called_);
// Make the first listener stop the event propagation, click the text field
// and make sure only the first listener is notified.
test_listener1.notification_response_ = true;
EXPECT_TRUE(SimulateElementClick("text"));
EXPECT_TRUE(test_listener1.called_);
EXPECT_FALSE(test_listener2.called_);
test_listener1.ClearResults();
// Make sure removing a listener work.
view_->page_click_tracker()->RemoveListener(&test_listener1);
EXPECT_TRUE(SimulateElementClick("text"));
EXPECT_FALSE(test_listener1.called_);
EXPECT_TRUE(test_listener2.called_);
test_listener2.ClearResults();
// Make sure we don't choke when no listeners are registered.
view_->page_click_tracker()->RemoveListener(&test_listener2);
EXPECT_TRUE(SimulateElementClick("text"));
EXPECT_FALSE(test_listener1.called_);
EXPECT_FALSE(test_listener2.called_);
}
|