1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
// Copyright 2016 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/dom/custom/CustomElementReaction.h"
#include "core/dom/custom/CustomElementReactionQueue.h"
#include "core/dom/custom/CustomElementReactionStack.h"
#include "platform/heap/Handle.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "wtf/Functional.h"
#include "wtf/Noncopyable.h"
#include <initializer_list>
#include <memory>
#include <vector>
namespace blink {
class Element;
class Command : public GarbageCollectedFinalized<Command> {
WTF_MAKE_NONCOPYABLE(Command);
public:
Command() = default;
virtual ~Command() = default;
DEFINE_INLINE_VIRTUAL_TRACE() {}
virtual void run(Element*) = 0;
};
class Call : public Command {
WTF_MAKE_NONCOPYABLE(Call);
public:
using Callback = WTF::Function<void(Element*)>;
Call(std::unique_ptr<Callback> callback) : m_callback(std::move(callback)) {}
~Call() override = default;
void run(Element* element) override { m_callback->operator()(element); }
private:
std::unique_ptr<Callback> m_callback;
};
class Unreached : public Command {
WTF_MAKE_NONCOPYABLE(Unreached);
public:
Unreached(const char* message) : m_message(message) {}
~Unreached() override = default;
void run(Element*) override { EXPECT_TRUE(false) << m_message; }
private:
const char* m_message;
};
class Log : public Command {
WTF_MAKE_NONCOPYABLE(Log);
public:
Log(char what, std::vector<char>& where) : m_what(what), m_where(where) {}
~Log() override = default;
void run(Element*) override { m_where.push_back(m_what); }
private:
char m_what;
std::vector<char>& m_where;
};
class Recurse : public Command {
WTF_MAKE_NONCOPYABLE(Recurse);
public:
Recurse(CustomElementReactionQueue* queue) : m_queue(queue) {}
~Recurse() override = default;
DEFINE_INLINE_VIRTUAL_TRACE() {
Command::trace(visitor);
visitor->trace(m_queue);
}
void run(Element* element) override { m_queue->invokeReactions(element); }
private:
Member<CustomElementReactionQueue> m_queue;
};
class Enqueue : public Command {
WTF_MAKE_NONCOPYABLE(Enqueue);
public:
Enqueue(CustomElementReactionQueue* queue, CustomElementReaction* reaction)
: m_queue(queue), m_reaction(reaction) {}
~Enqueue() override = default;
DEFINE_INLINE_VIRTUAL_TRACE() {
Command::trace(visitor);
visitor->trace(m_queue);
visitor->trace(m_reaction);
}
void run(Element*) override { m_queue->add(m_reaction); }
private:
Member<CustomElementReactionQueue> m_queue;
Member<CustomElementReaction> m_reaction;
};
class TestReaction : public CustomElementReaction {
WTF_MAKE_NONCOPYABLE(TestReaction);
public:
TestReaction(std::initializer_list<Command*> commands)
: CustomElementReaction(nullptr) {
// TODO(dominicc): Simply pass the initializer list when
// HeapVector supports initializer lists like Vector.
for (auto& command : commands)
m_commands.push_back(command);
}
~TestReaction() override = default;
DEFINE_INLINE_VIRTUAL_TRACE() {
CustomElementReaction::trace(visitor);
visitor->trace(m_commands);
}
void invoke(Element* element) override {
for (auto& command : m_commands)
command->run(element);
}
private:
HeapVector<Member<Command>> m_commands;
};
class ResetCustomElementReactionStackForTest final {
STACK_ALLOCATED();
WTF_MAKE_NONCOPYABLE(ResetCustomElementReactionStackForTest);
public:
ResetCustomElementReactionStackForTest()
: m_stack(new CustomElementReactionStack),
m_oldStack(
CustomElementReactionStackTestSupport::setCurrentForTest(m_stack)) {
}
~ResetCustomElementReactionStackForTest() {
CustomElementReactionStackTestSupport::setCurrentForTest(m_oldStack);
}
CustomElementReactionStack& stack() { return *m_stack; }
private:
Member<CustomElementReactionStack> m_stack;
Member<CustomElementReactionStack> m_oldStack;
};
} // namespace blink
|