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/VisibleSelection.h" |
| 7 |
| 8 #include "core/dom/Document.h" |
| 9 #include "core/dom/Range.h" |
| 10 #include "core/dom/Text.h" |
| 11 #include "core/html/HTMLElement.h" |
| 12 #include "core/testing/DummyPageHolder.h" |
| 13 #include <gtest/gtest.h> |
| 14 |
| 15 #define LOREM_IPSUM \ |
| 16 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod te
mpor " \ |
| 17 "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud " \ |
| 18 "exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
aute irure " \ |
| 19 "dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat null
a pariatur." \ |
| 20 "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia d
eserunt " \ |
| 21 "mollit anim id est laborum." |
| 22 |
| 23 namespace WebCore { |
| 24 |
| 25 class VisibleSelectionTest : public ::testing::Test { |
| 26 protected: |
| 27 virtual void SetUp() OVERRIDE; |
| 28 |
| 29 Document& document() const { return m_dummyPageHolder->document(); } |
| 30 Text* textNode() const { return m_textNode.get(); } |
| 31 VisibleSelection& selection() { return m_selection; } |
| 32 |
| 33 // Helper function to set the VisibleSelection base/extent. |
| 34 void setSelection(int base) { setSelection(base, base); } |
| 35 |
| 36 // Helper function to set the VisibleSelection base/extent. |
| 37 void setSelection(int base, int extend) |
| 38 { |
| 39 m_selection.setBase(Position(textNode(), base)); |
| 40 m_selection.setExtent(Position(textNode(), extend)); |
| 41 } |
| 42 |
| 43 private: |
| 44 OwnPtr<DummyPageHolder> m_dummyPageHolder; |
| 45 RefPtr<Text> m_textNode; |
| 46 VisibleSelection m_selection; |
| 47 }; |
| 48 |
| 49 void WebCore::VisibleSelectionTest::SetUp() |
| 50 { |
| 51 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); |
| 52 m_textNode = document().createTextNode(LOREM_IPSUM); |
| 53 document().body()->appendChild(m_textNode.get()); |
| 54 } |
| 55 |
| 56 } // namespace WebCore |
| 57 |
| 58 namespace { |
| 59 |
| 60 using namespace WebCore; |
| 61 |
| 62 TEST_F(VisibleSelectionTest, Initialisation) |
| 63 { |
| 64 setSelection(0); |
| 65 |
| 66 EXPECT_FALSE(selection().isNone()); |
| 67 EXPECT_TRUE(selection().isCaret()); |
| 68 |
| 69 RefPtrWillBeRawPtr<Range> range = selection().firstRange(); |
| 70 EXPECT_EQ(0, range->startOffset()); |
| 71 EXPECT_EQ(0, range->endOffset()); |
| 72 EXPECT_EQ("", range->text()); |
| 73 } |
| 74 |
| 75 TEST_F(VisibleSelectionTest, WordGranularity) |
| 76 { |
| 77 // Beginning of a word. |
| 78 { |
| 79 setSelection(0); |
| 80 selection().expandUsingGranularity(WordGranularity); |
| 81 |
| 82 RefPtrWillBeRawPtr<Range> range = selection().firstRange(); |
| 83 EXPECT_EQ(0, range->startOffset()); |
| 84 EXPECT_EQ(5, range->endOffset()); |
| 85 EXPECT_EQ("Lorem", range->text()); |
| 86 } |
| 87 |
| 88 // Middle of a word. |
| 89 { |
| 90 setSelection(8); |
| 91 selection().expandUsingGranularity(WordGranularity); |
| 92 |
| 93 RefPtrWillBeRawPtr<Range> range = selection().firstRange(); |
| 94 EXPECT_EQ(6, range->startOffset()); |
| 95 EXPECT_EQ(11, range->endOffset()); |
| 96 EXPECT_EQ("ipsum", range->text()); |
| 97 } |
| 98 |
| 99 // End of a word. |
| 100 // FIXME: that sounds buggy, we might want to select the word _before_ inste
ad |
| 101 // of the space... |
| 102 { |
| 103 setSelection(5); |
| 104 selection().expandUsingGranularity(WordGranularity); |
| 105 |
| 106 RefPtrWillBeRawPtr<Range> range = selection().firstRange(); |
| 107 EXPECT_EQ(5, range->startOffset()); |
| 108 EXPECT_EQ(6, range->endOffset()); |
| 109 EXPECT_EQ(" ", range->text()); |
| 110 } |
| 111 |
| 112 // Before comma. |
| 113 // FIXME: that sounds buggy, we might want to select the word _before_ inste
ad |
| 114 // of the comma. |
| 115 { |
| 116 setSelection(26); |
| 117 selection().expandUsingGranularity(WordGranularity); |
| 118 |
| 119 RefPtrWillBeRawPtr<Range> range = selection().firstRange(); |
| 120 EXPECT_EQ(26, range->startOffset()); |
| 121 EXPECT_EQ(27, range->endOffset()); |
| 122 EXPECT_EQ(",", range->text()); |
| 123 } |
| 124 |
| 125 // After comma. |
| 126 { |
| 127 setSelection(27); |
| 128 selection().expandUsingGranularity(WordGranularity); |
| 129 |
| 130 RefPtrWillBeRawPtr<Range> range = selection().firstRange(); |
| 131 EXPECT_EQ(27, range->startOffset()); |
| 132 EXPECT_EQ(28, range->endOffset()); |
| 133 EXPECT_EQ(" ", range->text()); |
| 134 } |
| 135 |
| 136 // When selecting part of a word. |
| 137 { |
| 138 setSelection(0, 1); |
| 139 selection().expandUsingGranularity(WordGranularity); |
| 140 |
| 141 RefPtrWillBeRawPtr<Range> range = selection().firstRange(); |
| 142 EXPECT_EQ(0, range->startOffset()); |
| 143 EXPECT_EQ(5, range->endOffset()); |
| 144 EXPECT_EQ("Lorem", range->text()); |
| 145 } |
| 146 |
| 147 // When selecting part of two words. |
| 148 { |
| 149 setSelection(2, 8); |
| 150 selection().expandUsingGranularity(WordGranularity); |
| 151 |
| 152 RefPtrWillBeRawPtr<Range> range = selection().firstRange(); |
| 153 EXPECT_EQ(0, range->startOffset()); |
| 154 EXPECT_EQ(11, range->endOffset()); |
| 155 EXPECT_EQ("Lorem ipsum", range->text()); |
| 156 } |
| 157 } |
| 158 |
| 159 } // namespace WebCore |
OLD | NEW |