Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2473)

Unified Diff: third_party/WebKit/Source/core/editing/markers/DocumentMarkerListEditorTest.cpp

Issue 2811083005: Add DocumentMarkerListEditor (Closed)
Patch Set: Fix test Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/Source/core/editing/markers/DocumentMarkerListEditor.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/core/editing/markers/DocumentMarkerListEditorTest.cpp
diff --git a/third_party/WebKit/Source/core/editing/markers/DocumentMarkerListEditorTest.cpp b/third_party/WebKit/Source/core/editing/markers/DocumentMarkerListEditorTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..6e08cbe788b26c6a3b728de3a2846573c1939dec
--- /dev/null
+++ b/third_party/WebKit/Source/core/editing/markers/DocumentMarkerListEditorTest.cpp
@@ -0,0 +1,333 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/editing/markers/DocumentMarkerListEditor.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace blink {
+
+class DocumentMarkerListForTesting : public DocumentMarkerList {
+ // only implement enough that this can be used as a destination for moving
+ // markers
+ public:
+ bool Empty() const final { return false; }
+ void Add(DocumentMarker* marker) final { markers_.push_back(marker); }
+ void Clear() final {}
+ void AppendMarkersToInputList(DocumentMarkerVector* list) const final {
+ list->AppendVector(markers_);
+ }
+ bool MoveMarkers(int, DocumentMarkerList*) final { return false; }
+ bool RemoveMarkers(unsigned, int) final { return false; }
+ bool ShiftMarkers(unsigned, unsigned, unsigned) final { return false; }
+
+ protected:
+ DocumentMarkerVector markers_;
+
+ DEFINE_INLINE_VIRTUAL_TRACE() {
+ visitor->Trace(markers_);
+ DocumentMarkerList::Trace(visitor);
+ }
+};
+
+class DocumentMarkerListEditorTest : public ::testing::Test {
+ public:
+ DocumentMarkerListEditorTest() : marker_list_(new DocumentMarkerVector) {}
+
+ protected:
+ DocumentMarker* CreateMarker(unsigned StartOffset, unsigned EndOffset) {
+ return new DocumentMarker(StartOffset, EndOffset, Color::kBlack, false,
+ Color::kBlack);
+ }
+
+ Persistent<DocumentMarkerVector> marker_list_;
+};
+
+TEST_F(DocumentMarkerListEditorTest, RemoveUnsortedMarkersNoOpRemoval) {
+ marker_list_->push_back(CreateMarker(5, 10));
+ marker_list_->push_back(CreateMarker(0, 5));
+
+ EXPECT_FALSE(
+ DocumentMarkerListEditor::RemoveUnsortedMarkers(marker_list_, 100, 100));
+ EXPECT_EQ(2u, marker_list_->size());
+
+ EXPECT_EQ(5u, marker_list_->at(0)->StartOffset());
+ EXPECT_EQ(10u, marker_list_->at(0)->EndOffset());
+
+ EXPECT_EQ(0u, marker_list_->at(1)->StartOffset());
+ EXPECT_EQ(5u, marker_list_->at(1)->EndOffset());
+}
+
+TEST_F(DocumentMarkerListEditorTest, RemoveSortedMarkersNoOpRemoval) {
+ marker_list_->push_back(CreateMarker(0, 5));
+ marker_list_->push_back(CreateMarker(5, 10));
+
+ EXPECT_FALSE(
+ DocumentMarkerListEditor::RemoveSortedMarkers(marker_list_, 100, 100));
+ EXPECT_EQ(2u, marker_list_->size());
+
+ EXPECT_EQ(0u, marker_list_->at(0)->StartOffset());
+ EXPECT_EQ(5u, marker_list_->at(0)->EndOffset());
+
+ EXPECT_EQ(5u, marker_list_->at(1)->StartOffset());
+ EXPECT_EQ(10u, marker_list_->at(1)->EndOffset());
+}
+
+TEST_F(DocumentMarkerListEditorTest, RemoveUnsortedMarkersPartialIntersection) {
+ marker_list_->push_back(CreateMarker(20, 25));
+ marker_list_->push_back(CreateMarker(10, 15));
+ marker_list_->push_back(CreateMarker(0, 5));
+
+ EXPECT_EQ(true, DocumentMarkerListEditor::RemoveUnsortedMarkers(marker_list_,
+ 10, 2));
+
+ EXPECT_EQ(2u, marker_list_->size());
+
+ EXPECT_EQ(20u, marker_list_->at(0)->StartOffset());
+ EXPECT_EQ(25u, marker_list_->at(0)->EndOffset());
+
+ EXPECT_EQ(0u, marker_list_->at(1)->StartOffset());
+ EXPECT_EQ(5u, marker_list_->at(1)->EndOffset());
+}
+
+TEST_F(DocumentMarkerListEditorTest, RemoveSortedMarkersPartialIntersection) {
+ marker_list_->push_back(CreateMarker(0, 5));
+ marker_list_->push_back(CreateMarker(10, 15));
+ marker_list_->push_back(CreateMarker(20, 25));
+
+ EXPECT_EQ(true,
+ DocumentMarkerListEditor::RemoveSortedMarkers(marker_list_, 10, 2));
+
+ EXPECT_EQ(2u, marker_list_->size());
+
+ EXPECT_EQ(0u, marker_list_->at(0)->StartOffset());
+ EXPECT_EQ(5u, marker_list_->at(0)->EndOffset());
+
+ EXPECT_EQ(20u, marker_list_->at(1)->StartOffset());
+ EXPECT_EQ(25u, marker_list_->at(1)->EndOffset());
+}
+
+TEST_F(
+ DocumentMarkerListEditorTest,
+ RemoveUnsortedMarkersDoRemovePartiallyOverlappingDoublePartialIntersection) {
+ marker_list_->push_back(CreateMarker(30, 35));
+ marker_list_->push_back(CreateMarker(20, 25));
+ marker_list_->push_back(CreateMarker(10, 15));
+ marker_list_->push_back(CreateMarker(0, 5));
+
+ EXPECT_EQ(true, DocumentMarkerListEditor::RemoveUnsortedMarkers(marker_list_,
+ 13, 9));
+
+ EXPECT_EQ(2u, marker_list_->size());
+
+ EXPECT_EQ(30u, marker_list_->at(0)->StartOffset());
+ EXPECT_EQ(35u, marker_list_->at(0)->EndOffset());
+
+ EXPECT_EQ(0u, marker_list_->at(1)->StartOffset());
+ EXPECT_EQ(5u, marker_list_->at(1)->EndOffset());
+}
+
+TEST_F(
+ DocumentMarkerListEditorTest,
+ RemoveSortedMarkersDoRemovePartiallyOverlappingDoublePartialIntersection) {
+ marker_list_->push_back(CreateMarker(0, 5));
+ marker_list_->push_back(CreateMarker(10, 15));
+ marker_list_->push_back(CreateMarker(20, 25));
+ marker_list_->push_back(CreateMarker(30, 35));
+
+ EXPECT_EQ(true,
+ DocumentMarkerListEditor::RemoveSortedMarkers(marker_list_, 13, 9));
+
+ EXPECT_EQ(2u, marker_list_->size());
+
+ EXPECT_EQ(0u, marker_list_->at(0)->StartOffset());
+ EXPECT_EQ(5u, marker_list_->at(0)->EndOffset());
+
+ EXPECT_EQ(30u, marker_list_->at(1)->StartOffset());
+ EXPECT_EQ(35u, marker_list_->at(1)->EndOffset());
+}
+
+TEST_F(DocumentMarkerListEditorTest, ShiftUnsortedMarkersNoChange) {
+ marker_list_->push_back(CreateMarker(5, 10));
+ marker_list_->push_back(CreateMarker(0, 5));
+
+ EXPECT_FALSE(
+ DocumentMarkerListEditor::ShiftUnsortedMarkers(marker_list_, 15, 0, 5));
+ EXPECT_EQ(2u, marker_list_->size());
+
+ EXPECT_EQ(5u, marker_list_->at(0)->StartOffset());
+ EXPECT_EQ(10u, marker_list_->at(0)->EndOffset());
+
+ EXPECT_EQ(0u, marker_list_->at(1)->StartOffset());
+ EXPECT_EQ(5u, marker_list_->at(1)->EndOffset());
+}
+
+TEST_F(DocumentMarkerListEditorTest, ShiftSortedMarkersNoChange) {
+ marker_list_->push_back(CreateMarker(0, 5));
+ marker_list_->push_back(CreateMarker(5, 10));
+
+ EXPECT_FALSE(
+ DocumentMarkerListEditor::ShiftSortedMarkers(marker_list_, 15, 0, 5));
+ EXPECT_EQ(2u, marker_list_->size());
+
+ EXPECT_EQ(0u, marker_list_->at(0)->StartOffset());
+ EXPECT_EQ(5u, marker_list_->at(0)->EndOffset());
+
+ EXPECT_EQ(5u, marker_list_->at(1)->StartOffset());
+ EXPECT_EQ(10u, marker_list_->at(1)->EndOffset());
+}
+
+TEST_F(DocumentMarkerListEditorTest, ShiftUnsortedMarkersMarkerShifted) {
+ marker_list_->push_back(CreateMarker(5, 10));
+ marker_list_->push_back(CreateMarker(0, 5));
+
+ EXPECT_EQ(true, DocumentMarkerListEditor::ShiftUnsortedMarkers(marker_list_,
+ 0, 0, 5));
+ EXPECT_EQ(2u, marker_list_->size());
+
+ EXPECT_EQ(10u, marker_list_->at(0)->StartOffset());
+ EXPECT_EQ(15u, marker_list_->at(0)->EndOffset());
+
+ EXPECT_EQ(5u, marker_list_->at(1)->StartOffset());
+ EXPECT_EQ(10u, marker_list_->at(1)->EndOffset());
+}
+
+TEST_F(DocumentMarkerListEditorTest, ShiftSortedMarkersMarkerShifted) {
+ marker_list_->push_back(CreateMarker(0, 5));
+ marker_list_->push_back(CreateMarker(5, 10));
+
+ EXPECT_EQ(true, DocumentMarkerListEditor::ShiftSortedMarkers(marker_list_, 0,
+ 0, 5));
+ EXPECT_EQ(2u, marker_list_->size());
+
+ EXPECT_EQ(5u, marker_list_->at(0)->StartOffset());
+ EXPECT_EQ(10u, marker_list_->at(0)->EndOffset());
+
+ EXPECT_EQ(10u, marker_list_->at(1)->StartOffset());
+ EXPECT_EQ(15u, marker_list_->at(1)->EndOffset());
+}
+
+TEST_F(DocumentMarkerListEditorTest, ShiftUnsortedMarkersMarkerRemoved) {
+ marker_list_->push_back(CreateMarker(5, 10));
+ marker_list_->push_back(CreateMarker(0, 5));
+
+ EXPECT_EQ(true, DocumentMarkerListEditor::ShiftUnsortedMarkers(marker_list_,
+ 0, 10, 0));
+ EXPECT_EQ(0u, marker_list_->size());
+}
+
+TEST_F(DocumentMarkerListEditorTest, ShiftSortedMarkersMarkerRemoved) {
+ marker_list_->push_back(CreateMarker(0, 5));
+ marker_list_->push_back(CreateMarker(5, 10));
+
+ EXPECT_EQ(true, DocumentMarkerListEditor::ShiftSortedMarkers(marker_list_, 0,
+ 10, 0));
+ EXPECT_EQ(0u, marker_list_->size());
+}
+
+TEST_F(DocumentMarkerListEditorTest, MoveUnsortedMarkersNoOp) {
+ marker_list_->push_back(CreateMarker(7, 8));
+ marker_list_->push_back(CreateMarker(5, 6));
+
+ DocumentMarkerListForTesting* dst_list = new DocumentMarkerListForTesting();
+ EXPECT_FALSE(
+ DocumentMarkerListEditor::MoveUnsortedMarkers(marker_list_, 4, dst_list));
+
+ EXPECT_EQ(2u, marker_list_->size());
+
+ DocumentMarkerVector result;
+ dst_list->AppendMarkersToInputList(&result);
+ EXPECT_EQ(0u, result.size());
+}
+
+TEST_F(DocumentMarkerListEditorTest, MoveSortedMarkersNoOp) {
+ marker_list_->push_back(CreateMarker(5, 6));
+ marker_list_->push_back(CreateMarker(7, 8));
+
+ DocumentMarkerListForTesting* dst_list = new DocumentMarkerListForTesting();
+ EXPECT_FALSE(
+ DocumentMarkerListEditor::MoveSortedMarkers(marker_list_, 4, dst_list));
+
+ EXPECT_EQ(2u, marker_list_->size());
+
+ DocumentMarkerVector result;
+ dst_list->AppendMarkersToInputList(&result);
+ EXPECT_EQ(0u, result.size());
+}
+
+TEST_F(DocumentMarkerListEditorTest, MoveUnsortedMarkersNonEmpty) {
+ marker_list_->push_back(CreateMarker(7, 8));
+ marker_list_->push_back(CreateMarker(5, 6));
+ marker_list_->push_back(CreateMarker(3, 4));
+ marker_list_->push_back(CreateMarker(1, 2));
+
+ DocumentMarkerListForTesting* dst_list = new DocumentMarkerListForTesting();
+
+ // Move all markers that start before 6, i.e. the first three
+ EXPECT_EQ(true, DocumentMarkerListEditor::MoveUnsortedMarkers(marker_list_, 6,
+ dst_list));
+
+ DocumentMarkerVector result;
+ dst_list->AppendMarkersToInputList(&result);
+
+ // Markers are clamped to the range 0 through 5, so the third marker is now of
+ // length zero
+ // TODO(rlanday): should we change this behavior to remove zero-length
+ // markers?
+
+ EXPECT_EQ(1u, marker_list_->size());
+
+ EXPECT_EQ(7u, marker_list_->at(0)->StartOffset());
+ EXPECT_EQ(8u, marker_list_->at(0)->EndOffset());
+
+ EXPECT_EQ(3u, result.size());
+
+ EXPECT_EQ(5u, result[0]->StartOffset());
+ EXPECT_EQ(5u, result[0]->EndOffset());
+
+ EXPECT_EQ(3u, result[1]->StartOffset());
+ EXPECT_EQ(4u, result[1]->EndOffset());
+
+ EXPECT_EQ(1u, result[2]->StartOffset());
+ EXPECT_EQ(2u, result[2]->EndOffset());
+}
+
+TEST_F(DocumentMarkerListEditorTest, MoveSortedMarkersNonEmpty) {
+ marker_list_->push_back(CreateMarker(1, 2));
+ marker_list_->push_back(CreateMarker(3, 4));
+ marker_list_->push_back(CreateMarker(5, 6));
+ marker_list_->push_back(CreateMarker(7, 8));
+
+ DocumentMarkerListForTesting* dst_list = new DocumentMarkerListForTesting();
+
+ // Move all markers that start before 6, i.e. the first three
+ EXPECT_EQ(true, DocumentMarkerListEditor::MoveSortedMarkers(marker_list_, 6,
+ dst_list));
+
+ DocumentMarkerVector result;
+ dst_list->AppendMarkersToInputList(&result);
+
+ // Markers are clamped to the range 0 through 5, so the third marker is now of
+ // length zero
+ // TODO(rlanday): should we change this behavior to remove zero-length
+ // markers?
+
+ EXPECT_EQ(1u, marker_list_->size());
+
+ EXPECT_EQ(7u, marker_list_->at(0)->StartOffset());
+ EXPECT_EQ(8u, marker_list_->at(0)->EndOffset());
+
+ EXPECT_EQ(3u, result.size());
+
+ EXPECT_EQ(1u, result[0]->StartOffset());
+ EXPECT_EQ(2u, result[0]->EndOffset());
+
+ EXPECT_EQ(3u, result[1]->StartOffset());
+ EXPECT_EQ(4u, result[1]->EndOffset());
+
+ EXPECT_EQ(5u, result[2]->StartOffset());
+ EXPECT_EQ(5u, result[2]->EndOffset());
+}
+
+} // namespace blink
« no previous file with comments | « third_party/WebKit/Source/core/editing/markers/DocumentMarkerListEditor.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698