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

Side by Side Diff: chrome/browser/download/download_request_limiter_unittest.cc

Issue 10412061: Fix crashes in DownloadRequestLimiter when extension popups/bubbles initiate downloads automatically (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge Created 8 years, 6 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "chrome/browser/download/download_request_limiter.h" 6 #include "chrome/browser/download/download_request_limiter.h"
7 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
7 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" 8 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h"
8 #include "chrome/test/base/testing_profile.h" 9 #include "chrome/test/base/testing_profile.h"
9 #include "content/public/browser/navigation_controller.h" 10 #include "content/public/browser/navigation_controller.h"
11 #include "content/public/browser/web_contents.h"
10 #include "content/test/test_browser_thread.h" 12 #include "content/test/test_browser_thread.h"
11 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
12 14
13 using content::BrowserThread; 15 using content::BrowserThread;
16 using content::WebContents;
14 17
15 class DownloadRequestLimiterTest : public TabContentsWrapperTestHarness { 18 class DownloadRequestLimiterTest : public TabContentsWrapperTestHarness {
16 public: 19 public:
17 DownloadRequestLimiterTest() 20 DownloadRequestLimiterTest()
18 : ui_thread_(BrowserThread::UI, &message_loop_), 21 : ui_thread_(BrowserThread::UI, &message_loop_),
19 file_user_blocking_thread_( 22 file_user_blocking_thread_(
20 BrowserThread::FILE_USER_BLOCKING, &message_loop_), 23 BrowserThread::FILE_USER_BLOCKING, &message_loop_),
21 io_thread_(BrowserThread::IO, &message_loop_) { 24 io_thread_(BrowserThread::IO, &message_loop_) {
22 } 25 }
23 26
24 virtual void SetUp() { 27 virtual void SetUp() {
25 TabContentsWrapperTestHarness::SetUp(); 28 TabContentsWrapperTestHarness::SetUp();
26 29
27 allow_download_ = true; 30 allow_download_ = true;
28 ask_allow_count_ = cancel_count_ = continue_count_ = 0; 31 ask_allow_count_ = cancel_count_ = continue_count_ = 0;
29 32
30 download_request_limiter_ = new DownloadRequestLimiter(); 33 download_request_limiter_ = new DownloadRequestLimiter();
31 test_delegate_.reset(new DownloadRequestLimiterTestDelegate(this)); 34 test_delegate_.reset(new DownloadRequestLimiterTestDelegate(this));
32 DownloadRequestLimiter::SetTestingDelegate(test_delegate_.get()); 35 DownloadRequestLimiter::SetTestingDelegate(test_delegate_.get());
33 } 36 }
34 37
35 virtual void TearDown() { 38 virtual void TearDown() {
36 DownloadRequestLimiter::SetTestingDelegate(NULL); 39 UnsetDelegate();
37
38 TabContentsWrapperTestHarness::TearDown(); 40 TabContentsWrapperTestHarness::TearDown();
39 } 41 }
40 42
43 virtual void UnsetDelegate() {
44 DownloadRequestLimiter::SetTestingDelegate(NULL);
45 }
46
41 void CanDownload() { 47 void CanDownload() {
48 CanDownloadFor(web_contents());
49 }
50
51 void CanDownloadFor(WebContents* web_contents) {
42 download_request_limiter_->CanDownloadImpl( 52 download_request_limiter_->CanDownloadImpl(
43 contents_wrapper(), 53 web_contents,
44 -1, // request id 54 -1, // request id
45 "GET", // request method 55 "GET", // request method
46 base::Bind(&DownloadRequestLimiterTest::ContinueDownload, 56 base::Bind(&DownloadRequestLimiterTest::ContinueDownload,
47 base::Unretained(this))); 57 base::Unretained(this)));
48 message_loop_.RunAllPending(); 58 message_loop_.RunAllPending();
49 } 59 }
50 60
61 void OnUserGesture() {
62 OnUserGestureFor(web_contents());
63 }
64
65 void OnUserGestureFor(WebContents* web_contents) {
66 DownloadRequestLimiter::TabDownloadState* state =
67 download_request_limiter_->GetDownloadState(web_contents, NULL, false);
68 if (state)
69 state->DidGetUserGesture();
70 }
71
51 bool ShouldAllowDownload() { 72 bool ShouldAllowDownload() {
52 ask_allow_count_++; 73 ask_allow_count_++;
53 return allow_download_; 74 return allow_download_;
54 } 75 }
55 76
56 protected: 77 protected:
57 void ContinueDownload(bool allow) { 78 void ContinueDownload(bool allow) {
58 if (allow) { 79 if (allow) {
59 continue_count_++; 80 continue_count_++;
60 } else { 81 } else {
(...skipping 29 matching lines...) Expand all
90 bool allow_download_; 111 bool allow_download_;
91 112
92 // Number of times ShouldAllowDownload was invoked. 113 // Number of times ShouldAllowDownload was invoked.
93 int ask_allow_count_; 114 int ask_allow_count_;
94 115
95 content::TestBrowserThread ui_thread_; 116 content::TestBrowserThread ui_thread_;
96 content::TestBrowserThread file_user_blocking_thread_; 117 content::TestBrowserThread file_user_blocking_thread_;
97 content::TestBrowserThread io_thread_; 118 content::TestBrowserThread io_thread_;
98 }; 119 };
99 120
100 TEST_F(DownloadRequestLimiterTest, Allow) { 121 TEST_F(DownloadRequestLimiterTest,
122 DownloadRequestLimiter_Allow) {
101 // All tabs should initially start at ALLOW_ONE_DOWNLOAD. 123 // All tabs should initially start at ALLOW_ONE_DOWNLOAD.
102 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, 124 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
103 download_request_limiter_->GetDownloadStatus(contents())); 125 download_request_limiter_->GetDownloadStatus(contents()));
104 126
105 // Ask if the tab can do a download. This moves to PROMPT_BEFORE_DOWNLOAD. 127 // Ask if the tab can do a download. This moves to PROMPT_BEFORE_DOWNLOAD.
106 CanDownload(); 128 CanDownload();
107 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, 129 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
108 download_request_limiter_->GetDownloadStatus(contents())); 130 download_request_limiter_->GetDownloadStatus(contents()));
109 // We should have been told we can download. 131 // We should have been told we can download.
110 ASSERT_EQ(1, continue_count_); 132 ASSERT_EQ(1, continue_count_);
(...skipping 19 matching lines...) Expand all
130 // The state is at allow_all, which means the delegate shouldn't be asked. 152 // The state is at allow_all, which means the delegate shouldn't be asked.
131 ASSERT_EQ(0, ask_allow_count_); 153 ASSERT_EQ(0, ask_allow_count_);
132 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, 154 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
133 download_request_limiter_->GetDownloadStatus(contents())); 155 download_request_limiter_->GetDownloadStatus(contents()));
134 // We should have been told we can download. 156 // We should have been told we can download.
135 ASSERT_EQ(1, continue_count_); 157 ASSERT_EQ(1, continue_count_);
136 ASSERT_EQ(0, cancel_count_); 158 ASSERT_EQ(0, cancel_count_);
137 continue_count_ = 0; 159 continue_count_ = 0;
138 } 160 }
139 161
140 TEST_F(DownloadRequestLimiterTest, ResetOnNavigation) { 162 TEST_F(DownloadRequestLimiterTest,
163 DownloadRequestLimiter_ResetOnNavigation) {
141 NavigateAndCommit(GURL("http://foo.com/bar")); 164 NavigateAndCommit(GURL("http://foo.com/bar"));
142 165
143 // Do two downloads, allowing the second so that we end up with allow all. 166 // Do two downloads, allowing the second so that we end up with allow all.
144 CanDownload(); 167 CanDownload();
145 allow_download_ = true; 168 allow_download_ = true;
146 CanDownload(); 169 CanDownload();
147 ask_allow_count_ = continue_count_ = cancel_count_ = 0; 170 ask_allow_count_ = continue_count_ = cancel_count_ = 0;
148 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, 171 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
149 download_request_limiter_->GetDownloadStatus(contents())); 172 download_request_limiter_->GetDownloadStatus(contents()));
150 173
151 // Navigate to a new URL with the same host, which shouldn't reset the allow 174 // Navigate to a new URL with the same host, which shouldn't reset the allow
152 // all state. 175 // all state.
153 NavigateAndCommit(GURL("http://foo.com/bar2")); 176 NavigateAndCommit(GURL("http://foo.com/bar2"));
154 CanDownload(); 177 CanDownload();
155 ASSERT_EQ(1, continue_count_); 178 ASSERT_EQ(1, continue_count_);
156 ASSERT_EQ(0, cancel_count_); 179 ASSERT_EQ(0, cancel_count_);
157 ASSERT_EQ(0, ask_allow_count_); 180 ASSERT_EQ(0, ask_allow_count_);
158 ask_allow_count_ = continue_count_ = cancel_count_ = 0; 181 ask_allow_count_ = continue_count_ = cancel_count_ = 0;
159 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, 182 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
160 download_request_limiter_->GetDownloadStatus(contents())); 183 download_request_limiter_->GetDownloadStatus(contents()));
161 184
162 // Do a user gesture, because we're at allow all, this shouldn't change the 185 // Do a user gesture, because we're at allow all, this shouldn't change the
163 // state. 186 // state.
164 download_request_limiter_->OnUserGesture(contents()); 187 OnUserGesture();
165 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, 188 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
166 download_request_limiter_->GetDownloadStatus(contents())); 189 download_request_limiter_->GetDownloadStatus(contents()));
167 190
168 // Navigate to a completely different host, which should reset the state. 191 // Navigate to a completely different host, which should reset the state.
169 NavigateAndCommit(GURL("http://fooey.com")); 192 NavigateAndCommit(GURL("http://fooey.com"));
170 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, 193 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
171 download_request_limiter_->GetDownloadStatus(contents())); 194 download_request_limiter_->GetDownloadStatus(contents()));
172 } 195 }
173 196
174 TEST_F(DownloadRequestLimiterTest, ResetOnUserGesture) { 197 TEST_F(DownloadRequestLimiterTest,
198 DownloadRequestLimiter_ResetOnUserGesture) {
175 NavigateAndCommit(GURL("http://foo.com/bar")); 199 NavigateAndCommit(GURL("http://foo.com/bar"));
176 200
177 // Do one download, which should change to prompt before download. 201 // Do one download, which should change to prompt before download.
178 CanDownload(); 202 CanDownload();
179 ask_allow_count_ = continue_count_ = cancel_count_ = 0; 203 ask_allow_count_ = continue_count_ = cancel_count_ = 0;
180 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, 204 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
181 download_request_limiter_->GetDownloadStatus(contents())); 205 download_request_limiter_->GetDownloadStatus(contents()));
182 206
183 // Do a user gesture, which should reset back to allow one. 207 // Do a user gesture, which should reset back to allow one.
184 download_request_limiter_->OnUserGesture(contents()); 208 OnUserGesture();
185 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, 209 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
186 download_request_limiter_->GetDownloadStatus(contents())); 210 download_request_limiter_->GetDownloadStatus(contents()));
187 211
188 // Ask twice, which triggers calling the delegate. Don't allow the download 212 // Ask twice, which triggers calling the delegate. Don't allow the download
189 // so that we end up with not allowed. 213 // so that we end up with not allowed.
190 allow_download_ = false; 214 allow_download_ = false;
191 CanDownload(); 215 CanDownload();
192 CanDownload(); 216 CanDownload();
193 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, 217 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
194 download_request_limiter_->GetDownloadStatus(contents())); 218 download_request_limiter_->GetDownloadStatus(contents()));
195 219
196 // A user gesture now should NOT change the state. 220 // A user gesture now should NOT change the state.
197 download_request_limiter_->OnUserGesture(contents()); 221 OnUserGesture();
198 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, 222 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
199 download_request_limiter_->GetDownloadStatus(contents())); 223 download_request_limiter_->GetDownloadStatus(contents()));
200 // And make sure we really can't download. 224 // And make sure we really can't download.
201 ask_allow_count_ = continue_count_ = cancel_count_ = 0; 225 ask_allow_count_ = continue_count_ = cancel_count_ = 0;
202 CanDownload(); 226 CanDownload();
203 ASSERT_EQ(0, ask_allow_count_); 227 ASSERT_EQ(0, ask_allow_count_);
204 ASSERT_EQ(0, continue_count_); 228 ASSERT_EQ(0, continue_count_);
205 ASSERT_EQ(1, cancel_count_); 229 ASSERT_EQ(1, cancel_count_);
206 // And the state shouldn't have changed. 230 // And the state shouldn't have changed.
207 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, 231 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
208 download_request_limiter_->GetDownloadStatus(contents())); 232 download_request_limiter_->GetDownloadStatus(contents()));
209 } 233 }
234
235 TEST_F(DownloadRequestLimiterTest,
236 DownloadRequestLimiter_RawWebContents) {
237 // By-pass TabContentsWrapperTestHarness and use
238 // RenderViewHostTestHarness::CreateTestWebContents() directly so that there
239 // will be no TabContentsWrapper for web_contents.
240 scoped_ptr<WebContents> web_contents(CreateTestWebContents());
241 TabContentsWrapper* tab_wrapper =
242 TabContentsWrapper::GetCurrentWrapperForContents(web_contents.get());
243 ASSERT_TRUE(tab_wrapper == NULL);
244 // DRL won't try to make an infobar if it doesn't have a TCW, and we want to
245 // test that it will Cancel() instead of prompting when it doesn't have a TCW,
246 // so unset the delegate.
247 UnsetDelegate();
248 EXPECT_EQ(0, continue_count_);
249 EXPECT_EQ(0, cancel_count_);
250 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
251 download_request_limiter_->GetDownloadStatus(web_contents.get()));
252 // You get one freebie.
253 CanDownloadFor(web_contents.get());
254 EXPECT_EQ(1, continue_count_);
255 EXPECT_EQ(0, cancel_count_);
256 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
257 download_request_limiter_->GetDownloadStatus(web_contents.get()));
258 OnUserGestureFor(web_contents.get());
259 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
260 download_request_limiter_->GetDownloadStatus(web_contents.get()));
261 CanDownloadFor(web_contents.get());
262 EXPECT_EQ(2, continue_count_);
263 EXPECT_EQ(0, cancel_count_);
264 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
265 download_request_limiter_->GetDownloadStatus(web_contents.get()));
266 CanDownloadFor(web_contents.get());
267 EXPECT_EQ(2, continue_count_);
268 EXPECT_EQ(1, cancel_count_);
269 EXPECT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
270 download_request_limiter_->GetDownloadStatus(web_contents.get()));
271 OnUserGestureFor(web_contents.get());
272 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
273 download_request_limiter_->GetDownloadStatus(web_contents.get()));
274 CanDownloadFor(web_contents.get());
275 EXPECT_EQ(3, continue_count_);
276 EXPECT_EQ(1, cancel_count_);
277 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
278 download_request_limiter_->GetDownloadStatus(web_contents.get()));
279 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_request_limiter_observer.cc ('k') | chrome/browser/ui/tab_contents/tab_contents_wrapper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698