summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/html/custom/custom_element_reaction_test_helpers.h
blob: 6450df0d6ec264add9282abd973c0a32c230ced6 (plain)
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
152
153
154
// 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_HTML_CUSTOM_CUSTOM_ELEMENT_REACTION_TEST_HELPERS_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_HTML_CUSTOM_CUSTOM_ELEMENT_REACTION_TEST_HELPERS_H_

#include "third_party/blink/renderer/core/html/custom/custom_element_reaction.h"

#include <initializer_list>
#include <memory>

#include "base/macros.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/html/custom/custom_element_reaction_queue.h"
#include "third_party/blink/renderer/core/html/custom/custom_element_reaction_stack.h"
#include "third_party/blink/renderer/core/html/custom/custom_element_test_helpers.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"

namespace blink {

class Element;

class Command : public GarbageCollected<Command> {
 public:
  Command() = default;
  virtual ~Command() = default;
  virtual void Trace(Visitor* visitor) {}
  virtual void Run(Element&) = 0;

  DISALLOW_COPY_AND_ASSIGN(Command);
};

class Call : public Command {
 public:
  using Callback = base::OnceCallback<void(Element&)>;
  Call(Callback callback) : callback_(std::move(callback)) {}
  ~Call() override = default;
  void Run(Element& element) override { std::move(callback_).Run(element); }

 private:
  Callback callback_;

  DISALLOW_COPY_AND_ASSIGN(Call);
};

class Unreached : public Command {
 public:
  Unreached(const char* message) : message_(message) {}
  ~Unreached() override = default;
  void Run(Element&) override { EXPECT_TRUE(false) << message_; }

 private:
  const char* message_;

  DISALLOW_COPY_AND_ASSIGN(Unreached);
};

class Log : public Command {
 public:
  Log(char what, Vector<char>& where) : what_(what), where_(where) {}
  ~Log() override = default;
  void Run(Element&) override { where_.push_back(what_); }

 private:
  char what_;
  Vector<char>& where_;

  DISALLOW_COPY_AND_ASSIGN(Log);
};

class Recurse : public Command {
 public:
  Recurse(CustomElementReactionQueue* queue) : queue_(queue) {}
  ~Recurse() override = default;
  void Trace(Visitor* visitor) override {
    Command::Trace(visitor);
    visitor->Trace(queue_);
  }
  void Run(Element& element) override { queue_->InvokeReactions(element); }

 private:
  Member<CustomElementReactionQueue> queue_;

  DISALLOW_COPY_AND_ASSIGN(Recurse);
};

class Enqueue : public Command {
 public:
  Enqueue(CustomElementReactionQueue* queue, CustomElementReaction* reaction)
      : queue_(queue), reaction_(reaction) {}
  ~Enqueue() override = default;
  void Trace(Visitor* visitor) override {
    Command::Trace(visitor);
    visitor->Trace(queue_);
    visitor->Trace(reaction_);
  }
  void Run(Element&) override { queue_->Add(*reaction_); }

 private:
  Member<CustomElementReactionQueue> queue_;
  Member<CustomElementReaction> reaction_;

  DISALLOW_COPY_AND_ASSIGN(Enqueue);
};

class TestReaction : public CustomElementReaction {
 public:
  TestReaction(HeapVector<Member<Command>>* commands)
      : CustomElementReaction(
            *MakeGarbageCollected<TestCustomElementDefinition>(
                CustomElementDescriptor("mock-element", "mock-element"))),
        commands_(commands) {}
  ~TestReaction() override = default;
  void Trace(Visitor* visitor) override {
    CustomElementReaction::Trace(visitor);
    visitor->Trace(commands_);
  }
  void Invoke(Element& element) override {
    for (auto& command : *commands_)
      command->Run(element);
  }

 private:
  Member<HeapVector<Member<Command>>> commands_;

  DISALLOW_COPY_AND_ASSIGN(TestReaction);
};

class ResetCustomElementReactionStackForTest final {
  STACK_ALLOCATED();
 public:
  ResetCustomElementReactionStackForTest()
      : stack_(MakeGarbageCollected<CustomElementReactionStack>()),
        old_stack_(
            CustomElementReactionStackTestSupport::SetCurrentForTest(stack_)) {}

  ~ResetCustomElementReactionStackForTest() {
    CustomElementReactionStackTestSupport::SetCurrentForTest(old_stack_);
  }

  CustomElementReactionStack& Stack() { return *stack_; }

 private:
  CustomElementReactionStack* stack_;
  CustomElementReactionStack* old_stack_;

  DISALLOW_COPY_AND_ASSIGN(ResetCustomElementReactionStackForTest);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_HTML_CUSTOM_CUSTOM_ELEMENT_REACTION_TEST_HELPERS_H_