OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <stdint.h> | |
6 | |
7 #include <list> | |
8 #include <string> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/strings/stringprintf.h" | |
12 #include "media/blink/url_index.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace media { | |
16 | |
17 class UrlIndexTest : public testing::Test { | |
18 public: | |
19 UrlIndexTest() : url_index_(NULL) {} | |
xhwang
2015/11/19 23:34:19
nit: s/NULL/nullptr
hubbe
2015/11/20 23:24:24
Done.
| |
20 | |
21 scoped_refptr<UrlData> GetByUrl(const GURL& gurl, | |
22 UrlData::CORSMode cors_mode) { | |
23 scoped_refptr<UrlData> ret = url_index_.GetByUrl(gurl, cors_mode); | |
24 EXPECT_EQ(ret->url(), gurl); | |
25 EXPECT_EQ(ret->cors_mode(), cors_mode); | |
26 return ret; | |
27 } | |
28 | |
29 UrlIndex url_index_; | |
30 }; | |
31 | |
32 TEST_F(UrlIndexTest, SimpleTest) { | |
33 GURL url1("http://foo.bar.com"); | |
34 GURL url2("http://foo.bar.com/urgel"); | |
35 scoped_refptr<UrlData> a = GetByUrl(url1, UrlData::kUnspecified); | |
36 // Make sure it's valid, we still shouldn't get the same one. | |
37 a->Use(); | |
38 a->set_range_supported(); | |
39 EXPECT_TRUE(a->Valid()); | |
40 EXPECT_EQ(a, url_index_.TryInsert(a)); | |
41 scoped_refptr<UrlData> b = GetByUrl(url1, UrlData::kAnonymous); | |
42 b->Use(); | |
43 b->set_range_supported(); | |
44 EXPECT_TRUE(b->Valid()); | |
45 EXPECT_EQ(b, url_index_.TryInsert(b)); | |
46 scoped_refptr<UrlData> c = GetByUrl(url1, UrlData::kUseCredentials); | |
47 c->Use(); | |
48 c->set_range_supported(); | |
49 EXPECT_TRUE(c->Valid()); | |
50 EXPECT_EQ(c, url_index_.TryInsert(c)); | |
51 scoped_refptr<UrlData> d = GetByUrl(url2, UrlData::kUnspecified); | |
52 d->Use(); | |
53 d->set_range_supported(); | |
54 EXPECT_TRUE(d->Valid()); | |
55 EXPECT_EQ(d, url_index_.TryInsert(d)); | |
56 | |
57 EXPECT_NE(a, b); | |
58 EXPECT_NE(a, c); | |
59 EXPECT_NE(a, d); | |
60 EXPECT_NE(b, c); | |
61 EXPECT_NE(b, d); | |
62 EXPECT_NE(c, d); | |
63 EXPECT_EQ(a, GetByUrl(url1, UrlData::kUnspecified)); | |
64 EXPECT_EQ(b, GetByUrl(url1, UrlData::kAnonymous)); | |
65 EXPECT_EQ(c, GetByUrl(url1, UrlData::kUseCredentials)); | |
66 EXPECT_EQ(d, GetByUrl(url2, UrlData::kUnspecified)); | |
67 } | |
68 | |
69 TEST_F(UrlIndexTest, UrlDataTest) { | |
70 GURL url("http://foo.bar.com"); | |
71 scoped_refptr<UrlData> a = GetByUrl(url, UrlData::kUnspecified); | |
72 | |
73 // Check default values. | |
74 EXPECT_FALSE(a->range_supported()); | |
75 EXPECT_FALSE(a->cacheable()); | |
76 EXPECT_EQ(a->length(), kPositionNotSpecified); | |
77 EXPECT_FALSE(a->Valid()); | |
78 | |
79 a->set_length(117); | |
80 EXPECT_EQ(a->length(), 117); | |
81 | |
82 a->set_cacheable(true); | |
83 EXPECT_TRUE(a->cacheable()); | |
84 | |
85 base::TimeTicks now = base::TimeTicks::Now(); | |
86 base::TimeTicks valid_until = now + base::TimeDelta::FromSeconds(500); | |
87 a->set_valid_until(valid_until); | |
88 a->set_range_supported(); | |
89 EXPECT_EQ(valid_until, a->valid_until()); | |
90 EXPECT_TRUE(a->Valid()); | |
91 | |
92 base::Time time_now = base::Time::Now(); | |
93 base::Time last_modified = time_now - base::TimeDelta::FromSeconds(500); | |
94 a->set_last_modified(last_modified); | |
95 EXPECT_EQ(last_modified, a->last_modified()); | |
96 } | |
97 | |
98 TEST_F(UrlIndexTest, UseTest) { | |
99 GURL url("http://foo.bar.com"); | |
100 scoped_refptr<UrlData> a = GetByUrl(url, UrlData::kUnspecified); | |
101 EXPECT_FALSE(a->Valid()); | |
102 a->Use(); | |
103 a->set_range_supported(); | |
104 EXPECT_TRUE(a->Valid()); | |
105 } | |
106 | |
107 TEST_F(UrlIndexTest, TryInsert) { | |
108 GURL url("http://foo.bar.com"); | |
109 scoped_refptr<UrlData> a = GetByUrl(url, UrlData::kUnspecified); | |
110 scoped_refptr<UrlData> c = GetByUrl(url, UrlData::kUnspecified); | |
111 EXPECT_NE(a, c); | |
112 EXPECT_FALSE(a->Valid()); | |
113 base::Time now_time = base::Time::Now(); | |
114 base::Time last_modified = now_time - base::TimeDelta::FromSeconds(500); | |
115 base::TimeTicks now = base::TimeTicks::Now(); | |
116 base::TimeTicks valid_until = now + base::TimeDelta::FromSeconds(500); | |
117 | |
118 // Not sharable yet. (no ranges) | |
119 EXPECT_EQ(a, url_index_.TryInsert(a)); | |
120 EXPECT_NE(a, GetByUrl(url, UrlData::kUnspecified)); | |
121 a->set_last_modified(last_modified); | |
122 | |
123 // Not sharable yet. (no ranges) | |
124 EXPECT_EQ(a, url_index_.TryInsert(a)); | |
125 EXPECT_NE(a, GetByUrl(url, UrlData::kUnspecified)); | |
126 | |
127 // Now we should be able to insert it into the index. | |
128 a->set_range_supported(); | |
129 a->set_valid_until(valid_until); | |
130 EXPECT_TRUE(a->Valid()); | |
131 EXPECT_EQ(a, url_index_.TryInsert(a)); | |
132 EXPECT_EQ(a, GetByUrl(url, UrlData::kUnspecified)); | |
133 | |
134 // |a| becomes expired... | |
135 a->set_valid_until(now - base::TimeDelta::FromSeconds(1)); | |
136 scoped_refptr<UrlData> b = GetByUrl(url, UrlData::kUnspecified); | |
137 EXPECT_NE(a, b); | |
138 | |
139 b->set_range_supported(); | |
140 b->set_valid_until(valid_until); | |
141 b->set_last_modified(last_modified); | |
142 EXPECT_TRUE(b->Valid()); | |
143 | |
144 EXPECT_EQ(b, url_index_.TryInsert(b)); | |
145 EXPECT_EQ(b, GetByUrl(url, UrlData::kUnspecified)); | |
146 | |
147 c->set_range_supported(); | |
148 c->set_valid_until(valid_until); | |
149 c->set_last_modified(last_modified); | |
150 EXPECT_TRUE(c->Valid()); | |
151 | |
152 // B is still valid, so it should be preferred over C. | |
153 EXPECT_EQ(b, url_index_.TryInsert(c)); | |
154 EXPECT_EQ(b, GetByUrl(url, UrlData::kUnspecified)); | |
155 } | |
156 | |
157 } // namespace media | |
OLD | NEW |