2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2013 Google Inc. All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
29 #include "core/dom/MainThreadTaskRunner.h"
31 #include "core/dom/ExecutionContextTask.h"
32 #include "core/testing/NullExecutionContext.h"
33 #include "core/testing/UnitTestHelpers.h"
34 #include "platform/heap/Handle.h"
35 #include "wtf/Forward.h"
36 #include "wtf/OwnPtr.h"
37 #include "wtf/PassOwnPtr.h"
38 #include <gtest/gtest.h>
40 using namespace blink;
44 class MarkingBooleanTask FINAL : public ExecutionContextTask {
46 static PassOwnPtr<MarkingBooleanTask> create(bool* toBeMarked)
48 return adoptPtr(new MarkingBooleanTask(toBeMarked));
52 virtual ~MarkingBooleanTask() { }
55 MarkingBooleanTask(bool* toBeMarked) : m_toBeMarked(toBeMarked) { }
57 virtual void performTask(ExecutionContext* context) OVERRIDE
65 TEST(MainThreadTaskRunnerTest, PostTask)
67 RefPtrWillBeRawPtr<NullExecutionContext> context = adoptRefWillBeNoop(new NullExecutionContext());
68 OwnPtr<MainThreadTaskRunner> runner = MainThreadTaskRunner::create(context.get());
69 bool isMarked = false;
71 runner->postTask(MarkingBooleanTask::create(&isMarked));
72 EXPECT_FALSE(isMarked);
73 blink::testing::runPendingTasks();
74 EXPECT_TRUE(isMarked);
77 TEST(MainThreadTaskRunnerTest, SuspendTask)
79 RefPtrWillBeRawPtr<NullExecutionContext> context = adoptRefWillBeNoop(new NullExecutionContext());
80 OwnPtr<MainThreadTaskRunner> runner = MainThreadTaskRunner::create(context.get());
81 bool isMarked = false;
83 context->setTasksNeedSuspension(true);
84 runner->postTask(MarkingBooleanTask::create(&isMarked));
86 blink::testing::runPendingTasks();
87 EXPECT_FALSE(isMarked);
89 context->setTasksNeedSuspension(false);
91 blink::testing::runPendingTasks();
92 EXPECT_TRUE(isMarked);
95 TEST(MainThreadTaskRunnerTest, RemoveRunner)
97 RefPtrWillBeRawPtr<NullExecutionContext> context = adoptRefWillBeNoop(new NullExecutionContext());
98 OwnPtr<MainThreadTaskRunner> runner = MainThreadTaskRunner::create(context.get());
99 bool isMarked = false;
101 context->setTasksNeedSuspension(true);
102 runner->postTask(MarkingBooleanTask::create(&isMarked));
104 blink::testing::runPendingTasks();
105 EXPECT_FALSE(isMarked);