@@ -35,6 +35,17 @@ namespace content {
35
35
36
36
namespace {
37
37
38
+ // Sets the document URL for |host| and associates it with |registration|.
39
+ // A dumb version of
40
+ // ServiceWorkerControlleeRequestHandler::PrepareForMainResource().
41
+ void SimulateServiceWorkerControlleeRequestHandler (
42
+ ServiceWorkerProviderHost* host,
43
+ ServiceWorkerRegistration* registration) {
44
+ host->SetDocumentUrl (GURL (" https://www.example.com/page" ));
45
+ host->AssociateRegistration (registration,
46
+ false /* notify_controllerchange */ );
47
+ }
48
+
38
49
const char kServiceWorkerScheme [] = " i-can-use-service-worker" ;
39
50
40
51
class ServiceWorkerTestContentClient : public TestContentClient {
@@ -361,27 +372,6 @@ TEST_F(ServiceWorkerProviderHostTest, RemoveProvider) {
361
372
EXPECT_FALSE (context_->GetProviderHost (process_id, provider_id));
362
373
}
363
374
364
- class MockServiceWorkerContainer : public mojom ::ServiceWorkerContainer {
365
- public:
366
- explicit MockServiceWorkerContainer (
367
- mojom::ServiceWorkerContainerAssociatedRequest request)
368
- : binding_(this , std::move(request)) {}
369
-
370
- ~MockServiceWorkerContainer () override = default ;
371
-
372
- void SetController (const ServiceWorkerObjectInfo& controller,
373
- const std::vector<blink::mojom::WebFeature>& used_features,
374
- bool should_notify_controllerchange) override {
375
- was_set_controller_called_ = true ;
376
- }
377
-
378
- bool was_set_controller_called () const { return was_set_controller_called_; }
379
-
380
- private:
381
- bool was_set_controller_called_ = false ;
382
- mojo::AssociatedBinding<mojom::ServiceWorkerContainer> binding_;
383
- };
384
-
385
375
TEST_F (ServiceWorkerProviderHostTest, Controller) {
386
376
base::CommandLine::ForCurrentProcess ()->AppendSwitch (
387
377
switches::kEnableBrowserSideNavigation );
@@ -395,31 +385,27 @@ TEST_F(ServiceWorkerProviderHostTest, Controller) {
395
385
true /* is_parent_frame_secure */ );
396
386
remote_endpoints_.emplace_back ();
397
387
remote_endpoints_.back ().BindWithProviderHostInfo (&info);
398
- auto container = base::MakeUnique<MockServiceWorkerContainer>(
399
- std::move (*remote_endpoints_.back ().client_request ()));
400
388
401
389
// Create an active version and then start the navigation.
402
390
scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion (
403
391
registration1_.get (), GURL (" https://www.example.com/sw.js" ),
404
392
1 /* version_id */ , helper_->context ()->AsWeakPtr ());
405
393
registration1_->SetActiveVersion (version);
394
+ SimulateServiceWorkerControlleeRequestHandler (host.get (),
395
+ registration1_.get ());
406
396
407
397
// Finish the navigation.
408
- host->SetDocumentUrl (GURL (" https://www.example.com/page" ));
409
398
host->CompleteNavigationInitialized (
410
399
helper_->mock_render_process_id (), std::move (info),
411
400
helper_->GetDispatcherHostForProcess (helper_->mock_render_process_id ())
412
401
->AsWeakPtr ());
413
402
414
- host->AssociateRegistration (registration1_.get (),
415
- false /* notify_controllerchange */ );
416
- base::RunLoop ().RunUntilIdle ();
417
-
418
403
// The page should be controlled since there was an active version at the
419
404
// time navigation started. The SetController IPC should have been sent.
420
405
EXPECT_TRUE (host->active_version ());
421
406
EXPECT_EQ (host->active_version (), host->controller ());
422
- EXPECT_TRUE (container->was_set_controller_called ());
407
+ EXPECT_TRUE (helper_->ipc_sink ()->GetUniqueMessageMatching (
408
+ ServiceWorkerMsg_SetControllerServiceWorker::ID));
423
409
}
424
410
425
411
TEST_F (ServiceWorkerProviderHostTest, ActiveIsNotController) {
@@ -435,34 +421,31 @@ TEST_F(ServiceWorkerProviderHostTest, ActiveIsNotController) {
435
421
true /* is_parent_frame_secure */ );
436
422
remote_endpoints_.emplace_back ();
437
423
remote_endpoints_.back ().BindWithProviderHostInfo (&info);
438
- auto container = base::MakeUnique<MockServiceWorkerContainer>(
439
- std::move (*remote_endpoints_.back ().client_request ()));
440
424
441
- // Create an installing version and then start the navigation.
425
+ // Associate it with an installing registration then start the navigation.
442
426
scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion (
443
427
registration1_.get (), GURL (" https://www.example.com/sw.js" ),
444
428
1 /* version_id */ , helper_->context ()->AsWeakPtr ());
445
429
registration1_->SetInstallingVersion (version);
430
+ SimulateServiceWorkerControlleeRequestHandler (host.get (),
431
+ registration1_.get ());
432
+
433
+ // Promote the worker to active while navigation is still happening.
434
+ registration1_->SetActiveVersion (version);
446
435
447
436
// Finish the navigation.
448
- host->SetDocumentUrl (GURL (" https://www.example.com/page" ));
449
437
host->CompleteNavigationInitialized (
450
438
helper_->mock_render_process_id (), std::move (info),
451
439
helper_->GetDispatcherHostForProcess (helper_->mock_render_process_id ())
452
440
->AsWeakPtr ());
453
441
454
- host->AssociateRegistration (registration1_.get (),
455
- false /* notify_controllerchange */ );
456
- // Promote the worker to active while navigation is still happening.
457
- registration1_->SetActiveVersion (version);
458
- base::RunLoop ().RunUntilIdle ();
459
-
460
442
// The page should not be controlled since there was no active version at the
461
443
// time navigation started. Furthermore, no SetController IPC should have been
462
444
// sent.
463
445
EXPECT_TRUE (host->active_version ());
464
446
EXPECT_FALSE (host->controller ());
465
- EXPECT_FALSE (container->was_set_controller_called ());
447
+ EXPECT_EQ (nullptr , helper_->ipc_sink ()->GetFirstMessageMatching (
448
+ ServiceWorkerMsg_SetControllerServiceWorker::ID));
466
449
}
467
450
468
451
} // namespace content
0 commit comments