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

Side by Side Diff: chrome/browser/prerender/prerender_resource_handler_unittest.cc

Issue 6136006: Fix leak in PrerenderResourceHandlerTest. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged Created 9 years, 11 months 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | tools/heapcheck/suppressions.txt » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/browser/prerender/prerender_resource_handler.h" 5 #include "chrome/browser/prerender/prerender_resource_handler.h"
6 #include "chrome/common/resource_response.h" 6 #include "chrome/common/resource_response.h"
7 #include "net/http/http_response_headers.h" 7 #include "net/http/http_response_headers.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 namespace { 10 namespace {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 } 80 }
81 return new net::HttpResponseHeaders(headers); 81 return new net::HttpResponseHeaders(headers);
82 } 82 }
83 83
84 } // namespace 84 } // namespace
85 85
86 class PrerenderResourceHandlerTest : public testing::Test { 86 class PrerenderResourceHandlerTest : public testing::Test {
87 protected: 87 protected:
88 PrerenderResourceHandlerTest() 88 PrerenderResourceHandlerTest()
89 : prerender_duration_(base::TimeDelta::FromSeconds(10)), 89 : prerender_duration_(base::TimeDelta::FromSeconds(10)),
90 mock_handler_(new MockResourceHandler()),
91 ALLOW_THIS_IN_INITIALIZER_LIST( 90 ALLOW_THIS_IN_INITIALIZER_LIST(
92 pre_handler_(new PrerenderResourceHandler( 91 pre_handler_(new PrerenderResourceHandler(
93 mock_handler_, 92 new MockResourceHandler(),
94 NewCallback( 93 NewCallback(
95 this, 94 this,
96 &PrerenderResourceHandlerTest::SetLastHandledURL)))), 95 &PrerenderResourceHandlerTest::SetLastHandledURL)))),
97 ui_thread_(BrowserThread::UI, &loop_), 96 ui_thread_(BrowserThread::UI, &loop_),
97 io_thread_(BrowserThread::IO, &loop_),
98 default_url_("http://www.prerender.com") { 98 default_url_("http://www.prerender.com") {
99 pre_handler_->set_prerender_duration(prerender_duration_); 99 pre_handler_->set_prerender_duration(prerender_duration_);
100 pre_handler_->set_get_current_time_function(&FixedGetCurrentTime); 100 pre_handler_->set_get_current_time_function(&FixedGetCurrentTime);
101 } 101 }
102 102
103 virtual ~PrerenderResourceHandlerTest() {
104 // When a ResourceHandler's reference count drops to 0, it is not
105 // deleted immediately. Instead, a task is posted to the IO thread's
106 // message loop to delete it.
107 // So, drop the reference count to 0 and run the message loop once
108 // to ensure that all resources are cleaned up before the test exits.
109 pre_handler_ = NULL;
110 loop_.RunAllPending();
111 }
112
103 void SetLastHandledURL(const GURL& url, const std::vector<GURL>& alias_urls) { 113 void SetLastHandledURL(const GURL& url, const std::vector<GURL>& alias_urls) {
104 last_handled_url_ = url; 114 last_handled_url_ = url;
105 alias_urls_ = alias_urls; 115 alias_urls_ = alias_urls;
106 } 116 }
107 117
108 // Common logic shared by many of the tests 118 // Common logic shared by many of the tests
109 void StartPrerendering(const std::string& mime_type, 119 void StartPrerendering(const std::string& mime_type,
110 const char* headers) { 120 const char* headers) {
111 int request_id = 1; 121 int request_id = 1;
112 bool defer = false; 122 bool defer = false;
113 EXPECT_TRUE(pre_handler_->OnWillStart(request_id, default_url_, &defer)); 123 EXPECT_TRUE(pre_handler_->OnWillStart(request_id, default_url_, &defer));
114 EXPECT_FALSE(defer); 124 EXPECT_FALSE(defer);
115 scoped_refptr<ResourceResponse> response(new ResourceResponse); 125 scoped_refptr<ResourceResponse> response(new ResourceResponse);
116 response->response_head.request_time = FixedGetCurrentTime(); 126 response->response_head.request_time = FixedGetCurrentTime();
117 response->response_head.response_time = FixedGetCurrentTime(); 127 response->response_head.response_time = FixedGetCurrentTime();
118 response->response_head.mime_type = mime_type; 128 response->response_head.mime_type = mime_type;
119 response->response_head.headers = CreateResponseHeaders(headers); 129 response->response_head.headers = CreateResponseHeaders(headers);
120 EXPECT_TRUE(last_handled_url_.is_empty()); 130 EXPECT_TRUE(last_handled_url_.is_empty());
121 131
122 // Start the response. If it is able to prerender, a task will 132 // Start the response. If it is able to prerender, a task will
123 // be posted to loop_ (masquerading as the UI thread), and 133 // be posted to the UI thread and |SetLastHandledURL| will be called.
124 // |SetLastHandledURL| will be called.
125 EXPECT_TRUE(pre_handler_->OnResponseStarted(request_id, response)); 134 EXPECT_TRUE(pre_handler_->OnResponseStarted(request_id, response));
126 loop_.RunAllPending(); 135 loop_.RunAllPending();
127 } 136 }
128 137
129 // Test whether a given URL is part of alias_urls_. 138 // Test whether a given URL is part of alias_urls_.
130 bool ContainsAliasURL(const GURL& url) { 139 bool ContainsAliasURL(const GURL& url) {
131 return std::find(alias_urls_.begin(), alias_urls_.end(), url) 140 return std::find(alias_urls_.begin(), alias_urls_.end(), url)
132 != alias_urls_.end(); 141 != alias_urls_.end();
133 } 142 }
134 143
135 base::TimeDelta prerender_duration_; 144 base::TimeDelta prerender_duration_;
136 scoped_refptr<MockResourceHandler> mock_handler_;
137 scoped_refptr<PrerenderResourceHandler> pre_handler_; 145 scoped_refptr<PrerenderResourceHandler> pre_handler_;
138 MessageLoop loop_; 146 MessageLoop loop_;
139 BrowserThread ui_thread_; 147 BrowserThread ui_thread_;
148 BrowserThread io_thread_;
140 GURL last_handled_url_; 149 GURL last_handled_url_;
141 GURL default_url_; 150 GURL default_url_;
142 std::vector<GURL> alias_urls_; 151 std::vector<GURL> alias_urls_;
143 }; 152 };
144 153
145 namespace { 154 namespace {
146 155
147 TEST_F(PrerenderResourceHandlerTest, NoOp) { 156 TEST_F(PrerenderResourceHandlerTest, NoOp) {
148 } 157 }
149 158
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 EXPECT_TRUE(pre_handler_->OnResponseStarted(request_id, response)); 224 EXPECT_TRUE(pre_handler_->OnResponseStarted(request_id, response));
216 EXPECT_TRUE(last_handled_url_.is_empty()); 225 EXPECT_TRUE(last_handled_url_.is_empty());
217 loop_.RunAllPending(); 226 loop_.RunAllPending();
218 EXPECT_EQ(url_redirect, last_handled_url_); 227 EXPECT_EQ(url_redirect, last_handled_url_);
219 EXPECT_EQ(true, ContainsAliasURL(url_redirect)); 228 EXPECT_EQ(true, ContainsAliasURL(url_redirect));
220 EXPECT_EQ(true, ContainsAliasURL(default_url_)); 229 EXPECT_EQ(true, ContainsAliasURL(default_url_));
221 EXPECT_EQ(2, static_cast<int>(alias_urls_.size())); 230 EXPECT_EQ(2, static_cast<int>(alias_urls_.size()));
222 } 231 }
223 232
224 } 233 }
OLDNEW
« no previous file with comments | « no previous file | tools/heapcheck/suppressions.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698