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/SurroundingText.h" |
| 7 |
| 8 #include "core/dom/Position.h" |
| 9 #include "core/dom/Range.h" |
| 10 #include "core/dom/Text.h" |
| 11 #include "core/editing/VisibleSelection.h" |
| 12 #include "core/html/HTMLElement.h" |
| 13 #include "core/testing/DummyPageHolder.h" |
| 14 #include <gtest/gtest.h> |
| 15 |
| 16 using namespace WebCore; |
| 17 |
| 18 namespace { |
| 19 |
| 20 class SurroundingTextTest : public ::testing::Test { |
| 21 protected: |
| 22 Document& document() const { return m_dummyPageHolder->document(); } |
| 23 void setHTML(const String&); |
| 24 VisibleSelection select(int offset) { return select(offset, offset); } |
| 25 VisibleSelection select(int start, int end); |
| 26 |
| 27 private: |
| 28 virtual void SetUp() OVERRIDE; |
| 29 |
| 30 OwnPtr<DummyPageHolder> m_dummyPageHolder; |
| 31 }; |
| 32 |
| 33 void SurroundingTextTest::SetUp() |
| 34 { |
| 35 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); |
| 36 } |
| 37 |
| 38 void SurroundingTextTest::setHTML(const String& content) |
| 39 { |
| 40 document().body()->setInnerHTML(content, ASSERT_NO_EXCEPTION); |
| 41 } |
| 42 |
| 43 VisibleSelection SurroundingTextTest::select(int start, int end) |
| 44 { |
| 45 Element* element = document().getElementById("selection"); |
| 46 VisibleSelection selection; |
| 47 selection.setBase(Position(toText(element->firstChild()), start)); |
| 48 selection.setExtent(Position(toText(element->firstChild()), end)); |
| 49 return selection; |
| 50 } |
| 51 |
| 52 TEST_F(SurroundingTextTest, BasicCaretSelection) |
| 53 { |
| 54 setHTML(String("<p id='selection'>foo bar</p>")); |
| 55 |
| 56 { |
| 57 VisibleSelection selection = select(0); |
| 58 SurroundingText surroundingText(selection.start(), 1); |
| 59 |
| 60 EXPECT_EQ("f", surroundingText.content()); |
| 61 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
| 62 } |
| 63 |
| 64 { |
| 65 VisibleSelection selection = select(0); |
| 66 SurroundingText surroundingText(selection.start(), 5); |
| 67 |
| 68 // maxlength/2 is used on the left and right. |
| 69 EXPECT_EQ("foo", surroundingText.content()); |
| 70 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
| 71 } |
| 72 |
| 73 { |
| 74 VisibleSelection selection = select(0); |
| 75 SurroundingText surroundingText(selection.start(), 42); |
| 76 |
| 77 EXPECT_EQ("foo bar", surroundingText.content()); |
| 78 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
| 79 } |
| 80 |
| 81 { |
| 82 // FIXME: if the selection is at the end of the text, SurroundingText |
| 83 // will return nothing. |
| 84 VisibleSelection selection = select(7); |
| 85 SurroundingText surroundingText(selection.start(), 42); |
| 86 |
| 87 EXPECT_EQ(0u, surroundingText.content().length()); |
| 88 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
| 89 } |
| 90 |
| 91 { |
| 92 VisibleSelection selection = select(6); |
| 93 SurroundingText surroundingText(selection.start(), 2); |
| 94 |
| 95 EXPECT_EQ("ar", surroundingText.content()); |
| 96 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); |
| 97 } |
| 98 |
| 99 { |
| 100 VisibleSelection selection = select(6); |
| 101 SurroundingText surroundingText(selection.start(), 42); |
| 102 |
| 103 EXPECT_EQ("foo bar", surroundingText.content()); |
| 104 EXPECT_EQ(6u, surroundingText.positionOffsetInContent()); |
| 105 } |
| 106 } |
| 107 |
| 108 TEST_F(SurroundingTextTest, TreeCaretSelection) |
| 109 { |
| 110 setHTML(String("<div>This is outside of <p id='selection'>foo bar</p> the se
lected node</div>")); |
| 111 |
| 112 { |
| 113 VisibleSelection selection = select(0); |
| 114 SurroundingText surroundingText(selection.start(), 1); |
| 115 |
| 116 EXPECT_EQ("f", surroundingText.content()); |
| 117 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
| 118 } |
| 119 |
| 120 { |
| 121 VisibleSelection selection = select(0); |
| 122 SurroundingText surroundingText(selection.start(), 5); |
| 123 |
| 124 EXPECT_EQ("foo", surroundingText.content().simplifyWhiteSpace()); |
| 125 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); |
| 126 } |
| 127 |
| 128 { |
| 129 VisibleSelection selection = select(0); |
| 130 SurroundingText surroundingText(selection.start(), 1337); |
| 131 |
| 132 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); |
| 133 EXPECT_EQ(19u, surroundingText.positionOffsetInContent()); |
| 134 } |
| 135 |
| 136 { |
| 137 VisibleSelection selection = select(6); |
| 138 SurroundingText surroundingText(selection.start(), 2); |
| 139 |
| 140 EXPECT_EQ("ar", surroundingText.content()); |
| 141 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); |
| 142 } |
| 143 |
| 144 { |
| 145 VisibleSelection selection = select(6); |
| 146 SurroundingText surroundingText(selection.start(), 1337); |
| 147 |
| 148 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); |
| 149 EXPECT_EQ(25u, surroundingText.positionOffsetInContent()); |
| 150 } |
| 151 } |
| 152 |
| 153 TEST_F(SurroundingTextTest, ComplexHTMLStructuresCaret) |
| 154 { |
| 155 // Those tests are imported from what was previously layout tests. |
| 156 { |
| 157 setHTML(String("<button>.</button>12345<p id='selection'>6789 12345</p>6
789<button>.</button>")); |
| 158 |
| 159 VisibleSelection selection = select(0); |
| 160 SurroundingText surroundingText(selection.start(), 100); |
| 161 |
| 162 EXPECT_EQ("12345 6789 12345 6789", surroundingText.content().simplifyWhi
teSpace()); |
| 163 EXPECT_EQ(6u, surroundingText.positionOffsetInContent()); |
| 164 } |
| 165 |
| 166 { |
| 167 setHTML(String("<button>.</button>12345<p id='selection'>6789 12345</p>6
789<button>.</button>")); |
| 168 |
| 169 VisibleSelection selection = select(5); |
| 170 SurroundingText surroundingText(selection.start(), 6); |
| 171 |
| 172 EXPECT_EQ("89 123", surroundingText.content()); |
| 173 EXPECT_EQ(3u, surroundingText.positionOffsetInContent()); |
| 174 } |
| 175 |
| 176 { |
| 177 setHTML(String("<button>.</button>12345<p id='selection'>6789 12345</p>6
789<button>.</button>")); |
| 178 |
| 179 VisibleSelection selection = select(5); |
| 180 SurroundingText surroundingText(selection.start(), 0); |
| 181 |
| 182 EXPECT_TRUE(surroundingText.content().isEmpty()); |
| 183 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
| 184 } |
| 185 |
| 186 { |
| 187 setHTML(String("<button>.</button>12345<p id='selection'>6789 12345</p>6
789<button>.</button>")); |
| 188 |
| 189 VisibleSelection selection = select(5); |
| 190 SurroundingText surroundingText(selection.start(), 1); |
| 191 |
| 192 EXPECT_EQ("1", surroundingText.content()); |
| 193 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
| 194 } |
| 195 |
| 196 { |
| 197 setHTML(String("<button>.</button>12345<p id='selection'>6789 12345</p>6
789<button>.</button>")); |
| 198 |
| 199 VisibleSelection selection = select(6); |
| 200 SurroundingText surroundingText(selection.start(), 2); |
| 201 |
| 202 EXPECT_EQ("12", surroundingText.content()); |
| 203 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); |
| 204 } |
| 205 |
| 206 { |
| 207 setHTML(String("<select>.</select><div>57th Street and Lake Shore Drive<
/div> <span>Chicago</span> <span id='selection'>IL</span> <span>60637</span><sel
ect>.</select>")); |
| 208 |
| 209 VisibleSelection selection = select(0); |
| 210 SurroundingText surroundingText(selection.start(), 100); |
| 211 |
| 212 EXPECT_EQ("57th Street and Lake Shore Drive Chicago IL 60637", surroundi
ngText.content().simplifyWhiteSpace()); |
| 213 EXPECT_EQ(42u, surroundingText.positionOffsetInContent()); |
| 214 } |
| 215 |
| 216 { |
| 217 setHTML(String("<fieldset>.</fieldset>12345<button>abc</button><p id='se
lection'>6789<br>12345</p>6789<textarea>abc</textarea>0123<fieldset>.</fieldset>
")); |
| 218 |
| 219 VisibleSelection selection = select(5); |
| 220 SurroundingText surroundingText(selection.end(), 100); |
| 221 |
| 222 EXPECT_EQ("6789 12345 6789", surroundingText.content().simplifyWhiteSpac
e()); |
| 223 EXPECT_EQ(5u, surroundingText.positionOffsetInContent()); |
| 224 } |
| 225 |
| 226 { |
| 227 setHTML(String("<button>.</button><div id='selection'>This is <!-- comme
nt --!>a test <script language='javascript'></script>example<button>.</button>")
); |
| 228 |
| 229 VisibleSelection selection = select(0); |
| 230 SurroundingText surroundingText(selection.start(), 100); |
| 231 |
| 232 EXPECT_EQ("This is a test example", surroundingText.content().simplifyWh
iteSpace()); |
| 233 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); |
| 234 } |
| 235 |
| 236 { |
| 237 setHTML(String("<button>.</button><div id='selection'>012345678901234567
890123456789</div><button>.</button>")); |
| 238 |
| 239 VisibleSelection selection = select(15); |
| 240 SurroundingText surroundingText(selection.start(), 12); |
| 241 |
| 242 EXPECT_EQ("901234567890", surroundingText.content().simplifyWhiteSpace()
); |
| 243 EXPECT_EQ(6u, surroundingText.positionOffsetInContent()); |
| 244 } |
| 245 |
| 246 { |
| 247 setHTML(String("<option>.</option>12345<button id='selection'>test</butt
on><option>.</option>")); |
| 248 |
| 249 VisibleSelection selection = select(0); |
| 250 SurroundingText surroundingText(selection.start(), 100); |
| 251 |
| 252 EXPECT_TRUE(surroundingText.content().isEmpty()); |
| 253 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
| 254 } |
| 255 |
| 256 { |
| 257 setHTML(String("<option>.</option>12345<button>te<span id='selection'>st
</span></button><option>.</option>")); |
| 258 |
| 259 VisibleSelection selection = select(0); |
| 260 SurroundingText surroundingText(selection.start(), 100); |
| 261 |
| 262 EXPECT_TRUE(surroundingText.content().isEmpty()); |
| 263 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
| 264 |
| 265 } |
| 266 |
| 267 { |
| 268 setHTML(String("<p id='selection'>.</p>")); |
| 269 |
| 270 VisibleSelection selection = select(0); |
| 271 SurroundingText surroundingText(selection.start(), 2); |
| 272 |
| 273 EXPECT_EQ(".", surroundingText.content()); |
| 274 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
| 275 } |
| 276 } |
| 277 |
| 278 |
| 279 } // anonymous namespace |
OLD | NEW |