Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_provider_host_unittest.cc
blob79a8ac164eb3d244865b076b37dc0bb5609be22d
1 // Copyright 2014 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.
5 #include "base/basictypes.h"
6 #include "base/memory/weak_ptr.h"
7 #include "base/thread_task_runner_handle.h"
8 #include "content/browser/service_worker/embedded_worker_test_helper.h"
9 #include "content/browser/service_worker/service_worker_context_core.h"
10 #include "content/browser/service_worker/service_worker_provider_host.h"
11 #include "content/browser/service_worker/service_worker_register_job.h"
12 #include "content/browser/service_worker/service_worker_registration.h"
13 #include "content/browser/service_worker/service_worker_version.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace content {
19 static const int kRenderProcessId = 33; // Dummy process ID for testing.
21 class ServiceWorkerProviderHostTest : public testing::Test {
22 protected:
23 ServiceWorkerProviderHostTest()
24 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
25 ~ServiceWorkerProviderHostTest() override {}
27 void SetUp() override {
28 helper_.reset(
29 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId));
30 context_ = helper_->context();
31 script_url_ = GURL("http://www.example.com/service_worker.js");
32 registration1_ = new ServiceWorkerRegistration(
33 GURL("http://www.example.com/"), 1L, context_->AsWeakPtr());
34 registration2_ = new ServiceWorkerRegistration(
35 GURL("http://www.example.com/example"), 2L, context_->AsWeakPtr());
37 // Prepare provider hosts (for the same process).
38 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost(
39 kRenderProcessId, MSG_ROUTING_NONE, 1 /* provider_id */,
40 SERVICE_WORKER_PROVIDER_FOR_WINDOW, context_->AsWeakPtr(), NULL));
41 host1->SetDocumentUrl(GURL("http://www.example.com/example1.html"));
42 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost(
43 kRenderProcessId, MSG_ROUTING_NONE, 2 /* provider_id */,
44 SERVICE_WORKER_PROVIDER_FOR_WINDOW, context_->AsWeakPtr(), NULL));
45 host2->SetDocumentUrl(GURL("http://www.example.com/example2.html"));
46 provider_host1_ = host1->AsWeakPtr();
47 provider_host2_ = host2->AsWeakPtr();
48 context_->AddProviderHost(make_scoped_ptr(host1.release()));
49 context_->AddProviderHost(make_scoped_ptr(host2.release()));
52 void TearDown() override {
53 registration1_ = 0;
54 registration2_ = 0;
55 helper_.reset();
58 bool PatternHasProcessToRun(const GURL& pattern) const {
59 return context_->process_manager()->PatternHasProcessToRun(pattern);
62 content::TestBrowserThreadBundle thread_bundle_;
63 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
64 ServiceWorkerContextCore* context_;
65 scoped_refptr<ServiceWorkerRegistration> registration1_;
66 scoped_refptr<ServiceWorkerRegistration> registration2_;
67 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_;
68 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_;
69 GURL script_url_;
71 private:
72 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest);
75 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) {
76 provider_host1_->AddMatchingRegistration(registration1_.get());
77 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern()));
79 // Adding the same registration twice has no effect.
80 provider_host1_->AddMatchingRegistration(registration1_.get());
81 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern()));
83 // Different matching registrations can be added.
84 provider_host1_->AddMatchingRegistration(registration2_.get());
85 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern()));
87 // Removing a matching registration will decrease the process refs for its
88 // pattern.
89 provider_host1_->RemoveMatchingRegistration(registration1_.get());
90 ASSERT_FALSE(PatternHasProcessToRun(registration1_->pattern()));
92 // Multiple provider hosts could add the same matching registration.
93 // The process refs will become 0 after all provider hosts removed them.
94 provider_host2_->AddMatchingRegistration(registration2_.get());
95 provider_host1_->RemoveMatchingRegistration(registration2_.get());
96 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern()));
97 provider_host2_->RemoveMatchingRegistration(registration2_.get());
98 ASSERT_FALSE(PatternHasProcessToRun(registration2_->pattern()));
101 TEST_F(ServiceWorkerProviderHostTest, AssociatedRegistration_ProcessStatus) {
102 // Associating the registration will also increase the process refs for
103 // the registration's pattern.
104 provider_host1_->AssociateRegistration(registration1_.get(),
105 false /* notify_controllerchange */);
106 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern()));
108 // Disassociating the registration shouldn't affect the process refs for
109 // the registration's pattern.
110 provider_host1_->DisassociateRegistration();
111 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern()));
114 TEST_F(ServiceWorkerProviderHostTest, MatchRegistration) {
115 provider_host1_->AddMatchingRegistration(registration1_.get());
116 provider_host1_->AddMatchingRegistration(registration2_.get());
118 // Match registration should return the longest matching one.
119 ASSERT_EQ(provider_host1_->MatchRegistration(), registration2_);
120 provider_host1_->RemoveMatchingRegistration(registration2_.get());
121 ASSERT_EQ(provider_host1_->MatchRegistration(), registration1_);
123 // Should return nullptr after removing all matching registrations.
124 provider_host1_->RemoveMatchingRegistration(registration1_.get());
125 ASSERT_EQ(provider_host1_->MatchRegistration(), nullptr);
128 } // namespace content