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

Side by Side Diff: third_party/WebKit/Source/web/WebEmbeddedWorkerImplTest.cpp

Issue 1687803002: Expand WebEmbeddedWorkerImplTest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 10 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
« no previous file with comments | « third_party/WebKit/PRESUBMIT.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 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 "public/web/WebEmbeddedWorker.h" 5 #include "public/web/WebEmbeddedWorker.h"
6 6
7 #include "platform/testing/URLTestHelpers.h" 7 #include "platform/testing/URLTestHelpers.h"
8 #include "platform/testing/UnitTestHelpers.h" 8 #include "platform/testing/UnitTestHelpers.h"
9 #include "public/platform/Platform.h" 9 #include "public/platform/Platform.h"
10 #include "public/platform/WebURLResponse.h" 10 #include "public/platform/WebURLResponse.h"
11 #include "public/platform/WebUnitTestSupport.h" 11 #include "public/platform/WebUnitTestSupport.h"
12 #include "public/platform/modules/serviceworker/WebServiceWorkerProvider.h"
12 #include "public/web/WebEmbeddedWorkerStartData.h" 13 #include "public/web/WebEmbeddedWorkerStartData.h"
13 #include "public/web/WebSettings.h" 14 #include "public/web/WebSettings.h"
14 #include "public/web/modules/serviceworker/WebServiceWorkerContextClient.h" 15 #include "public/web/modules/serviceworker/WebServiceWorkerContextClient.h"
15 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 namespace blink { 19 namespace blink {
19 namespace { 20 namespace {
20 21
21 class MockServiceWorkerContextClient 22 class MockServiceWorkerContextClient
22 : public WebServiceWorkerContextClient { 23 : public WebServiceWorkerContextClient {
23 public: 24 public:
24 MockServiceWorkerContextClient() { } 25 MockServiceWorkerContextClient() { }
25 ~MockServiceWorkerContextClient() override { } 26 ~MockServiceWorkerContextClient() override { }
26 MOCK_METHOD0(workerReadyForInspection, void()); 27 MOCK_METHOD0(workerReadyForInspection, void());
27 MOCK_METHOD0(workerContextFailedToStart, void()); 28 MOCK_METHOD0(workerContextFailedToStart, void());
29 MOCK_METHOD0(workerScriptLoaded, void());
28 MOCK_METHOD1(createServiceWorkerNetworkProvider, WebServiceWorkerNetworkProv ider*(WebDataSource*)); 30 MOCK_METHOD1(createServiceWorkerNetworkProvider, WebServiceWorkerNetworkProv ider*(WebDataSource*));
31 MOCK_METHOD0(createServiceWorkerProvider, WebServiceWorkerProvider*());
29 }; 32 };
30 33
31 class WebEmbeddedWorkerImplFailureTest : public ::testing::Test { 34 class WebEmbeddedWorkerImplTest : public ::testing::Test {
32 protected: 35 protected:
33 void SetUp() override 36 void SetUp() override
34 { 37 {
35 m_mockClient = new MockServiceWorkerContextClient(); 38 m_mockClient = new MockServiceWorkerContextClient();
36 m_worker = adoptPtr(WebEmbeddedWorker::create(m_mockClient, nullptr)); 39 m_worker = adoptPtr(WebEmbeddedWorker::create(m_mockClient, nullptr));
37 40
38 WebURL invalidScriptURL = URLTestHelpers::toKURL("https://www.example.co m/sw.js"); 41 WebURL scriptURL = URLTestHelpers::toKURL("https://www.example.com/sw.js ");
39 WebURLResponse errorResponse; 42 WebURLResponse response;
40 errorResponse.initialize(); 43 response.initialize();
41 errorResponse.setMIMEType("text/html"); 44 response.setMIMEType("text/javascript");
42 errorResponse.setHTTPStatusCode(404); 45 response.setHTTPStatusCode(200);
43 WebURLError error; 46 Platform::current()->unitTestSupport()->registerMockedURL(scriptURL, res ponse, "");
44 error.reason = 1010;
45 error.domain = "WebEmbeddedWorkerImplTest";
46 Platform::current()->unitTestSupport()->registerMockedErrorURL(invalidSc riptURL, errorResponse, error);
47 47
48 m_startData.scriptURL = invalidScriptURL; 48 m_startData.scriptURL = scriptURL;
49 m_startData.userAgent = WebString("dummy user agent"); 49 m_startData.userAgent = WebString("dummy user agent");
50 m_startData.waitForDebuggerMode = WebEmbeddedWorkerStartData::DontWaitFo rDebugger; 50 m_startData.waitForDebuggerMode = WebEmbeddedWorkerStartData::DontWaitFo rDebugger;
51 m_startData.v8CacheOptions = WebSettings::V8CacheOptionsDefault; 51 m_startData.v8CacheOptions = WebSettings::V8CacheOptionsDefault;
52 } 52 }
53 53
54 void TearDown() override 54 void TearDown() override
55 { 55 {
56 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); 56 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
57 } 57 }
58 58
59 WebEmbeddedWorkerStartData m_startData; 59 WebEmbeddedWorkerStartData m_startData;
60 MockServiceWorkerContextClient* m_mockClient; 60 MockServiceWorkerContextClient* m_mockClient;
61 OwnPtr<WebEmbeddedWorker> m_worker; 61 OwnPtr<WebEmbeddedWorker> m_worker;
62 }; 62 };
63 63
64 } // namespace 64 } // namespace
65 65
66 TEST_F(WebEmbeddedWorkerImplFailureTest, TerminateSoonAfterStart) 66 TEST_F(WebEmbeddedWorkerImplTest, TerminateSoonAfterStart)
67 { 67 {
68 EXPECT_CALL(*m_mockClient, workerReadyForInspection()).Times(1); 68 EXPECT_CALL(*m_mockClient, workerReadyForInspection()).Times(1);
69 m_worker->startWorkerContext(m_startData); 69 m_worker->startWorkerContext(m_startData);
70 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); 70 ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
71 71
72 EXPECT_CALL(*m_mockClient, workerContextFailedToStart()).Times(1); 72 EXPECT_CALL(*m_mockClient, workerContextFailedToStart()).Times(1);
73 m_worker->terminateWorkerContext(); 73 m_worker->terminateWorkerContext();
74 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); 74 ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
75 } 75 }
76 76
77 TEST_F(WebEmbeddedWorkerImplFailureTest, TerminateWhileWaitingForDebugger) 77 TEST_F(WebEmbeddedWorkerImplTest, TerminateWhileWaitingForDebugger)
78 { 78 {
79 EXPECT_CALL(*m_mockClient, workerReadyForInspection()).Times(1); 79 EXPECT_CALL(*m_mockClient, workerReadyForInspection()).Times(1);
80 m_startData.waitForDebuggerMode = WebEmbeddedWorkerStartData::WaitForDebugge r; 80 m_startData.waitForDebuggerMode = WebEmbeddedWorkerStartData::WaitForDebugge r;
81 m_worker->startWorkerContext(m_startData); 81 m_worker->startWorkerContext(m_startData);
82 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); 82 ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
83 83
84 EXPECT_CALL(*m_mockClient, workerContextFailedToStart()).Times(1); 84 EXPECT_CALL(*m_mockClient, workerContextFailedToStart()).Times(1);
85 m_worker->terminateWorkerContext(); 85 m_worker->terminateWorkerContext();
86 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); 86 ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
87 } 87 }
88 88
89 TEST_F(WebEmbeddedWorkerImplFailureTest, TerminateWhileLoadingScript) 89 TEST_F(WebEmbeddedWorkerImplTest, TerminateWhileLoadingScript)
90 { 90 {
91 EXPECT_CALL(*m_mockClient, workerReadyForInspection()).Times(1); 91 EXPECT_CALL(*m_mockClient, workerReadyForInspection()).Times(1);
92 m_worker->startWorkerContext(m_startData); 92 m_worker->startWorkerContext(m_startData);
93 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); 93 ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
94 94
95 // Load the shadow page.
95 EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_)) .WillOnce(::testing::Return(nullptr)); 96 EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_)) .WillOnce(::testing::Return(nullptr));
96 testing::runPendingTasks(); 97 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
97 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); 98 ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
98 99
100 // Terminate before loading the script.
99 EXPECT_CALL(*m_mockClient, workerContextFailedToStart()).Times(1); 101 EXPECT_CALL(*m_mockClient, workerContextFailedToStart()).Times(1);
100 m_worker->terminateWorkerContext(); 102 m_worker->terminateWorkerContext();
101 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); 103 ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
102 } 104 }
103 105
106 TEST_F(WebEmbeddedWorkerImplTest, ScriptNotFound)
107 {
108 WebURL scriptURL = URLTestHelpers::toKURL("https://www.example.com/sw-404.js ");
109 WebURLResponse response;
110 response.initialize();
111 response.setMIMEType("text/javascript");
112 response.setHTTPStatusCode(404);
113 WebURLError error;
114 error.reason = 1010;
115 error.domain = "WebEmbeddedWorkerImplTest";
116 Platform::current()->unitTestSupport()->registerMockedErrorURL(scriptURL, re sponse, error);
117 m_startData.scriptURL = scriptURL;
118
119 EXPECT_CALL(*m_mockClient, workerReadyForInspection())
120 .Times(1);
121 m_worker->startWorkerContext(m_startData);
122 ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
123
124 // Load the shadow page.
125 EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_))
126 .WillOnce(::testing::Return(nullptr));
127 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
128 ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
129
130 // Load the script.
131 EXPECT_CALL(*m_mockClient, workerScriptLoaded())
132 .Times(0);
133 EXPECT_CALL(*m_mockClient, createServiceWorkerProvider())
134 .Times(0);
135 EXPECT_CALL(*m_mockClient, workerContextFailedToStart())
136 .Times(1);
137 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
138 ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
139 }
140
141 TEST_F(WebEmbeddedWorkerImplTest, Success)
142 {
143 EXPECT_CALL(*m_mockClient, workerReadyForInspection())
144 .Times(1);
145 m_worker->startWorkerContext(m_startData);
146 ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
147
148 // Load the shadow page.
149 EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_))
150 .WillOnce(::testing::Return(nullptr));
151 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
152 ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
153
154 // Load the script.
155 EXPECT_CALL(*m_mockClient, workerScriptLoaded())
156 .Times(1);
157 EXPECT_CALL(*m_mockClient, createServiceWorkerProvider())
158 .WillOnce(::testing::Return(nullptr));
159 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
160 ::testing::Mock::VerifyAndClearExpectations(m_mockClient);
161 }
162
104 } // namespace blink 163 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/PRESUBMIT.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698