OLD | NEW |
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 "chrome/browser/task_manager/task_manager_browsertest_util.h" | 5 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/memory/ptr_util.h" |
9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
10 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
11 #include "base/strings/pattern.h" | 12 #include "base/strings/pattern.h" |
12 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
15 #include "base/test/test_timeouts.h" | 16 #include "base/test/test_timeouts.h" |
16 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
17 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
18 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/sessions/session_tab_helper.h" |
| 22 #include "chrome/browser/task_management/task_manager_interface.h" |
| 23 #include "chrome/browser/task_management/task_manager_observer.h" |
20 #include "chrome/browser/task_manager/resource_provider.h" | 24 #include "chrome/browser/task_manager/resource_provider.h" |
21 #include "chrome/browser/task_manager/task_manager.h" | 25 #include "chrome/browser/task_manager/task_manager.h" |
| 26 #include "chrome/browser/ui/task_manager/task_manager_table_model.h" |
| 27 #include "chrome/browser/ui/views/new_task_manager_view.h" |
22 #include "chrome/common/chrome_switches.h" | 28 #include "chrome/common/chrome_switches.h" |
23 #include "chrome/grit/generated_resources.h" | 29 #include "chrome/grit/generated_resources.h" |
24 #include "extensions/strings/grit/extensions_strings.h" | 30 #include "extensions/strings/grit/extensions_strings.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
27 | 33 |
| 34 namespace task_management { |
| 35 |
| 36 class TaskManagerTester |
| 37 : public task_manager::browsertest_util::TaskManagerTesterInterface, |
| 38 public TaskManagerObserver { |
| 39 public: |
| 40 explicit TaskManagerTester(const base::Closure& on_resource_change) |
| 41 : TaskManagerObserver(NewTaskManagerView::GetInstanceForTests() |
| 42 ->table_model_->desired_refresh_time(), |
| 43 NewTaskManagerView::GetInstanceForTests() |
| 44 ->table_model_->desired_resources_flags()), |
| 45 on_resource_change_(on_resource_change), |
| 46 model_(NewTaskManagerView::GetInstanceForTests()->table_model_.get()) {} |
| 47 |
| 48 virtual int GetRowCount() { return model_->RowCount(); } |
| 49 |
| 50 virtual base::string16 GetRowTitle(int row) { |
| 51 return model_->GetText(row, IDS_TASK_MANAGER_TASK_COLUMN); |
| 52 } |
| 53 |
| 54 virtual int64_t GetColumnValue( |
| 55 task_manager::browsertest_util::ColumnSpecifier column, |
| 56 int row) { |
| 57 TaskId task_id = model_->tasks_[row]; |
| 58 int64_t value = 0; |
| 59 int64_t ignored = 0; |
| 60 bool success = false; |
| 61 |
| 62 switch (column) { |
| 63 case task_manager::browsertest_util::COLUMN_NONE: |
| 64 break; |
| 65 case task_manager::browsertest_util::V8_MEMORY: |
| 66 success = |
| 67 observed_task_manager()->GetV8Memory(task_id, &value, &ignored); |
| 68 break; |
| 69 case task_manager::browsertest_util::V8_MEMORY_USED: |
| 70 success = |
| 71 observed_task_manager()->GetV8Memory(task_id, &ignored, &value); |
| 72 break; |
| 73 case task_manager::browsertest_util::SQLITE_MEMORY_USED: |
| 74 value = observed_task_manager()->GetSqliteMemoryUsed(task_id); |
| 75 success = true; |
| 76 break; |
| 77 } |
| 78 if (!success) |
| 79 return 0; |
| 80 return value; |
| 81 } |
| 82 |
| 83 virtual int32_t GetTabId(int row) { |
| 84 TaskId task_id = model_->tasks_[row]; |
| 85 return observed_task_manager()->GetTabId(task_id); |
| 86 } |
| 87 |
| 88 virtual void Kill(int row) { model_->KillTask(row); } |
| 89 |
| 90 // TaskManagerObserver |
| 91 void OnTaskAdded(TaskId id) { on_resource_change_.Run(); } |
| 92 |
| 93 void OnTaskToBeRemoved(TaskId id) { on_resource_change_.Run(); } |
| 94 |
| 95 void OnTasksRefreshed(const TaskIdList& task_ids) { |
| 96 on_resource_change_.Run(); |
| 97 } |
| 98 |
| 99 void OnTasksRefreshedWithBackgroundCalculations(const TaskIdList& task_ids) { |
| 100 on_resource_change_.Run(); |
| 101 } |
| 102 |
| 103 void OnTaskUnresponsive(TaskId id) { on_resource_change_.Run(); } |
| 104 |
| 105 private: |
| 106 base::Closure on_resource_change_; |
| 107 TaskManagerTableModel* model_; |
| 108 }; |
| 109 |
| 110 } // namespace task_management |
| 111 |
28 namespace task_manager { | 112 namespace task_manager { |
29 namespace browsertest_util { | 113 namespace browsertest_util { |
30 | 114 |
31 namespace { | 115 namespace { |
32 | 116 |
33 class ResourceChangeObserver : public TaskManagerModelObserver { | 117 class OldTaskManagerTester : public TaskManagerTesterInterface, |
| 118 public TaskManagerModelObserver { |
34 public: | 119 public: |
35 ResourceChangeObserver(const TaskManagerModel* model, | 120 explicit OldTaskManagerTester(const base::Closure& on_resource_change) |
36 int required_count, | 121 : on_resource_change_(on_resource_change), |
| 122 model_(TaskManager::GetInstance()->model()) { |
| 123 model_->AddObserver(this); |
| 124 } |
| 125 ~OldTaskManagerTester() { model_->RemoveObserver(this); } |
| 126 |
| 127 // TaskManagerTesterInterface: |
| 128 virtual int GetRowCount() { return model_->ResourceCount(); } |
| 129 |
| 130 virtual base::string16 GetRowTitle(int row) { |
| 131 return model_->GetResourceTitle(row); |
| 132 } |
| 133 |
| 134 virtual int64_t GetColumnValue(ColumnSpecifier column, int row) { |
| 135 size_t value = 0; |
| 136 bool success = false; |
| 137 switch (column) { |
| 138 case COLUMN_NONE: |
| 139 break; |
| 140 case V8_MEMORY: |
| 141 success = model_->GetV8Memory(row, &value); |
| 142 break; |
| 143 case V8_MEMORY_USED: |
| 144 success = model_->GetV8MemoryUsed(row, &value); |
| 145 break; |
| 146 case SQLITE_MEMORY_USED: |
| 147 success = model_->GetSqliteMemoryUsedBytes(row, &value); |
| 148 break; |
| 149 } |
| 150 if (!success) |
| 151 return 0; |
| 152 return static_cast<int64_t>(value); |
| 153 } |
| 154 |
| 155 virtual int32_t GetTabId(int row) { |
| 156 if (model_->GetResourceWebContents(row)) { |
| 157 return SessionTabHelper::IdForTab(model_->GetResourceWebContents(row)); |
| 158 } |
| 159 return -1; |
| 160 } |
| 161 |
| 162 virtual void Kill(int row) { TaskManager::GetInstance()->KillProcess(row); } |
| 163 |
| 164 // TaskManagerModelObserver: |
| 165 void OnModelChanged() override { OnResourceChange(); } |
| 166 void OnItemsChanged(int start, int length) override { OnResourceChange(); } |
| 167 void OnItemsAdded(int start, int length) override { OnResourceChange(); } |
| 168 void OnItemsRemoved(int start, int length) override { OnResourceChange(); } |
| 169 |
| 170 private: |
| 171 void OnResourceChange() { on_resource_change_.Run(); } |
| 172 base::Closure on_resource_change_; |
| 173 TaskManagerModel* model_; |
| 174 }; |
| 175 |
| 176 class ResourceChangeObserver { |
| 177 public: |
| 178 ResourceChangeObserver(int required_count, |
37 const base::string16& title_pattern, | 179 const base::string16& title_pattern, |
38 ColumnSpecifier column_specifier, | 180 ColumnSpecifier column_specifier, |
39 size_t min_column_value) | 181 size_t min_column_value) |
40 : model_(model), | 182 : required_count_(required_count), |
41 required_count_(required_count), | |
42 title_pattern_(title_pattern), | 183 title_pattern_(title_pattern), |
43 column_specifier_(column_specifier), | 184 column_specifier_(column_specifier), |
44 min_column_value_(min_column_value) {} | 185 min_column_value_(min_column_value) { |
| 186 base::Closure on_resource_change = base::Bind( |
| 187 &ResourceChangeObserver::OnResourceChange, base::Unretained(this)); |
45 | 188 |
46 void OnModelChanged() override { OnResourceChange(); } | 189 if (switches::NewTaskManagerEnabled()) { |
47 | 190 model_.reset(new task_management::TaskManagerTester(on_resource_change)); |
48 void OnItemsChanged(int start, int length) override { OnResourceChange(); } | 191 } else { |
49 | 192 model_.reset(new OldTaskManagerTester(on_resource_change)); |
50 void OnItemsAdded(int start, int length) override { OnResourceChange(); } | 193 } |
51 | 194 } |
52 void OnItemsRemoved(int start, int length) override { OnResourceChange(); } | |
53 | 195 |
54 void RunUntilSatisfied() { | 196 void RunUntilSatisfied() { |
55 // See if the condition is satisfied without having to run the loop. This | 197 // See if the condition is satisfied without having to run the loop. This |
56 // check has to be placed after the installation of the | 198 // check has to be placed after the installation of the |
57 // TaskManagerModelObserver, because resources may change before that. | 199 // TaskManagerModelObserver, because resources may change before that. |
58 if (IsSatisfied()) | 200 if (IsSatisfied()) |
59 return; | 201 return; |
60 | 202 |
61 timer_.Start(FROM_HERE, | 203 timer_.Start(FROM_HERE, |
62 TestTimeouts::action_timeout(), | 204 TestTimeouts::action_timeout(), |
(...skipping 10 matching lines...) Expand all Loading... |
73 FAIL() << "Wait condition satisfied only emphemerally. Likely test " | 215 FAIL() << "Wait condition satisfied only emphemerally. Likely test " |
74 << "problem. Maybe wait instead for the state below?\n" | 216 << "problem. Maybe wait instead for the state below?\n" |
75 << DumpTaskManagerModel(); | 217 << DumpTaskManagerModel(); |
76 } | 218 } |
77 | 219 |
78 timer_.Stop(); | 220 timer_.Stop(); |
79 } | 221 } |
80 | 222 |
81 private: | 223 private: |
82 void OnResourceChange() { | 224 void OnResourceChange() { |
| 225 DLOG(INFO) << DumpTaskManagerModel(); |
83 if (!IsSatisfied()) | 226 if (!IsSatisfied()) |
84 return; | 227 return; |
85 | 228 |
86 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 229 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
87 run_loop_.QuitClosure()); | 230 run_loop_.QuitClosure()); |
88 } | 231 } |
89 | 232 |
90 bool IsSatisfied() { return CountMatches() == required_count_; } | 233 bool IsSatisfied() { return CountMatches() == required_count_; } |
91 | 234 |
92 int CountMatches() { | 235 int CountMatches() { |
93 int match_count = 0; | 236 int match_count = 0; |
94 for (int i = 0; i < model_->ResourceCount(); i++) { | 237 for (int i = 0; i < model_->GetRowCount(); i++) { |
95 if (!base::MatchPattern(model_->GetResourceTitle(i), title_pattern_)) | 238 if (!base::MatchPattern(model_->GetRowTitle(i), title_pattern_)) |
96 continue; | 239 continue; |
97 | 240 |
98 if (GetColumnValue(i) < min_column_value_) | 241 if (GetColumnValue(i) < min_column_value_) |
99 continue; | 242 continue; |
100 | 243 |
101 match_count++; | 244 match_count++; |
102 } | 245 } |
103 return match_count; | 246 return match_count; |
104 } | 247 } |
105 | 248 |
106 size_t GetColumnValue(int index) { | 249 int64_t GetColumnValue(int index) { |
107 size_t value = 0; | 250 return model_->GetColumnValue(column_specifier_, index); |
108 bool success = false; | |
109 switch (column_specifier_) { | |
110 case COLUMN_NONE: | |
111 break; | |
112 case V8_MEMORY: | |
113 success = model_->GetV8Memory(index, &value); | |
114 break; | |
115 case V8_MEMORY_USED: | |
116 success = model_->GetV8MemoryUsed(index, &value); | |
117 break; | |
118 case SQLITE_MEMORY_USED: | |
119 success = model_->GetSqliteMemoryUsedBytes(index, &value); | |
120 break; | |
121 } | |
122 if (!success) | |
123 return 0; | |
124 return value; | |
125 } | 251 } |
126 | 252 |
127 const char* GetColumnName() { | 253 const char* GetColumnName() { |
128 switch (column_specifier_) { | 254 switch (column_specifier_) { |
129 case COLUMN_NONE: | 255 case COLUMN_NONE: |
130 return "N/A"; | 256 return "N/A"; |
131 case V8_MEMORY: | 257 case V8_MEMORY: |
132 return "V8 Memory"; | 258 return "V8 Memory"; |
133 case V8_MEMORY_USED: | 259 case V8_MEMORY_USED: |
134 return "V8 Memory Used"; | 260 return "V8 Memory Used"; |
(...skipping 14 matching lines...) Expand all Loading... |
149 task_manager_state_dump << "Waiting for exactly " << required_count_ | 275 task_manager_state_dump << "Waiting for exactly " << required_count_ |
150 << " matches of wildcard pattern \"" | 276 << " matches of wildcard pattern \"" |
151 << base::UTF16ToASCII(title_pattern_) << "\""; | 277 << base::UTF16ToASCII(title_pattern_) << "\""; |
152 if (min_column_value_ > 0) { | 278 if (min_column_value_ > 0) { |
153 task_manager_state_dump << " && [" << GetColumnName() | 279 task_manager_state_dump << " && [" << GetColumnName() |
154 << " >= " << min_column_value_ << "]"; | 280 << " >= " << min_column_value_ << "]"; |
155 } | 281 } |
156 task_manager_state_dump << "\nCurrently there are " << CountMatches() | 282 task_manager_state_dump << "\nCurrently there are " << CountMatches() |
157 << " matches."; | 283 << " matches."; |
158 task_manager_state_dump << "\nCurrent Task Manager Model is:"; | 284 task_manager_state_dump << "\nCurrent Task Manager Model is:"; |
159 for (int i = 0; i < model_->ResourceCount(); i++) { | 285 for (int i = 0; i < model_->GetRowCount(); i++) { |
160 task_manager_state_dump | 286 task_manager_state_dump << "\n > " << std::setw(40) << std::left |
161 << "\n > " << std::setw(40) << std::left | 287 << base::UTF16ToASCII(model_->GetRowTitle(i)); |
162 << base::UTF16ToASCII(model_->GetResourceTitle(i)); | |
163 if (min_column_value_ > 0) { | 288 if (min_column_value_ > 0) { |
164 task_manager_state_dump << " [" << GetColumnName() | 289 task_manager_state_dump << " [" << GetColumnName() |
165 << " == " << GetColumnValue(i) << "]"; | 290 << " == " << GetColumnValue(i) << "]"; |
166 } | 291 } |
167 } | 292 } |
168 return task_manager_state_dump; | 293 return task_manager_state_dump; |
169 } | 294 } |
170 | 295 |
171 const TaskManagerModel* model_; | 296 scoped_ptr<TaskManagerTesterInterface> model_; |
172 const int required_count_; | 297 const int required_count_; |
173 const base::string16 title_pattern_; | 298 const base::string16 title_pattern_; |
174 const ColumnSpecifier column_specifier_; | 299 const ColumnSpecifier column_specifier_; |
175 const size_t min_column_value_; | 300 const int64_t min_column_value_; |
176 base::RunLoop run_loop_; | 301 base::RunLoop run_loop_; |
177 base::OneShotTimer timer_; | 302 base::OneShotTimer timer_; |
178 }; | 303 }; |
179 | 304 |
180 } // namespace | 305 } // namespace |
181 | 306 |
182 void EnableOldTaskManager() { | 307 std::unique_ptr<TaskManagerTesterInterface> GetTaskManagerTester() { |
183 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 308 if (switches::NewTaskManagerEnabled()) { |
184 switches::kDisableNewTaskManager); | 309 return base::WrapUnique( |
| 310 new task_management::TaskManagerTester(base::Closure())); |
| 311 } else { |
| 312 return base::WrapUnique(new OldTaskManagerTester(base::Closure())); |
| 313 } |
185 } | 314 } |
186 | 315 |
187 void WaitForTaskManagerRows(int required_count, | 316 void WaitForTaskManagerRows(int required_count, |
188 const base::string16& title_pattern) { | 317 const base::string16& title_pattern) { |
189 TaskManagerModel* model = TaskManager::GetInstance()->model(); | |
190 | |
191 const int column_value_dont_care = 0; | 318 const int column_value_dont_care = 0; |
192 ResourceChangeObserver observer(model, required_count, title_pattern, | 319 ResourceChangeObserver observer(required_count, title_pattern, COLUMN_NONE, |
193 COLUMN_NONE, column_value_dont_care); | 320 column_value_dont_care); |
194 model->AddObserver(&observer); | |
195 observer.RunUntilSatisfied(); | 321 observer.RunUntilSatisfied(); |
196 model->RemoveObserver(&observer); | |
197 } | 322 } |
198 | 323 |
199 void WaitForTaskManagerStatToExceed(const base::string16& title_pattern, | 324 void WaitForTaskManagerStatToExceed(const base::string16& title_pattern, |
200 ColumnSpecifier column_getter, | 325 ColumnSpecifier column_getter, |
201 size_t min_column_value) { | 326 size_t min_column_value) { |
202 TaskManagerModel* model = TaskManager::GetInstance()->model(); | |
203 | |
204 const int wait_for_one_match = 1; | 327 const int wait_for_one_match = 1; |
205 ResourceChangeObserver observer(model, wait_for_one_match, title_pattern, | 328 ResourceChangeObserver observer(wait_for_one_match, title_pattern, |
206 column_getter, min_column_value); | 329 column_getter, min_column_value); |
207 model->AddObserver(&observer); | |
208 observer.RunUntilSatisfied(); | 330 observer.RunUntilSatisfied(); |
209 model->RemoveObserver(&observer); | |
210 } | 331 } |
211 | 332 |
212 base::string16 MatchTab(const char* title) { | 333 base::string16 MatchTab(const char* title) { |
213 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX, | 334 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX, |
214 base::ASCIIToUTF16(title)); | 335 base::ASCIIToUTF16(title)); |
215 } | 336 } |
216 | 337 |
217 base::string16 MatchAnyTab() { return MatchTab("*"); } | 338 base::string16 MatchAnyTab() { return MatchTab("*"); } |
218 | 339 |
219 base::string16 MatchAboutBlankTab() { return MatchTab("about:blank"); } | 340 base::string16 MatchAboutBlankTab() { return MatchTab("about:blank"); } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 base::string16 MatchUtility(const base::string16& title) { | 387 base::string16 MatchUtility(const base::string16& title) { |
267 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_UTILITY_PREFIX, title); | 388 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_UTILITY_PREFIX, title); |
268 } | 389 } |
269 | 390 |
270 base::string16 MatchAnyUtility() { | 391 base::string16 MatchAnyUtility() { |
271 return MatchUtility(base::ASCIIToUTF16("*")); | 392 return MatchUtility(base::ASCIIToUTF16("*")); |
272 } | 393 } |
273 | 394 |
274 } // namespace browsertest_util | 395 } // namespace browsertest_util |
275 } // namespace task_manager | 396 } // namespace task_manager |
OLD | NEW |