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

Side by Side Diff: chrome/test/ui_test_utils.cc

Issue 151189: reenable RenderViewHostManager browertest, add DownloadsCompleteObserver (Closed)
Patch Set: oops Created 11 years, 5 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
« no previous file with comments | « chrome/test/ui_test_utils.h ('k') | no next file » | 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) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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/test/ui_test_utils.h" 5 #include "chrome/test/ui_test_utils.h"
6 6
7 #include "base/json_reader.h" 7 #include "base/json_reader.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "chrome/browser/browser.h" 11 #include "chrome/browser/browser.h"
12 #include "chrome/browser/dom_operation_notification_details.h" 12 #include "chrome/browser/dom_operation_notification_details.h"
13 #include "chrome/browser/download/download_manager.h"
13 #include "chrome/browser/tab_contents/navigation_controller.h" 14 #include "chrome/browser/tab_contents/navigation_controller.h"
14 #include "chrome/browser/tab_contents/tab_contents.h" 15 #include "chrome/browser/tab_contents/tab_contents.h"
15 #include "chrome/common/chrome_paths.h" 16 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/notification_registrar.h" 17 #include "chrome/common/notification_registrar.h"
17 #include "chrome/common/notification_service.h" 18 #include "chrome/common/notification_service.h"
18 #if defined (OS_WIN) 19 #if defined (OS_WIN)
19 #include "views/widget/accelerator_handler.h" 20 #include "views/widget/accelerator_handler.h"
20 #endif 21 #endif
21 #include "googleurl/src/gurl.h" 22 #include "googleurl/src/gurl.h"
22 #include "net/base/net_util.h" 23 #include "net/base/net_util.h"
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 92
92 std::string response() const { return response_; } 93 std::string response() const { return response_; }
93 94
94 private: 95 private:
95 NotificationRegistrar registrar_; 96 NotificationRegistrar registrar_;
96 std::string response_; 97 std::string response_;
97 98
98 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver); 99 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver);
99 }; 100 };
100 101
102 // DownloadsCompleteObserver waits for a given number of downloads to complete.
103 // Example usage:
104 //
105 // ui_test_utils::NavigateToURL(browser(), zip_url);
106 // DownloadsCompleteObserver wait_on_download(
107 // browser()->profile()->GetDownloadManager(), 1);
108 // /* |zip_url| download will be complete by this line. */
109 //
110 class DownloadsCompleteObserver : public DownloadManager::Observer,
111 public DownloadItem::Observer {
112 public:
113 explicit DownloadsCompleteObserver(DownloadManager* download_manager,
114 size_t wait_count)
115 : download_manager_(download_manager),
116 wait_count_(wait_count),
117 waiting_(false) {
118 download_manager_->AddObserver(this);
119 }
120
121 // CheckAllDownloadsComplete will be called when the DownloadManager
122 // fires it's ModelChanged() call, and also when incomplete downloads
123 // fire their OnDownloadUpdated().
124 bool CheckAllDownloadsComplete() {
125 if (downloads_.size() < wait_count_)
126 return false;
127
128 bool still_waiting = false;
129 std::vector<DownloadItem*>::iterator it = downloads_.begin();
130 for (; it != downloads_.end(); ++it) {
131 // We always remove ourselves as an observer, then re-add if the download
132 // isn't complete. This is to avoid having to track which downloads we
133 // are currently observing. Removing has no effect if we are not currently
134 // an observer.
135 (*it)->RemoveObserver(this);
136 if ((*it)->state() != DownloadItem::COMPLETE) {
137 (*it)->AddObserver(this);
138 still_waiting = true;
139 }
140 }
141
142 if (still_waiting)
143 return false;
144
145 download_manager_->RemoveObserver(this);
146 // waiting_ will have been set if not all downloads were complete on first
147 // pass below in SetDownloads().
148 if (waiting_)
149 MessageLoopForUI::current()->Quit();
150 return true;
151 }
152
153 // DownloadItem::Observer
154 virtual void OnDownloadUpdated(DownloadItem* download) {
155 if (download->state() == DownloadItem::COMPLETE) {
156 CheckAllDownloadsComplete();
157 }
158 }
159
160 virtual void OnDownloadOpened(DownloadItem* download) {}
161
162 // DownloadManager::Observer
163 virtual void ModelChanged() {
164 download_manager_->GetDownloads(this, L"");
165 }
166
167 virtual void SetDownloads(std::vector<DownloadItem*>& downloads) {
168 downloads_ = downloads;
169 if (CheckAllDownloadsComplete())
170 return;
171
172 if (!waiting_) {
173 waiting_ = true;
174 ui_test_utils::RunMessageLoop();
175 }
176 }
177
178
179 private:
180 // The observed download manager.
181 DownloadManager* download_manager_;
182
183 // The current downloads being tracked.
184 std::vector<DownloadItem*> downloads_;
185
186 // The number of downloads to wait on completing.
187 size_t wait_count_;
188
189 // Whether an internal message loop has been started and must be quit upon
190 // all downloads completing.
191 bool waiting_;
192
193 DISALLOW_COPY_AND_ASSIGN(DownloadsCompleteObserver);
194 };
195
101 } // namespace 196 } // namespace
102 197
103 void RunMessageLoop() { 198 void RunMessageLoop() {
104 MessageLoopForUI* loop = MessageLoopForUI::current(); 199 MessageLoopForUI* loop = MessageLoopForUI::current();
105 bool did_allow_task_nesting = loop->NestableTasksAllowed(); 200 bool did_allow_task_nesting = loop->NestableTasksAllowed();
106 loop->SetNestableTasksAllowed(true); 201 loop->SetNestableTasksAllowed(true);
107 #if defined (OS_WIN) 202 #if defined (OS_WIN)
108 views::AcceleratorHandler handler; 203 views::AcceleratorHandler handler;
109 loop->Run(&handler); 204 loop->Run(&handler);
110 #else 205 #else
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 } 296 }
202 297
203 GURL GetTestUrl(const std::wstring& dir, const std::wstring file) { 298 GURL GetTestUrl(const std::wstring& dir, const std::wstring file) {
204 FilePath path; 299 FilePath path;
205 PathService::Get(chrome::DIR_TEST_DATA, &path); 300 PathService::Get(chrome::DIR_TEST_DATA, &path);
206 path = path.Append(FilePath::FromWStringHack(dir)); 301 path = path.Append(FilePath::FromWStringHack(dir));
207 path = path.Append(FilePath::FromWStringHack(file)); 302 path = path.Append(FilePath::FromWStringHack(file));
208 return net::FilePathToFileURL(path); 303 return net::FilePathToFileURL(path);
209 } 304 }
210 305
306 void WaitForDownloadCount(DownloadManager* download_manager, size_t count) {
307 DownloadsCompleteObserver download_observer(download_manager, count);
308 }
309
211 } // namespace ui_test_utils 310 } // namespace ui_test_utils
OLDNEW
« no previous file with comments | « chrome/test/ui_test_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698