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

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

Issue 9568003: Fixed issue with DownloadTestObserver. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed comment. Created 8 years, 9 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 <vector> 5 #include <vector>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "chrome/browser/download/download_test_observer.h" 11 #include "chrome/browser/download/download_test_observer.h"
12 #include "chrome/test/base/ui_test_utils.h" 12 #include "chrome/test/base/ui_test_utils.h"
13 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
14 14
15 using content::BrowserThread; 15 using content::BrowserThread;
16 using content::DownloadItem; 16 using content::DownloadItem;
17 using content::DownloadManager; 17 using content::DownloadManager;
18 18
19 // These functions take scoped_refptr's to DownloadManager because they 19 // These functions take scoped_refptr's to DownloadManager because they
20 // are posted to message queues, and hence may execute arbitrarily after 20 // are posted to message queues, and hence may execute arbitrarily after
21 // their actual posting. Once posted, there is no connection between 21 // their actual posting. Once posted, there is no connection between
22 // these routines and the DownloadTestObserver class from which they came, 22 // these routines and the DownloadTestObserver class from which
23 // so the DownloadTestObserver's reference to the DownloadManager cannot 23 // they came, so the DownloadTestObserver's reference to the
24 // be counted on to keep the DownloadManager around. 24 // DownloadManager cannot be counted on to keep the DownloadManager around.
25 25
26 // Fake user click on "Accept". 26 // Fake user click on "Accept".
27 void AcceptDangerousDownload(scoped_refptr<DownloadManager> download_manager, 27 void AcceptDangerousDownload(scoped_refptr<DownloadManager> download_manager,
28 int32 download_id) { 28 int32 download_id) {
29 DownloadItem* download = download_manager->GetDownloadItem(download_id); 29 DownloadItem* download = download_manager->GetDownloadItem(download_id);
30 download->DangerousDownloadValidated(); 30 download->DangerousDownloadValidated();
31 } 31 }
32 32
33 // Fake user click on "Deny". 33 // Fake user click on "Deny".
34 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager, 34 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager,
35 int32 download_id) { 35 int32 download_id) {
36 DownloadItem* download = download_manager->GetDownloadItem(download_id); 36 DownloadItem* download = download_manager->GetDownloadItem(download_id);
37 ASSERT_TRUE(download->IsPartialDownload()); 37 ASSERT_TRUE(download->IsPartialDownload());
38 download->Cancel(true); 38 download->Cancel(true);
39 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); 39 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD);
40 } 40 }
41 41
42 DownloadTestObserver::DownloadTestObserver( 42 DownloadTestObserver::DownloadTestObserver(
43 DownloadManager* download_manager, 43 DownloadManager* download_manager,
44 size_t wait_count, 44 size_t wait_count,
45 DownloadItem::DownloadState download_finished_state,
46 bool finish_on_select_file, 45 bool finish_on_select_file,
47 DangerousDownloadAction dangerous_download_action) 46 DangerousDownloadAction dangerous_download_action)
48 : download_manager_(download_manager), 47 : download_manager_(download_manager),
49 wait_count_(wait_count), 48 wait_count_(wait_count),
50 finished_downloads_at_construction_(0), 49 finished_downloads_at_construction_(0),
51 waiting_(false), 50 waiting_(false),
52 download_finished_state_(download_finished_state),
53 finish_on_select_file_(finish_on_select_file), 51 finish_on_select_file_(finish_on_select_file),
54 select_file_dialog_seen_(false), 52 select_file_dialog_seen_(false),
55 dangerous_download_action_(dangerous_download_action) { 53 dangerous_download_action_(dangerous_download_action) {
56 download_manager_->AddObserver(this); // Will call initial ModelChanged().
57 finished_downloads_at_construction_ = finished_downloads_.size();
58 EXPECT_NE(DownloadItem::REMOVING, download_finished_state)
59 << "Waiting for REMOVING is not supported. Try COMPLETE.";
60 } 54 }
61 55
62 DownloadTestObserver::~DownloadTestObserver() { 56 DownloadTestObserver::~DownloadTestObserver() {
63 for (DownloadSet::iterator it = downloads_observed_.begin(); 57 for (DownloadSet::iterator it = downloads_observed_.begin();
64 it != downloads_observed_.end(); ++it) 58 it != downloads_observed_.end(); ++it)
65 (*it)->RemoveObserver(this); 59 (*it)->RemoveObserver(this);
66 60
67 download_manager_->RemoveObserver(this); 61 download_manager_->RemoveObserver(this);
68 } 62 }
69 63
64 void DownloadTestObserver::Init() {
65 download_manager_->AddObserver(this); // Will call initial ModelChanged().
66 finished_downloads_at_construction_ = finished_downloads_.size();
67 states_observed_.clear();
68 }
69
70 void DownloadTestObserver::WaitForFinished() { 70 void DownloadTestObserver::WaitForFinished() {
71 if (!IsFinished()) { 71 if (!IsFinished()) {
72 waiting_ = true; 72 waiting_ = true;
73 ui_test_utils::RunMessageLoop(); 73 ui_test_utils::RunMessageLoop();
74 waiting_ = false; 74 waiting_ = false;
75 } 75 }
76 } 76 }
77 77
78 bool DownloadTestObserver::IsFinished() const { 78 bool DownloadTestObserver::IsFinished() const {
79 if (finished_downloads_.size() - finished_downloads_at_construction_ >= 79 if (finished_downloads_.size() - finished_downloads_at_construction_ >=
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 122
123 case ON_DANGEROUS_DOWNLOAD_FAIL: 123 case ON_DANGEROUS_DOWNLOAD_FAIL:
124 ADD_FAILURE() << "Unexpected dangerous download item."; 124 ADD_FAILURE() << "Unexpected dangerous download item.";
125 break; 125 break;
126 126
127 default: 127 default:
128 NOTREACHED(); 128 NOTREACHED();
129 } 129 }
130 } 130 }
131 131
132 if (download->GetState() == download_finished_state_) { 132 if (IsDownloadInFinalState(download))
133 DownloadInFinalState(download); 133 DownloadInFinalState(download);
134 }
135 } 134 }
136 135
137 void DownloadTestObserver::ModelChanged(DownloadManager* manager) { 136 void DownloadTestObserver::ModelChanged(DownloadManager* manager) {
138 DCHECK_EQ(manager, download_manager_); 137 DCHECK_EQ(manager, download_manager_);
139 138
140 // Regenerate DownloadItem observers. If there are any download items 139 // Regenerate DownloadItem observers. If there are any download items
141 // in our final state, note them in |finished_downloads_| 140 // in our final state, note them in |finished_downloads_|
142 // (done by |OnDownloadUpdated()|). 141 // (done by |OnDownloadUpdated()|).
143 std::vector<DownloadItem*> downloads; 142 std::vector<DownloadItem*> downloads;
144 download_manager_->GetAllDownloads(FilePath(), &downloads); 143 download_manager_->GetAllDownloads(FilePath(), &downloads);
(...skipping 30 matching lines...) Expand all
175 DownloadManager* manager, int32 /* id */) { 174 DownloadManager* manager, int32 /* id */) {
176 DCHECK_EQ(manager, download_manager_); 175 DCHECK_EQ(manager, download_manager_);
177 select_file_dialog_seen_ = true; 176 select_file_dialog_seen_ = true;
178 SignalIfFinished(); 177 SignalIfFinished();
179 } 178 }
180 179
181 size_t DownloadTestObserver::NumDangerousDownloadsSeen() const { 180 size_t DownloadTestObserver::NumDangerousDownloadsSeen() const {
182 return dangerous_downloads_seen_.size(); 181 return dangerous_downloads_seen_.size();
183 } 182 }
184 183
184 size_t DownloadTestObserver::NumDownloadsSeenInState(
185 content::DownloadItem::DownloadState state) const {
186 StateMap::const_iterator it = states_observed_.find(state);
187
188 if (it == states_observed_.end())
189 return 0;
190
191 return it->second;
192 }
193
185 void DownloadTestObserver::DownloadInFinalState(DownloadItem* download) { 194 void DownloadTestObserver::DownloadInFinalState(DownloadItem* download) {
186 if (finished_downloads_.find(download) != finished_downloads_.end()) { 195 if (finished_downloads_.find(download) != finished_downloads_.end()) {
187 // We've already seen terminal state on this download. 196 // We've already seen the final state on this download.
188 return; 197 return;
189 } 198 }
190 199
191 // Record the transition. 200 // Record the transition.
192 finished_downloads_.insert(download); 201 finished_downloads_.insert(download);
193 202
203 // Record the state.
204 states_observed_[download->GetState()]++; // Initializes to 0 the first time.
205
194 SignalIfFinished(); 206 SignalIfFinished();
195 } 207 }
196 208
197 void DownloadTestObserver::SignalIfFinished() { 209 void DownloadTestObserver::SignalIfFinished() {
198 if (waiting_ && IsFinished()) 210 if (waiting_ && IsFinished())
199 MessageLoopForUI::current()->Quit(); 211 MessageLoopForUI::current()->Quit();
200 } 212 }
201 213
214 DownloadTestObserverTerminal::DownloadTestObserverTerminal(
215 content::DownloadManager* download_manager,
216 size_t wait_count,
217 bool finish_on_select_file,
218 DangerousDownloadAction dangerous_download_action)
219 : DownloadTestObserver(download_manager,
220 wait_count,
221 finish_on_select_file,
222 dangerous_download_action) {
223 // You can't rely on overriden virtual functions in a base class constructor;
224 // the virtual function table hasn't been set up yet. So, we have to do any
225 // work that depends on those functions in the derived class constructor
226 // instead. In this case, it's because of |IsDownloadInFinalState()|.
227 Init();
228 }
229
230 DownloadTestObserverTerminal::~DownloadTestObserverTerminal() {
231 }
232
233
234 bool DownloadTestObserverTerminal::IsDownloadInFinalState(
235 content::DownloadItem* download) {
236 return (download->GetState() != DownloadItem::IN_PROGRESS);
237 }
238
239 DownloadTestObserverInProgress::DownloadTestObserverInProgress(
240 content::DownloadManager* download_manager,
241 size_t wait_count,
242 bool finish_on_select_file)
243 : DownloadTestObserver(download_manager,
244 wait_count,
245 finish_on_select_file,
246 ON_DANGEROUS_DOWNLOAD_ACCEPT) {
247 // You can't override virtual functions in a base class constructor; the
248 // virtual function table hasn't been set up yet. So, we have to do any
249 // work that depends on those functions in the derived class constructor
250 // instead. In this case, it's because of |IsDownloadInFinalState()|.
251 Init();
252 }
253
254 DownloadTestObserverInProgress::~DownloadTestObserverInProgress() {
255 }
256
257
258 bool DownloadTestObserverInProgress::IsDownloadInFinalState(
259 content::DownloadItem* download) {
260 return (download->GetState() == DownloadItem::IN_PROGRESS);
261 }
262
202 DownloadTestFlushObserver::DownloadTestFlushObserver( 263 DownloadTestFlushObserver::DownloadTestFlushObserver(
203 DownloadManager* download_manager) 264 DownloadManager* download_manager)
204 : download_manager_(download_manager), 265 : download_manager_(download_manager),
205 waiting_for_zero_inprogress_(true) {} 266 waiting_for_zero_inprogress_(true) {}
206 267
207 void DownloadTestFlushObserver::WaitForFlush() { 268 void DownloadTestFlushObserver::WaitForFlush() {
208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
209 download_manager_->AddObserver(this); 270 download_manager_->AddObserver(this);
210 ui_test_utils::RunMessageLoop(); 271 ui_test_utils::RunMessageLoop();
211 } 272 }
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 void DownloadTestFlushObserver::PingIOThread(int cycle) { 353 void DownloadTestFlushObserver::PingIOThread(int cycle) {
293 if (--cycle) { 354 if (--cycle) {
294 BrowserThread::PostTask( 355 BrowserThread::PostTask(
295 BrowserThread::UI, FROM_HERE, 356 BrowserThread::UI, FROM_HERE,
296 base::Bind(&DownloadTestFlushObserver::PingFileThread, this, cycle)); 357 base::Bind(&DownloadTestFlushObserver::PingFileThread, this, cycle));
297 } else { 358 } else {
298 BrowserThread::PostTask( 359 BrowserThread::PostTask(
299 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); 360 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure());
300 } 361 }
301 } 362 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_test_observer.h ('k') | chrome/browser/ui/browser_close_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698