OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/editing/markers/DocumentMarkerList.h" |
| 6 |
| 7 #include "platform/heap/Handle.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace blink { |
| 11 |
| 12 class DocumentMarkerListForTesting : public DocumentMarkerList { |
| 13 public: |
| 14 DocumentMarkerListForTesting() {} |
| 15 |
| 16 DocumentMarker::MarkerType allowedMarkerType() const final { |
| 17 return DocumentMarker::Composition; |
| 18 } |
| 19 |
| 20 protected: |
| 21 bool markerListIsSorted() const final { return false; } |
| 22 }; |
| 23 |
| 24 class SortedDocumentMarkerListForTesting : public DocumentMarkerList { |
| 25 public: |
| 26 SortedDocumentMarkerListForTesting() {} |
| 27 |
| 28 DocumentMarker::MarkerType allowedMarkerType() const final { |
| 29 return DocumentMarker::Composition; |
| 30 } |
| 31 |
| 32 protected: |
| 33 bool markerListIsSorted() const final { return true; } |
| 34 }; |
| 35 |
| 36 class DocumentMarkerListTest : public ::testing::Test { |
| 37 protected: |
| 38 DocumentMarkerListTest() |
| 39 : m_markerList(new DocumentMarkerListForTesting()), |
| 40 m_sortedMarkerList(new SortedDocumentMarkerListForTesting()) {} |
| 41 |
| 42 DocumentMarker* createMarker(unsigned startOffset, unsigned endOffset) { |
| 43 return new DocumentMarker(startOffset, endOffset, Color::black, false, |
| 44 Color::black); |
| 45 } |
| 46 |
| 47 Persistent<DocumentMarkerListForTesting> m_markerList; |
| 48 Persistent<SortedDocumentMarkerListForTesting> m_sortedMarkerList; |
| 49 }; |
| 50 |
| 51 TEST_F(DocumentMarkerListTest, |
| 52 RemoveMarkersDoRemovePartiallyOverlappingNoOpRemoval) { |
| 53 m_markerList->add(createMarker(0, 5)); |
| 54 |
| 55 EXPECT_EQ(DocumentMarkerList::DidRemoveMarkerOrNot::DidNotRemoveMarker, |
| 56 m_markerList->removeMarkers(100, 100, true)); |
| 57 EXPECT_EQ(1u, m_markerList->size()); |
| 58 |
| 59 EXPECT_EQ(0u, m_markerList->at(0)->startOffset()); |
| 60 EXPECT_EQ(5u, m_markerList->at(0)->endOffset()); |
| 61 } |
| 62 |
| 63 TEST_F(DocumentMarkerListTest, |
| 64 RemoveMarkersDoRemovePartiallyOverlappingPartialIntersection) { |
| 65 m_markerList->add(createMarker(20, 25)); |
| 66 m_markerList->add(createMarker(10, 15)); |
| 67 m_markerList->add(createMarker(0, 5)); |
| 68 |
| 69 EXPECT_EQ(DocumentMarkerList::DidRemoveMarkerOrNot::DidRemoveMarker, |
| 70 m_markerList->removeMarkers(10, 2, true)); |
| 71 |
| 72 EXPECT_EQ(2u, m_markerList->size()); |
| 73 |
| 74 EXPECT_EQ(20u, m_markerList->at(0)->startOffset()); |
| 75 EXPECT_EQ(25u, m_markerList->at(0)->endOffset()); |
| 76 |
| 77 EXPECT_EQ(0u, m_markerList->at(1)->startOffset()); |
| 78 EXPECT_EQ(5u, m_markerList->at(1)->endOffset()); |
| 79 } |
| 80 |
| 81 TEST_F(DocumentMarkerListTest, |
| 82 RemoveMarkersDoRemovePartiallyOverlappingDoublePartialIntersection) { |
| 83 m_markerList->add(createMarker(30, 35)); |
| 84 m_markerList->add(createMarker(20, 25)); |
| 85 m_markerList->add(createMarker(10, 15)); |
| 86 m_markerList->add(createMarker(0, 5)); |
| 87 |
| 88 EXPECT_EQ(DocumentMarkerList::DidRemoveMarkerOrNot::DidRemoveMarker, |
| 89 m_markerList->removeMarkers(13, 9, true)); |
| 90 |
| 91 EXPECT_EQ(2u, m_markerList->size()); |
| 92 |
| 93 EXPECT_EQ(30u, m_markerList->at(0)->startOffset()); |
| 94 EXPECT_EQ(35u, m_markerList->at(0)->endOffset()); |
| 95 |
| 96 EXPECT_EQ(0u, m_markerList->at(1)->startOffset()); |
| 97 EXPECT_EQ(5u, m_markerList->at(1)->endOffset()); |
| 98 } |
| 99 |
| 100 TEST_F(DocumentMarkerListTest, |
| 101 RemoveMarkersDontRemovePartiallyOverlappingNoOpRemoval) { |
| 102 m_markerList->add(createMarker(0, 5)); |
| 103 |
| 104 // test no-op remove |
| 105 EXPECT_EQ(DocumentMarkerList::DidRemoveMarkerOrNot::DidNotRemoveMarker, |
| 106 m_markerList->removeMarkers(100, 100, false)); |
| 107 EXPECT_EQ(1u, m_markerList->size()); |
| 108 |
| 109 EXPECT_EQ(0u, m_markerList->at(0)->startOffset()); |
| 110 EXPECT_EQ(5u, m_markerList->at(0)->endOffset()); |
| 111 } |
| 112 |
| 113 TEST_F(DocumentMarkerListTest, |
| 114 RemoveMarkersDontRemovePartiallyOverlappingRemoveBeginningOfMarker) { |
| 115 m_markerList->add(createMarker(20, 25)); |
| 116 m_markerList->add(createMarker(10, 15)); |
| 117 m_markerList->add(createMarker(0, 5)); |
| 118 |
| 119 EXPECT_EQ(DocumentMarkerList::DidRemoveMarkerOrNot::DidRemoveMarker, |
| 120 m_markerList->removeMarkers(10, 2, false)); |
| 121 EXPECT_EQ(3u, m_markerList->size()); |
| 122 |
| 123 EXPECT_EQ(20u, m_markerList->at(0)->startOffset()); |
| 124 EXPECT_EQ(25u, m_markerList->at(0)->endOffset()); |
| 125 |
| 126 EXPECT_EQ(12u, m_markerList->at(1)->startOffset()); |
| 127 EXPECT_EQ(15u, m_markerList->at(1)->endOffset()); |
| 128 |
| 129 EXPECT_EQ(0u, m_markerList->at(2)->startOffset()); |
| 130 EXPECT_EQ(5u, m_markerList->at(2)->endOffset()); |
| 131 } |
| 132 |
| 133 TEST_F(DocumentMarkerListTest, |
| 134 RemoveMarkersDontRemovePartiallyOverlappingRemoveEndOfMarker) { |
| 135 m_markerList->add(createMarker(20, 25)); |
| 136 m_markerList->add(createMarker(10, 15)); |
| 137 m_markerList->add(createMarker(0, 5)); |
| 138 |
| 139 EXPECT_EQ(DocumentMarkerList::DidRemoveMarkerOrNot::DidRemoveMarker, |
| 140 m_markerList->removeMarkers(13, 2, false)); |
| 141 EXPECT_EQ(3u, m_markerList->size()); |
| 142 |
| 143 EXPECT_EQ(20u, m_markerList->at(0)->startOffset()); |
| 144 EXPECT_EQ(25u, m_markerList->at(0)->endOffset()); |
| 145 |
| 146 EXPECT_EQ(10u, m_markerList->at(1)->startOffset()); |
| 147 EXPECT_EQ(13u, m_markerList->at(1)->endOffset()); |
| 148 |
| 149 EXPECT_EQ(0u, m_markerList->at(2)->startOffset()); |
| 150 EXPECT_EQ(5u, m_markerList->at(2)->endOffset()); |
| 151 } |
| 152 |
| 153 TEST_F(DocumentMarkerListTest, |
| 154 RemoveMarkersDontRemovePartiallyOverlappingRemoveMiddleOfMarker) { |
| 155 m_markerList->add(createMarker(20, 25)); |
| 156 m_markerList->add(createMarker(10, 15)); |
| 157 m_markerList->add(createMarker(0, 5)); |
| 158 |
| 159 EXPECT_EQ(DocumentMarkerList::DidRemoveMarkerOrNot::DidRemoveMarker, |
| 160 m_markerList->removeMarkers(11, 3, false)); |
| 161 EXPECT_EQ(4u, m_markerList->size()); |
| 162 |
| 163 EXPECT_EQ(20u, m_markerList->at(0)->startOffset()); |
| 164 EXPECT_EQ(25u, m_markerList->at(0)->endOffset()); |
| 165 |
| 166 EXPECT_EQ(10u, m_markerList->at(1)->startOffset()); |
| 167 EXPECT_EQ(11u, m_markerList->at(1)->endOffset()); |
| 168 |
| 169 EXPECT_EQ(14u, m_markerList->at(2)->startOffset()); |
| 170 EXPECT_EQ(15u, m_markerList->at(2)->endOffset()); |
| 171 |
| 172 EXPECT_EQ(0u, m_markerList->at(3)->startOffset()); |
| 173 EXPECT_EQ(5u, m_markerList->at(3)->endOffset()); |
| 174 } |
| 175 |
| 176 TEST_F(DocumentMarkerListTest, |
| 177 RemoveMarkersDontRemovePartiallyOverlappingRemoveEntireMarker) { |
| 178 m_markerList->add(createMarker(20, 25)); |
| 179 m_markerList->add(createMarker(10, 15)); |
| 180 m_markerList->add(createMarker(0, 5)); |
| 181 |
| 182 EXPECT_EQ(DocumentMarkerList::DidRemoveMarkerOrNot::DidRemoveMarker, |
| 183 m_markerList->removeMarkers(10, 5, false)); |
| 184 EXPECT_EQ(2u, m_markerList->size()); |
| 185 |
| 186 EXPECT_EQ(20u, m_markerList->at(0)->startOffset()); |
| 187 EXPECT_EQ(25u, m_markerList->at(0)->endOffset()); |
| 188 |
| 189 EXPECT_EQ(0u, m_markerList->at(1)->startOffset()); |
| 190 EXPECT_EQ(5u, m_markerList->at(1)->endOffset()); |
| 191 } |
| 192 |
| 193 TEST_F(DocumentMarkerListTest, ShiftMarkersNoChange) { |
| 194 m_markerList->add(createMarker(0, 5)); |
| 195 |
| 196 EXPECT_EQ(DocumentMarkerList::DidShiftMarkerOrNot::DidNotShiftMarker, |
| 197 m_markerList->shiftMarkers(10, 0, 5)); |
| 198 EXPECT_EQ(1u, m_markerList->size()); |
| 199 |
| 200 EXPECT_EQ(0u, m_markerList->at(0)->startOffset()); |
| 201 EXPECT_EQ(5u, m_markerList->at(0)->endOffset()); |
| 202 } |
| 203 |
| 204 TEST_F(DocumentMarkerListTest, ShiftMarkersMarkerShifted) { |
| 205 m_markerList->add(createMarker(0, 5)); |
| 206 |
| 207 EXPECT_EQ(DocumentMarkerList::DidShiftMarkerOrNot::DidShiftMarker, |
| 208 m_markerList->shiftMarkers(0, 0, 5)); |
| 209 EXPECT_EQ(1u, m_markerList->size()); |
| 210 |
| 211 EXPECT_EQ(5u, m_markerList->at(0)->startOffset()); |
| 212 EXPECT_EQ(10u, m_markerList->at(0)->endOffset()); |
| 213 } |
| 214 |
| 215 TEST_F(DocumentMarkerListTest, ShiftMarkersMarkerRemoved) { |
| 216 m_markerList->add(createMarker(0, 5)); |
| 217 |
| 218 EXPECT_EQ(DocumentMarkerList::DidShiftMarkerOrNot::DidShiftMarker, |
| 219 m_markerList->shiftMarkers(0, 5, 0)); |
| 220 EXPECT_EQ(0u, m_markerList->size()); |
| 221 } |
| 222 |
| 223 TEST_F(DocumentMarkerListTest, CopyMarkersNoOp) { |
| 224 m_markerList->add(createMarker(0, 1)); |
| 225 |
| 226 DocumentMarkerListForTesting* dstList = new DocumentMarkerListForTesting(); |
| 227 EXPECT_EQ(DocumentMarkerList::DidCopyMarkerOrNot::DidNotCopyMarker, |
| 228 m_markerList->copyMarkers(2, 4, dstList, -1)); |
| 229 |
| 230 EXPECT_EQ(0u, dstList->size()); |
| 231 } |
| 232 |
| 233 TEST_F(DocumentMarkerListTest, CopyMarkersNonEmpty) { |
| 234 m_markerList->add(createMarker(1, 2)); |
| 235 m_markerList->add(createMarker(3, 4)); |
| 236 m_markerList->add(createMarker(5, 6)); |
| 237 m_markerList->add(createMarker(7, 8)); |
| 238 |
| 239 DocumentMarkerListForTesting* dstList = new DocumentMarkerListForTesting(); |
| 240 |
| 241 // Copy all markers that intersect the range 2 through 5, i.e. the first three |
| 242 EXPECT_EQ(DocumentMarkerList::DidCopyMarkerOrNot::DidCopyMarker, |
| 243 m_markerList->copyMarkers(2, 4, dstList, -1)); |
| 244 |
| 245 EXPECT_EQ(3u, dstList->size()); |
| 246 |
| 247 // Markers are clamped to the range 2 through 5, so the first and third |
| 248 // markers are now length 0 |
| 249 // TODO(rlanday): should we change this behavior to remove the zero-length |
| 250 // markers? |
| 251 EXPECT_EQ(1u, dstList->at(0)->startOffset()); |
| 252 EXPECT_EQ(1u, dstList->at(0)->endOffset()); |
| 253 |
| 254 EXPECT_EQ(2u, dstList->at(1)->startOffset()); |
| 255 EXPECT_EQ(3u, dstList->at(1)->endOffset()); |
| 256 |
| 257 EXPECT_EQ(4u, dstList->at(2)->startOffset()); |
| 258 EXPECT_EQ(4u, dstList->at(2)->endOffset()); |
| 259 } |
| 260 |
| 261 } // namespace blink |
OLD | NEW |