TEST_F(WebEmbeddedWorkerImplTest, ScriptNotFound)
{
    WebURL scriptURL = URLTestHelpers::toKURL("https://www.example.com/sw-404.js");
    WebURLResponse response;
    response.initialize();
    response.setMIMEType("text/javascript");
    response.setHTTPStatusCode(404);
    WebURLError error;
    error.reason = 1010;
    error.domain = "WebEmbeddedWorkerImplTest";
    Platform::current()->unitTestSupport()->registerMockedErrorURL(scriptURL, response, error);
    m_startData.scriptURL = scriptURL;

    EXPECT_CALL(*m_mockClient, workerReadyForInspection())
        .Times(1);
    m_worker->startWorkerContext(m_startData);
    ::testing::Mock::VerifyAndClearExpectations(m_mockClient);

    // Load the shadow page.
    EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_))
        .WillOnce(::testing::Return(nullptr));
    Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
    ::testing::Mock::VerifyAndClearExpectations(m_mockClient);

    // Load the script.
    EXPECT_CALL(*m_mockClient, workerScriptLoaded())
        .Times(0);
    EXPECT_CALL(*m_mockClient, createServiceWorkerProvider())
        .Times(0);
    EXPECT_CALL(*m_mockClient, workerContextFailedToStart())
        .Times(1);
    Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
    ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
}
TEST_F(WebEmbeddedWorkerImplTest, MAYBE_PauseAfterDownload)
{
    EXPECT_CALL(*m_mockClient, workerReadyForInspection())
        .Times(1);
    m_startData.pauseAfterDownloadMode = WebEmbeddedWorkerStartData::PauseAfterDownload;
    m_worker->startWorkerContext(m_startData);
    ::testing::Mock::VerifyAndClearExpectations(m_mockClient);

    // Load the shadow page.
    EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_))
        .WillOnce(::testing::Return(nullptr));
    Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
    ::testing::Mock::VerifyAndClearExpectations(m_mockClient);

    // Load the script.
    EXPECT_CALL(*m_mockClient, workerScriptLoaded())
        .Times(1);
    EXPECT_CALL(*m_mockClient, createServiceWorkerProvider())
        .Times(0);
    Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
    ::testing::Mock::VerifyAndClearExpectations(m_mockClient);

    // Resume after download.
    EXPECT_CALL(*m_mockClient, createServiceWorkerProvider())
        .WillOnce(::testing::Return(nullptr));
    m_worker->resumeAfterDownload();
    ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
}
TEST_F(WebEmbeddedWorkerImplTest, TerminateWhilePausedAfterDownload) {
    EXPECT_CALL(*m_mockClient, workerReadyForInspection()).Times(1);
    m_startData.pauseAfterDownloadMode =
        WebEmbeddedWorkerStartData::PauseAfterDownload;
    m_worker->startWorkerContext(m_startData);
    ::testing::Mock::VerifyAndClearExpectations(m_mockClient);

    // Load the shadow page.
    EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_))
    .WillOnce(::testing::Return(nullptr));
    Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
    ::testing::Mock::VerifyAndClearExpectations(m_mockClient);

    // Load the script.
    EXPECT_CALL(*m_mockClient, workerScriptLoaded()).Times(1);
    EXPECT_CALL(*m_mockClient, createServiceWorkerProvider()).Times(0);
    Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
    ::testing::Mock::VerifyAndClearExpectations(m_mockClient);

    // Terminate before resuming after download.
    EXPECT_CALL(*m_mockClient, createServiceWorkerProvider()).Times(0);
    EXPECT_CALL(*m_mockClient, workerContextFailedToStart()).Times(1);
    m_worker->terminateWorkerContext();
    ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
}
TEST_F(WebEmbeddedWorkerImplTest, TerminateWhileLoadingScript)
{
    EXPECT_CALL(*m_mockClient, workerReadyForInspection()).Times(1);
    m_worker->startWorkerContext(m_startData);
    ::testing::Mock::VerifyAndClearExpectations(m_mockClient);

    // Load the shadow page.
    EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_)).WillOnce(::testing::Return(nullptr));
    Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
    ::testing::Mock::VerifyAndClearExpectations(m_mockClient);

    // Terminate before loading the script.
    EXPECT_CALL(*m_mockClient, workerContextFailedToStart()).Times(1);
    m_worker->terminateWorkerContext();
    ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
}