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/CustomElementReactionQueue.h" | 5 #include "core/dom/custom/CustomElementReactionQueue.h" |
6 | 6 |
7 #include "core/dom/custom/CustomElementReaction.h" | 7 #include "core/dom/custom/CustomElementReaction.h" |
| 8 #include "core/dom/custom/CustomElementReactionTestHelpers.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #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 Command : public GarbageCollectedFinalized<Command> { | |
16 WTF_MAKE_NONCOPYABLE(Command); | |
17 public: | |
18 Command() { } | |
19 virtual ~Command() { } | |
20 DEFINE_INLINE_VIRTUAL_TRACE() { } | |
21 virtual void run(Element*) = 0; | |
22 }; | |
23 | |
24 class Log : public Command { | |
25 WTF_MAKE_NONCOPYABLE(Log); | |
26 public: | |
27 Log(char what, std::vector<char>& where) : m_what(what), m_where(where) { } | |
28 virtual ~Log() { } | |
29 void run(Element*) override { m_where.push_back(m_what); } | |
30 private: | |
31 char m_what; | |
32 std::vector<char>& m_where; | |
33 }; | |
34 | |
35 class Recurse : public Command { | |
36 WTF_MAKE_NONCOPYABLE(Recurse); | |
37 public: | |
38 Recurse(CustomElementReactionQueue* queue) : m_queue(queue) { } | |
39 virtual ~Recurse() { } | |
40 DEFINE_INLINE_VIRTUAL_TRACE() | |
41 { | |
42 Command::trace(visitor); | |
43 visitor->trace(m_queue); | |
44 } | |
45 void run(Element* element) override { m_queue->invokeReactions(element); } | |
46 private: | |
47 Member<CustomElementReactionQueue> m_queue; | |
48 }; | |
49 | |
50 class Enqueue : public Command { | |
51 WTF_MAKE_NONCOPYABLE(Enqueue); | |
52 public: | |
53 Enqueue(CustomElementReactionQueue* queue, CustomElementReaction* reaction) | |
54 : m_queue(queue) | |
55 , m_reaction(reaction) | |
56 { | |
57 } | |
58 virtual ~Enqueue() { } | |
59 DEFINE_INLINE_VIRTUAL_TRACE() | |
60 { | |
61 Command::trace(visitor); | |
62 visitor->trace(m_queue); | |
63 visitor->trace(m_reaction); | |
64 } | |
65 void run(Element*) override | |
66 { | |
67 m_queue->add(m_reaction); | |
68 } | |
69 private: | |
70 Member<CustomElementReactionQueue> m_queue; | |
71 Member<CustomElementReaction> m_reaction; | |
72 }; | |
73 | |
74 class TestReaction : public CustomElementReaction { | |
75 WTF_MAKE_NONCOPYABLE(TestReaction); | |
76 public: | |
77 TestReaction(std::initializer_list<Command*> commands) | |
78 { | |
79 // TODO(dominicc): Simply pass the initializer list when | |
80 // HeapVector supports initializer lists like Vector. | |
81 for (auto& command : commands) | |
82 m_commands.append(command); | |
83 } | |
84 virtual ~TestReaction() = default; | |
85 DEFINE_INLINE_VIRTUAL_TRACE() | |
86 { | |
87 CustomElementReaction::trace(visitor); | |
88 visitor->trace(m_commands); | |
89 } | |
90 void invoke(Element* element) override | |
91 { | |
92 for (auto& command : m_commands) | |
93 command->run(element); | |
94 } | |
95 | |
96 private: | |
97 HeapVector<Member<Command>> m_commands; | |
98 }; | |
99 | |
100 TEST(CustomElementReactionQueueTest, invokeReactions_one) | 15 TEST(CustomElementReactionQueueTest, invokeReactions_one) |
101 { | 16 { |
102 std::vector<char> log; | 17 std::vector<char> log; |
103 CustomElementReactionQueue* queue = new CustomElementReactionQueue(); | 18 CustomElementReactionQueue* queue = new CustomElementReactionQueue(); |
104 queue->add(new TestReaction({new Log('a', log)})); | 19 queue->add(new TestReaction({new Log('a', log)})); |
105 queue->invokeReactions(nullptr); | 20 queue->invokeReactions(nullptr); |
106 EXPECT_EQ(log, std::vector<char>({'a'})) | 21 EXPECT_EQ(log, std::vector<char>({'a'})) |
107 << "the reaction should have been invoked"; | 22 << "the reaction should have been invoked"; |
108 } | 23 } |
109 | 24 |
(...skipping 27 matching lines...) Expand all Loading... |
137 new Enqueue(queue, second), | 52 new Enqueue(queue, second), |
138 new Recurse(queue)}); // Non-empty recursion | 53 new Recurse(queue)}); // Non-empty recursion |
139 | 54 |
140 queue->add(first); | 55 queue->add(first); |
141 queue->invokeReactions(nullptr); | 56 queue->invokeReactions(nullptr); |
142 EXPECT_EQ(log, std::vector<char>({'a', 'b', 'c'})) | 57 EXPECT_EQ(log, std::vector<char>({'a', 'b', 'c'})) |
143 << "the reactions should have been invoked"; | 58 << "the reactions should have been invoked"; |
144 } | 59 } |
145 | 60 |
146 } // namespace blink | 61 } // namespace blink |
OLD | NEW |