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

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 CLANG issue. 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 DownloadTestObserverTerminal class from which
23 // so the DownloadTestObserver's reference to the DownloadManager cannot 23 // they came, so the DownloadTestObserverTerminal'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 DownloadTestObserverTerminal::DownloadTestObserverTerminal(
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(). 54 download_manager_->AddObserver(this); // Will call initial ModelChanged().
57 finished_downloads_at_construction_ = finished_downloads_.size(); 55 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 } 56 }
61 57
62 DownloadTestObserver::~DownloadTestObserver() { 58 DownloadTestObserverTerminal::~DownloadTestObserverTerminal() {
63 for (DownloadSet::iterator it = downloads_observed_.begin(); 59 for (DownloadSet::iterator it = downloads_observed_.begin();
64 it != downloads_observed_.end(); ++it) 60 it != downloads_observed_.end(); ++it)
65 (*it)->RemoveObserver(this); 61 (*it)->RemoveObserver(this);
66 62
67 download_manager_->RemoveObserver(this); 63 download_manager_->RemoveObserver(this);
68 } 64 }
69 65
70 void DownloadTestObserver::WaitForFinished() { 66 void DownloadTestObserverTerminal::WaitForFinished() {
71 if (!IsFinished()) { 67 if (!IsFinished()) {
72 waiting_ = true; 68 waiting_ = true;
73 ui_test_utils::RunMessageLoop(); 69 ui_test_utils::RunMessageLoop();
74 waiting_ = false; 70 waiting_ = false;
75 } 71 }
76 } 72 }
77 73
78 bool DownloadTestObserver::IsFinished() const { 74 bool DownloadTestObserverTerminal::IsFinished() const {
79 if (finished_downloads_.size() - finished_downloads_at_construction_ >= 75 if (finished_downloads_.size() - finished_downloads_at_construction_ >=
80 wait_count_) 76 wait_count_)
81 return true; 77 return true;
82 return (finish_on_select_file_ && select_file_dialog_seen_); 78 return (finish_on_select_file_ && select_file_dialog_seen_);
83 } 79 }
84 80
85 void DownloadTestObserver::OnDownloadUpdated(DownloadItem* download) { 81 void DownloadTestObserverTerminal::OnDownloadUpdated(DownloadItem* download) {
86 // The REMOVING state indicates that the download is being destroyed. 82 // The REMOVING state indicates that the download is being destroyed.
87 // Stop observing. Do not do anything with it, as it is about to be gone. 83 // Stop observing. Do not do anything with it, as it is about to be gone.
88 if (download->GetState() == DownloadItem::REMOVING) { 84 if (download->GetState() == DownloadItem::REMOVING) {
89 DownloadSet::iterator it = downloads_observed_.find(download); 85 DownloadSet::iterator it = downloads_observed_.find(download);
90 ASSERT_TRUE(it != downloads_observed_.end()); 86 ASSERT_TRUE(it != downloads_observed_.end());
91 downloads_observed_.erase(it); 87 downloads_observed_.erase(it);
92 download->RemoveObserver(this); 88 download->RemoveObserver(this);
93 return; 89 return;
94 } 90 }
95 91
(...skipping 26 matching lines...) Expand all
122 118
123 case ON_DANGEROUS_DOWNLOAD_FAIL: 119 case ON_DANGEROUS_DOWNLOAD_FAIL:
124 ADD_FAILURE() << "Unexpected dangerous download item."; 120 ADD_FAILURE() << "Unexpected dangerous download item.";
125 break; 121 break;
126 122
127 default: 123 default:
128 NOTREACHED(); 124 NOTREACHED();
129 } 125 }
130 } 126 }
131 127
132 if (download->GetState() == download_finished_state_) { 128 if (IsDownloadInFinalState(download))
133 DownloadInFinalState(download); 129 DownloadInFinalState(download);
134 }
135 } 130 }
136 131
137 void DownloadTestObserver::ModelChanged(DownloadManager* manager) { 132 void DownloadTestObserverTerminal::ModelChanged(DownloadManager* manager) {
138 DCHECK_EQ(manager, download_manager_); 133 DCHECK_EQ(manager, download_manager_);
139 134
140 // Regenerate DownloadItem observers. If there are any download items 135 // Regenerate DownloadItem observers. If there are any download items
141 // in our final state, note them in |finished_downloads_| 136 // in our final state, note them in |finished_downloads_|
142 // (done by |OnDownloadUpdated()|). 137 // (done by |OnDownloadUpdated()|).
143 std::vector<DownloadItem*> downloads; 138 std::vector<DownloadItem*> downloads;
144 download_manager_->GetAllDownloads(FilePath(), &downloads); 139 download_manager_->GetAllDownloads(FilePath(), &downloads);
145 // As a test class, we're generally interested in whatever's there, 140 // As a test class, we're generally interested in whatever's there,
146 // so we include temporary downloads. 141 // so we include temporary downloads.
147 download_manager_->GetTemporaryDownloads(FilePath(), &downloads); 142 download_manager_->GetTemporaryDownloads(FilePath(), &downloads);
(...skipping 16 matching lines...) Expand all
164 // If it is finished and we are observing it, stop. 159 // If it is finished and we are observing it, stop.
165 if (finished_it != finished_downloads_.end() && 160 if (finished_it != finished_downloads_.end() &&
166 observed_it != downloads_observed_.end()) { 161 observed_it != downloads_observed_.end()) {
167 (*it)->RemoveObserver(this); 162 (*it)->RemoveObserver(this);
168 downloads_observed_.erase(observed_it); 163 downloads_observed_.erase(observed_it);
169 continue; 164 continue;
170 } 165 }
171 } 166 }
172 } 167 }
173 168
174 void DownloadTestObserver::SelectFileDialogDisplayed( 169 void DownloadTestObserverTerminal::SelectFileDialogDisplayed(
175 DownloadManager* manager, int32 /* id */) { 170 DownloadManager* manager, int32 /* id */) {
176 DCHECK_EQ(manager, download_manager_); 171 DCHECK_EQ(manager, download_manager_);
177 select_file_dialog_seen_ = true; 172 select_file_dialog_seen_ = true;
178 SignalIfFinished(); 173 SignalIfFinished();
179 } 174 }
180 175
181 size_t DownloadTestObserver::NumDangerousDownloadsSeen() const { 176 size_t DownloadTestObserverTerminal::NumDangerousDownloadsSeen() const {
182 return dangerous_downloads_seen_.size(); 177 return dangerous_downloads_seen_.size();
183 } 178 }
184 179
185 void DownloadTestObserver::DownloadInFinalState(DownloadItem* download) { 180 void DownloadTestObserverTerminal::DownloadInFinalState(
181 DownloadItem* download) {
186 if (finished_downloads_.find(download) != finished_downloads_.end()) { 182 if (finished_downloads_.find(download) != finished_downloads_.end()) {
187 // We've already seen terminal state on this download. 183 // We've already seen the final state on this download.
188 return; 184 return;
189 } 185 }
190 186
191 // Record the transition. 187 // Record the transition.
192 finished_downloads_.insert(download); 188 finished_downloads_.insert(download);
193 189
194 SignalIfFinished(); 190 SignalIfFinished();
195 } 191 }
196 192
197 void DownloadTestObserver::SignalIfFinished() { 193 bool DownloadTestObserverTerminal::IsDownloadInFinalState(
194 content::DownloadItem* download) {
195 return (download->GetState() != DownloadItem::IN_PROGRESS);
196 }
197
198 void DownloadTestObserverTerminal::SignalIfFinished() {
198 if (waiting_ && IsFinished()) 199 if (waiting_ && IsFinished())
199 MessageLoopForUI::current()->Quit(); 200 MessageLoopForUI::current()->Quit();
200 } 201 }
201 202
203 DownloadTestObserverInProgress::DownloadTestObserverInProgress(
204 content::DownloadManager* download_manager,
205 size_t wait_count)
206 : DownloadTestObserverTerminal(download_manager,
207 wait_count,
208 true,
209 ON_DANGEROUS_DOWNLOAD_FAIL) {
210 }
211
212 DownloadTestObserverInProgress::~DownloadTestObserverInProgress() {
213 }
214
215
216 bool DownloadTestObserverInProgress::IsDownloadInFinalState(
217 content::DownloadItem* download) {
218 return (download->GetState() == DownloadItem::IN_PROGRESS);
219 }
220
202 DownloadTestFlushObserver::DownloadTestFlushObserver( 221 DownloadTestFlushObserver::DownloadTestFlushObserver(
203 DownloadManager* download_manager) 222 DownloadManager* download_manager)
204 : download_manager_(download_manager), 223 : download_manager_(download_manager),
205 waiting_for_zero_inprogress_(true) {} 224 waiting_for_zero_inprogress_(true) {}
206 225
207 void DownloadTestFlushObserver::WaitForFlush() { 226 void DownloadTestFlushObserver::WaitForFlush() {
208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 227 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
209 download_manager_->AddObserver(this); 228 download_manager_->AddObserver(this);
210 ui_test_utils::RunMessageLoop(); 229 ui_test_utils::RunMessageLoop();
211 } 230 }
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 void DownloadTestFlushObserver::PingIOThread(int cycle) { 311 void DownloadTestFlushObserver::PingIOThread(int cycle) {
293 if (--cycle) { 312 if (--cycle) {
294 BrowserThread::PostTask( 313 BrowserThread::PostTask(
295 BrowserThread::UI, FROM_HERE, 314 BrowserThread::UI, FROM_HERE,
296 base::Bind(&DownloadTestFlushObserver::PingFileThread, this, cycle)); 315 base::Bind(&DownloadTestFlushObserver::PingFileThread, this, cycle));
297 } else { 316 } else {
298 BrowserThread::PostTask( 317 BrowserThread::PostTask(
299 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); 318 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure());
300 } 319 }
301 } 320 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698