1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "public/web/WebEmbeddedWorker.h"
8 #include "platform/testing/URLTestHelpers.h"
9 #include "platform/testing/UnitTestHelpers.h"
10 #include "public/platform/Platform.h"
11 #include "public/platform/WebURLResponse.h"
12 #include "public/platform/WebUnitTestSupport.h"
13 #include "public/web/WebEmbeddedWorkerStartData.h"
14 #include "public/web/WebServiceWorkerContextClient.h"
15 #include "public/web/WebSettings.h"
16 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
22 class MockServiceWorkerContextClient
23 : public WebServiceWorkerContextClient
{
25 MockServiceWorkerContextClient() { }
26 ~MockServiceWorkerContextClient() override
{ }
27 MOCK_METHOD0(workerReadyForInspection
, void());
28 MOCK_METHOD0(workerContextFailedToStart
, void());
29 MOCK_METHOD1(createServiceWorkerNetworkProvider
, WebServiceWorkerNetworkProvider
*(WebDataSource
*));
32 class WebEmbeddedWorkerImplFailureTest
: public ::testing::Test
{
36 m_mockClient
= new MockServiceWorkerContextClient();
37 m_worker
= adoptPtr(WebEmbeddedWorker::create(m_mockClient
, nullptr));
39 WebURL invalidScriptURL
= URLTestHelpers::toKURL("https://www.example.com/sw.js");
40 WebURLResponse errorResponse
;
41 errorResponse
.initialize();
42 errorResponse
.setMIMEType("text/html");
43 errorResponse
.setHTTPStatusCode(404);
46 error
.domain
= "WebEmbeddedWorkerImplTest";
47 Platform::current()->unitTestSupport()->registerMockedErrorURL(invalidScriptURL
, errorResponse
, error
);
49 m_startData
.scriptURL
= invalidScriptURL
;
50 m_startData
.userAgent
= WebString("dummy user agent");
51 m_startData
.waitForDebuggerMode
= WebEmbeddedWorkerStartData::DontWaitForDebugger
;
52 m_startData
.v8CacheOptions
= WebSettings::V8CacheOptionsDefault
;
55 void TearDown() override
57 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
60 WebEmbeddedWorkerStartData m_startData
;
61 MockServiceWorkerContextClient
* m_mockClient
;
62 OwnPtr
<WebEmbeddedWorker
> m_worker
;
67 TEST_F(WebEmbeddedWorkerImplFailureTest
, TerminateSoonAfterStart
)
69 EXPECT_CALL(*m_mockClient
, workerReadyForInspection()).Times(1);
70 m_worker
->startWorkerContext(m_startData
);
71 ::testing::Mock::VerifyAndClearExpectations(m_mockClient
);
73 EXPECT_CALL(*m_mockClient
, workerContextFailedToStart()).Times(1);
74 m_worker
->terminateWorkerContext();
75 ::testing::Mock::VerifyAndClearExpectations(m_mockClient
);
78 TEST_F(WebEmbeddedWorkerImplFailureTest
, TerminateWhileLoadingScript
)
80 EXPECT_CALL(*m_mockClient
, workerReadyForInspection()).Times(1);
81 m_worker
->startWorkerContext(m_startData
);
82 ::testing::Mock::VerifyAndClearExpectations(m_mockClient
);
84 EXPECT_CALL(*m_mockClient
, createServiceWorkerNetworkProvider(::testing::_
)).WillOnce(::testing::Return(nullptr));
85 testing::runPendingTasks();
86 ::testing::Mock::VerifyAndClearExpectations(m_mockClient
);
88 EXPECT_CALL(*m_mockClient
, workerContextFailedToStart()).Times(1);
89 m_worker
->terminateWorkerContext();
90 ::testing::Mock::VerifyAndClearExpectations(m_mockClient
);