OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "config.h" |
| 6 #include "core/editing/FrameSelection.h" |
| 7 |
| 8 #include "bindings/core/v8/ExceptionStatePlaceholder.h" |
| 9 #include "core/dom/Document.h" |
| 10 #include "core/dom/Element.h" |
| 11 #include "core/dom/Text.h" |
| 12 #include "core/html/HTMLBodyElement.h" |
| 13 #include "core/html/HTMLDocument.h" |
| 14 #include "core/testing/DummyPageHolder.h" |
| 15 #include "wtf/OwnPtr.h" |
| 16 #include "wtf/PassRefPtr.h" |
| 17 #include "wtf/RefPtr.h" |
| 18 #include "wtf/StdLibExtras.h" |
| 19 #include "wtf/testing/WTFTestHelpers.h" |
| 20 #include <gtest/gtest.h> |
| 21 |
| 22 using namespace WebCore; |
| 23 |
| 24 namespace { |
| 25 |
| 26 class FrameSelectionTest : public ::testing::Test { |
| 27 protected: |
| 28 virtual void SetUp() OVERRIDE; |
| 29 |
| 30 HTMLDocument& document() const; |
| 31 void setSelection(const VisibleSelection&); |
| 32 const FrameSelection& selection() const; |
| 33 Text* textNode() { return m_textNode.get(); } |
| 34 |
| 35 private: |
| 36 OwnPtr<DummyPageHolder> m_dummyPageHolder; |
| 37 RawPtr<HTMLDocument> m_document; |
| 38 RefPtrWillBePersistent<Text> m_textNode; |
| 39 }; |
| 40 |
| 41 void FrameSelectionTest::SetUp() |
| 42 { |
| 43 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); |
| 44 m_document = toHTMLDocument(&m_dummyPageHolder->document()); |
| 45 ASSERT(m_document); |
| 46 m_textNode = m_document->createTextNode("Hello, World!"); |
| 47 m_document->body()->appendChild(m_textNode); |
| 48 } |
| 49 |
| 50 HTMLDocument& FrameSelectionTest::document() const |
| 51 { |
| 52 return *m_document; |
| 53 } |
| 54 |
| 55 void FrameSelectionTest::setSelection(const VisibleSelection& newSelection) |
| 56 { |
| 57 m_dummyPageHolder->frame().selection().setSelection(newSelection); |
| 58 } |
| 59 |
| 60 const FrameSelection& FrameSelectionTest::selection() const |
| 61 { |
| 62 return m_dummyPageHolder->frame().selection(); |
| 63 } |
| 64 |
| 65 TEST_F(FrameSelectionTest, SetValidSelection) |
| 66 { |
| 67 VisibleSelection validSelection(Position(textNode(), 0), Position(textNode()
, 5)); |
| 68 EXPECT_FALSE(validSelection.isNone()); |
| 69 setSelection(validSelection); |
| 70 EXPECT_FALSE(selection().isNone()); |
| 71 } |
| 72 |
| 73 TEST_F(FrameSelectionTest, SetInvalidSelection) |
| 74 { |
| 75 // Create a new document without frame by using DOMImplementation. |
| 76 DocumentInit dummy; |
| 77 RefPtrWillBeRawPtr<Document> documentWithoutFrame = Document::create(); |
| 78 RefPtrWillBeRawPtr<Element> body = documentWithoutFrame->createElement(HTMLN
ames::bodyTag, false); |
| 79 documentWithoutFrame->appendChild(body); |
| 80 RefPtrWillBeRawPtr<Text> anotherText = documentWithoutFrame->createTextNode(
"Hello, another world"); |
| 81 body->appendChild(anotherText); |
| 82 |
| 83 // Create a new VisibleSelection for the new document without frame and |
| 84 // update FrameSelection with the selection. |
| 85 VisibleSelection invalidSelection; |
| 86 invalidSelection.setWithoutValidation(Position(anotherText, 0), Position(ano
therText, 5)); |
| 87 setSelection(invalidSelection); |
| 88 |
| 89 EXPECT_TRUE(selection().isNone()); |
| 90 } |
| 91 |
| 92 } |
OLD | NEW |