Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(563)

Side by Side Diff: net/url_request/sdch_dictionary_fetcher_unittest.cc

Issue 664263002: Restructure SDCH layering to allow more separation (observer/1->[0,n] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync'd to p300953. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/sdch_dictionary_fetcher.h" 5 #include "net/url_request/sdch_dictionary_fetcher.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h"
9 #include "base/run_loop.h" 10 #include "base/run_loop.h"
10 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
11 #include "base/thread_task_runner_handle.h" 12 #include "base/thread_task_runner_handle.h"
12 #include "net/base/sdch_manager.h" 13 #include "net/base/sdch_manager.h"
13 #include "net/url_request/url_request_data_job.h" 14 #include "net/url_request/url_request_data_job.h"
14 #include "net/url_request/url_request_filter.h" 15 #include "net/url_request/url_request_filter.h"
15 #include "net/url_request/url_request_interceptor.h" 16 #include "net/url_request/url_request_interceptor.h"
16 #include "net/url_request/url_request_test_util.h" 17 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 #include "url/gurl.h" 19 #include "url/gurl.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 GURL url(request_->url()); 67 GURL url(request_->url());
67 *data = ExpectedResponseForURL(url); 68 *data = ExpectedResponseForURL(url);
68 return OK; 69 return OK;
69 } 70 }
70 71
71 static int jobs_requested_; 72 static int jobs_requested_;
72 }; 73 };
73 74
74 int URLRequestSpecifiedResponseJob::jobs_requested_(0); 75 int URLRequestSpecifiedResponseJob::jobs_requested_(0);
75 76
76 class SdchTestDelegate : public SdchFetcher::Delegate { 77 class SdchDictionaryFetcherTest : public ::testing::Test {
77 public: 78 public:
78 struct DictionaryAdditions { 79 struct DictionaryAdditions {
79 DictionaryAdditions(const std::string& dictionary_text, 80 DictionaryAdditions(const std::string& dictionary_text,
80 const GURL& dictionary_url) 81 const GURL& dictionary_url)
81 : dictionary_text(dictionary_text), 82 : dictionary_text(dictionary_text),
82 dictionary_url(dictionary_url) {} 83 dictionary_url(dictionary_url) {}
83 84
84 85
85 std::string dictionary_text; 86 std::string dictionary_text;
86 GURL dictionary_url; 87 GURL dictionary_url;
87 }; 88 };
88 89
89 void AddSdchDictionary(const std::string& dictionary_text, 90 SdchDictionaryFetcherTest() {}
90 const GURL& dictionary_url) override { 91
92 virtual void SetUp() override {
93 DCHECK(!fetcher_.get());
94
95 URLRequestSpecifiedResponseJob::AddUrlHandler();
96 context_.reset(new TestURLRequestContext);
97 fetcher_.reset(new SdchDictionaryFetcher(
98 context_.get(),
99 base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched,
100 base::Unretained(this))));
101 }
102
103 virtual void TearDown() override {
104 URLRequestSpecifiedResponseJob::RemoveUrlHandler();
105 fetcher_.reset();
106 context_.reset();
107 }
108
109 void OnDictionaryFetched(const std::string& dictionary_text,
110 const GURL& dictionary_url) {
91 dictionary_additions.push_back( 111 dictionary_additions.push_back(
92 DictionaryAdditions(dictionary_text, dictionary_url)); 112 DictionaryAdditions(dictionary_text, dictionary_url));
93 } 113 }
94 114
95 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) { 115 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) {
96 out->swap(dictionary_additions); 116 out->swap(dictionary_additions);
97 dictionary_additions.clear(); 117 dictionary_additions.clear();
98 } 118 }
99 119
100 private:
101 std::vector<DictionaryAdditions> dictionary_additions;
102 };
103
104 class SdchDictionaryFetcherTest : public ::testing::Test {
105 public:
106 SdchDictionaryFetcherTest() {}
107
108 virtual void SetUp() override {
109 DCHECK(!fetcher_.get());
110
111 URLRequestSpecifiedResponseJob::AddUrlHandler();
112 fetcher_delegate_.reset(new SdchTestDelegate);
113 context_.reset(new TestURLRequestContext);
114 fetcher_.reset(new SdchDictionaryFetcher(
115 fetcher_delegate_.get(), context_.get()));
116 }
117
118 virtual void TearDown() override {
119 URLRequestSpecifiedResponseJob::RemoveUrlHandler();
120 fetcher_.reset();
121 context_.reset();
122 fetcher_delegate_.reset();
123 }
124
125 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } 120 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); }
126 SdchTestDelegate* manager() { return fetcher_delegate_.get(); }
127 121
128 // May not be called outside the SetUp()/TearDown() interval. 122 // May not be called outside the SetUp()/TearDown() interval.
129 int JobsRequested() { 123 int JobsRequested() {
130 return URLRequestSpecifiedResponseJob::jobs_requested(); 124 return URLRequestSpecifiedResponseJob::jobs_requested();
131 } 125 }
132 126
133 GURL PathToGurl(const char* path) { 127 GURL PathToGurl(const char* path) {
134 std::string gurl_string("http://"); 128 std::string gurl_string("http://");
135 gurl_string += kTestDomain; 129 gurl_string += kTestDomain;
136 gurl_string += "/"; 130 gurl_string += "/";
137 gurl_string += path; 131 gurl_string += path;
138 return GURL(gurl_string); 132 return GURL(gurl_string);
139 } 133 }
140 134
141 private: 135 private:
142 scoped_ptr<SdchTestDelegate> fetcher_delegate_;
143 scoped_ptr<TestURLRequestContext> context_; 136 scoped_ptr<TestURLRequestContext> context_;
144 scoped_ptr<SdchDictionaryFetcher> fetcher_; 137 scoped_ptr<SdchDictionaryFetcher> fetcher_;
138 std::vector<DictionaryAdditions> dictionary_additions;
145 }; 139 };
146 140
147 // Schedule a fetch and make sure it happens. 141 // Schedule a fetch and make sure it happens.
148 TEST_F(SdchDictionaryFetcherTest, Basic) { 142 TEST_F(SdchDictionaryFetcherTest, Basic) {
149 GURL dictionary_url(PathToGurl("dictionary")); 143 GURL dictionary_url(PathToGurl("dictionary"));
150 fetcher()->Schedule(dictionary_url); 144 fetcher()->Schedule(dictionary_url);
151 145
152 base::RunLoop().RunUntilIdle(); 146 base::RunLoop().RunUntilIdle();
153 EXPECT_EQ(1, JobsRequested()); 147 EXPECT_EQ(1, JobsRequested());
154 std::vector<SdchTestDelegate::DictionaryAdditions> additions; 148 std::vector<DictionaryAdditions> additions;
155 manager()->GetDictionaryAdditions(&additions); 149 GetDictionaryAdditions(&additions);
156 ASSERT_EQ(1u, additions.size()); 150 ASSERT_EQ(1u, additions.size());
157 EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL( 151 EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL(
158 dictionary_url), additions[0].dictionary_text); 152 dictionary_url), additions[0].dictionary_text);
159 } 153 }
160 154
161 // Multiple fetches of the same URL should result in only one request. 155 // Multiple fetches of the same URL should result in only one request.
162 TEST_F(SdchDictionaryFetcherTest, Multiple) { 156 TEST_F(SdchDictionaryFetcherTest, Multiple) {
163 GURL dictionary_url(PathToGurl("dictionary")); 157 GURL dictionary_url(PathToGurl("dictionary"));
164 fetcher()->Schedule(dictionary_url); 158 fetcher()->Schedule(dictionary_url);
165 fetcher()->Schedule(dictionary_url); 159 fetcher()->Schedule(dictionary_url);
166 fetcher()->Schedule(dictionary_url); 160 fetcher()->Schedule(dictionary_url);
167 base::RunLoop().RunUntilIdle(); 161 base::RunLoop().RunUntilIdle();
168 162
169 EXPECT_EQ(1, JobsRequested()); 163 EXPECT_EQ(1, JobsRequested());
170 std::vector<SdchTestDelegate::DictionaryAdditions> additions; 164 std::vector<DictionaryAdditions> additions;
171 manager()->GetDictionaryAdditions(&additions); 165 GetDictionaryAdditions(&additions);
172 ASSERT_EQ(1u, additions.size()); 166 ASSERT_EQ(1u, additions.size());
173 EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL( 167 EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL(
174 dictionary_url), additions[0].dictionary_text); 168 dictionary_url), additions[0].dictionary_text);
175 } 169 }
176 170
177 // A cancel should result in no actual requests being generated. 171 // A cancel should result in no actual requests being generated.
178 TEST_F(SdchDictionaryFetcherTest, Cancel) { 172 TEST_F(SdchDictionaryFetcherTest, Cancel) {
179 GURL dictionary_url_1(PathToGurl("dictionary_1")); 173 GURL dictionary_url_1(PathToGurl("dictionary_1"));
180 GURL dictionary_url_2(PathToGurl("dictionary_2")); 174 GURL dictionary_url_2(PathToGurl("dictionary_2"));
181 GURL dictionary_url_3(PathToGurl("dictionary_3")); 175 GURL dictionary_url_3(PathToGurl("dictionary_3"));
182 176
183 fetcher()->Schedule(dictionary_url_1); 177 fetcher()->Schedule(dictionary_url_1);
184 fetcher()->Schedule(dictionary_url_2); 178 fetcher()->Schedule(dictionary_url_2);
185 fetcher()->Schedule(dictionary_url_3); 179 fetcher()->Schedule(dictionary_url_3);
186 fetcher()->Cancel(); 180 fetcher()->Cancel();
187 base::RunLoop().RunUntilIdle(); 181 base::RunLoop().RunUntilIdle();
188 182
189 // Synchronous execution may have resulted in a single job being scheduled. 183 // Synchronous execution may have resulted in a single job being scheduled.
190 EXPECT_GE(1, JobsRequested()); 184 EXPECT_GE(1, JobsRequested());
191 } 185 }
192 186
193 } 187 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698