OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_BROWSERTEST_UTIL_H_ | |
6 #define CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_BROWSERTEST_UTIL_H_ | |
7 #pragma once | |
8 | |
9 #include "base/message_loop.h" | |
10 #include "chrome/browser/browser_process.h" | |
11 #include "chrome/browser/profiles/profile.h" | |
12 #include "chrome/browser/task_manager/task_manager.h" | |
13 #include "chrome/browser/ui/browser.h" | |
14 #include "chrome/browser/ui/browser_window.h" | |
15 #include "chrome/common/chrome_notification_types.h" | |
16 #include "chrome/test/ui_test_utils.h" | |
17 #include "content/common/notification_source.h" | |
18 | |
19 namespace { | |
20 | |
21 class ResourceChangeObserver : public TaskManagerModelObserver { | |
xiyuan
2011/07/29 18:04:56
nit: Is ResourceChangeObserver used anywhere else?
| |
22 public: | |
23 ResourceChangeObserver(const TaskManagerModel* model, | |
24 int target_resource_count) | |
25 : model_(model), | |
26 target_resource_count_(target_resource_count) { | |
27 } | |
28 | |
29 virtual void OnModelChanged() { | |
xiyuan
2011/07/29 18:04:56
nit: OVERRIDE and other virtual functions below.
| |
30 OnResourceChange(); | |
31 } | |
32 | |
33 virtual void OnItemsChanged(int start, int length) { | |
34 OnResourceChange(); | |
35 } | |
36 | |
37 virtual void OnItemsAdded(int start, int length) { | |
38 OnResourceChange(); | |
39 } | |
40 | |
41 virtual void OnItemsRemoved(int start, int length) { | |
42 OnResourceChange(); | |
43 } | |
44 | |
45 private: | |
46 void OnResourceChange() { | |
47 if (model_->ResourceCount() == target_resource_count_) | |
48 MessageLoopForUI::current()->Quit(); | |
49 } | |
50 | |
51 const TaskManagerModel* model_; | |
52 const int target_resource_count_; | |
53 }; | |
54 | |
55 // Helper class used to wait for a TaskManager to be ready. | |
56 class TaskManagerReadyListener : public NotificationObserver { | |
xiyuan
2011/07/29 18:04:56
nit: If TaskManagerReadyListener not used anywhere
| |
57 public: | |
58 explicit TaskManagerReadyListener(TaskManagerModel* model) { | |
59 registrar_.Add(this, chrome::NOTIFICATION_TASK_MANAGER_WINDOW_READY, | |
60 Source<TaskManagerModel>(model)); | |
61 LOG(ERROR) << "added"; | |
xiyuan
2011/07/29 18:04:56
nit: remove this LOG or fix indentation
| |
62 } | |
63 virtual void Observe(int type, | |
xiyuan
2011/07/29 18:04:56
nit: insert an empty line before
| |
64 const NotificationSource& source, | |
65 const NotificationDetails& details) { | |
66 // Quit once the BackgroundContents has been loaded. | |
67 if (type == chrome::NOTIFICATION_TASK_MANAGER_WINDOW_READY) | |
68 MessageLoopForUI::current()->Quit(); | |
69 LOG(ERROR) << "Observed"; | |
xiyuan
2011/07/29 18:04:56
nit: remove this LOG or fix the indentation.
| |
70 } | |
71 private: | |
72 NotificationRegistrar registrar_; | |
73 }; | |
74 | |
75 // Helper class used to wait for a BackgroundContents to finish loading. | |
76 class BackgroundContentsListener : public NotificationObserver { | |
77 public: | |
78 explicit BackgroundContentsListener(Profile* profile) { | |
79 registrar_.Add(this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED, | |
80 Source<Profile>(profile)); | |
81 } | |
82 virtual void Observe(int type, | |
xiyuan
2011/07/29 18:04:56
nit: insert an empty line before
| |
83 const NotificationSource& source, | |
84 const NotificationDetails& details) { | |
85 // Quit once the BackgroundContents has been loaded. | |
86 if (type == chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED) | |
87 MessageLoopForUI::current()->Quit(); | |
88 } | |
89 | |
90 void ShowTaskManagerAndWait() { | |
91 ui_test_utils::RunMessageLoop(); | |
92 } | |
93 | |
94 private: | |
95 NotificationRegistrar registrar_; | |
96 }; | |
97 | |
98 } // namespace | |
99 | |
100 class TaskManagerBrowserTestUtil { | |
101 public: | |
102 static void WaitForResourceChange(int target_count) { | |
103 TaskManagerModel* model = TaskManager::GetInstance()->model(); | |
104 | |
105 if (model->ResourceCount() == target_count) | |
106 return; | |
107 ResourceChangeObserver observer(model, target_count); | |
108 model->AddObserver(&observer); | |
109 ui_test_utils::RunMessageLoop(); | |
110 model->RemoveObserver(&observer); | |
111 } | |
112 | |
113 static void ShowTaskManagerAndWaitForReady(Browser* browser) { | |
114 #if defined(WEBUI_TASK_MANAGER) | |
115 TaskManagerReadyListener listener(TaskManager::GetInstance()->model()); | |
116 browser->window()->ShowTaskManager(); | |
117 ui_test_utils::RunMessageLoop(); | |
118 #else | |
119 browser->window()->ShowTaskManager(); | |
120 #endif // defined(WEBUI_TASK_MANAGER) | |
121 } | |
122 | |
123 // Wait for any pending BackgroundContents to finish starting up. | |
124 static void WaitForBackgroundContents(Browser* browser) { | |
125 BackgroundContentsListener listener(browser->profile()); | |
126 ui_test_utils::RunMessageLoop(); | |
127 } | |
128 }; | |
129 | |
130 #endif // CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_BROWSERTEST_UTIL_H_ | |
131 | |
mazda
2011/07/29 18:20:38
redundant line?
| |
OLD | NEW |