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.
4
5#include "chrome/browser/browsing_data/browsing_data_service_worker_helper.h"
6
7#include <vector>
8
9#include "base/message_loop/message_loop_proxy.h"
10#include "base/strings/utf_string_conversions.h"
11#include "chrome/test/base/testing_profile.h"
12#include "content/public/browser/browser_context.h"
13#include "content/public/browser/storage_partition.h"
14#include "content/public/test/test_browser_thread_bundle.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17namespace {
18
19class CannedBrowsingDataServiceWorkerHelperTest : public testing::Test {
20 public:
21  content::ServiceWorkerContext* ServiceWorkerContext() {
22    return content::BrowserContext::GetDefaultStoragePartition(&profile_)->
23        GetServiceWorkerContext();
24  }
25
26 private:
27  content::TestBrowserThreadBundle thread_bundle_;
28  TestingProfile profile_;
29};
30
31TEST_F(CannedBrowsingDataServiceWorkerHelperTest, Empty) {
32  const GURL origin("https://host1:1/");
33  std::vector<GURL> scopes;
34  scopes.push_back(GURL("https://host1:1/*"));
35
36  scoped_refptr<CannedBrowsingDataServiceWorkerHelper> helper(
37      new CannedBrowsingDataServiceWorkerHelper(ServiceWorkerContext()));
38
39  ASSERT_TRUE(helper->empty());
40  helper->AddServiceWorker(origin, scopes);
41  ASSERT_FALSE(helper->empty());
42  helper->Reset();
43  ASSERT_TRUE(helper->empty());
44}
45
46TEST_F(CannedBrowsingDataServiceWorkerHelperTest, Delete) {
47  const GURL origin1("http://host1:9000");
48  std::vector<GURL> scopes1;
49  scopes1.push_back(GURL("http://host1:9000/*"));
50
51  const GURL origin2("https://example.com");
52  std::vector<GURL> scopes2;
53  scopes2.push_back(GURL("https://example.com/app1/*"));
54  scopes2.push_back(GURL("https://example.com/app2/*"));
55
56  scoped_refptr<CannedBrowsingDataServiceWorkerHelper> helper(
57      new CannedBrowsingDataServiceWorkerHelper(ServiceWorkerContext()));
58
59  EXPECT_TRUE(helper->empty());
60  helper->AddServiceWorker(origin1, scopes1);
61  helper->AddServiceWorker(origin2, scopes2);
62  EXPECT_EQ(2u, helper->GetServiceWorkerCount());
63  helper->DeleteServiceWorkers(origin2);
64  EXPECT_EQ(1u, helper->GetServiceWorkerCount());
65}
66
67TEST_F(CannedBrowsingDataServiceWorkerHelperTest, IgnoreExtensionsAndDevTools) {
68  const GURL origin1("chrome-extension://abcdefghijklmnopqrstuvwxyz/");
69  const GURL origin2("chrome-devtools://abcdefghijklmnopqrstuvwxyz/");
70  const std::vector<GURL> scopes;
71
72  scoped_refptr<CannedBrowsingDataServiceWorkerHelper> helper(
73      new CannedBrowsingDataServiceWorkerHelper(ServiceWorkerContext()));
74
75  ASSERT_TRUE(helper->empty());
76  helper->AddServiceWorker(origin1, scopes);
77  ASSERT_TRUE(helper->empty());
78  helper->AddServiceWorker(origin2, scopes);
79  ASSERT_TRUE(helper->empty());
80}
81
82}  // namespace
83