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 "base/basictypes.h"
6#include "base/memory/scoped_ptr.h"
7#include "base/strings/string16.h"
8#include "base/strings/utf_string_conversions.h"
9#include "content/browser/shared_worker/shared_worker_instance.h"
10#include "content/browser/shared_worker/worker_storage_partition.h"
11#include "content/public/test/test_browser_context.h"
12#include "content/public/test/test_browser_thread_bundle.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15namespace content {
16
17class SharedWorkerInstanceTest : public testing::Test {
18 protected:
19  SharedWorkerInstanceTest()
20      : browser_context_(new TestBrowserContext()),
21        partition_(
22            new WorkerStoragePartition(browser_context_->GetRequestContext(),
23                                       NULL,
24                                       NULL,
25                                       NULL,
26                                       NULL,
27                                       NULL,
28                                       NULL,
29                                       NULL)),
30        partition_id_(*partition_.get()) {}
31
32  bool Matches(const SharedWorkerInstance& instance,
33               const std::string& url,
34               const base::StringPiece& name) {
35    return instance.Matches(GURL(url),
36                            base::ASCIIToUTF16(name),
37                            partition_id_,
38                            browser_context_->GetResourceContext());
39  }
40
41  TestBrowserThreadBundle thread_bundle_;
42  scoped_ptr<TestBrowserContext> browser_context_;
43  scoped_ptr<WorkerStoragePartition> partition_;
44  const WorkerStoragePartitionId partition_id_;
45
46  DISALLOW_COPY_AND_ASSIGN(SharedWorkerInstanceTest);
47};
48
49TEST_F(SharedWorkerInstanceTest, MatchesTest) {
50  SharedWorkerInstance instance1(GURL("http://example.com/w.js"),
51                                 base::string16(),
52                                 base::string16(),
53                                 blink::WebContentSecurityPolicyTypeReport,
54                                 browser_context_->GetResourceContext(),
55                                 partition_id_);
56  EXPECT_TRUE(Matches(instance1, "http://example.com/w.js", ""));
57  EXPECT_FALSE(Matches(instance1, "http://example.com/w2.js", ""));
58  EXPECT_FALSE(Matches(instance1, "http://example.net/w.js", ""));
59  EXPECT_FALSE(Matches(instance1, "http://example.net/w2.js", ""));
60  EXPECT_FALSE(Matches(instance1, "http://example.com/w.js", "name"));
61  EXPECT_FALSE(Matches(instance1, "http://example.com/w2.js", "name"));
62  EXPECT_FALSE(Matches(instance1, "http://example.net/w.js", "name"));
63  EXPECT_FALSE(Matches(instance1, "http://example.net/w2.js", "name"));
64
65  SharedWorkerInstance instance2(GURL("http://example.com/w.js"),
66                                 base::ASCIIToUTF16("name"),
67                                 base::string16(),
68                                 blink::WebContentSecurityPolicyTypeReport,
69                                 browser_context_->GetResourceContext(),
70                                 partition_id_);
71  EXPECT_FALSE(Matches(instance2, "http://example.com/w.js", ""));
72  EXPECT_FALSE(Matches(instance2, "http://example.com/w2.js", ""));
73  EXPECT_FALSE(Matches(instance2, "http://example.net/w.js", ""));
74  EXPECT_FALSE(Matches(instance2, "http://example.net/w2.js", ""));
75  EXPECT_TRUE(Matches(instance2, "http://example.com/w.js", "name"));
76  EXPECT_TRUE(Matches(instance2, "http://example.com/w2.js", "name"));
77  EXPECT_FALSE(Matches(instance2, "http://example.net/w.js", "name"));
78  EXPECT_FALSE(Matches(instance2, "http://example.net/w2.js", "name"));
79  EXPECT_FALSE(Matches(instance2, "http://example.com/w.js", "name2"));
80  EXPECT_FALSE(Matches(instance2, "http://example.com/w2.js", "name2"));
81  EXPECT_FALSE(Matches(instance2, "http://example.net/w.js", "name2"));
82  EXPECT_FALSE(Matches(instance2, "http://example.net/w2.js", "name2"));
83}
84
85}  // namespace content
86