Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(577)

Side by Side Diff: third_party/WebKit/Source/core/editing/ClipboardEventFlowTest.cpp

Issue 2685723005: evaluating clipboard event target acording to w3c specification (Closed)
Patch Set: Make Editor::findEventTargetFrom() to align Clipboard API specification Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "core/dom/Document.h"
6 #include "core/editing/EditingTestBase.h"
7 #include "core/editing/FrameSelection.h"
8 #include "core/editing/Position.h"
9 #include "core/editing/SelectionTemplate.h"
10 #include "core/events/EventListener.h"
11 #include "core/frame/LocalFrame.h"
12 #include "core/frame/Settings.h"
13 #include "core/html/HTMLBodyElement.h"
14 #include "core/html/HTMLButtonElement.h"
15 #include "core/html/HTMLDivElement.h"
16 #include "core/html/HTMLHtmlElement.h"
17 #include "core/layout/LayoutObject.h"
18
19 #include "testing/gmock/include/gmock/gmock.h"
20
21 namespace blink {
22
23 namespace {
24 class MockEventListener : public EventListener {
25 public:
26 MockEventListener() : EventListener(EventListener::CPPEventListenerType) {}
27
28 bool operator==(const EventListener& other) const final {
29 return this == &other;
30 }
31
32 MOCK_METHOD2(handleEvent, void(ExecutionContext*, Event*));
33 };
34 } // namespace
35
36 class ClipboardEventFlowTest : public EditingTestBase {
37 private:
38 void makeDocumentEmpty() {
39 while (document().firstChild())
40 document().removeChild(document().firstChild());
41 }
42
43 void setElementText(Element& element, const std::string& text) {
44 element.setInnerHTML(String::fromUTF8(text.c_str()), ASSERT_NO_EXCEPTION);
45 updateAllLifecyclePhases();
46 }
47
48 void setElementTextAndSelectIt(Element& element,
49 const std::string& text,
50 bool selectionEditable) {
51 setElementText(element, text);
52
53 frame().selection().setSelection(
54 SelectionInDOMTree::Builder()
55 .collapse(Position(&element, 0))
56 .extend(Position(&element, text.size()))
57 .build());
58
59 element.layoutObject()->mutableStyle()->setUserModify(
yosin_UTC9 2017/02/15 10:13:58 Accessing layout object is layering violation. Ple
mwrobel 2017/02/15 10:51:58 Done.
60 selectionEditable ? READ_WRITE : READ_ONLY);
61 }
62
63 protected:
64 void clipboardEventTargetDependsOnSelectionEditabilityTest(
65 const char* command,
66 bool selectionEditable) {
67 using testing::_;
68
69 auto* html = HTMLHtmlElement::create(document());
70 auto* body = HTMLBodyElement::create(document());
71 auto* focusableElement = HTMLButtonElement::create(document());
72 auto* elementWithSelection = HTMLDivElement::create(document());
73
74 auto* eventListenerInstalledOnFocusedElement = new MockEventListener;
75 auto* eventListenerInstalledOnElementWithSelection = new MockEventListener;
76
77 focusableElement->addEventListener(command,
78 eventListenerInstalledOnFocusedElement);
79 elementWithSelection->addEventListener(
80 command, eventListenerInstalledOnElementWithSelection);
81
82 makeDocumentEmpty();
83 document().setDesignMode("on");
84
85 body->appendChild(focusableElement);
86 body->appendChild(elementWithSelection);
87 html->appendChild(body);
88 document().appendChild(html);
89
90 focusableElement->focus();
91
92 setElementTextAndSelectIt(*elementWithSelection, "some dummy text",
93 selectionEditable);
94
95 // double check selection and focus
96 EXPECT_EQ(selectionEditable,
yosin_UTC9 2017/02/15 10:13:58 We don't need to verify this. Other test verifies
mwrobel 2017/02/15 10:51:58 I wanted to 'show' that test preconditions are met
yosin_UTC9 2017/02/16 10:53:39 I still think precondition checking is not require
97 frame().selection().selection().hasEditableStyle());
98 EXPECT_EQ(focusableElement, document().activeElement());
99
100 // test expectations
101 EXPECT_CALL(*eventListenerInstalledOnFocusedElement, handleEvent(_, _))
yosin_UTC9 2017/02/15 10:13:58 Could you move EXPEC_CALL's L111. Events are fired
mwrobel 2017/02/15 10:51:58 Done.
102 .Times(selectionEditable ? 0 : 1);
103
104 EXPECT_CALL(*eventListenerInstalledOnElementWithSelection,
105 handleEvent(_, _))
106 .Times(selectionEditable ? 1 : 0);
107
108 // allow |document.execCommand| to access clipboard
109 EXPECT_NE(frame().settings(), nullptr);
yosin_UTC9 2017/02/15 10:13:58 No need to check this. We'll get SEGV if |!frame()
mwrobel 2017/02/15 10:51:58 will do (the same goes with setting flag in paste*
110 frame().settings()->setJavaScriptCanAccessClipboard(true);
111 // execute command
112 NonThrowableExceptionState exceptionState;
113 document().execCommand(command, false, "", exceptionState);
114 }
115 };
116
117 TEST_F(ClipboardEventFlowTest,
118 copySetsClipboardEventTargetToActiveElementWhenSelectionIsNotEditable) {
119 clipboardEventTargetDependsOnSelectionEditabilityTest("copy", false);
120 }
121
122 TEST_F(
123 ClipboardEventFlowTest,
124 copySetsClipboardEventTargetToElementWithSelectionWhenSelectionIsEditable) {
125 clipboardEventTargetDependsOnSelectionEditabilityTest("copy", true);
126 }
127
128 TEST_F(ClipboardEventFlowTest,
129 cutSetsClipboardEventTargetToActiveElementWhenSelectionIsNotEditable) {
130 clipboardEventTargetDependsOnSelectionEditabilityTest("cut", false);
131 }
132
133 TEST_F(
134 ClipboardEventFlowTest,
135 cutSetsClipboardEventTargetToElementWithSelectionWhenSelectionIsEditable) {
136 clipboardEventTargetDependsOnSelectionEditabilityTest("cut", true);
137 }
138
139 TEST_F(ClipboardEventFlowTest,
140 pasteSetsClipboardEventTargetToActiveElementWhenSelectionIsNotEditable) {
141 // allow |document.execCommand| to execute 'paste' command
142 EXPECT_NE(frame().settings(), nullptr);
143 frame().settings()->setDOMPasteAllowed(true);
144
145 clipboardEventTargetDependsOnSelectionEditabilityTest("paste", false);
146 }
147
148 TEST_F(
149 ClipboardEventFlowTest,
150 pasteSetsClipboardEventTargetToElementWithSelectionWhenSelectionIsEditable) {
151 // allow |document.execCommand| to execute 'paste'
152 EXPECT_NE(frame().settings(), nullptr);
153 frame().settings()->setDOMPasteAllowed(true);
154
155 clipboardEventTargetDependsOnSelectionEditabilityTest("paste", true);
156 }
157 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/editing/BUILD.gn ('k') | third_party/WebKit/Source/core/editing/Editor.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698