Add document undo history tests

This commit is contained in:
2026-06-01 07:39:42 +02:00
parent 126280ff7c
commit 3d80791245
5 changed files with 231 additions and 4 deletions

View File

@@ -4,7 +4,9 @@
#include <string_view>
using pp::document::CanvasDocument;
using pp::document::DocumentHistory;
using pp::document::DocumentConfig;
using pp::document::max_document_history_entries;
using pp::document::max_canvas_dimension;
using pp::document::max_frame_count;
using pp::document::max_layer_count;
@@ -145,6 +147,117 @@ void rejects_invalid_animation_frame_operations(pp::tests::Harness& h)
PP_EXPECT(h, max_frame_count > document.frames().size());
}
void records_document_history_and_restores_snapshots(pp::tests::Harness& h)
{
auto document_result = CanvasDocument::create(
DocumentConfig { .width = 64, .height = 64, .layer_count = 1 });
PP_EXPECT(h, document_result.ok());
auto history_result = DocumentHistory::create(document_result.value(), 4);
PP_EXPECT(h, history_result.ok());
auto history = history_result.value();
auto with_layer = history.current();
const auto added_layer = with_layer.add_layer("Paint");
PP_EXPECT(h, added_layer.ok());
PP_EXPECT(h, history.apply(with_layer).ok());
auto with_frame = history.current();
const auto added_frame = with_frame.add_frame(250);
PP_EXPECT(h, added_frame.ok());
PP_EXPECT(h, history.apply(with_frame).ok());
PP_EXPECT(h, history.size() == 3U);
PP_EXPECT(h, history.current_index() == 2U);
PP_EXPECT(h, history.current().layers().size() == 2U);
PP_EXPECT(h, history.current().frames().size() == 2U);
PP_EXPECT(h, history.can_undo());
PP_EXPECT(h, !history.can_redo());
PP_EXPECT(h, history.undo().ok());
PP_EXPECT(h, history.current().layers().size() == 2U);
PP_EXPECT(h, history.current().frames().size() == 1U);
PP_EXPECT(h, history.can_redo());
PP_EXPECT(h, history.undo().ok());
PP_EXPECT(h, history.current().layers().size() == 1U);
PP_EXPECT(h, history.current().frames().size() == 1U);
const auto undo_past_start = history.undo();
PP_EXPECT(h, !undo_past_start.ok());
PP_EXPECT(h, undo_past_start.code == StatusCode::out_of_range);
PP_EXPECT(h, history.redo().ok());
PP_EXPECT(h, history.current().layers().size() == 2U);
}
void applying_after_undo_discards_redo_branch(pp::tests::Harness& h)
{
auto document_result = CanvasDocument::create(
DocumentConfig { .width = 64, .height = 64, .layer_count = 1 });
PP_EXPECT(h, document_result.ok());
auto history_result = DocumentHistory::create(document_result.value(), 5);
PP_EXPECT(h, history_result.ok());
auto history = history_result.value();
auto first_branch = history.current();
PP_EXPECT(h, first_branch.add_layer("Branch A").ok());
PP_EXPECT(h, history.apply(first_branch).ok());
auto second_branch = history.current();
PP_EXPECT(h, second_branch.add_layer("Branch B").ok());
PP_EXPECT(h, history.apply(second_branch).ok());
PP_EXPECT(h, history.undo().ok());
PP_EXPECT(h, history.can_redo());
auto replacement_branch = history.current();
PP_EXPECT(h, replacement_branch.add_layer("Replacement").ok());
PP_EXPECT(h, history.apply(replacement_branch).ok());
PP_EXPECT(h, !history.can_redo());
PP_EXPECT(h, history.current().layers().size() == 3U);
PP_EXPECT(h, history.current().layers()[2].name == std::string_view("Replacement"));
}
void bounds_document_history_capacity(pp::tests::Harness& h)
{
auto document_result = CanvasDocument::create(
DocumentConfig { .width = 64, .height = 64, .layer_count = 1 });
PP_EXPECT(h, document_result.ok());
auto too_small = DocumentHistory::create(document_result.value(), 1);
auto too_large = DocumentHistory::create(document_result.value(), max_document_history_entries + 1U);
PP_EXPECT(h, !too_small.ok());
PP_EXPECT(h, too_small.status().code == StatusCode::invalid_argument);
PP_EXPECT(h, !too_large.ok());
PP_EXPECT(h, too_large.status().code == StatusCode::out_of_range);
auto history_result = DocumentHistory::create(document_result.value(), 3);
PP_EXPECT(h, history_result.ok());
auto history = history_result.value();
for (std::uint32_t i = 0; i < 5U; ++i) {
auto next = history.current();
const auto added = next.add_frame(100U + i);
PP_EXPECT(h, added.ok());
PP_EXPECT(h, history.apply(next).ok());
PP_EXPECT(h, history.size() <= 3U);
}
PP_EXPECT(h, history.size() == 3U);
PP_EXPECT(h, history.current_index() == 2U);
PP_EXPECT(h, history.current().frames().size() == 6U);
PP_EXPECT(h, history.undo().ok());
PP_EXPECT(h, history.current().frames().size() == 5U);
PP_EXPECT(h, history.undo().ok());
PP_EXPECT(h, history.current().frames().size() == 4U);
const auto undo_evicted_entry = history.undo();
PP_EXPECT(h, !undo_evicted_entry.ok());
PP_EXPECT(h, undo_evicted_entry.code == StatusCode::out_of_range);
}
}
int main()
@@ -156,5 +269,8 @@ int main()
harness.run("moves_layers_and_preserves_active_layer_identity", moves_layers_and_preserves_active_layer_identity);
harness.run("manages_animation_frames_and_duration", manages_animation_frames_and_duration);
harness.run("rejects_invalid_animation_frame_operations", rejects_invalid_animation_frame_operations);
harness.run("records_document_history_and_restores_snapshots", records_document_history_and_restores_snapshots);
harness.run("applying_after_undo_discards_redo_branch", applying_after_undo_discards_redo_branch);
harness.run("bounds_document_history_capacity", bounds_document_history_capacity);
return harness.finish();
}