Commit d5b6670e authored by Peter Marshall's avatar Peter Marshall Committed by Commit Bot

devtools: Add unit tests for InspectorHistory

Change-Id: I06732b384f760a235f42078899e54c80123d4e4e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2369312Reviewed-by: default avatarMike West <mkwst@chromium.org>
Reviewed-by: default avatarSimon Zünd <szuend@chromium.org>
Commit-Queue: Peter Marshall <petermarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#803093}
parent 73f65b93
...@@ -1153,6 +1153,7 @@ source_set("unit_tests") { ...@@ -1153,6 +1153,7 @@ source_set("unit_tests") {
"input/scroll_snap_test.cc", "input/scroll_snap_test.cc",
"input/touch_action_test.cc", "input/touch_action_test.cc",
"input/touch_event_manager_test.cc", "input/touch_event_manager_test.cc",
"inspector/inspector_history_test.cc",
"inspector/inspector_session_state_test.cc", "inspector/inspector_session_state_test.cc",
"inspector/inspector_style_resolver_test.cc", "inspector/inspector_style_resolver_test.cc",
"inspector/main_thread_debugger_test.cc", "inspector/main_thread_debugger_test.cc",
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/heap/handle.h" #include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h" #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h" #include "third_party/blink/renderer/platform/wtf/vector.h"
...@@ -41,9 +42,10 @@ namespace blink { ...@@ -41,9 +42,10 @@ namespace blink {
class ExceptionState; class ExceptionState;
class InspectorHistory final : public GarbageCollected<InspectorHistory> { class CORE_EXPORT InspectorHistory final
: public GarbageCollected<InspectorHistory> {
public: public:
class Action : public GarbageCollected<Action> { class CORE_EXPORT Action : public GarbageCollected<Action> {
public: public:
explicit Action(const String& name); explicit Action(const String& name);
virtual ~Action(); virtual ~Action();
......
// Copyright 2020 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 "third_party/blink/renderer/core/inspector/inspector_history.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace blink {
class TestAction : public InspectorHistory::Action {
public:
TestAction() : InspectorHistory::Action("TestAction") {}
bool Perform(ExceptionState&) override {
performed++;
return true;
}
bool Undo(ExceptionState&) override {
undone++;
return true;
}
bool Redo(ExceptionState&) override {
redone++;
return true;
}
int performed = 0;
int undone = 0;
int redone = 0;
};
class PerformFailsAction final : public TestAction {
public:
bool Perform(ExceptionState&) override { return false; }
};
class MergeableAction : public TestAction {
public:
explicit MergeableAction(String token) { this->token = token; }
String MergeId() override {
return "mergeMe!"; // Everything can merge.
}
void Merge(Action* other) override {
this->token = static_cast<MergeableAction*>(other)->token;
}
String token;
};
// Becomes a no-op after merge.
class NoOpMergeableAction : public MergeableAction {
public:
explicit NoOpMergeableAction(String token) : MergeableAction(token) {}
void Merge(Action* other) override {
merged = true;
this->token = static_cast<MergeableAction*>(other)->token;
}
bool IsNoop() override { return merged; }
bool merged = false;
};
TEST(InspectorHistoryTest, UndoEmptyHistorySucceeds) {
InspectorHistory* history = MakeGarbageCollected<InspectorHistory>();
DummyExceptionStateForTesting exception_state;
ASSERT_TRUE(history->Undo(exception_state));
}
TEST(InspectorHistoryTest, UndoUndoableMarkerSucceeds) {
InspectorHistory* history = MakeGarbageCollected<InspectorHistory>();
history->MarkUndoableState();
DummyExceptionStateForTesting exception_state;
ASSERT_TRUE(history->Undo(exception_state));
}
TEST(InspectorHistoryTest, PerformedActionIsPerformed) {
InspectorHistory* history = MakeGarbageCollected<InspectorHistory>();
TestAction* action = MakeGarbageCollected<TestAction>();
DummyExceptionStateForTesting exception_state;
ASSERT_TRUE(history->Perform(action, exception_state));
ASSERT_EQ(action->performed, 1);
ASSERT_EQ(action->undone, 0);
ASSERT_EQ(action->redone, 0);
}
TEST(InspectorHistoryTest, UndoPerformedAction) {
InspectorHistory* history = MakeGarbageCollected<InspectorHistory>();
TestAction* action = MakeGarbageCollected<TestAction>();
DummyExceptionStateForTesting exception_state;
history->Perform(action, exception_state);
history->Undo(exception_state);
ASSERT_EQ(action->performed, 1);
ASSERT_EQ(action->undone, 1);
ASSERT_EQ(action->redone, 0);
}
TEST(InspectorHistoryTest, RedoUndoneAction) {
InspectorHistory* history = MakeGarbageCollected<InspectorHistory>();
TestAction* action = MakeGarbageCollected<TestAction>();
DummyExceptionStateForTesting exception_state;
history->Perform(action, exception_state);
history->Undo(exception_state);
history->Redo(exception_state);
ASSERT_EQ(action->performed, 1);
ASSERT_EQ(action->undone, 1);
ASSERT_EQ(action->redone, 1);
}
TEST(InspectorHistoryTest, TwoActionsBothAreUndone) {
InspectorHistory* history = MakeGarbageCollected<InspectorHistory>();
TestAction* action = MakeGarbageCollected<TestAction>();
TestAction* action2 = MakeGarbageCollected<TestAction>();
DummyExceptionStateForTesting exception_state;
history->MarkUndoableState();
history->Perform(action, exception_state);
history->Perform(action2, exception_state);
history->Undo(exception_state);
ASSERT_EQ(action->performed, 1);
ASSERT_EQ(action->undone, 1);
ASSERT_EQ(action->redone, 0);
ASSERT_EQ(action2->performed, 1);
ASSERT_EQ(action2->undone, 1);
ASSERT_EQ(action2->redone, 0);
}
TEST(InspectorHistoryTest, TwoActionsBothAreRedone) {
InspectorHistory* history = MakeGarbageCollected<InspectorHistory>();
TestAction* action = MakeGarbageCollected<TestAction>();
TestAction* action2 = MakeGarbageCollected<TestAction>();
DummyExceptionStateForTesting exception_state;
history->MarkUndoableState();
history->Perform(action, exception_state);
history->Perform(action2, exception_state);
history->Undo(exception_state);
history->Redo(exception_state);
ASSERT_EQ(action->performed, 1);
ASSERT_EQ(action->undone, 1);
ASSERT_EQ(action->redone, 1);
ASSERT_EQ(action2->performed, 1);
ASSERT_EQ(action2->undone, 1);
ASSERT_EQ(action2->redone, 1);
}
TEST(InspectorHistoryTest, PerformFails) {
InspectorHistory* history = MakeGarbageCollected<InspectorHistory>();
PerformFailsAction* action = MakeGarbageCollected<PerformFailsAction>();
DummyExceptionStateForTesting exception_state;
ASSERT_FALSE(history->Perform(action, exception_state));
ASSERT_TRUE(history->Undo(exception_state));
ASSERT_TRUE(history->Redo(exception_state));
ASSERT_EQ(action->undone, 0);
ASSERT_EQ(action->redone, 0);
}
TEST(InspectorHistoryTest, ResetClearsPerformedAction) {
InspectorHistory* history = MakeGarbageCollected<InspectorHistory>();
TestAction* action = MakeGarbageCollected<TestAction>();
DummyExceptionStateForTesting exception_state;
ASSERT_TRUE(history->Perform(action, exception_state));
history->Reset();
ASSERT_TRUE(history->Undo(exception_state));
ASSERT_EQ(action->performed, 1);
ASSERT_EQ(action->undone, 0);
ASSERT_EQ(action->redone, 0);
}
TEST(InspectorHistoryTest, MergeableActionIsNotStored) {
InspectorHistory* history = MakeGarbageCollected<InspectorHistory>();
MergeableAction* action = MakeGarbageCollected<MergeableAction>("A");
MergeableAction* action2 = MakeGarbageCollected<MergeableAction>("B");
DummyExceptionStateForTesting exception_state;
ASSERT_TRUE(history->Perform(action, exception_state));
ASSERT_TRUE(history->Perform(action2, exception_state));
ASSERT_EQ(action->token, "B"); // Merge happened successfully.
ASSERT_TRUE(history->Undo(exception_state));
ASSERT_EQ(action->performed, 1);
ASSERT_EQ(action->undone, 1);
ASSERT_EQ(action2->performed, 1);
// The second action was never stored after the merge.
ASSERT_EQ(action2->undone, 0);
}
TEST(InspectorHistoryTest, NoOpMergeableActionIsCleared) {
InspectorHistory* history = MakeGarbageCollected<InspectorHistory>();
NoOpMergeableAction* action = MakeGarbageCollected<NoOpMergeableAction>("A");
NoOpMergeableAction* action2 = MakeGarbageCollected<NoOpMergeableAction>("B");
DummyExceptionStateForTesting exception_state;
ASSERT_TRUE(history->Perform(action, exception_state));
// This will cause action to become a no-op.
ASSERT_TRUE(history->Perform(action2, exception_state));
ASSERT_TRUE(history->Undo(exception_state));
ASSERT_EQ(action->performed, 1);
// The first action was cleared after merge because it became a no-op.
ASSERT_EQ(action->undone, 0);
ASSERT_EQ(action2->performed, 1);
ASSERT_EQ(action2->undone, 0);
}
TEST(InspectorHistoryTest, RedoEmptyHistorySucceeds) {
InspectorHistory* history = MakeGarbageCollected<InspectorHistory>();
DummyExceptionStateForTesting exception_state;
ASSERT_TRUE(history->Redo(exception_state));
}
} // namespace blink
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment