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 void setElementTextAndSelectIt(Element& element, | |
|
yosin_UTC9
2017/02/15 01:30:49
nit: We may want to have a blank line.
mwrobel
2017/02/15 10:51:58
Done.
| |
| 48 const std::string& text, | |
| 49 bool selectionEditable) { | |
| 50 setElementText(element, text); | |
| 51 | |
| 52 VisibleSelection selection; | |
| 53 selection = createVisibleSelection( | |
|
yosin_UTC9
2017/02/15 01:30:49
You don't need to use |VisibleSelection| here.
You
mwrobel
2017/02/15 10:51:57
Done.
| |
| 54 SelectionInDOMTree::Builder(selection.asSelection()) | |
| 55 .collapse(Position(&element, 0)) | |
| 56 .extend(Position(&element, text.size())) | |
| 57 .build()); | |
| 58 | |
| 59 frame().selection().setSelection(selection); | |
| 60 | |
| 61 element.layoutObject()->mutableStyle()->setUserModify( | |
| 62 selectionEditable ? READ_WRITE : READ_ONLY); | |
| 63 } | |
| 64 | |
| 65 protected: | |
| 66 void clipboardEventTargetDependsOnSelectionEditabilityTest( | |
| 67 const char* command, | |
| 68 bool selectionEditable) { | |
| 69 using namespace testing; | |
|
yosin_UTC9
2017/02/15 01:30:49
Please avoid to import all identifiers from namesp
mwrobel
2017/02/15 10:51:57
Done.
| |
| 70 | |
| 71 auto* html = HTMLHtmlElement::create(document()); | |
| 72 auto* body = HTMLBodyElement::create(document()); | |
| 73 auto* focusableElement = HTMLButtonElement::create(document()); | |
| 74 auto* elementWithSelection = HTMLDivElement::create(document()); | |
| 75 | |
| 76 auto* eventListenerInstalledOnFocusedElement = new MockEventListener; | |
| 77 auto* eventListenerInstalledOnElementWithSelection = new MockEventListener; | |
| 78 | |
| 79 focusableElement->addEventListener(command, | |
| 80 eventListenerInstalledOnFocusedElement); | |
| 81 elementWithSelection->addEventListener( | |
| 82 command, eventListenerInstalledOnElementWithSelection); | |
| 83 | |
| 84 makeDocumentEmpty(); | |
| 85 document().setDesignMode("on"); | |
| 86 | |
| 87 body->appendChild(focusableElement); | |
| 88 body->appendChild(elementWithSelection); | |
| 89 html->appendChild(body); | |
| 90 document().appendChild(html); | |
| 91 | |
| 92 focusableElement->focus(); | |
| 93 | |
| 94 setElementTextAndSelectIt(*elementWithSelection, "some dummy text", | |
| 95 selectionEditable); | |
| 96 | |
| 97 // double check selection and focus | |
| 98 EXPECT_EQ(selectionEditable, | |
| 99 frame().selection().selection().hasEditableStyle()); | |
| 100 EXPECT_EQ(focusableElement, document().activeElement()); | |
| 101 | |
| 102 // test expectations | |
| 103 EXPECT_CALL(*eventListenerInstalledOnFocusedElement, handleEvent(_, _)) | |
| 104 .Times(selectionEditable ? 0 : 1); | |
| 105 | |
| 106 EXPECT_CALL(*eventListenerInstalledOnElementWithSelection, | |
| 107 handleEvent(_, _)) | |
| 108 .Times(selectionEditable ? 1 : 0); | |
| 109 | |
| 110 // allow |document.execCommand| to access clipboard | |
| 111 EXPECT_NE(frame().settings(), nullptr); | |
| 112 frame().settings()->setJavaScriptCanAccessClipboard(true); | |
| 113 // execute command | |
| 114 NonThrowableExceptionState exceptionState; | |
| 115 document().execCommand(command, false, "", exceptionState); | |
| 116 } | |
| 117 }; | |
| 118 | |
| 119 TEST_F(ClipboardEventFlowTest, | |
| 120 copySetsClipboardEventTargetToActiveElementWhenSelectionIsNotEditable) { | |
| 121 clipboardEventTargetDependsOnSelectionEditabilityTest("copy", false); | |
| 122 } | |
| 123 | |
| 124 TEST_F( | |
| 125 ClipboardEventFlowTest, | |
| 126 copySetsClipboardEventTargetToElementWithSelectionWhenSelectionIsEditable) { | |
| 127 clipboardEventTargetDependsOnSelectionEditabilityTest("copy", true); | |
| 128 } | |
| 129 | |
| 130 TEST_F(ClipboardEventFlowTest, | |
| 131 cutSetsClipboardEventTargetToActiveElementWhenSelectionIsNotEditable) { | |
| 132 clipboardEventTargetDependsOnSelectionEditabilityTest("cut", false); | |
| 133 } | |
| 134 | |
| 135 TEST_F( | |
| 136 ClipboardEventFlowTest, | |
| 137 cutSetsClipboardEventTargetToElementWithSelectionWhenSelectionIsEditable) { | |
| 138 clipboardEventTargetDependsOnSelectionEditabilityTest("cut", true); | |
| 139 } | |
| 140 | |
| 141 TEST_F(ClipboardEventFlowTest, | |
| 142 pasteSetsClipboardEventTargetToActiveElementWhenSelectionIsNotEditable) { | |
| 143 // allow |document.execCommand| to execute 'paste' command | |
| 144 EXPECT_NE(frame().settings(), nullptr); | |
| 145 frame().settings()->setDOMPasteAllowed(true); | |
| 146 | |
| 147 clipboardEventTargetDependsOnSelectionEditabilityTest("paste", false); | |
| 148 } | |
| 149 | |
| 150 TEST_F( | |
| 151 ClipboardEventFlowTest, | |
| 152 pasteSetsClipboardEventTargetToElementWithSelectionWhenSelectionIsEditable) { | |
| 153 // allow |document.execCommand| to execute 'paste' | |
| 154 EXPECT_NE(frame().settings(), nullptr); | |
| 155 frame().settings()->setDOMPasteAllowed(true); | |
| 156 | |
| 157 clipboardEventTargetDependsOnSelectionEditabilityTest("paste", true); | |
| 158 } | |
| 159 } // namespace blink | |
| OLD | NEW |