Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(481)

Side by Side Diff: third_party/WebKit/Source/core/workers/ThreadedWorkletTest.cpp

Issue 2857583003: Worker: Avoid sending IPC messages for features already counted (Closed)
Patch Set: wip Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/TaskRunnerHelper.h" 5 #include "core/dom/TaskRunnerHelper.h"
6 #include "core/inspector/ConsoleMessageStorage.h" 6 #include "core/inspector/ConsoleMessageStorage.h"
7 #include "core/testing/DummyPageHolder.h" 7 #include "core/testing/DummyPageHolder.h"
8 #include "core/workers/ThreadedWorkletGlobalScope.h" 8 #include "core/workers/ThreadedWorkletGlobalScope.h"
9 #include "core/workers/ThreadedWorkletMessagingProxy.h" 9 #include "core/workers/ThreadedWorkletMessagingProxy.h"
10 #include "core/workers/ThreadedWorkletObjectProxy.h" 10 #include "core/workers/ThreadedWorkletObjectProxy.h"
11 #include "core/workers/WorkerInspectorProxy.h" 11 #include "core/workers/WorkerInspectorProxy.h"
12 #include "core/workers/WorkerThread.h" 12 #include "core/workers/WorkerThread.h"
13 #include "core/workers/WorkerThreadStartupData.h" 13 #include "core/workers/WorkerThreadStartupData.h"
14 #include "core/workers/WorkerThreadTestHelper.h" 14 #include "core/workers/WorkerThreadTestHelper.h"
15 #include "core/workers/WorkletThreadHolder.h" 15 #include "core/workers/WorkletThreadHolder.h"
16 #include "platform/CrossThreadFunctional.h" 16 #include "platform/CrossThreadFunctional.h"
17 #include "platform/testing/UnitTestHelpers.h" 17 #include "platform/testing/UnitTestHelpers.h"
18 #include "platform/weborigin/SecurityOrigin.h" 18 #include "platform/weborigin/SecurityOrigin.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 20
21 namespace blink { 21 namespace blink {
22 22
23 class ThreadedWorkletObjectProxyForTest final
24 : public ThreadedWorkletObjectProxy {
25 public:
26 ThreadedWorkletObjectProxyForTest(
27 const WeakPtr<ThreadedWorkletMessagingProxy>& messaging_proxy_weak_ptr,
28 ParentFrameTaskRunners* parent_frame_task_runners)
29 : ThreadedWorkletObjectProxy(messaging_proxy_weak_ptr,
30 parent_frame_task_runners),
31 reported_features_(UseCounter::kNumberOfFeatures) {}
32
33 protected:
34 void CountFeature(UseCounter::Feature feature) override {
35 // Any feature should be reported only one time.
36 EXPECT_FALSE(reported_features_.QuickGet(feature));
37 reported_features_.QuickSet(feature);
38 ThreadedWorkletObjectProxy::CountFeature(feature);
39 }
40
41 void CountDeprecation(UseCounter::Feature feature) final {
42 // Any feature should be reported only one time.
43 EXPECT_FALSE(reported_features_.QuickGet(feature));
44 reported_features_.QuickSet(feature);
45 ThreadedWorkletObjectProxy::CountDeprecation(feature);
46 }
47
48 private:
49 BitVector reported_features_;
50 };
51
23 class ThreadedWorkletThreadForTest : public WorkerThread { 52 class ThreadedWorkletThreadForTest : public WorkerThread {
24 public: 53 public:
25 ThreadedWorkletThreadForTest( 54 ThreadedWorkletThreadForTest(
26 WorkerLoaderProxyProvider* worker_loader_proxy_provider, 55 WorkerLoaderProxyProvider* worker_loader_proxy_provider,
27 WorkerReportingProxy& worker_reporting_proxy) 56 WorkerReportingProxy& worker_reporting_proxy)
28 : WorkerThread(WorkerLoaderProxy::Create(worker_loader_proxy_provider), 57 : WorkerThread(WorkerLoaderProxy::Create(worker_loader_proxy_provider),
29 worker_reporting_proxy) {} 58 worker_reporting_proxy) {}
30 ~ThreadedWorkletThreadForTest() override{}; 59 ~ThreadedWorkletThreadForTest() override {}
31 60
32 WorkerBackingThread& GetWorkerBackingThread() override { 61 WorkerBackingThread& GetWorkerBackingThread() override {
33 auto worklet_thread_holder = 62 auto worklet_thread_holder =
34 WorkletThreadHolder<ThreadedWorkletThreadForTest>::GetInstance(); 63 WorkletThreadHolder<ThreadedWorkletThreadForTest>::GetInstance();
35 DCHECK(worklet_thread_holder); 64 DCHECK(worklet_thread_holder);
36 return *worklet_thread_holder->GetThread(); 65 return *worklet_thread_holder->GetThread();
37 } 66 }
38 67
39 void ClearWorkerBackingThread() override {} 68 void ClearWorkerBackingThread() override {}
40 69
(...skipping 24 matching lines...) Expand all
65 EXPECT_TRUE(IsCurrentThread()); 94 EXPECT_TRUE(IsCurrentThread());
66 GlobalScope()->CountFeature(feature); 95 GlobalScope()->CountFeature(feature);
67 GetParentFrameTaskRunners() 96 GetParentFrameTaskRunners()
68 ->Get(TaskType::kUnspecedTimer) 97 ->Get(TaskType::kUnspecedTimer)
69 ->PostTask(BLINK_FROM_HERE, CrossThreadBind(&testing::ExitRunLoop)); 98 ->PostTask(BLINK_FROM_HERE, CrossThreadBind(&testing::ExitRunLoop));
70 } 99 }
71 100
72 // Emulates deprecated API use on ThreadedWorkletGlobalScope. 101 // Emulates deprecated API use on ThreadedWorkletGlobalScope.
73 void CountDeprecation(UseCounter::Feature feature) { 102 void CountDeprecation(UseCounter::Feature feature) {
74 EXPECT_TRUE(IsCurrentThread()); 103 EXPECT_TRUE(IsCurrentThread());
75 EXPECT_EQ(0u, GetConsoleMessageStorage()->size());
76 GlobalScope()->CountDeprecation(feature); 104 GlobalScope()->CountDeprecation(feature);
77 105
78 // countDeprecation() should add a warning message. 106 // countDeprecation() should add a warning message.
79 EXPECT_EQ(1u, GetConsoleMessageStorage()->size()); 107 EXPECT_EQ(1u, GetConsoleMessageStorage()->size());
80 String console_message = GetConsoleMessageStorage()->at(0)->Message(); 108 String console_message = GetConsoleMessageStorage()->at(0)->Message();
81 EXPECT_TRUE(console_message.Contains("deprecated")); 109 EXPECT_TRUE(console_message.Contains("deprecated"));
82 110
83 GetParentFrameTaskRunners() 111 GetParentFrameTaskRunners()
84 ->Get(TaskType::kUnspecedTimer) 112 ->Get(TaskType::kUnspecedTimer)
85 ->PostTask(BLINK_FROM_HERE, CrossThreadBind(&testing::ExitRunLoop)); 113 ->PostTask(BLINK_FROM_HERE, CrossThreadBind(&testing::ExitRunLoop));
86 } 114 }
87 }; 115 };
88 116
89 class ThreadedWorkletMessagingProxyForTest 117 class ThreadedWorkletMessagingProxyForTest
90 : public ThreadedWorkletMessagingProxy { 118 : public ThreadedWorkletMessagingProxy {
91 public: 119 public:
92 ThreadedWorkletMessagingProxyForTest(ExecutionContext* execution_context) 120 ThreadedWorkletMessagingProxyForTest(ExecutionContext* execution_context)
93 : ThreadedWorkletMessagingProxy(execution_context) { 121 : ThreadedWorkletMessagingProxy(execution_context) {
122 worklet_object_proxy_ = WTF::MakeUnique<ThreadedWorkletObjectProxyForTest>(
123 weak_ptr_factory_.CreateWeakPtr(), GetParentFrameTaskRunners());
94 worker_loader_proxy_provider_ = 124 worker_loader_proxy_provider_ =
95 WTF::MakeUnique<WorkerLoaderProxyProvider>(); 125 WTF::MakeUnique<WorkerLoaderProxyProvider>();
96 worker_thread_ = WTF::MakeUnique<ThreadedWorkletThreadForTest>( 126 worker_thread_ = WTF::MakeUnique<ThreadedWorkletThreadForTest>(
97 worker_loader_proxy_provider_.get(), WorkletObjectProxy()); 127 worker_loader_proxy_provider_.get(), WorkletObjectProxy());
98 ThreadedWorkletThreadForTest::EnsureSharedBackingThread(); 128 ThreadedWorkletThreadForTest::EnsureSharedBackingThread();
99 } 129 }
100 130
101 ~ThreadedWorkletMessagingProxyForTest() override { 131 ~ThreadedWorkletMessagingProxyForTest() override {
102 worker_thread_->GetWorkerLoaderProxy()->DetachProvider( 132 worker_thread_->GetWorkerLoaderProxy()->DetachProvider(
103 worker_loader_proxy_provider_.get()); 133 worker_loader_proxy_provider_.get());
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 std::unique_ptr<DummyPageHolder> page_; 193 std::unique_ptr<DummyPageHolder> page_;
164 std::unique_ptr<ThreadedWorkletMessagingProxyForTest> messaging_proxy_; 194 std::unique_ptr<ThreadedWorkletMessagingProxyForTest> messaging_proxy_;
165 }; 195 };
166 196
167 TEST_F(ThreadedWorkletTest, UseCounter) { 197 TEST_F(ThreadedWorkletTest, UseCounter) {
168 MessagingProxy()->Start(); 198 MessagingProxy()->Start();
169 199
170 // This feature is randomly selected. 200 // This feature is randomly selected.
171 const UseCounter::Feature kFeature1 = UseCounter::Feature::kRequestFileSystem; 201 const UseCounter::Feature kFeature1 = UseCounter::Feature::kRequestFileSystem;
172 202
173 // API use on the DedicatedWorkerGlobalScope should be recorded in UseCounter 203 // API use on the ThreadedWorkletGlobalScope should be recorded in UseCounter
174 // on the Document. 204 // on the Document.
175 EXPECT_FALSE(UseCounter::IsCounted(GetDocument(), kFeature1)); 205 EXPECT_FALSE(UseCounter::IsCounted(GetDocument(), kFeature1));
176 TaskRunnerHelper::Get(TaskType::kUnspecedTimer, GetWorkerThread()) 206 TaskRunnerHelper::Get(TaskType::kUnspecedTimer, GetWorkerThread())
177 ->PostTask( 207 ->PostTask(
178 BLINK_FROM_HERE, 208 BLINK_FROM_HERE,
179 CrossThreadBind(&ThreadedWorkletThreadForTest::CountFeature, 209 CrossThreadBind(&ThreadedWorkletThreadForTest::CountFeature,
180 CrossThreadUnretained(GetWorkerThread()), kFeature1)); 210 CrossThreadUnretained(GetWorkerThread()), kFeature1));
181 testing::EnterRunLoop(); 211 testing::EnterRunLoop();
182 EXPECT_TRUE(UseCounter::IsCounted(GetDocument(), kFeature1)); 212 EXPECT_TRUE(UseCounter::IsCounted(GetDocument(), kFeature1));
183 213
214 // API use should be reported to the Document only one time. See comments in
215 // ThreadedWorkletGlobalScopeForTest::CountFeature.
216 TaskRunnerHelper::Get(TaskType::kUnspecedTimer, GetWorkerThread())
217 ->PostTask(
218 BLINK_FROM_HERE,
219 CrossThreadBind(&ThreadedWorkletThreadForTest::CountFeature,
220 CrossThreadUnretained(GetWorkerThread()), kFeature1));
221 testing::EnterRunLoop();
222
184 // This feature is randomly selected from Deprecation::deprecationMessage(). 223 // This feature is randomly selected from Deprecation::deprecationMessage().
185 const UseCounter::Feature kFeature2 = 224 const UseCounter::Feature kFeature2 =
186 UseCounter::Feature::kPrefixedStorageInfo; 225 UseCounter::Feature::kPrefixedStorageInfo;
187 226
188 // Deprecated API use on the ThreadedWorkletGlobalScope should be recorded in 227 // Deprecated API use on the ThreadedWorkletGlobalScope should be recorded in
189 // UseCounter on the Document. 228 // UseCounter on the Document.
190 EXPECT_FALSE(UseCounter::IsCounted(GetDocument(), kFeature2)); 229 EXPECT_FALSE(UseCounter::IsCounted(GetDocument(), kFeature2));
191 TaskRunnerHelper::Get(TaskType::kUnspecedTimer, GetWorkerThread()) 230 TaskRunnerHelper::Get(TaskType::kUnspecedTimer, GetWorkerThread())
192 ->PostTask( 231 ->PostTask(
193 BLINK_FROM_HERE, 232 BLINK_FROM_HERE,
194 CrossThreadBind(&ThreadedWorkletThreadForTest::CountDeprecation, 233 CrossThreadBind(&ThreadedWorkletThreadForTest::CountDeprecation,
195 CrossThreadUnretained(GetWorkerThread()), kFeature2)); 234 CrossThreadUnretained(GetWorkerThread()), kFeature2));
196 testing::EnterRunLoop(); 235 testing::EnterRunLoop();
197 EXPECT_TRUE(UseCounter::IsCounted(GetDocument(), kFeature2)); 236 EXPECT_TRUE(UseCounter::IsCounted(GetDocument(), kFeature2));
237
238 // API use should be reported to the Document only one time. See comments in
239 // ThreadedWorkletGlobalScopeForTest::CountDeprecation.
240 TaskRunnerHelper::Get(TaskType::kUnspecedTimer, GetWorkerThread())
241 ->PostTask(
242 BLINK_FROM_HERE,
243 CrossThreadBind(&ThreadedWorkletThreadForTest::CountDeprecation,
244 CrossThreadUnretained(GetWorkerThread()), kFeature2));
245 testing::EnterRunLoop();
198 } 246 }
199 247
200 } // namespace blink 248 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698