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

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

Issue 251064: Fix focus restore bug in find-in-bar (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 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 | « 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 <vector> 7 #include <vector>
8 8
9 #include "base/json_reader.h" 9 #include "base/json_reader.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 const NotificationDetails& details) { 246 const NotificationDetails& details) {
247 MessageLoopForUI::current()->Quit(); 247 MessageLoopForUI::current()->Quit();
248 } 248 }
249 249
250 private: 250 private:
251 NotificationRegistrar registrar_; 251 NotificationRegistrar registrar_;
252 252
253 DISALLOW_COPY_AND_ASSIGN(SimpleNotificationObserver); 253 DISALLOW_COPY_AND_ASSIGN(SimpleNotificationObserver);
254 }; 254 };
255 255
256 class FindInPageNotificationObserver : public NotificationObserver {
257 public:
258 explicit FindInPageNotificationObserver(TabContents* parent_tab)
259 : parent_tab_(parent_tab),
260 active_match_ordinal_(-1),
261 number_of_matches_(0) {
262 current_find_request_id_ = parent_tab->current_find_request_id();
263 registrar_.Add(this, NotificationType::FIND_RESULT_AVAILABLE,
264 Source<TabContents>(parent_tab_));
265 ui_test_utils::RunMessageLoop();
266 }
267
268 int active_match_ordinal() const { return active_match_ordinal_; }
269
270 int number_of_matches() const { return number_of_matches_; }
271
272 virtual void Observe(NotificationType type, const NotificationSource& source,
273 const NotificationDetails& details) {
274 if (type == NotificationType::FIND_RESULT_AVAILABLE) {
275 Details<FindNotificationDetails> find_details(details);
276 if (find_details->request_id() == current_find_request_id_) {
277 // We get multiple responses and one of those will contain the ordinal.
278 // This message comes to us before the final update is sent.
279 if (find_details->active_match_ordinal() > -1)
280 active_match_ordinal_ = find_details->active_match_ordinal();
281 if (find_details->final_update()) {
282 number_of_matches_ = find_details->number_of_matches();
283 MessageLoopForUI::current()->Quit();
284 } else {
285 DLOG(INFO) << "Ignoring, since we only care about the final message";
286 }
287 }
288 } else {
289 NOTREACHED();
290 }
291 }
292
293 private:
294 NotificationRegistrar registrar_;
295 TabContents* parent_tab_;
296 // We will at some point (before final update) be notified of the ordinal and
297 // we need to preserve it so we can send it later.
298 int active_match_ordinal_;
299 int number_of_matches_;
300 // The id of the current find request, obtained from TabContents. Allows us
301 // to monitor when the search completes.
302 int current_find_request_id_;
303
304 DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver);
305 };
306
256 } // namespace 307 } // namespace
257 308
258 void RunMessageLoop() { 309 void RunMessageLoop() {
259 MessageLoopForUI* loop = MessageLoopForUI::current(); 310 MessageLoopForUI* loop = MessageLoopForUI::current();
260 bool did_allow_task_nesting = loop->NestableTasksAllowed(); 311 bool did_allow_task_nesting = loop->NestableTasksAllowed();
261 loop->SetNestableTasksAllowed(true); 312 loop->SetNestableTasksAllowed(true);
262 #if defined(TOOLKIT_VIEWS) 313 #if defined(TOOLKIT_VIEWS)
263 views::AcceleratorHandler handler; 314 views::AcceleratorHandler handler;
264 loop->Run(&handler); 315 loop->Run(&handler);
265 #elif defined(OS_LINUX) 316 #elif defined(OS_LINUX)
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 SimpleNotificationObserver<RenderViewHost> 476 SimpleNotificationObserver<RenderViewHost>
426 focus_observer(NotificationType::FOCUS_CHANGED_IN_PAGE, rvh); 477 focus_observer(NotificationType::FOCUS_CHANGED_IN_PAGE, rvh);
427 } 478 }
428 479
429 void WaitForFocusInBrowser(Browser* browser) { 480 void WaitForFocusInBrowser(Browser* browser) {
430 SimpleNotificationObserver<Browser> 481 SimpleNotificationObserver<Browser>
431 focus_observer(NotificationType::FOCUS_RETURNED_TO_BROWSER, 482 focus_observer(NotificationType::FOCUS_RETURNED_TO_BROWSER,
432 browser); 483 browser);
433 } 484 }
434 485
486 int FindInPage(TabContents* tab_contents, const string16& search_string,
487 bool forward, bool match_case, int* ordinal) {
488 tab_contents->StartFinding(search_string, forward, match_case);
489 FindInPageNotificationObserver observer(tab_contents);
490 if (ordinal)
491 *ordinal = observer.active_match_ordinal();
492 return observer.number_of_matches();
493 }
494
435 } // namespace ui_test_utils 495 } // 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