Chromium Code Reviews| OLD | NEW |
|---|---|
| (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())) | |
|
yosin_UTC9
2017/02/16 10:53:39
nit: s/text.size()/1/
It seems |element| has only
mwrobel
2017/02/16 14:15:26
done (I didn't get your main idea so I used propos
| |
| 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 // double check test preconditions | |
|
yosin_UTC9
2017/02/16 10:53:39
I think we don't need to check preconditions.
If t
mwrobel
2017/02/16 14:15:26
Done.
| |
| 96 EXPECT_EQ(selectionEditable, | |
| 97 frame().selection().selection().hasEditableStyle()); | |
| 98 EXPECT_EQ(focusableElement, document().activeElement()); | |
| 99 | |
| 100 // allow |document.execCommand| to access clipboard | |
| 101 frame().settings()->setJavaScriptCanAccessClipboard(true); | |
| 102 | |
| 103 // test expectations | |
| 104 EXPECT_CALL(*eventListenerInstalledOnFocusedElement, handleEvent(_, _)) | |
| 105 .Times(selectionEditable ? 0 : 1); | |
| 106 | |
| 107 EXPECT_CALL(*eventListenerInstalledOnElementWithSelection, | |
| 108 handleEvent(_, _)) | |
| 109 .Times(selectionEditable ? 1 : 0); | |
| 110 | |
| 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 frame().settings()->setDOMPasteAllowed(true); | |
| 143 | |
| 144 clipboardEventTargetDependsOnSelectionEditabilityTest("paste", false); | |
| 145 } | |
| 146 | |
| 147 TEST_F( | |
| 148 ClipboardEventFlowTest, | |
| 149 pasteSetsClipboardEventTargetToElementWithSelectionWhenSelectionIsEditable) { | |
| 150 // allow |document.execCommand| to execute 'paste' | |
| 151 frame().settings()->setDOMPasteAllowed(true); | |
| 152 | |
| 153 clipboardEventTargetDependsOnSelectionEditabilityTest("paste", true); | |
| 154 } | |
| 155 } // namespace blink | |
| OLD | NEW |