1/*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2013 Google Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
25 *
26 */
27
28#include "config.h"
29#include "core/dom/MainThreadTaskRunner.h"
30
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>
39
40using namespace blink;
41
42namespace {
43
44class MarkingBooleanTask FINAL : public ExecutionContextTask {
45public:
46    static PassOwnPtr<MarkingBooleanTask> create(bool* toBeMarked)
47    {
48        return adoptPtr(new MarkingBooleanTask(toBeMarked));
49    }
50
51
52    virtual ~MarkingBooleanTask() { }
53
54private:
55    MarkingBooleanTask(bool* toBeMarked) : m_toBeMarked(toBeMarked) { }
56
57    virtual void performTask(ExecutionContext* context) OVERRIDE
58    {
59        *m_toBeMarked = true;
60    }
61
62    bool* m_toBeMarked;
63};
64
65TEST(MainThreadTaskRunnerTest, PostTask)
66{
67    RefPtrWillBeRawPtr<NullExecutionContext> context = adoptRefWillBeNoop(new NullExecutionContext());
68    OwnPtr<MainThreadTaskRunner> runner = MainThreadTaskRunner::create(context.get());
69    bool isMarked = false;
70
71    runner->postTask(MarkingBooleanTask::create(&isMarked));
72    EXPECT_FALSE(isMarked);
73    blink::testing::runPendingTasks();
74    EXPECT_TRUE(isMarked);
75}
76
77TEST(MainThreadTaskRunnerTest, SuspendTask)
78{
79    RefPtrWillBeRawPtr<NullExecutionContext> context = adoptRefWillBeNoop(new NullExecutionContext());
80    OwnPtr<MainThreadTaskRunner> runner = MainThreadTaskRunner::create(context.get());
81    bool isMarked = false;
82
83    context->setTasksNeedSuspension(true);
84    runner->postTask(MarkingBooleanTask::create(&isMarked));
85    runner->suspend();
86    blink::testing::runPendingTasks();
87    EXPECT_FALSE(isMarked);
88
89    context->setTasksNeedSuspension(false);
90    runner->resume();
91    blink::testing::runPendingTasks();
92    EXPECT_TRUE(isMarked);
93}
94
95TEST(MainThreadTaskRunnerTest, RemoveRunner)
96{
97    RefPtrWillBeRawPtr<NullExecutionContext> context = adoptRefWillBeNoop(new NullExecutionContext());
98    OwnPtr<MainThreadTaskRunner> runner = MainThreadTaskRunner::create(context.get());
99    bool isMarked = false;
100
101    context->setTasksNeedSuspension(true);
102    runner->postTask(MarkingBooleanTask::create(&isMarked));
103    runner.clear();
104    blink::testing::runPendingTasks();
105    EXPECT_FALSE(isMarked);
106}
107
108}
109