| 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/CustomElementReactionStack.h" | 5 #include "core/dom/custom/CustomElementReactionStack.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 "core/dom/custom/CustomElementReactionTestHelpers.h" |
| 9 #include "core/dom/custom/CustomElementTestHelpers.h" | 9 #include "core/dom/custom/CustomElementTestHelpers.h" |
| 10 #include "core/html/HTMLDocument.h" | 10 #include "core/html/HTMLDocument.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "wtf/text/AtomicString.h" | 12 #include "wtf/text/AtomicString.h" |
| 13 #include <initializer_list> | 13 #include <initializer_list> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 namespace blink { | 16 namespace blink { |
| 17 | 17 |
| 18 TEST(CustomElementReactionStackTest, one) | 18 TEST(CustomElementReactionStackTest, one) |
| 19 { | 19 { |
| 20 std::vector<char> log; | 20 std::vector<char> log; |
| 21 | 21 |
| 22 CustomElementReactionStack* stack = new CustomElementReactionStack(); | 22 CustomElementReactionStack* stack = new CustomElementReactionStack(); |
| 23 stack->push(); | 23 stack->push(); |
| 24 stack->enqueue(CreateElement("a"), new TestReaction({new Log('a', log)})); | 24 stack->enqueueToCurrentQueue(CreateElement("a"), new TestReaction({new Log('
a', log)})); |
| 25 stack->popInvokingReactions(); | 25 stack->popInvokingReactions(); |
| 26 | 26 |
| 27 EXPECT_EQ(log, std::vector<char>({'a'})) | 27 EXPECT_EQ(log, std::vector<char>({'a'})) |
| 28 << "popping the reaction stack should run reactions"; | 28 << "popping the reaction stack should run reactions"; |
| 29 } | 29 } |
| 30 | 30 |
| 31 TEST(CustomElementReactionStackTest, multipleElements) | 31 TEST(CustomElementReactionStackTest, multipleElements) |
| 32 { | 32 { |
| 33 std::vector<char> log; | 33 std::vector<char> log; |
| 34 | 34 |
| 35 CustomElementReactionStack* stack = new CustomElementReactionStack(); | 35 CustomElementReactionStack* stack = new CustomElementReactionStack(); |
| 36 stack->push(); | 36 stack->push(); |
| 37 stack->enqueue(CreateElement("a"), new TestReaction({new Log('a', log)})); | 37 stack->enqueueToCurrentQueue(CreateElement("a"), new TestReaction({new Log('
a', log)})); |
| 38 stack->enqueue(CreateElement("a"), new TestReaction({new Log('b', log)})); | 38 stack->enqueueToCurrentQueue(CreateElement("a"), new TestReaction({new Log('
b', log)})); |
| 39 stack->popInvokingReactions(); | 39 stack->popInvokingReactions(); |
| 40 | 40 |
| 41 EXPECT_EQ(log, std::vector<char>({'a', 'b'})) | 41 EXPECT_EQ(log, std::vector<char>({'a', 'b'})) |
| 42 << "reactions should run in the order the elements queued"; | 42 << "reactions should run in the order the elements queued"; |
| 43 } | 43 } |
| 44 | 44 |
| 45 TEST(CustomElementReactionStackTest, popTopEmpty) | 45 TEST(CustomElementReactionStackTest, popTopEmpty) |
| 46 { | 46 { |
| 47 std::vector<char> log; | 47 std::vector<char> log; |
| 48 | 48 |
| 49 CustomElementReactionStack* stack = new CustomElementReactionStack(); | 49 CustomElementReactionStack* stack = new CustomElementReactionStack(); |
| 50 stack->push(); | 50 stack->push(); |
| 51 stack->enqueue(CreateElement("a"), new TestReaction({new Log('a', log)})); | 51 stack->enqueueToCurrentQueue(CreateElement("a"), new TestReaction({new Log('
a', log)})); |
| 52 stack->push(); | 52 stack->push(); |
| 53 stack->popInvokingReactions(); | 53 stack->popInvokingReactions(); |
| 54 | 54 |
| 55 EXPECT_EQ(log, std::vector<char>()) | 55 EXPECT_EQ(log, std::vector<char>()) |
| 56 << "popping the empty top-of-stack should not run any reactions"; | 56 << "popping the empty top-of-stack should not run any reactions"; |
| 57 } | 57 } |
| 58 | 58 |
| 59 TEST(CustomElementReactionStackTest, popTop) | 59 TEST(CustomElementReactionStackTest, popTop) |
| 60 { | 60 { |
| 61 std::vector<char> log; | 61 std::vector<char> log; |
| 62 | 62 |
| 63 CustomElementReactionStack* stack = new CustomElementReactionStack(); | 63 CustomElementReactionStack* stack = new CustomElementReactionStack(); |
| 64 stack->push(); | 64 stack->push(); |
| 65 stack->enqueue(CreateElement("a"), new TestReaction({new Log('a', log)})); | 65 stack->enqueueToCurrentQueue(CreateElement("a"), new TestReaction({new Log('
a', log)})); |
| 66 stack->push(); | 66 stack->push(); |
| 67 stack->enqueue(CreateElement("a"), new TestReaction({new Log('b', log)})); | 67 stack->enqueueToCurrentQueue(CreateElement("a"), new TestReaction({new Log('
b', log)})); |
| 68 stack->popInvokingReactions(); | 68 stack->popInvokingReactions(); |
| 69 | 69 |
| 70 EXPECT_EQ(log, std::vector<char>({'b'})) | 70 EXPECT_EQ(log, std::vector<char>({'b'})) |
| 71 << "popping the top-of-stack should only run top-of-stack reactions"; | 71 << "popping the top-of-stack should only run top-of-stack reactions"; |
| 72 } | 72 } |
| 73 | 73 |
| 74 TEST(CustomElementReactionStackTest, requeueingDoesNotReorderElements) | 74 TEST(CustomElementReactionStackTest, requeueingDoesNotReorderElements) |
| 75 { | 75 { |
| 76 std::vector<char> log; | 76 std::vector<char> log; |
| 77 | 77 |
| 78 Element* element = CreateElement("a"); | 78 Element* element = CreateElement("a"); |
| 79 | 79 |
| 80 CustomElementReactionStack* stack = new CustomElementReactionStack(); | 80 CustomElementReactionStack* stack = new CustomElementReactionStack(); |
| 81 stack->push(); | 81 stack->push(); |
| 82 stack->enqueue(element, new TestReaction({new Log('a', log)})); | 82 stack->enqueueToCurrentQueue(element, new TestReaction({new Log('a', log)}))
; |
| 83 stack->enqueue(CreateElement("a"), new TestReaction({new Log('z', log)})); | 83 stack->enqueueToCurrentQueue(CreateElement("a"), new TestReaction({new Log('
z', log)})); |
| 84 stack->enqueue(element, new TestReaction({new Log('b', log)})); | 84 stack->enqueueToCurrentQueue(element, new TestReaction({new Log('b', log)}))
; |
| 85 stack->popInvokingReactions(); | 85 stack->popInvokingReactions(); |
| 86 | 86 |
| 87 EXPECT_EQ(log, std::vector<char>({'a', 'b', 'z'})) | 87 EXPECT_EQ(log, std::vector<char>({'a', 'b', 'z'})) |
| 88 << "reactions should run together in the order elements were queued"; | 88 << "reactions should run together in the order elements were queued"; |
| 89 } | 89 } |
| 90 | 90 |
| 91 TEST(CustomElementReactionStackTest, oneReactionQueuePerElement) | 91 TEST(CustomElementReactionStackTest, oneReactionQueuePerElement) |
| 92 { | 92 { |
| 93 std::vector<char> log; | 93 std::vector<char> log; |
| 94 | 94 |
| 95 Element* element = CreateElement("a"); | 95 Element* element = CreateElement("a"); |
| 96 | 96 |
| 97 CustomElementReactionStack* stack = new CustomElementReactionStack(); | 97 CustomElementReactionStack* stack = new CustomElementReactionStack(); |
| 98 stack->push(); | 98 stack->push(); |
| 99 stack->enqueue(element, new TestReaction({new Log('a', log)})); | 99 stack->enqueueToCurrentQueue(element, new TestReaction({new Log('a', log)}))
; |
| 100 stack->enqueue(CreateElement("a"), new TestReaction({new Log('z', log)})); | 100 stack->enqueueToCurrentQueue(CreateElement("a"), new TestReaction({new Log('
z', log)})); |
| 101 stack->push(); | 101 stack->push(); |
| 102 stack->enqueue(CreateElement("a"), new TestReaction({new Log('y', log)})); | 102 stack->enqueueToCurrentQueue(CreateElement("a"), new TestReaction({new Log('
y', log)})); |
| 103 stack->enqueue(element, new TestReaction({new Log('b', log)})); | 103 stack->enqueueToCurrentQueue(element, new TestReaction({new Log('b', log)}))
; |
| 104 stack->popInvokingReactions(); | 104 stack->popInvokingReactions(); |
| 105 | 105 |
| 106 EXPECT_EQ(log, std::vector<char>({'y', 'a', 'b'})) | 106 EXPECT_EQ(log, std::vector<char>({'y', 'a', 'b'})) |
| 107 << "reactions should run together in the order elements were queued"; | 107 << "reactions should run together in the order elements were queued"; |
| 108 | 108 |
| 109 log.clear(); | 109 log.clear(); |
| 110 stack->popInvokingReactions(); | 110 stack->popInvokingReactions(); |
| 111 EXPECT_EQ(log, std::vector<char>({'z'})) << "reactions should be run once"; | 111 EXPECT_EQ(log, std::vector<char>({'z'})) << "reactions should be run once"; |
| 112 } | 112 } |
| 113 | 113 |
| 114 class EnqueueToStack : public Command { | 114 class EnqueueToStack : public Command { |
| 115 WTF_MAKE_NONCOPYABLE(EnqueueToStack); | 115 WTF_MAKE_NONCOPYABLE(EnqueueToStack); |
| 116 public: | 116 public: |
| 117 EnqueueToStack(CustomElementReactionStack* stack, Element* element, CustomEl
ementReaction* reaction) | 117 EnqueueToStack(CustomElementReactionStack* stack, Element* element, CustomEl
ementReaction* reaction) |
| 118 : m_stack(stack) | 118 : m_stack(stack) |
| 119 , m_element(element) | 119 , m_element(element) |
| 120 , m_reaction(reaction) | 120 , m_reaction(reaction) |
| 121 { | 121 { |
| 122 } | 122 } |
| 123 ~EnqueueToStack() override = default; | 123 ~EnqueueToStack() override = default; |
| 124 DEFINE_INLINE_VIRTUAL_TRACE() | 124 DEFINE_INLINE_VIRTUAL_TRACE() |
| 125 { | 125 { |
| 126 Command::trace(visitor); | 126 Command::trace(visitor); |
| 127 visitor->trace(m_stack); | 127 visitor->trace(m_stack); |
| 128 visitor->trace(m_element); | 128 visitor->trace(m_element); |
| 129 visitor->trace(m_reaction); | 129 visitor->trace(m_reaction); |
| 130 } | 130 } |
| 131 void run(Element*) override | 131 void run(Element*) override |
| 132 { | 132 { |
| 133 m_stack->enqueue(m_element, m_reaction); | 133 m_stack->enqueueToCurrentQueue(m_element, m_reaction); |
| 134 } | 134 } |
| 135 private: | 135 private: |
| 136 Member<CustomElementReactionStack> m_stack; | 136 Member<CustomElementReactionStack> m_stack; |
| 137 Member<Element> m_element; | 137 Member<Element> m_element; |
| 138 Member<CustomElementReaction> m_reaction; | 138 Member<CustomElementReaction> m_reaction; |
| 139 }; | 139 }; |
| 140 | 140 |
| 141 TEST(CustomElementReactionStackTest, enqueueFromReaction) | 141 TEST(CustomElementReactionStackTest, enqueueFromReaction) |
| 142 { | 142 { |
| 143 std::vector<char> log; | 143 std::vector<char> log; |
| 144 | 144 |
| 145 Element* element = CreateElement("a"); | 145 Element* element = CreateElement("a"); |
| 146 | 146 |
| 147 CustomElementReactionStack* stack = new CustomElementReactionStack(); | 147 CustomElementReactionStack* stack = new CustomElementReactionStack(); |
| 148 stack->push(); | 148 stack->push(); |
| 149 stack->enqueue(element, new TestReaction({ | 149 stack->enqueueToCurrentQueue(element, new TestReaction({ |
| 150 new EnqueueToStack(stack, element, | 150 new EnqueueToStack(stack, element, |
| 151 new TestReaction({ new Log('a', log) }) ) | 151 new TestReaction({ new Log('a', log) }) ) |
| 152 })); | 152 })); |
| 153 stack->popInvokingReactions(); | 153 stack->popInvokingReactions(); |
| 154 | 154 |
| 155 EXPECT_EQ(log, std::vector<char>({ 'a' })) << "enqueued reaction from anothe
r reaction should run in the same invoke"; | 155 EXPECT_EQ(log, std::vector<char>({ 'a' })) << "enqueued reaction from anothe
r reaction should run in the same invoke"; |
| 156 } | 156 } |
| 157 | 157 |
| 158 } // namespace blink | 158 } // namespace blink |
| OLD | NEW |