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

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.
tkent 2017/02/17 04:55:18 2016 -> 2017
mwrobel 2017/02/17 07:59:23 Done.
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
tkent 2017/02/17 04:55:18 This blank line is unnecessary.
mwrobel 2017/02/17 07:59:23 Done.
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.firstChild(), 0))
56 .extend(Position(element.firstChild(), text.size()))
57 .build());
58
59 element.setAttribute(HTMLNames::contenteditableAttr,
60 selectionEditable ? "true" : "false");
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 // allow |document.execCommand| to access clipboard
96 frame().settings()->setJavaScriptCanAccessClipboard(true);
97
98 // test expectations
99 EXPECT_CALL(*eventListenerInstalledOnFocusedElement, handleEvent(_, _))
100 .Times(selectionEditable ? 0 : 1);
101
102 EXPECT_CALL(*eventListenerInstalledOnElementWithSelection,
103 handleEvent(_, _))
104 .Times(selectionEditable ? 1 : 0);
105
106 // execute command
107 NonThrowableExceptionState exceptionState;
108 document().execCommand(command, false, "", exceptionState);
109 }
110 };
111
112 TEST_F(ClipboardEventFlowTest,
113 copySetsClipboardEventTargetToActiveElementWhenSelectionIsNotEditable) {
114 clipboardEventTargetDependsOnSelectionEditabilityTest("copy", false);
115 }
116
117 TEST_F(
118 ClipboardEventFlowTest,
119 copySetsClipboardEventTargetToElementWithSelectionWhenSelectionIsEditable) {
120 clipboardEventTargetDependsOnSelectionEditabilityTest("copy", true);
121 }
122
123 TEST_F(ClipboardEventFlowTest,
124 cutSetsClipboardEventTargetToActiveElementWhenSelectionIsNotEditable) {
125 clipboardEventTargetDependsOnSelectionEditabilityTest("cut", false);
126 }
127
128 TEST_F(
129 ClipboardEventFlowTest,
130 cutSetsClipboardEventTargetToElementWithSelectionWhenSelectionIsEditable) {
131 clipboardEventTargetDependsOnSelectionEditabilityTest("cut", true);
132 }
133
134 TEST_F(ClipboardEventFlowTest,
135 pasteSetsClipboardEventTargetToActiveElementWhenSelectionIsNotEditable) {
136 // allow |document.execCommand| to execute 'paste' command
137 frame().settings()->setDOMPasteAllowed(true);
138
139 clipboardEventTargetDependsOnSelectionEditabilityTest("paste", false);
140 }
141
142 TEST_F(
143 ClipboardEventFlowTest,
144 pasteSetsClipboardEventTargetToElementWithSelectionWhenSelectionIsEditable) {
145 // allow |document.execCommand| to execute 'paste'
146 frame().settings()->setDOMPasteAllowed(true);
147
148 clipboardEventTargetDependsOnSelectionEditabilityTest("paste", true);
149 }
150 } // 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