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 #include "base/message_loop.h" | |
6 #include "chrome/browser/browser_process.h" | |
7 #include "chrome/browser/profiles/profile.h" | |
8 #include "chrome/browser/task_manager/task_manager.h" | |
9 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" | |
10 #include "chrome/browser/ui/browser.h" | |
11 #include "chrome/browser/ui/browser_window.h" | |
12 #include "chrome/common/chrome_notification_types.h" | |
13 #include "chrome/test/ui_test_utils.h" | |
14 #include "content/common/notification_source.h" | |
15 | |
16 namespace { | |
17 | |
18 class ResourceChangeObserver : public TaskManagerModelObserver { | |
19 public: | |
20 ResourceChangeObserver(const TaskManagerModel* model, | |
21 int target_resource_count) | |
22 : model_(model), | |
23 target_resource_count_(target_resource_count) { | |
24 } | |
25 | |
26 virtual void OnModelChanged() OVERRIDE { | |
27 OnResourceChange(); | |
28 } | |
29 | |
30 virtual void OnItemsChanged(int start, int length) OVERRIDE { | |
31 OnResourceChange(); | |
32 } | |
33 | |
34 virtual void OnItemsAdded(int start, int length) OVERRIDE { | |
35 OnResourceChange(); | |
36 } | |
37 | |
38 virtual void OnItemsRemoved(int start, int length) OVERRIDE { | |
39 OnResourceChange(); | |
40 } | |
41 | |
42 private: | |
43 void OnResourceChange() { | |
44 if (model_->ResourceCount() == target_resource_count_) | |
45 MessageLoopForUI::current()->Quit(); | |
46 } | |
47 | |
48 const TaskManagerModel* model_; | |
49 const int target_resource_count_; | |
50 }; | |
51 | |
52 // Helper class used to wait for a TaskManager to be ready. | |
53 class TaskManagerReadyListener : public NotificationObserver { | |
54 public: | |
55 explicit TaskManagerReadyListener(TaskManagerModel* model) { | |
56 registrar_.Add(this, chrome::NOTIFICATION_TASK_MANAGER_WINDOW_READY, | |
57 Source<TaskManagerModel>(model)); | |
58 } | |
59 | |
60 virtual void Observe(int type, | |
61 const NotificationSource& source, | |
62 const NotificationDetails& details) { | |
63 // Quit once the BackgroundContents has been loaded. | |
64 if (type == chrome::NOTIFICATION_TASK_MANAGER_WINDOW_READY) | |
65 MessageLoopForUI::current()->Quit(); | |
66 } | |
67 private: | |
68 NotificationRegistrar registrar_; | |
69 }; | |
70 | |
71 // Helper class used to wait for a BackgroundContents to finish loading. | |
72 class BackgroundContentsListener : public NotificationObserver { | |
73 public: | |
74 explicit BackgroundContentsListener(Profile* profile) { | |
75 registrar_.Add(this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED, | |
76 Source<Profile>(profile)); | |
77 } | |
78 | |
79 virtual void Observe(int type, | |
80 const NotificationSource& source, | |
81 const NotificationDetails& details) { | |
82 // Quit once the BackgroundContents has been loaded. | |
83 if (type == chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED) | |
84 MessageLoopForUI::current()->Quit(); | |
85 } | |
86 | |
87 void ShowTaskManagerAndWait() { | |
88 ui_test_utils::RunMessageLoop(); | |
89 } | |
90 | |
91 private: | |
92 NotificationRegistrar registrar_; | |
93 }; | |
94 | |
95 } // namespace | |
96 | |
97 // static | |
98 void TaskManagerBrowserTestUtil::WaitForResourceChange(int target_count) { | |
99 TaskManagerModel* model = TaskManager::GetInstance()->model(); | |
100 | |
101 if (model->ResourceCount() == target_count) | |
102 return; | |
103 ResourceChangeObserver observer(model, target_count); | |
104 model->AddObserver(&observer); | |
105 ui_test_utils::RunMessageLoop(); | |
106 model->RemoveObserver(&observer); | |
107 } | |
108 | |
109 // static | |
110 void TaskManagerBrowserTestUtil::ShowTaskManagerAndWaitForReady( | |
111 Browser* browser) { | |
112 #if defined(WEBUI_TASK_MANAGER) | |
113 TaskManagerReadyListener listener(TaskManager::GetInstance()->model()); | |
114 browser->window()->ShowTaskManager(); | |
115 ui_test_utils::RunMessageLoop(); | |
116 #else | |
117 browser->window()->ShowTaskManager(); | |
118 #endif // defined(WEBUI_TASK_MANAGER) | |
119 } | |
120 | |
121 // static | |
122 void TaskManagerBrowserTestUtil::WaitForBackgroundContents(Browser* browser) { | |
123 BackgroundContentsListener listener(browser->profile()); | |
124 ui_test_utils::RunMessageLoop(); | |
125 } | |
126 | |
OLD | NEW |