File: AnimationWorkletThreadTest.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (231 lines) | stat: -rw-r--r-- 8,963 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "modules/compositorworker/AnimationWorkletThread.h"

#include "bindings/core/v8/ScriptSourceCode.h"
#include "bindings/core/v8/SourceLocation.h"
#include "bindings/core/v8/V8GCController.h"
#include "bindings/core/v8/WorkerOrWorkletScriptController.h"
#include "core/inspector/ConsoleMessage.h"
#include "core/workers/InProcessWorkerObjectProxy.h"
#include "core/workers/ParentFrameTaskRunners.h"
#include "core/workers/WorkerBackingThread.h"
#include "core/workers/WorkerLoaderProxy.h"
#include "core/workers/WorkerOrWorkletGlobalScope.h"
#include "core/workers/WorkerThreadStartupData.h"
#include "platform/CrossThreadFunctional.h"
#include "platform/WaitableEvent.h"
#include "platform/WebThreadSupportingGC.h"
#include "platform/heap/Handle.h"
#include "platform/testing/TestingPlatformSupport.h"
#include "platform/testing/UnitTestHelpers.h"
#include "public/platform/Platform.h"
#include "public/platform/WebAddressSpace.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "wtf/PtrUtil.h"
#include <memory>

namespace blink {
namespace {

// A null WorkerReportingProxy, supplied when creating AnimationWorkletThreads.
class TestAnimationWorkletReportingProxy : public WorkerReportingProxy {
 public:
  static std::unique_ptr<TestAnimationWorkletReportingProxy> create() {
    return WTF::wrapUnique(new TestAnimationWorkletReportingProxy());
  }

  // (Empty) WorkerReportingProxy implementation:
  void countFeature(UseCounter::Feature) override {}
  void countDeprecation(UseCounter::Feature) override {}
  void reportException(const String& errorMessage,
                       std::unique_ptr<SourceLocation>,
                       int exceptionId) override {}
  void reportConsoleMessage(MessageSource,
                            MessageLevel,
                            const String& message,
                            SourceLocation*) override {}
  void postMessageToPageInspector(const String&) override {}
  ParentFrameTaskRunners* getParentFrameTaskRunners() override {
    return m_parentFrameTaskRunners.get();
  }

  void didEvaluateWorkerScript(bool success) override {}
  void didCloseWorkerGlobalScope() override {}
  void willDestroyWorkerGlobalScope() override {}
  void didTerminateWorkerThread() override {}

 private:
  TestAnimationWorkletReportingProxy()
      : m_parentFrameTaskRunners(ParentFrameTaskRunners::create(nullptr)) {}

  Persistent<ParentFrameTaskRunners> m_parentFrameTaskRunners;
};

class AnimationWorkletTestPlatform : public TestingPlatformSupport {
 public:
  AnimationWorkletTestPlatform()
      : m_thread(WTF::wrapUnique(m_oldPlatform->createThread("Compositor"))) {}

  WebThread* compositorThread() const override { return m_thread.get(); }

  WebCompositorSupport* compositorSupport() override {
    return &m_compositorSupport;
  }

 private:
  std::unique_ptr<WebThread> m_thread;
  TestingCompositorSupport m_compositorSupport;
};

}  // namespace

class AnimationWorkletThreadTest : public ::testing::Test {
 public:
  void SetUp() override {
    AnimationWorkletThread::createSharedBackingThreadForTest();
    m_reportingProxy = TestAnimationWorkletReportingProxy::create();
    m_securityOrigin =
        SecurityOrigin::create(KURL(ParsedURLString, "http://fake.url/"));
  }

  void TearDown() override {
    AnimationWorkletThread::clearSharedBackingThread();
  }

  std::unique_ptr<AnimationWorkletThread> createAnimationWorkletThread() {
    std::unique_ptr<AnimationWorkletThread> thread =
        AnimationWorkletThread::create(nullptr, *m_reportingProxy);
    thread->start(WorkerThreadStartupData::create(
        KURL(ParsedURLString, "http://fake.url/"), "fake user agent", "",
        nullptr, DontPauseWorkerGlobalScopeOnStart, nullptr, "",
        m_securityOrigin.get(), nullptr, WebAddressSpaceLocal, nullptr, nullptr,
        WorkerV8Settings::Default()));
    return thread;
  }

  // Attempts to run some simple script for |thread|.
  void checkWorkletCanExecuteScript(WorkerThread* thread) {
    std::unique_ptr<WaitableEvent> waitEvent = WTF::makeUnique<WaitableEvent>();
    thread->workerBackingThread().backingThread().postTask(
        BLINK_FROM_HERE,
        crossThreadBind(&AnimationWorkletThreadTest::executeScriptInWorklet,
                        crossThreadUnretained(this),
                        crossThreadUnretained(thread),
                        crossThreadUnretained(waitEvent.get())));
    waitEvent->wait();
  }

