Chromium Code Reviews| 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/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/strings/pattern.h" | 13 #include "base/strings/pattern.h" |
| 14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/test/test_timeouts.h" | 17 #include "base/test/test_timeouts.h" |
| 18 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
| 19 #include "base/timer/timer.h" | 19 #include "base/timer/timer.h" |
| 20 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/sessions/session_tab_helper.h" | 22 #include "chrome/browser/sessions/session_tab_helper.h" |
| 23 #include "chrome/browser/task_management/task_manager_interface.h" | 23 #include "chrome/browser/task_management/task_manager_interface.h" |
| 24 #include "chrome/browser/task_manager/legacy_task_manager_tester.h" | |
| 24 #include "chrome/browser/task_manager/resource_provider.h" | 25 #include "chrome/browser/task_manager/resource_provider.h" |
| 25 #include "chrome/browser/task_manager/task_manager.h" | 26 #include "chrome/browser/task_manager/task_manager.h" |
| 26 #include "chrome/browser/ui/browser_dialogs.h" | 27 #include "chrome/browser/ui/browser_dialogs.h" |
| 27 #include "chrome/browser/ui/task_manager/task_manager_table_model.h" | 28 #include "chrome/browser/ui/task_manager/task_manager_table_model.h" |
| 28 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
| 29 #include "chrome/grit/generated_resources.h" | 30 #include "chrome/grit/generated_resources.h" |
| 30 #include "extensions/strings/grit/extensions_strings.h" | 31 #include "extensions/strings/grit/extensions_strings.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 32 #include "ui/base/l10n/l10n_util.h" | 33 #include "ui/base/l10n/l10n_util.h" |
| 33 #include "ui/base/models/table_model_observer.h" | 34 #include "ui/base/models/table_model_observer.h" |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 chrome::ShowTaskManager(nullptr)); | 180 chrome::ShowTaskManager(nullptr)); |
| 180 return result; | 181 return result; |
| 181 } | 182 } |
| 182 | 183 |
| 183 task_management::TaskManagerTableModel* model_; | 184 task_management::TaskManagerTableModel* model_; |
| 184 std::unique_ptr<ScopedInterceptTableModelObserver> interceptor_; | 185 std::unique_ptr<ScopedInterceptTableModelObserver> interceptor_; |
| 185 }; | 186 }; |
| 186 | 187 |
| 187 namespace { | 188 namespace { |
| 188 | 189 |
| 189 class LegacyTaskManagerTesterImpl : public TaskManagerTester, | |
| 190 public TaskManagerModelObserver { | |
| 191 public: | |
| 192 explicit LegacyTaskManagerTesterImpl(const base::Closure& on_resource_change) | |
| 193 : on_resource_change_(on_resource_change), | |
| 194 model_(TaskManager::GetInstance()->model()) { | |
| 195 if (!on_resource_change_.is_null()) | |
| 196 model_->AddObserver(this); | |
| 197 } | |
| 198 ~LegacyTaskManagerTesterImpl() override { | |
| 199 if (!on_resource_change_.is_null()) | |
| 200 model_->RemoveObserver(this); | |
| 201 } | |
| 202 | |
| 203 // TaskManagerTester: | |
| 204 int GetRowCount() override { return model_->ResourceCount(); } | |
| 205 | |
| 206 base::string16 GetRowTitle(int row) override { | |
| 207 return model_->GetResourceTitle(row); | |
| 208 } | |
| 209 | |
| 210 int64_t GetColumnValue(ColumnSpecifier column, int row) override { | |
| 211 size_t value = 0; | |
| 212 bool success = false; | |
| 213 switch (column) { | |
| 214 case ColumnSpecifier::COLUMN_NONE: | |
| 215 break; | |
| 216 case ColumnSpecifier::V8_MEMORY: | |
| 217 success = model_->GetV8Memory(row, &value); | |
| 218 break; | |
| 219 case ColumnSpecifier::V8_MEMORY_USED: | |
| 220 success = model_->GetV8MemoryUsed(row, &value); | |
| 221 break; | |
| 222 case ColumnSpecifier::SQLITE_MEMORY_USED: | |
| 223 success = model_->GetSqliteMemoryUsedBytes(row, &value); | |
| 224 break; | |
| 225 } | |
| 226 if (!success) | |
| 227 return 0; | |
| 228 return static_cast<int64_t>(value); | |
| 229 } | |
| 230 | |
| 231 void ToggleColumnVisibility(ColumnSpecifier column) override { | |
| 232 // Doing nothing is okay here; the legacy TaskManager always collects all | |
| 233 // stats. | |
| 234 } | |
| 235 | |
| 236 int32_t GetTabId(int row) override { | |
| 237 if (model_->GetResourceWebContents(row)) { | |
| 238 return SessionTabHelper::IdForTab(model_->GetResourceWebContents(row)); | |
| 239 } | |
| 240 return -1; | |
| 241 } | |
| 242 | |
| 243 void Kill(int row) override { TaskManager::GetInstance()->KillProcess(row); } | |
| 244 | |
| 245 // TaskManagerModelObserver: | |
| 246 void OnModelChanged() override { OnResourceChange(); } | |
| 247 void OnItemsChanged(int start, int length) override { OnResourceChange(); } | |
| 248 void OnItemsAdded(int start, int length) override { OnResourceChange(); } | |
| 249 void OnItemsRemoved(int start, int length) override { OnResourceChange(); } | |
| 250 | |
| 251 private: | |
| 252 void OnResourceChange() { | |
| 253 if (!on_resource_change_.is_null()) | |
| 254 on_resource_change_.Run(); | |
| 255 } | |
| 256 base::Closure on_resource_change_; | |
| 257 TaskManagerModel* model_; | |
| 258 }; | |
| 259 | |
| 260 // Helper class to run a message loop until a TaskManagerTester is in an | 190 // Helper class to run a message loop until a TaskManagerTester is in an |
| 261 // expected state. If timeout occurs, an ASCII version of the task manager's | 191 // expected state. If timeout occurs, an ASCII version of the task manager's |
| 262 // contents, along with a summary of the expected state, are dumped to test | 192 // contents, along with a summary of the expected state, are dumped to test |
| 263 // output, to assist debugging. | 193 // output, to assist debugging. |
| 264 class ResourceChangeObserver { | 194 class ResourceChangeObserver { |
| 265 public: | 195 public: |
| 266 ResourceChangeObserver(int required_count, | 196 ResourceChangeObserver(int required_count, |
| 267 const base::string16& title_pattern, | 197 const base::string16& title_pattern, |
| 268 ColumnSpecifier column_specifier, | 198 ColumnSpecifier column_specifier, |
| 269 size_t min_column_value) | 199 size_t min_column_value) |
| 270 : required_count_(required_count), | 200 : required_count_(required_count), |
| 271 title_pattern_(title_pattern), | 201 title_pattern_(title_pattern), |
| 272 column_specifier_(column_specifier), | 202 column_specifier_(column_specifier), |
| 273 min_column_value_(min_column_value) { | 203 min_column_value_(min_column_value) { |
| 274 base::Closure callback = base::Bind( | 204 base::Closure callback = base::Bind( |
| 275 &ResourceChangeObserver::OnResourceChange, base::Unretained(this)); | 205 &ResourceChangeObserver::OnResourceChange, base::Unretained(this)); |
| 276 | 206 |
| 277 if (IsNewTaskManagerViewEnabled()) | 207 if (IsNewTaskManagerViewEnabled()) |
| 278 task_manager_tester_.reset(new TaskManagerTesterImpl(callback)); | 208 task_manager_tester_.reset(new TaskManagerTesterImpl(callback)); |
| 279 else | 209 else |
| 280 task_manager_tester_.reset(new LegacyTaskManagerTesterImpl(callback)); | 210 task_manager_tester_ = LegacyTaskManagerTester::Create(callback); |
| 281 } | 211 } |
| 282 | 212 |
| 283 void RunUntilSatisfied() { | 213 void RunUntilSatisfied() { |
| 284 // See if the condition is satisfied without having to run the loop. This | 214 // See if the condition is satisfied without having to run the loop. This |
| 285 // check has to be placed after the installation of the | 215 // check has to be placed after the installation of the |
| 286 // TaskManagerModelObserver, because resources may change before that. | 216 // TaskManagerModelObserver, because resources may change before that. |
| 287 if (IsSatisfied()) | 217 if (IsSatisfied()) |
| 288 return; | 218 return; |
| 289 | 219 |
| 290 timer_.Start(FROM_HERE, TestTimeouts::action_timeout(), this, | 220 timer_.Start(FROM_HERE, TestTimeouts::action_timeout(), this, |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 387 base::RunLoop run_loop_; | 317 base::RunLoop run_loop_; |
| 388 base::OneShotTimer timer_; | 318 base::OneShotTimer timer_; |
| 389 }; | 319 }; |
| 390 | 320 |
| 391 } // namespace | 321 } // namespace |
| 392 | 322 |
| 393 std::unique_ptr<TaskManagerTester> GetTaskManagerTester() { | 323 std::unique_ptr<TaskManagerTester> GetTaskManagerTester() { |
| 394 if (IsNewTaskManagerViewEnabled()) | 324 if (IsNewTaskManagerViewEnabled()) |
| 395 return base::WrapUnique(new TaskManagerTesterImpl(base::Closure())); | 325 return base::WrapUnique(new TaskManagerTesterImpl(base::Closure())); |
| 396 else | 326 else |
| 397 return base::WrapUnique(new LegacyTaskManagerTesterImpl(base::Closure())); | 327 return LegacyTaskManagerTester::Create(base::Closure()); |
|
afakhry
2016/05/07 00:16:09
I just noticed this. There's a code duplication be
ncarter (slow)
2016/05/07 22:29:08
Your suggestion is implemented happens in PS5. And
| |
| 398 } | 328 } |
| 399 | 329 |
| 400 void WaitForTaskManagerRows(int required_count, | 330 void WaitForTaskManagerRows(int required_count, |
| 401 const base::string16& title_pattern) { | 331 const base::string16& title_pattern) { |
| 402 const int column_value_dont_care = 0; | 332 const int column_value_dont_care = 0; |
| 403 ResourceChangeObserver observer(required_count, title_pattern, | 333 ResourceChangeObserver observer(required_count, title_pattern, |
| 404 ColumnSpecifier::COLUMN_NONE, | 334 ColumnSpecifier::COLUMN_NONE, |
| 405 column_value_dont_care); | 335 column_value_dont_care); |
| 406 observer.RunUntilSatisfied(); | 336 observer.RunUntilSatisfied(); |
| 407 } | 337 } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 485 base::string16 MatchUtility(const base::string16& title) { | 415 base::string16 MatchUtility(const base::string16& title) { |
| 486 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_UTILITY_PREFIX, title); | 416 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_UTILITY_PREFIX, title); |
| 487 } | 417 } |
| 488 | 418 |
| 489 base::string16 MatchAnyUtility() { | 419 base::string16 MatchAnyUtility() { |
| 490 return MatchUtility(base::ASCIIToUTF16("*")); | 420 return MatchUtility(base::ASCIIToUTF16("*")); |
| 491 } | 421 } |
| 492 | 422 |
| 493 } // namespace browsertest_util | 423 } // namespace browsertest_util |
| 494 } // namespace task_manager | 424 } // namespace task_manager |
| OLD | NEW |