OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
3 * Copyright (C) 2013 Google Inc. All Rights Reserved. | 3 * Copyright (C) 2013 Google Inc. All Rights Reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 10 matching lines...) Expand all Loading... |
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 * | 25 * |
26 */ | 26 */ |
27 | 27 |
28 #include "config.h" | 28 #include "config.h" |
29 #include "core/dom/MainThreadTaskRunner.h" | 29 #include "core/dom/MainThreadTaskRunner.h" |
30 | 30 |
31 #include "core/dom/ExecutionContext.h" | |
32 #include "core/dom/ExecutionContextTask.h" | 31 #include "core/dom/ExecutionContextTask.h" |
33 #include "core/dom/SecurityContext.h" | 32 #include "core/testing/NullExecutionContext.h" |
34 #include "core/events/EventQueue.h" | |
35 #include "core/testing/UnitTestHelpers.h" | 33 #include "core/testing/UnitTestHelpers.h" |
| 34 #include "platform/heap/Handle.h" |
36 #include "wtf/Forward.h" | 35 #include "wtf/Forward.h" |
| 36 #include "wtf/OwnPtr.h" |
| 37 #include "wtf/PassOwnPtr.h" |
37 #include <gtest/gtest.h> | 38 #include <gtest/gtest.h> |
38 | 39 |
39 using namespace WebCore; | 40 using namespace WebCore; |
40 | 41 |
41 namespace { | 42 namespace { |
42 | 43 |
43 class NullEventQueue : public EventQueue { | |
44 public: | |
45 NullEventQueue() { } | |
46 virtual ~NullEventQueue() { } | |
47 virtual bool enqueueEvent(PassRefPtrWillBeRawPtr<Event>) OVERRIDE { return t
rue; } | |
48 virtual bool cancelEvent(Event*) OVERRIDE { return true; } | |
49 virtual void close() OVERRIDE { } | |
50 }; | |
51 | |
52 class NullExecutionContext : public RefCountedWillBeGarbageCollectedFinalized<Nu
llExecutionContext>, public SecurityContext, public ExecutionContext { | |
53 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(NullExecutionContext); | |
54 public: | |
55 NullExecutionContext(); | |
56 | |
57 virtual EventQueue* eventQueue() const OVERRIDE { return m_queue.get(); } | |
58 virtual bool tasksNeedSuspension() { return m_tasksNeedSuspension; } | |
59 | |
60 void setTasksNeedSuspention(bool flag) { m_tasksNeedSuspension = flag; } | |
61 | |
62 void trace(Visitor* visitor) | |
63 { | |
64 visitor->trace(m_queue); | |
65 ExecutionContext::trace(visitor); | |
66 } | |
67 | |
68 virtual void reportBlockedScriptExecutionToInspector(const String& directive
Text) OVERRIDE { } | |
69 virtual SecurityContext& securityContext() { return *this; } | |
70 | |
71 #if !ENABLE(OILPAN) | |
72 using RefCounted<NullExecutionContext>::ref; | |
73 using RefCounted<NullExecutionContext>::deref; | |
74 | |
75 virtual void refExecutionContext() OVERRIDE { ref(); } | |
76 virtual void derefExecutionContext() OVERRIDE { deref(); } | |
77 #endif | |
78 | |
79 protected: | |
80 virtual const KURL& virtualURL() const OVERRIDE { return m_dummyURL; } | |
81 virtual KURL virtualCompleteURL(const String&) const OVERRIDE { return m_dum
myURL; } | |
82 | |
83 private: | |
84 bool m_tasksNeedSuspension; | |
85 OwnPtrWillBeMember<EventQueue> m_queue; | |
86 | |
87 KURL m_dummyURL; | |
88 }; | |
89 | |
90 NullExecutionContext::NullExecutionContext() | |
91 : m_tasksNeedSuspension(false) | |
92 , m_queue(adoptPtrWillBeNoop(new NullEventQueue())) | |
93 { | |
94 } | |
95 | |
96 class MarkingBooleanTask FINAL : public ExecutionContextTask { | 44 class MarkingBooleanTask FINAL : public ExecutionContextTask { |
97 public: | 45 public: |
98 static PassOwnPtr<MarkingBooleanTask> create(bool* toBeMarked) | 46 static PassOwnPtr<MarkingBooleanTask> create(bool* toBeMarked) |
99 { | 47 { |
100 return adoptPtr(new MarkingBooleanTask(toBeMarked)); | 48 return adoptPtr(new MarkingBooleanTask(toBeMarked)); |
101 } | 49 } |
102 | 50 |
103 | 51 |
104 virtual ~MarkingBooleanTask() { } | 52 virtual ~MarkingBooleanTask() { } |
105 | 53 |
(...skipping 19 matching lines...) Expand all Loading... |
125 WebCore::testing::runPendingTasks(); | 73 WebCore::testing::runPendingTasks(); |
126 EXPECT_TRUE(isMarked); | 74 EXPECT_TRUE(isMarked); |
127 } | 75 } |
128 | 76 |
129 TEST(MainThreadTaskRunnerTest, SuspendTask) | 77 TEST(MainThreadTaskRunnerTest, SuspendTask) |
130 { | 78 { |
131 RefPtrWillBeRawPtr<NullExecutionContext> context = adoptRefWillBeNoop(new Nu
llExecutionContext()); | 79 RefPtrWillBeRawPtr<NullExecutionContext> context = adoptRefWillBeNoop(new Nu
llExecutionContext()); |
132 OwnPtr<MainThreadTaskRunner> runner = MainThreadTaskRunner::create(context.g
et()); | 80 OwnPtr<MainThreadTaskRunner> runner = MainThreadTaskRunner::create(context.g
et()); |
133 bool isMarked = false; | 81 bool isMarked = false; |
134 | 82 |
135 context->setTasksNeedSuspention(true); | 83 context->setTasksNeedSuspension(true); |
136 runner->postTask(MarkingBooleanTask::create(&isMarked)); | 84 runner->postTask(MarkingBooleanTask::create(&isMarked)); |
137 runner->suspend(); | 85 runner->suspend(); |
138 WebCore::testing::runPendingTasks(); | 86 WebCore::testing::runPendingTasks(); |
139 EXPECT_FALSE(isMarked); | 87 EXPECT_FALSE(isMarked); |
140 | 88 |
141 context->setTasksNeedSuspention(false); | 89 context->setTasksNeedSuspension(false); |
142 runner->resume(); | 90 runner->resume(); |
143 WebCore::testing::runPendingTasks(); | 91 WebCore::testing::runPendingTasks(); |
144 EXPECT_TRUE(isMarked); | 92 EXPECT_TRUE(isMarked); |
145 } | 93 } |
146 | 94 |
147 TEST(MainThreadTaskRunnerTest, RemoveRunner) | 95 TEST(MainThreadTaskRunnerTest, RemoveRunner) |
148 { | 96 { |
149 RefPtrWillBeRawPtr<NullExecutionContext> context = adoptRefWillBeNoop(new Nu
llExecutionContext()); | 97 RefPtrWillBeRawPtr<NullExecutionContext> context = adoptRefWillBeNoop(new Nu
llExecutionContext()); |
150 OwnPtr<MainThreadTaskRunner> runner = MainThreadTaskRunner::create(context.g
et()); | 98 OwnPtr<MainThreadTaskRunner> runner = MainThreadTaskRunner::create(context.g
et()); |
151 bool isMarked = false; | 99 bool isMarked = false; |
152 | 100 |
153 context->setTasksNeedSuspention(true); | 101 context->setTasksNeedSuspension(true); |
154 runner->postTask(MarkingBooleanTask::create(&isMarked)); | 102 runner->postTask(MarkingBooleanTask::create(&isMarked)); |
155 runner.clear(); | 103 runner.clear(); |
156 WebCore::testing::runPendingTasks(); | 104 WebCore::testing::runPendingTasks(); |
157 EXPECT_FALSE(isMarked); | 105 EXPECT_FALSE(isMarked); |
158 } | 106 } |
159 | 107 |
160 } | 108 } |
OLD | NEW |