 private:
  void executeScriptInWorklet(WorkerThread* thread, WaitableEvent* waitEvent) {
    WorkerOrWorkletScriptController* scriptController =
        thread->globalScope()->scriptController();
    scriptController->evaluate(ScriptSourceCode("var counter = 0; ++counter;"));
    waitEvent->signal();
  }

  RefPtr<SecurityOrigin> m_securityOrigin;
  std::unique_ptr<WorkerReportingProxy> m_reportingProxy;
  ScopedTestingPlatformSupport<AnimationWorkletTestPlatform> m_platform;
};

TEST_F(AnimationWorkletThreadTest, Basic) {
  ScopedTestingPlatformSupport<AnimationWorkletTestPlatform> platform;

  std::unique_ptr<AnimationWorkletThread> worklet =
      createAnimationWorkletThread();
  checkWorkletCanExecuteScript(worklet.get());
  worklet->terminateAndWait();
}

// Tests that the same WebThread is used for new worklets if the WebThread is
// still alive.
TEST_F(AnimationWorkletThreadTest, CreateSecondAndTerminateFirst) {
  // Create the first worklet and wait until it is initialized.
  std::unique_ptr<AnimationWorkletThread> firstWorklet =
      createAnimationWorkletThread();
  WebThreadSupportingGC* firstThread =
      &firstWorklet->workerBackingThread().backingThread();
  checkWorkletCanExecuteScript(firstWorklet.get());
  v8::Isolate* firstIsolate = firstWorklet->isolate();
  ASSERT_TRUE(firstIsolate);

  // Create the second worklet and immediately destroy the first worklet.
  std::unique_ptr<AnimationWorkletThread> secondWorklet =
      createAnimationWorkletThread();
  // We don't use terminateAndWait here to avoid forcible termination.
  firstWorklet->terminate();
  firstWorklet->waitForShutdownForTesting();

  // Wait until the second worklet is initialized. Verify that the second
  // worklet is using the same thread and Isolate as the first worklet.
  WebThreadSupportingGC* secondThread =
      &secondWorklet->workerBackingThread().backingThread();
  ASSERT_EQ(firstThread, secondThread);

  v8::Isolate* secondIsolate = secondWorklet->isolate();
  ASSERT_TRUE(secondIsolate);
  EXPECT_EQ(firstIsolate, secondIsolate);

  // Verify that the worklet can still successfully execute script.
  checkWorkletCanExecuteScript(secondWorklet.get());

  secondWorklet->terminateAndWait();
}

// Tests that a new WebThread is created if all existing worklets are
// terminated before a new worklet is created.
TEST_F(AnimationWorkletThreadTest, TerminateFirstAndCreateSecond) {
  // Create the first worklet, wait until it is initialized, and terminate it.
  std::unique_ptr<AnimationWorkletThread> worklet =
      createAnimationWorkletThread();
  WebThreadSupportingGC* firstThread =
      &worklet->workerBackingThread().backingThread();
  checkWorkletCanExecuteScript(worklet.get());

  // We don't use terminateAndWait here to avoid forcible termination.
  worklet->terminate();
  worklet->waitForShutdownForTesting();

  // Create the second worklet. The backing thread is same.
  worklet = createAnimationWorkletThread();
  WebThreadSupportingGC* secondThread =
      &worklet->workerBackingThread().backingThread();
  EXPECT_EQ(firstThread, secondThread);
  checkWorkletCanExecuteScript(worklet.get());

  worklet->terminateAndWait();
}

// Tests that v8::Isolate and WebThread are correctly set-up if a worklet is
// created while another is terminating.
TEST_F(AnimationWorkletThreadTest, CreatingSecondDuringTerminationOfFirst) {
  std::unique_ptr<AnimationWorkletThread> firstWorklet =
      createAnimationWorkletThread();
  checkWorkletCanExecuteScript(firstWorklet.get());
  v8::Isolate* firstIsolate = firstWorklet->isolate();
  ASSERT_TRUE(firstIsolate);

  // Request termination of the first worklet and create the second worklet
  // as soon as possible.
  firstWorklet->terminate();
  // We don't wait for its termination.
  // Note: We rely on the assumption that the termination steps don't run
  // on the worklet thread so quickly. This could be a source of flakiness.

  std::unique_ptr<AnimationWorkletThread> secondWorklet =
      createAnimationWorkletThread();

  v8::Isolate* secondIsolate = secondWorklet->isolate();
  ASSERT_TRUE(secondIsolate);
  EXPECT_EQ(firstIsolate, secondIsolate);

  // Verify that the isolate can run some scripts correctly in the second
  // worklet.
  checkWorkletCanExecuteScript(secondWorklet.get());
  secondWorklet->terminateAndWait();
}

}  // namespace blink