Commit 6b040dc1 authored by nhiroki's avatar nhiroki Committed by Commit bot

Worker: Add unittests for UseCounter on DedicatedWorkerGlobalScope

BUG=667357

Review-Url: https://codereview.chromium.org/2571603003
Cr-Commit-Position: refs/heads/master@{#438760}
parent 382f1cf5
...@@ -3,11 +3,13 @@ ...@@ -3,11 +3,13 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "core/events/MessageEvent.h" #include "core/events/MessageEvent.h"
#include "core/inspector/ConsoleMessageStorage.h"
#include "core/testing/DummyPageHolder.h" #include "core/testing/DummyPageHolder.h"
#include "core/workers/DedicatedWorkerGlobalScope.h" #include "core/workers/DedicatedWorkerGlobalScope.h"
#include "core/workers/DedicatedWorkerThread.h" #include "core/workers/DedicatedWorkerThread.h"
#include "core/workers/InProcessWorkerMessagingProxy.h" #include "core/workers/InProcessWorkerMessagingProxy.h"
#include "core/workers/InProcessWorkerObjectProxy.h" #include "core/workers/InProcessWorkerObjectProxy.h"
#include "core/workers/WorkerInspectorProxy.h"
#include "core/workers/WorkerThread.h" #include "core/workers/WorkerThread.h"
#include "core/workers/WorkerThreadStartupData.h" #include "core/workers/WorkerThreadStartupData.h"
#include "core/workers/WorkerThreadTestHelper.h" #include "core/workers/WorkerThreadTestHelper.h"
...@@ -52,6 +54,33 @@ class DedicatedWorkerThreadForTest final : public DedicatedWorkerThread { ...@@ -52,6 +54,33 @@ class DedicatedWorkerThreadForTest final : public DedicatedWorkerThread {
std::move(startupData->m_starterOriginPrivilegeData), std::move(startupData->m_starterOriginPrivilegeData),
std::move(startupData->m_workerClients)); std::move(startupData->m_workerClients));
} }
// Emulates API use on DedicatedWorkerGlobalScope.
void countFeature(UseCounter::Feature feature) {
EXPECT_TRUE(isCurrentThread());
globalScope()->countFeature(feature);
workerReportingProxy()
.getParentFrameTaskRunners()
->get(TaskType::Internal)
->postTask(BLINK_FROM_HERE, crossThreadBind(&testing::exitRunLoop));
}
// Emulates deprecated API use on DedicatedWorkerGlobalScope.
void countDeprecation(UseCounter::Feature feature) {
EXPECT_TRUE(isCurrentThread());
EXPECT_EQ(0u, consoleMessageStorage()->size());
globalScope()->countDeprecation(feature);
// countDeprecation() should add a warning message.
EXPECT_EQ(1u, consoleMessageStorage()->size());
String consoleMessage = consoleMessageStorage()->at(0)->message();
EXPECT_TRUE(consoleMessage.contains("deprecated"));
workerReportingProxy()
.getParentFrameTaskRunners()
->get(TaskType::Internal)
->postTask(BLINK_FROM_HERE, crossThreadBind(&testing::exitRunLoop));
}
}; };
class InProcessWorkerMessagingProxyForTest class InProcessWorkerMessagingProxyForTest
...@@ -71,8 +100,6 @@ class InProcessWorkerMessagingProxyForTest ...@@ -71,8 +100,6 @@ class InProcessWorkerMessagingProxyForTest
m_workerThread = WTF::wrapUnique( m_workerThread = WTF::wrapUnique(
new DedicatedWorkerThreadForTest(m_mockWorkerLoaderProxyProvider.get(), new DedicatedWorkerThreadForTest(m_mockWorkerLoaderProxyProvider.get(),
workerObjectProxy(), threadHeapMode)); workerObjectProxy(), threadHeapMode));
workerThreadCreated();
m_mockWorkerThreadLifecycleObserver = new MockWorkerThreadLifecycleObserver( m_mockWorkerThreadLifecycleObserver = new MockWorkerThreadLifecycleObserver(
m_workerThread->getWorkerThreadLifecycleContext()); m_workerThread->getWorkerThreadLifecycleContext());
EXPECT_CALL(*m_mockWorkerThreadLifecycleObserver, contextDestroyed()) EXPECT_CALL(*m_mockWorkerThreadLifecycleObserver, contextDestroyed())
...@@ -85,6 +112,27 @@ class InProcessWorkerMessagingProxyForTest ...@@ -85,6 +112,27 @@ class InProcessWorkerMessagingProxyForTest
m_mockWorkerLoaderProxyProvider.get()); m_mockWorkerLoaderProxyProvider.get());
} }
void startWithSourceCode(const String& source) {
KURL scriptURL(ParsedURLString, "http://fake.url/");
m_securityOrigin = SecurityOrigin::create(scriptURL);
std::unique_ptr<Vector<CSPHeaderAndType>> headers =
WTF::makeUnique<Vector<CSPHeaderAndType>>();
CSPHeaderAndType headerAndType("contentSecurityPolicy",
ContentSecurityPolicyHeaderTypeReport);
headers->append(headerAndType);
workerThread()->start(WorkerThreadStartupData::create(
scriptURL, "fake user agent", source, nullptr /* cachedMetaData */,
DontPauseWorkerGlobalScopeOnStart, headers.get(),
"" /* referrerPolicy */, m_securityOrigin.get(),
nullptr /* workerClients */, WebAddressSpaceLocal,
nullptr /* originTrialTokens */, nullptr /* workerSettings */,
V8CacheOptionsDefault));
workerInspectorProxy()->workerThreadCreated(
toDocument(getExecutionContext()), m_workerThread.get(), scriptURL);
workerThreadCreated();
}
enum class Notification { enum class Notification {
MessageConfirmed, MessageConfirmed,
PendingActivityReported, PendingActivityReported,
...@@ -145,6 +193,7 @@ class InProcessWorkerMessagingProxyForTest ...@@ -145,6 +193,7 @@ class InProcessWorkerMessagingProxyForTest
m_mockWorkerLoaderProxyProvider; m_mockWorkerLoaderProxyProvider;
Persistent<MockWorkerThreadLifecycleObserver> Persistent<MockWorkerThreadLifecycleObserver>
m_mockWorkerThreadLifecycleObserver; m_mockWorkerThreadLifecycleObserver;
RefPtr<SecurityOrigin> m_securityOrigin;
WTF::Deque<Notification> m_events; WTF::Deque<Notification> m_events;
bool m_blocking = false; bool m_blocking = false;
...@@ -162,8 +211,6 @@ class DedicatedWorkerTest ...@@ -162,8 +211,6 @@ class DedicatedWorkerTest
m_workerMessagingProxy = m_workerMessagingProxy =
WTF::wrapUnique(new InProcessWorkerMessagingProxyForTest( WTF::wrapUnique(new InProcessWorkerMessagingProxyForTest(
&m_page->document(), m_threadHeapMode)); &m_page->document(), m_threadHeapMode));
m_securityOrigin =
SecurityOrigin::create(KURL(ParsedURLString, "http://fake.url/"));
} }
void TearDown() override { void TearDown() override {
...@@ -172,21 +219,6 @@ class DedicatedWorkerTest ...@@ -172,21 +219,6 @@ class DedicatedWorkerTest
workerMessagingProxy()->waitForNotification()); workerMessagingProxy()->waitForNotification());
} }
void startWithSourceCode(const String& source) {
std::unique_ptr<Vector<CSPHeaderAndType>> headers =
WTF::makeUnique<Vector<CSPHeaderAndType>>();
CSPHeaderAndType headerAndType("contentSecurityPolicy",
ContentSecurityPolicyHeaderTypeReport);
headers->append(headerAndType);
workerThread()->start(WorkerThreadStartupData::create(
KURL(ParsedURLString, "http://fake.url/"), "fake user agent", source,
nullptr /* cachedMetaData */, DontPauseWorkerGlobalScopeOnStart,
headers.get(), "" /* referrerPolicy */, m_securityOrigin.get(),
nullptr /* workerClients */, WebAddressSpaceLocal,
nullptr /* originTrialTokens */, nullptr /* workerSettings */,
V8CacheOptionsDefault));
}
void dispatchMessageEvent() { void dispatchMessageEvent() {
workerMessagingProxy()->postMessageToWorkerGlobalScope( workerMessagingProxy()->postMessageToWorkerGlobalScope(
nullptr /* message */, nullptr /* channels */); nullptr /* message */, nullptr /* channels */);
...@@ -200,8 +232,9 @@ class DedicatedWorkerTest ...@@ -200,8 +232,9 @@ class DedicatedWorkerTest
return m_workerMessagingProxy->workerThread(); return m_workerMessagingProxy->workerThread();
} }
Document& document() { return m_page->document(); }
private: private:
RefPtr<SecurityOrigin> m_securityOrigin;
std::unique_ptr<DummyPageHolder> m_page; std::unique_ptr<DummyPageHolder> m_page;
std::unique_ptr<InProcessWorkerMessagingProxyForTest> m_workerMessagingProxy; std::unique_ptr<InProcessWorkerMessagingProxyForTest> m_workerMessagingProxy;
const BlinkGC::ThreadHeapMode m_threadHeapMode; const BlinkGC::ThreadHeapMode m_threadHeapMode;
...@@ -217,7 +250,7 @@ INSTANTIATE_TEST_CASE_P(PerThreadHeap, ...@@ -217,7 +250,7 @@ INSTANTIATE_TEST_CASE_P(PerThreadHeap,
TEST_P(DedicatedWorkerTest, PendingActivity_NoActivity) { TEST_P(DedicatedWorkerTest, PendingActivity_NoActivity) {
const String sourceCode = "// Do nothing"; const String sourceCode = "// Do nothing";
startWithSourceCode(sourceCode); workerMessagingProxy()->startWithSourceCode(sourceCode);
// Worker initialization should be counted as a pending activity. // Worker initialization should be counted as a pending activity.
EXPECT_TRUE(workerMessagingProxy()->hasPendingActivity()); EXPECT_TRUE(workerMessagingProxy()->hasPendingActivity());
...@@ -231,7 +264,7 @@ TEST_P(DedicatedWorkerTest, PendingActivity_NoActivity) { ...@@ -231,7 +264,7 @@ TEST_P(DedicatedWorkerTest, PendingActivity_NoActivity) {
TEST_P(DedicatedWorkerTest, PendingActivity_SetTimeout) { TEST_P(DedicatedWorkerTest, PendingActivity_SetTimeout) {
// Start an oneshot timer on initial script evaluation. // Start an oneshot timer on initial script evaluation.
const String sourceCode = "setTimeout(function() {}, 0);"; const String sourceCode = "setTimeout(function() {}, 0);";
startWithSourceCode(sourceCode); workerMessagingProxy()->startWithSourceCode(sourceCode);
// Worker initialization should be counted as a pending activity. // Worker initialization should be counted as a pending activity.
EXPECT_TRUE(workerMessagingProxy()->hasPendingActivity()); EXPECT_TRUE(workerMessagingProxy()->hasPendingActivity());
...@@ -250,7 +283,7 @@ TEST_P(DedicatedWorkerTest, PendingActivity_SetInterval) { ...@@ -250,7 +283,7 @@ TEST_P(DedicatedWorkerTest, PendingActivity_SetInterval) {
const String sourceCode = const String sourceCode =
"var id = setInterval(function() {}, 50);" "var id = setInterval(function() {}, 50);"
"addEventListener('message', function(event) { clearInterval(id); });"; "addEventListener('message', function(event) { clearInterval(id); });";
startWithSourceCode(sourceCode); workerMessagingProxy()->startWithSourceCode(sourceCode);
// Worker initialization should be counted as a pending activity. // Worker initialization should be counted as a pending activity.
EXPECT_TRUE(workerMessagingProxy()->hasPendingActivity()); EXPECT_TRUE(workerMessagingProxy()->hasPendingActivity());
...@@ -276,7 +309,7 @@ TEST_P(DedicatedWorkerTest, PendingActivity_SetTimeoutOnMessageEvent) { ...@@ -276,7 +309,7 @@ TEST_P(DedicatedWorkerTest, PendingActivity_SetTimeoutOnMessageEvent) {
"addEventListener('message', function(event) {" "addEventListener('message', function(event) {"
" setTimeout(function() {}, 0);" " setTimeout(function() {}, 0);"
"});"; "});";
startWithSourceCode(sourceCode); workerMessagingProxy()->startWithSourceCode(sourceCode);
// Worker initialization should be counted as a pending activity. // Worker initialization should be counted as a pending activity.
EXPECT_TRUE(workerMessagingProxy()->hasPendingActivity()); EXPECT_TRUE(workerMessagingProxy()->hasPendingActivity());
...@@ -314,7 +347,7 @@ TEST_P(DedicatedWorkerTest, PendingActivity_SetIntervalOnMessageEvent) { ...@@ -314,7 +347,7 @@ TEST_P(DedicatedWorkerTest, PendingActivity_SetIntervalOnMessageEvent) {
" clearInterval(id);" " clearInterval(id);"
" }" " }"
"});"; "});";
startWithSourceCode(sourceCode); workerMessagingProxy()->startWithSourceCode(sourceCode);
// Worker initialization should be counted as a pending activity. // Worker initialization should be counted as a pending activity.
EXPECT_TRUE(workerMessagingProxy()->hasPendingActivity()); EXPECT_TRUE(workerMessagingProxy()->hasPendingActivity());
...@@ -354,4 +387,35 @@ TEST_P(DedicatedWorkerTest, PendingActivity_SetIntervalOnMessageEvent) { ...@@ -354,4 +387,35 @@ TEST_P(DedicatedWorkerTest, PendingActivity_SetIntervalOnMessageEvent) {
EXPECT_FALSE(workerMessagingProxy()->hasPendingActivity()); EXPECT_FALSE(workerMessagingProxy()->hasPendingActivity());
} }
TEST_P(DedicatedWorkerTest, UseCounter) {
const String sourceCode = "// Do nothing";
workerMessagingProxy()->startWithSourceCode(sourceCode);
// This feature is randomly selected.
const UseCounter::Feature feature1 = UseCounter::Feature::RequestFileSystem;
// API use on the DedicatedWorkerGlobalScope should be recorded in UseCounter
// on the Document.
EXPECT_FALSE(UseCounter::isCounted(document(), feature1));
workerThread()->postTask(
BLINK_FROM_HERE,
createCrossThreadTask(&DedicatedWorkerThreadForTest::countFeature,
crossThreadUnretained(workerThread()), feature1));
testing::enterRunLoop();
EXPECT_TRUE(UseCounter::isCounted(document(), feature1));
// This feature is randomly selected from Deprecation::deprecationMessage().
const UseCounter::Feature feature2 = UseCounter::Feature::PrefixedStorageInfo;
// Deprecated API use on the DedicatedWorkerGlobalScope should be recorded in
// UseCounter on the Document.
EXPECT_FALSE(UseCounter::isCounted(document(), feature2));
workerThread()->postTask(
BLINK_FROM_HERE,
createCrossThreadTask(&DedicatedWorkerThreadForTest::countDeprecation,
crossThreadUnretained(workerThread()), feature2));
testing::enterRunLoop();
EXPECT_TRUE(UseCounter::isCounted(document(), feature2));
}
} // namespace blink } // namespace blink
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment