| 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" | |
| 11 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 12 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 13 #include "base/strings/pattern.h" | 12 #include "base/strings/pattern.h" |
| 14 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 15 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/test/test_timeouts.h" | 16 #include "base/test/test_timeouts.h" |
| 18 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| 19 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
| 20 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/task_management/task_manager_tester.h" |
| 22 #include "chrome/browser/sessions/session_tab_helper.h" | |
| 23 #include "chrome/browser/task_management/task_manager_interface.h" | |
| 24 #include "chrome/browser/task_manager/legacy_task_manager_tester.h" | |
| 25 #include "chrome/browser/task_manager/resource_provider.h" | |
| 26 #include "chrome/browser/task_manager/task_manager.h" | |
| 27 #include "chrome/browser/ui/browser_dialogs.h" | |
| 28 #include "chrome/browser/ui/task_manager/task_manager_table_model.h" | |
| 29 #include "chrome/common/chrome_switches.h" | |
| 30 #include "chrome/grit/generated_resources.h" | 21 #include "chrome/grit/generated_resources.h" |
| 31 #include "extensions/strings/grit/extensions_strings.h" | 22 #include "extensions/strings/grit/extensions_strings.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 33 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
| 34 #include "ui/base/models/table_model_observer.h" | 25 #include "ui/base/models/table_model_observer.h" |
| 35 | 26 |
| 36 namespace task_manager { | 27 namespace task_manager { |
| 37 namespace browsertest_util { | 28 namespace browsertest_util { |
| 38 | 29 |
| 39 namespace { | 30 namespace { |
| 40 | 31 |
| 41 // Returns whether chrome::ShowTaskManager() will, for the current platform and | 32 using TaskManagerTester = task_management::TaskManagerTester; |
| 42 // command line, show a view backed by a task_management::TaskManagerTableModel. | |
| 43 bool IsNewTaskManagerViewEnabled() { | |
| 44 #if defined(OS_MACOSX) | |
| 45 if (!chrome::ToolkitViewsDialogsEnabled()) | |
| 46 return false; | |
| 47 #endif | |
| 48 return switches::NewTaskManagerEnabled(); | |
| 49 } | |
| 50 | |
| 51 // Temporarily intercepts the calls between a TableModel and its Observer, | |
| 52 // running |callback| whenever anything happens. | |
| 53 class ScopedInterceptTableModelObserver : public ui::TableModelObserver { | |
| 54 public: | |
| 55 ScopedInterceptTableModelObserver( | |
| 56 ui::TableModel* model_to_intercept, | |
| 57 ui::TableModelObserver* real_table_model_observer, | |
| 58 const base::Closure& callback) | |
| 59 : model_to_intercept_(model_to_intercept), | |
| 60 real_table_model_observer_(real_table_model_observer), | |
| 61 callback_(callback) { | |
| 62 model_to_intercept_->SetObserver(this); | |
| 63 } | |
| 64 | |
| 65 ~ScopedInterceptTableModelObserver() override { | |
| 66 model_to_intercept_->SetObserver(real_table_model_observer_); | |
| 67 } | |
| 68 | |
| 69 // ui::TableModelObserver: | |
| 70 void OnModelChanged() override { | |
| 71 real_table_model_observer_->OnModelChanged(); | |
| 72 callback_.Run(); | |
| 73 } | |
| 74 void OnItemsChanged(int start, int length) override { | |
| 75 real_table_model_observer_->OnItemsChanged(start, length); | |
| 76 callback_.Run(); | |
| 77 } | |
| 78 void OnItemsAdded(int start, int length) override { | |
| 79 real_table_model_observer_->OnItemsAdded(start, length); | |
| 80 callback_.Run(); | |
| 81 } | |
| 82 void OnItemsRemoved(int start, int length) override { | |
| 83 real_table_model_observer_->OnItemsRemoved(start, length); | |
| 84 callback_.Run(); | |
| 85 } | |
| 86 | |
| 87 private: | |
| 88 ui::TableModel* model_to_intercept_; | |
| 89 ui::TableModelObserver* real_table_model_observer_; | |
| 90 base::Closure callback_; | |
| 91 }; | |
| 92 | |
| 93 } // namespace | |
| 94 | |
| 95 // Implementation of TaskManagerTester for the 'new' TaskManager. | |
| 96 class TaskManagerTesterImpl : public TaskManagerTester { | |
| 97 public: | |
| 98 explicit TaskManagerTesterImpl(const base::Closure& on_resource_change) | |
| 99 : model_(GetRealModel()) { | |
| 100 // Eavesdrop the model->view conversation, since the model only supports | |
| 101 // single observation. | |
| 102 if (!on_resource_change.is_null()) { | |
| 103 interceptor_.reset(new ScopedInterceptTableModelObserver( | |
| 104 model_, model_->table_model_observer_, on_resource_change)); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 ~TaskManagerTesterImpl() override { | |
| 109 CHECK_EQ(GetRealModel(), model_) << "Task Manager should not be hidden " | |
| 110 "while TaskManagerTester is alive. " | |
| 111 "This indicates a test bug."; | |
| 112 } | |
| 113 | |
| 114 // TaskManagerTester: | |
| 115 int GetRowCount() override { return model_->RowCount(); } | |
| 116 | |
| 117 base::string16 GetRowTitle(int row) override { | |
| 118 return model_->GetText(row, IDS_TASK_MANAGER_TASK_COLUMN); | |
| 119 } | |
| 120 | |
| 121 void ToggleColumnVisibility(ColumnSpecifier column) override { | |
| 122 int column_id = 0; | |
| 123 switch (column) { | |
| 124 case ColumnSpecifier::COLUMN_NONE: | |
| 125 return; | |
| 126 case ColumnSpecifier::SQLITE_MEMORY_USED: | |
| 127 column_id = IDS_TASK_MANAGER_SQLITE_MEMORY_USED_COLUMN; | |
| 128 break; | |
| 129 case ColumnSpecifier::V8_MEMORY_USED: | |
| 130 case ColumnSpecifier::V8_MEMORY: | |
| 131 column_id = IDS_TASK_MANAGER_JAVASCRIPT_MEMORY_ALLOCATED_COLUMN; | |
| 132 break; | |
| 133 } | |
| 134 model_->ToggleColumnVisibility(column_id); | |
| 135 } | |
| 136 | |
| 137 int64_t GetColumnValue(ColumnSpecifier column, int row) override { | |
| 138 task_management::TaskId task_id = model_->tasks_[row]; | |
| 139 int64_t value = 0; | |
| 140 int64_t ignored = 0; | |
| 141 bool success = false; | |
| 142 | |
| 143 switch (column) { | |
| 144 case ColumnSpecifier::COLUMN_NONE: | |
| 145 break; | |
| 146 case ColumnSpecifier::V8_MEMORY: | |
| 147 success = task_manager()->GetV8Memory(task_id, &value, &ignored); | |
| 148 break; | |
| 149 case ColumnSpecifier::V8_MEMORY_USED: | |
| 150 success = task_manager()->GetV8Memory(task_id, &ignored, &value); | |
| 151 break; | |
| 152 case ColumnSpecifier::SQLITE_MEMORY_USED: | |
| 153 value = task_manager()->GetSqliteMemoryUsed(task_id); | |
| 154 success = true; | |
| 155 break; | |
| 156 } | |
| 157 if (!success) | |
| 158 return 0; | |
| 159 return value; | |
| 160 } | |
| 161 | |
| 162 int32_t GetTabId(int row) override { | |
| 163 task_management::TaskId task_id = model_->tasks_[row]; | |
| 164 return task_manager()->GetTabId(task_id); | |
| 165 } | |
| 166 | |
| 167 void Kill(int row) override { model_->KillTask(row); } | |
| 168 | |
| 169 private: | |
| 170 task_management::TaskManagerInterface* task_manager() { | |
| 171 return model_->observed_task_manager(); | |
| 172 } | |
| 173 | |
| 174 // Returns the TaskManagerTableModel for the the visible NewTaskManagerView. | |
| 175 static task_management::TaskManagerTableModel* GetRealModel() { | |
| 176 CHECK(IsNewTaskManagerViewEnabled()); | |
| 177 // This downcast is safe, as long as the new task manager is enabled. | |
| 178 task_management::TaskManagerTableModel* result = | |
| 179 static_cast<task_management::TaskManagerTableModel*>( | |
| 180 chrome::ShowTaskManager(nullptr)); | |
| 181 return result; | |
| 182 } | |
| 183 | |
| 184 task_management::TaskManagerTableModel* model_; | |
| 185 std::unique_ptr<ScopedInterceptTableModelObserver> interceptor_; | |
| 186 }; | |
| 187 | |
| 188 namespace { | |
| 189 | 33 |
| 190 // Helper class to run a message loop until a TaskManagerTester is in an | 34 // Helper class to run a message loop until a TaskManagerTester is in an |
| 191 // expected state. If timeout occurs, an ASCII version of the task manager's | 35 // expected state. If timeout occurs, an ASCII version of the task manager's |
| 192 // contents, along with a summary of the expected state, are dumped to test | 36 // contents, along with a summary of the expected state, are dumped to test |
| 193 // output, to assist debugging. | 37 // output, to assist debugging. |
| 194 class ResourceChangeObserver { | 38 class ResourceChangeObserver { |
| 195 public: | 39 public: |
| 196 ResourceChangeObserver(int required_count, | 40 ResourceChangeObserver(int required_count, |
| 197 const base::string16& title_pattern, | 41 const base::string16& title_pattern, |
| 198 ColumnSpecifier column_specifier, | 42 ColumnSpecifier column_specifier, |
| 199 size_t min_column_value) | 43 size_t min_column_value) |
| 200 : required_count_(required_count), | 44 : required_count_(required_count), |
| 201 title_pattern_(title_pattern), | 45 title_pattern_(title_pattern), |
| 202 column_specifier_(column_specifier), | 46 column_specifier_(column_specifier), |
| 203 min_column_value_(min_column_value) { | 47 min_column_value_(min_column_value) { |
| 204 base::Closure callback = base::Bind( | 48 task_manager_tester_ = TaskManagerTester::Create(base::Bind( |
| 205 &ResourceChangeObserver::OnResourceChange, base::Unretained(this)); | 49 &ResourceChangeObserver::OnResourceChange, base::Unretained(this))); |
| 206 | |
| 207 if (IsNewTaskManagerViewEnabled()) | |
| 208 task_manager_tester_.reset(new TaskManagerTesterImpl(callback)); | |
| 209 else | |
| 210 task_manager_tester_ = CreateLegacyTaskManagerTester(callback); | |
| 211 } | 50 } |
| 212 | 51 |
| 213 void RunUntilSatisfied() { | 52 void RunUntilSatisfied() { |
| 214 // See if the condition is satisfied without having to run the loop. This | 53 // See if the condition is satisfied without having to run the loop. This |
| 215 // check has to be placed after the installation of the | 54 // check has to be placed after the installation of the |
| 216 // TaskManagerModelObserver, because resources may change before that. | 55 // TaskManagerModelObserver, because resources may change before that. |
| 217 if (IsSatisfied()) | 56 if (IsSatisfied()) |
| 218 return; | 57 return; |
| 219 | 58 |
| 220 timer_.Start(FROM_HERE, TestTimeouts::action_timeout(), this, | 59 timer_.Start(FROM_HERE, TestTimeouts::action_timeout(), this, |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 const int required_count_; | 152 const int required_count_; |
| 314 const base::string16 title_pattern_; | 153 const base::string16 title_pattern_; |
| 315 const ColumnSpecifier column_specifier_; | 154 const ColumnSpecifier column_specifier_; |
| 316 const int64_t min_column_value_; | 155 const int64_t min_column_value_; |
| 317 base::RunLoop run_loop_; | 156 base::RunLoop run_loop_; |
| 318 base::OneShotTimer timer_; | 157 base::OneShotTimer timer_; |
| 319 }; | 158 }; |
| 320 | 159 |
| 321 } // namespace | 160 } // namespace |
| 322 | 161 |
| 323 std::unique_ptr<TaskManagerTester> GetTaskManagerTester() { | |
| 324 if (IsNewTaskManagerViewEnabled()) | |
| 325 return base::WrapUnique(new TaskManagerTesterImpl(base::Closure())); | |
| 326 else | |
| 327 return CreateLegacyTaskManagerTester(base::Closure()); | |
| 328 } | |
| 329 | |
| 330 void WaitForTaskManagerRows(int required_count, | 162 void WaitForTaskManagerRows(int required_count, |
| 331 const base::string16& title_pattern) { | 163 const base::string16& title_pattern) { |
| 332 const int column_value_dont_care = 0; | 164 const int column_value_dont_care = 0; |
| 333 ResourceChangeObserver observer(required_count, title_pattern, | 165 ResourceChangeObserver observer(required_count, title_pattern, |
| 334 ColumnSpecifier::COLUMN_NONE, | 166 ColumnSpecifier::COLUMN_NONE, |
| 335 column_value_dont_care); | 167 column_value_dont_care); |
| 336 observer.RunUntilSatisfied(); | 168 observer.RunUntilSatisfied(); |
| 337 } | 169 } |
| 338 | 170 |
| 339 void WaitForTaskManagerStatToExceed(const base::string16& title_pattern, | 171 void WaitForTaskManagerStatToExceed(const base::string16& title_pattern, |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 base::string16 MatchUtility(const base::string16& title) { | 247 base::string16 MatchUtility(const base::string16& title) { |
| 416 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_UTILITY_PREFIX, title); | 248 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_UTILITY_PREFIX, title); |
| 417 } | 249 } |
| 418 | 250 |
| 419 base::string16 MatchAnyUtility() { | 251 base::string16 MatchAnyUtility() { |
| 420 return MatchUtility(base::ASCIIToUTF16("*")); | 252 return MatchUtility(base::ASCIIToUTF16("*")); |
| 421 } | 253 } |
| 422 | 254 |
| 423 } // namespace browsertest_util | 255 } // namespace browsertest_util |
| 424 } // namespace task_manager | 256 } // namespace task_manager |
| OLD | NEW |