| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 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 #include "testing/gmock/include/gmock/gmock.h" | |
| 19 | |
| 20 namespace blink { | |
| 21 | |
| 22 namespace { | |
| 23 class MockEventListener : public EventListener { | |
| 24 public: | |
| 25 MockEventListener() : EventListener(EventListener::CPPEventListenerType) {} | |
| 26 | |
| 27 bool operator==(const EventListener& other) const final { | |
| 28 return this == &other; | |
| 29 } | |
| 30 | |
| 31 MOCK_METHOD2(handleEvent, void(ExecutionContext*, Event*)); | |
| 32 }; | |
| 33 } // namespace | |
| 34 | |
| 35 class ClipboardEventFlowTest : public EditingTestBase { | |
| 36 private: | |
| 37 void makeDocumentEmpty() { | |
| 38 while (document().firstChild()) | |
| 39 document().removeChild(document().firstChild()); | |
| 40 } | |
| 41 | |
| 42 void setElementText(Element& element, const std::string& text) { | |
| 43 element.setInnerHTML(String::fromUTF8(text.c_str()), ASSERT_NO_EXCEPTION); | |
| 44 updateAllLifecyclePhases(); | |
| 45 } | |
| 46 | |
| 47 void setElementTextAndSelectIt(Element& element, | |
| 48 const std::string& text, | |
| 49 bool selectionEditable) { | |
| 50 setElementText(element, text); | |
| 51 | |
| 52 frame().selection().setSelection( | |
| 53 SelectionInDOMTree::Builder() | |
| 54 .collapse(Position(element.firstChild(), 0)) | |
| 55 .extend(Position(element.firstChild(), text.size())) | |
| 56 .build()); | |
| 57 | |
| 58 element.setAttribute(HTMLNames::contenteditableAttr, | |
| 59 selectionEditable ? "true" : "false"); | |
| 60 } | |
| 61 | |
| 62 protected: | |
| 63 void clipboardEventTargetDependsOnSelectionEditabilityTest( | |
| 64 const char* command, | |
| 65 bool selectionEditable) { | |
| 66 using testing::_; | |
| 67 | |
| 68 auto* html = HTMLHtmlElement::create(document()); | |
| 69 auto* body = HTMLBodyElement::create(document()); | |
| 70 auto* focusableElement = HTMLButtonElement::create(document()); | |
| 71 auto* elementWithSelection = HTMLDivElement::create(document()); | |
| 72 | |
| 73 auto* eventListenerInstalledOnFocusedElement = new MockEventListener; | |
| 74 auto* eventListenerInstalledOnElementWithSelection = new MockEventListener; | |
| 75 | |
| 76 focusableElement->addEventListener(command, | |
| 77 eventListenerInstalledOnFocusedElement); | |
| 78 elementWithSelection->addEventListener( | |
| 79 command, eventListenerInstalledOnElementWithSelection); | |
| 80 | |
| 81 makeDocumentEmpty(); | |
| 82 document().setDesignMode("on"); | |
| 83 | |
| 84 body->appendChild(focusableElement); | |
| 85 body->appendChild(elementWithSelection); | |
| 86 html->appendChild(body); | |
| 87 document().appendChild(html); | |
| 88 | |
| 89 focusableElement->focus(); | |
| 90 | |
| 91 setElementTextAndSelectIt(*elementWithSelection, "some dummy text", | |
| 92 selectionEditable); | |
| 93 | |
| 94 // allow |document.execCommand| to access clipboard | |
| 95 frame().settings()->setJavaScriptCanAccessClipboard(true); | |
| 96 | |
| 97 // test expectations | |
| 98 EXPECT_CALL(*eventListenerInstalledOnFocusedElement, handleEvent(_, _)) | |
| 99 .Times(selectionEditable ? 0 : 1); | |
| 100 | |
| 101 EXPECT_CALL(*eventListenerInstalledOnElementWithSelection, | |
| 102 handleEvent(_, _)) | |
| 103 .Times(selectionEditable ? 1 : 0); | |
| 104 | |
| 105 // execute command | |
| 106 NonThrowableExceptionState exceptionState; | |
| 107 document().execCommand(command, false, "", exceptionState); | |
| 108 } | |
| 109 }; | |
| 110 | |
| 111 TEST_F(ClipboardEventFlowTest, | |
| 112 copySetsClipboardEventTargetToActiveElementWhenSelectionIsNotEditable) { | |
| 113 clipboardEventTargetDependsOnSelectionEditabilityTest("copy", false); | |
| 114 } | |
| 115 | |
| 116 TEST_F( | |
| 117 ClipboardEventFlowTest, | |
| 118 copySetsClipboardEventTargetToElementWithSelectionWhenSelectionIsEditable) { | |
| 119 clipboardEventTargetDependsOnSelectionEditabilityTest("copy", true); | |
| 120 } | |
| 121 | |
| 122 TEST_F(ClipboardEventFlowTest, | |
| 123 cutSetsClipboardEventTargetToActiveElementWhenSelectionIsNotEditable) { | |
| 124 clipboardEventTargetDependsOnSelectionEditabilityTest("cut", false); | |
| 125 } | |
| 126 | |
| 127 TEST_F( | |
| 128 ClipboardEventFlowTest, | |
| 129 cutSetsClipboardEventTargetToElementWithSelectionWhenSelectionIsEditable) { | |
| 130 clipboardEventTargetDependsOnSelectionEditabilityTest("cut", true); | |
| 131 } | |
| 132 | |
| 133 TEST_F(ClipboardEventFlowTest, | |
| 134 pasteSetsClipboardEventTargetToActiveElementWhenSelectionIsNotEditable) { | |
| 135 // allow |document.execCommand| to execute 'paste' command | |
| 136 frame().settings()->setDOMPasteAllowed(true); | |
| 137 | |
| 138 clipboardEventTargetDependsOnSelectionEditabilityTest("paste", false); | |
| 139 } | |
| 140 | |
| 141 TEST_F( | |
| 142 ClipboardEventFlowTest, | |
| 143 pasteSetsClipboardEventTargetToElementWithSelectionWhenSelectionIsEditable)
{ | |
| 144 // allow |document.execCommand| to execute 'paste' | |
| 145 frame().settings()->setDOMPasteAllowed(true); | |
| 146 | |
| 147 clipboardEventTargetDependsOnSelectionEditabilityTest("paste", true); | |
| 148 } | |
| 149 } // namespace blink | |
| OLD | NEW |