OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "core/dom/custom/CustomElementReaction.h" |
| 6 |
5 #include "core/dom/custom/CustomElementReactionQueue.h" | 7 #include "core/dom/custom/CustomElementReactionQueue.h" |
6 | 8 #include "platform/heap/Handle.h" |
7 #include "core/dom/custom/CustomElementReaction.h" | 9 #include "wtf/Noncopyable.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 #include "wtf/text/AtomicString.h" | |
10 #include <initializer_list> | 10 #include <initializer_list> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 namespace blink { | 13 namespace blink { |
14 | 14 |
| 15 class Element; |
| 16 |
15 class Command : public GarbageCollectedFinalized<Command> { | 17 class Command : public GarbageCollectedFinalized<Command> { |
16 WTF_MAKE_NONCOPYABLE(Command); | 18 WTF_MAKE_NONCOPYABLE(Command); |
17 public: | 19 public: |
18 Command() { } | 20 Command() { } |
19 virtual ~Command() { } | 21 virtual ~Command() { } |
20 DEFINE_INLINE_VIRTUAL_TRACE() { } | 22 DEFINE_INLINE_VIRTUAL_TRACE() { } |
21 virtual void run(Element*) = 0; | 23 virtual void run(Element*) = 0; |
22 }; | 24 }; |
23 | 25 |
24 class Log : public Command { | 26 class Log : public Command { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 void invoke(Element* element) override | 92 void invoke(Element* element) override |
91 { | 93 { |
92 for (auto& command : m_commands) | 94 for (auto& command : m_commands) |
93 command->run(element); | 95 command->run(element); |
94 } | 96 } |
95 | 97 |
96 private: | 98 private: |
97 HeapVector<Member<Command>> m_commands; | 99 HeapVector<Member<Command>> m_commands; |
98 }; | 100 }; |
99 | 101 |
100 TEST(CustomElementReactionQueueTest, invokeReactions_one) | |
101 { | |
102 std::vector<char> log; | |
103 CustomElementReactionQueue* queue = new CustomElementReactionQueue(); | |
104 queue->add(new TestReaction({new Log('a', log)})); | |
105 queue->invokeReactions(nullptr); | |
106 EXPECT_EQ(log, std::vector<char>({'a'})) | |
107 << "the reaction should have been invoked"; | |
108 } | |
109 | |
110 TEST(CustomElementReactionQueueTest, invokeReactions_many) | |
111 { | |
112 std::vector<char> log; | |
113 CustomElementReactionQueue* queue = new CustomElementReactionQueue(); | |
114 queue->add(new TestReaction({new Log('a', log)})); | |
115 queue->add(new TestReaction({new Log('b', log)})); | |
116 queue->add(new TestReaction({new Log('c', log)})); | |
117 queue->invokeReactions(nullptr); | |
118 EXPECT_EQ(log, std::vector<char>({'a', 'b', 'c'})) | |
119 << "the reaction should have been invoked"; | |
120 } | |
121 | |
122 TEST(CustomElementReactionQueueTest, invokeReactions_recursive) | |
123 { | |
124 std::vector<char> log; | |
125 CustomElementReactionQueue* queue = new CustomElementReactionQueue(); | |
126 | |
127 CustomElementReaction* third = new TestReaction({ | |
128 new Log('c', log), | |
129 new Recurse(queue)}); // "Empty" recursion | |
130 | |
131 CustomElementReaction* second = new TestReaction({ | |
132 new Log('b', log), | |
133 new Enqueue(queue, third)}); // Unwinds one level of recursion | |
134 | |
135 CustomElementReaction* first = new TestReaction({ | |
136 new Log('a', log), | |
137 new Enqueue(queue, second), | |
138 new Recurse(queue)}); // Non-empty recursion | |
139 | |
140 queue->add(first); | |
141 queue->invokeReactions(nullptr); | |
142 EXPECT_EQ(log, std::vector<char>({'a', 'b', 'c'})) | |
143 << "the reactions should have been invoked"; | |
144 } | |
145 | |
146 } // namespace blink | 102 } // namespace blink |
OLD | NEW |