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/command_line.h" | 8 #include "base/command_line.h" |
8 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/memory/ptr_util.h" | |
9 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
10 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
11 #include "base/strings/pattern.h" | 13 #include "base/strings/pattern.h" |
12 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
13 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
15 #include "base/test/test_timeouts.h" | 17 #include "base/test/test_timeouts.h" |
16 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
17 #include "base/timer/timer.h" | 19 #include "base/timer/timer.h" |
18 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.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_management/task_manager_observer.h" | |
20 #include "chrome/browser/task_manager/resource_provider.h" | 25 #include "chrome/browser/task_manager/resource_provider.h" |
21 #include "chrome/browser/task_manager/task_manager.h" | 26 #include "chrome/browser/task_manager/task_manager.h" |
27 #include "chrome/browser/ui/task_manager/task_manager_table_model.h" | |
28 #include "chrome/browser/ui/views/new_task_manager_view.h" | |
22 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
23 #include "chrome/grit/generated_resources.h" | 30 #include "chrome/grit/generated_resources.h" |
24 #include "extensions/strings/grit/extensions_strings.h" | 31 #include "extensions/strings/grit/extensions_strings.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "ui/base/l10n/l10n_util.h" | 33 #include "ui/base/l10n/l10n_util.h" |
27 | 34 |
35 // TODO(nick): Move everything here (except for LegacyTaskManagerTesterImpl) | |
36 // into the task_management namespace. | |
37 namespace task_management { | |
38 | |
39 class TaskManagerTesterImpl | |
40 : public task_manager::browsertest_util::TaskManagerTester, | |
41 public TaskManagerObserver { | |
42 public: | |
43 // TODO(nick): We have a couple ways we could implement this class: | |
44 // (1) Grab the view's model directly. | |
ncarter (slow)
2016/04/27 17:17:45
This is what the code does today. It gets the job
afakhry
2016/04/28 00:03:13
I think this is the best approach since we are act
| |
45 // (2) Instantiate our own TaskManagerTableModel instance and ignore | |
ncarter (slow)
2016/04/27 17:17:45
I think this is probably the best approach for wha
afakhry
2016/04/28 00:03:13
Your interceptor approach is better.
| |
46 // the "real" view. This would be pretty clean, but it makes the | |
47 // chrome::ShowTaskManager() steps of the tests kind of lame. | |
48 // (3) Some hybrid of the above (where we cross validate the contents) | |
49 // of the real table model | |
50 // (4) Make TaskManagerTableModel support multiple observers, and | |
ncarter (slow)
2016/04/27 17:17:45
This turns out to be pretty gross, since SetObserv
afakhry
2016/04/28 00:03:13
Yes, this doesn't sound like a clean solution.
| |
51 // observe the real view's model. | |
52 explicit TaskManagerTesterImpl(const base::Closure& on_resource_change) | |
53 : TaskManagerObserver(NewTaskManagerView::GetInstanceForTests() | |
54 ->table_model_->desired_refresh_time(), | |
55 NewTaskManagerView::GetInstanceForTests() | |
56 ->table_model_->desired_resources_flags()), | |
57 on_resource_change_(on_resource_change), | |
58 model_(NewTaskManagerView::GetInstanceForTests()->table_model_.get()) { | |
59 model_->observed_task_manager()->AddObserver(this); | |
60 } | |
61 | |
62 ~TaskManagerTesterImpl() override { | |
63 observed_task_manager()->RemoveObserver(this); | |
64 } | |
65 | |
66 int GetRowCount() override { return model_->RowCount(); } | |
67 | |
68 base::string16 GetRowTitle(int row) override { | |
69 return model_->GetText(row, IDS_TASK_MANAGER_TASK_COLUMN); | |
70 } | |
71 | |
72 void ToggleColumnVisibility( | |
73 task_manager::browsertest_util::ColumnSpecifier column) override { | |
74 int column_id = 0; | |
75 switch (column) { | |
76 case task_manager::browsertest_util::COLUMN_NONE: | |
77 return; | |
78 case task_manager::browsertest_util::SQLITE_MEMORY_USED: | |
79 column_id = IDS_TASK_MANAGER_SQLITE_MEMORY_USED_COLUMN; | |
80 break; | |
81 case task_manager::browsertest_util::V8_MEMORY_USED: | |
82 case task_manager::browsertest_util::V8_MEMORY: | |
83 column_id = IDS_TASK_MANAGER_JAVASCRIPT_MEMORY_ALLOCATED_COLUMN; | |
84 break; | |
85 } | |
86 model_->ToggleColumnVisibility(column_id); | |
87 } | |
88 | |
89 int64_t GetColumnValue(task_manager::browsertest_util::ColumnSpecifier column, | |
90 int row) override { | |
91 TaskId task_id = model_->tasks_[row]; | |
92 int64_t value = 0; | |
93 int64_t ignored = 0; | |
94 bool success = false; | |
95 | |
96 switch (column) { | |
97 case task_manager::browsertest_util::COLUMN_NONE: | |
98 break; | |
99 case task_manager::browsertest_util::V8_MEMORY: | |
100 success = | |
101 observed_task_manager()->GetV8Memory(task_id, &value, &ignored); | |
102 break; | |
103 case task_manager::browsertest_util::V8_MEMORY_USED: | |
104 success = | |
105 observed_task_manager()->GetV8Memory(task_id, &ignored, &value); | |
106 break; | |
107 case task_manager::browsertest_util::SQLITE_MEMORY_USED: | |
108 value = observed_task_manager()->GetSqliteMemoryUsed(task_id); | |
109 success = true; | |
110 break; | |
111 } | |
112 if (!success) | |
113 return 0; | |
114 return value; | |
115 } | |
116 | |
117 int32_t GetTabId(int row) override { | |
118 TaskId task_id = model_->tasks_[row]; | |
119 return observed_task_manager()->GetTabId(task_id); | |
120 } | |
121 | |
122 void Kill(int row) override { model_->KillTask(row); } | |
123 | |
124 // TaskManagerObserver | |
125 void OnTaskAdded(TaskId id) override { on_resource_change_.Run(); } | |
126 | |
127 void OnTaskToBeRemoved(TaskId id) override { on_resource_change_.Run(); } | |
128 | |
129 void OnTasksRefreshed(const TaskIdList& task_ids) override { | |
130 on_resource_change_.Run(); | |
131 } | |
132 | |
133 void OnTasksRefreshedWithBackgroundCalculations( | |
134 const TaskIdList& task_ids) override { | |
135 on_resource_change_.Run(); | |
136 } | |
137 | |
138 void OnTaskUnresponsive(TaskId id) override { on_resource_change_.Run(); } | |
139 | |
140 private: | |
141 base::Closure on_resource_change_; | |
142 TaskManagerTableModel* model_; | |
143 }; | |
144 | |
145 } // namespace task_management | |
146 | |
28 namespace task_manager { | 147 namespace task_manager { |
29 namespace browsertest_util { | 148 namespace browsertest_util { |
30 | 149 |
31 namespace { | 150 namespace { |
32 | 151 |
33 class ResourceChangeObserver : public TaskManagerModelObserver { | 152 class LegacyTaskManagerTesterImpl : public TaskManagerTester, |
153 public TaskManagerModelObserver { | |
34 public: | 154 public: |
35 ResourceChangeObserver(const TaskManagerModel* model, | 155 explicit LegacyTaskManagerTesterImpl(const base::Closure& on_resource_change) |
36 int required_count, | 156 : on_resource_change_(on_resource_change), |
157 model_(TaskManager::GetInstance()->model()) { | |
158 model_->AddObserver(this); | |
159 } | |
160 ~LegacyTaskManagerTesterImpl() override { model_->RemoveObserver(this); } | |
161 | |
162 // TaskManagerTester: | |
163 int GetRowCount() override { return model_->ResourceCount(); } | |
164 | |
165 base::string16 GetRowTitle(int row) override { | |
166 return model_->GetResourceTitle(row); | |
167 } | |
168 | |
169 int64_t GetColumnValue(ColumnSpecifier column, int row) override { | |
170 size_t value = 0; | |
171 bool success = false; | |
172 switch (column) { | |
173 case COLUMN_NONE: | |
174 break; | |
175 case V8_MEMORY: | |
176 success = model_->GetV8Memory(row, &value); | |
177 break; | |
178 case V8_MEMORY_USED: | |
179 success = model_->GetV8MemoryUsed(row, &value); | |
180 break; | |
181 case SQLITE_MEMORY_USED: | |
182 success = model_->GetSqliteMemoryUsedBytes(row, &value); | |
183 break; | |
184 } | |
185 if (!success) | |
186 return 0; | |
187 return static_cast<int64_t>(value); | |
188 } | |
189 | |
190 void ToggleColumnVisibility(ColumnSpecifier column) override { | |
191 // Doing nothing is okay here; the legacy TaskManager always collects all | |
192 // stats. | |
193 } | |
194 | |
195 int32_t GetTabId(int row) override { | |
196 if (model_->GetResourceWebContents(row)) { | |
197 return SessionTabHelper::IdForTab(model_->GetResourceWebContents(row)); | |
198 } | |
199 return -1; | |
200 } | |
201 | |
202 void Kill(int row) override { TaskManager::GetInstance()->KillProcess(row); } | |
203 | |
204 // TaskManagerModelObserver: | |
205 void OnModelChanged() override { OnResourceChange(); } | |
206 void OnItemsChanged(int start, int length) override { OnResourceChange(); } | |
207 void OnItemsAdded(int start, int length) override { OnResourceChange(); } | |
208 void OnItemsRemoved(int start, int length) override { OnResourceChange(); } | |
209 | |
210 private: | |
211 void OnResourceChange() { on_resource_change_.Run(); } | |
212 base::Closure on_resource_change_; | |
213 TaskManagerModel* model_; | |
214 }; | |
215 | |
216 class ResourceChangeObserver { | |
217 public: | |
218 ResourceChangeObserver(int required_count, | |
37 const base::string16& title_pattern, | 219 const base::string16& title_pattern, |
38 ColumnSpecifier column_specifier, | 220 ColumnSpecifier column_specifier, |
39 size_t min_column_value) | 221 size_t min_column_value) |
40 : model_(model), | 222 : required_count_(required_count), |
41 required_count_(required_count), | |
42 title_pattern_(title_pattern), | 223 title_pattern_(title_pattern), |
43 column_specifier_(column_specifier), | 224 column_specifier_(column_specifier), |
44 min_column_value_(min_column_value) {} | 225 min_column_value_(min_column_value) { |
45 | 226 base::Closure on_resource_change = base::Bind( |
46 void OnModelChanged() override { OnResourceChange(); } | 227 &ResourceChangeObserver::OnResourceChange, base::Unretained(this)); |
47 | 228 |
48 void OnItemsChanged(int start, int length) override { OnResourceChange(); } | 229 if (switches::NewTaskManagerEnabled()) { |
49 | 230 model_.reset( |
50 void OnItemsAdded(int start, int length) override { OnResourceChange(); } | 231 new task_management::TaskManagerTesterImpl(on_resource_change)); |
51 | 232 } else { |
52 void OnItemsRemoved(int start, int length) override { OnResourceChange(); } | 233 model_.reset(new LegacyTaskManagerTesterImpl(on_resource_change)); |
234 } | |
235 } | |
53 | 236 |
54 void RunUntilSatisfied() { | 237 void RunUntilSatisfied() { |
55 // See if the condition is satisfied without having to run the loop. This | 238 // See if the condition is satisfied without having to run the loop. This |
56 // check has to be placed after the installation of the | 239 // check has to be placed after the installation of the |
57 // TaskManagerModelObserver, because resources may change before that. | 240 // TaskManagerModelObserver, because resources may change before that. |
58 if (IsSatisfied()) | 241 if (IsSatisfied()) |
59 return; | 242 return; |
60 | 243 |
61 timer_.Start(FROM_HERE, | 244 timer_.Start(FROM_HERE, |
62 TestTimeouts::action_timeout(), | 245 TestTimeouts::action_timeout(), |
(...skipping 21 matching lines...) Expand all Loading... | |
84 return; | 267 return; |
85 | 268 |
86 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 269 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
87 run_loop_.QuitClosure()); | 270 run_loop_.QuitClosure()); |
88 } | 271 } |
89 | 272 |
90 bool IsSatisfied() { return CountMatches() == required_count_; } | 273 bool IsSatisfied() { return CountMatches() == required_count_; } |
91 | 274 |
92 int CountMatches() { | 275 int CountMatches() { |
93 int match_count = 0; | 276 int match_count = 0; |
94 for (int i = 0; i < model_->ResourceCount(); i++) { | 277 for (int i = 0; i < model_->GetRowCount(); i++) { |
95 if (!base::MatchPattern(model_->GetResourceTitle(i), title_pattern_)) | 278 if (!base::MatchPattern(model_->GetRowTitle(i), title_pattern_)) |
96 continue; | 279 continue; |
97 | 280 |
98 if (GetColumnValue(i) < min_column_value_) | 281 if (GetColumnValue(i) < min_column_value_) |
99 continue; | 282 continue; |
100 | 283 |
101 match_count++; | 284 match_count++; |
102 } | 285 } |
103 return match_count; | 286 return match_count; |
104 } | 287 } |
105 | 288 |
106 size_t GetColumnValue(int index) { | 289 int64_t GetColumnValue(int index) { |
107 size_t value = 0; | 290 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 } | 291 } |
126 | 292 |
127 const char* GetColumnName() { | 293 const char* GetColumnName() { |
128 switch (column_specifier_) { | 294 switch (column_specifier_) { |
129 case COLUMN_NONE: | 295 case COLUMN_NONE: |
130 return "N/A"; | 296 return "N/A"; |
131 case V8_MEMORY: | 297 case V8_MEMORY: |
132 return "V8 Memory"; | 298 return "V8 Memory"; |
133 case V8_MEMORY_USED: | 299 case V8_MEMORY_USED: |
134 return "V8 Memory Used"; | 300 return "V8 Memory Used"; |
(...skipping 14 matching lines...) Expand all Loading... | |
149 task_manager_state_dump << "Waiting for exactly " << required_count_ | 315 task_manager_state_dump << "Waiting for exactly " << required_count_ |
150 << " matches of wildcard pattern \"" | 316 << " matches of wildcard pattern \"" |
151 << base::UTF16ToASCII(title_pattern_) << "\""; | 317 << base::UTF16ToASCII(title_pattern_) << "\""; |
152 if (min_column_value_ > 0) { | 318 if (min_column_value_ > 0) { |
153 task_manager_state_dump << " && [" << GetColumnName() | 319 task_manager_state_dump << " && [" << GetColumnName() |
154 << " >= " << min_column_value_ << "]"; | 320 << " >= " << min_column_value_ << "]"; |
155 } | 321 } |
156 task_manager_state_dump << "\nCurrently there are " << CountMatches() | 322 task_manager_state_dump << "\nCurrently there are " << CountMatches() |
157 << " matches."; | 323 << " matches."; |
158 task_manager_state_dump << "\nCurrent Task Manager Model is:"; | 324 task_manager_state_dump << "\nCurrent Task Manager Model is:"; |
159 for (int i = 0; i < model_->ResourceCount(); i++) { | 325 for (int i = 0; i < model_->GetRowCount(); i++) { |
160 task_manager_state_dump | 326 task_manager_state_dump << "\n > " << std::setw(40) << std::left |
161 << "\n > " << std::setw(40) << std::left | 327 << base::UTF16ToASCII(model_->GetRowTitle(i)); |
162 << base::UTF16ToASCII(model_->GetResourceTitle(i)); | |
163 if (min_column_value_ > 0) { | 328 if (min_column_value_ > 0) { |
164 task_manager_state_dump << " [" << GetColumnName() | 329 task_manager_state_dump << " [" << GetColumnName() |
165 << " == " << GetColumnValue(i) << "]"; | 330 << " == " << GetColumnValue(i) << "]"; |
166 } | 331 } |
167 } | 332 } |
168 return task_manager_state_dump; | 333 return task_manager_state_dump; |
169 } | 334 } |
170 | 335 |
171 const TaskManagerModel* model_; | 336 std::unique_ptr<TaskManagerTester> model_; |
172 const int required_count_; | 337 const int required_count_; |
173 const base::string16 title_pattern_; | 338 const base::string16 title_pattern_; |
174 const ColumnSpecifier column_specifier_; | 339 const ColumnSpecifier column_specifier_; |
175 const size_t min_column_value_; | 340 const int64_t min_column_value_; |
176 base::RunLoop run_loop_; | 341 base::RunLoop run_loop_; |
177 base::OneShotTimer timer_; | 342 base::OneShotTimer timer_; |
178 }; | 343 }; |
179 | 344 |
180 } // namespace | 345 } // namespace |
181 | 346 |
182 void EnableOldTaskManager() { | 347 std::unique_ptr<TaskManagerTester> GetTaskManagerTester() { |
183 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 348 base::Closure do_nothing = base::Bind(&base::DoNothing); |
184 switches::kDisableNewTaskManager); | 349 if (switches::NewTaskManagerEnabled()) { |
350 return base::WrapUnique( | |
351 new task_management::TaskManagerTesterImpl(do_nothing)); | |
352 } else { | |
353 return base::WrapUnique(new LegacyTaskManagerTesterImpl(do_nothing)); | |
354 } | |
185 } | 355 } |
186 | 356 |
187 void WaitForTaskManagerRows(int required_count, | 357 void WaitForTaskManagerRows(int required_count, |
188 const base::string16& title_pattern) { | 358 const base::string16& title_pattern) { |
189 TaskManagerModel* model = TaskManager::GetInstance()->model(); | |
190 | |
191 const int column_value_dont_care = 0; | 359 const int column_value_dont_care = 0; |
192 ResourceChangeObserver observer(model, required_count, title_pattern, | 360 ResourceChangeObserver observer(required_count, title_pattern, COLUMN_NONE, |
193 COLUMN_NONE, column_value_dont_care); | 361 column_value_dont_care); |
194 model->AddObserver(&observer); | |
195 observer.RunUntilSatisfied(); | 362 observer.RunUntilSatisfied(); |
196 model->RemoveObserver(&observer); | |
197 } | 363 } |
198 | 364 |
199 void WaitForTaskManagerStatToExceed(const base::string16& title_pattern, | 365 void WaitForTaskManagerStatToExceed(const base::string16& title_pattern, |
200 ColumnSpecifier column_getter, | 366 ColumnSpecifier column_getter, |
201 size_t min_column_value) { | 367 size_t min_column_value) { |
202 TaskManagerModel* model = TaskManager::GetInstance()->model(); | |
203 | |
204 const int wait_for_one_match = 1; | 368 const int wait_for_one_match = 1; |
205 ResourceChangeObserver observer(model, wait_for_one_match, title_pattern, | 369 ResourceChangeObserver observer(wait_for_one_match, title_pattern, |
206 column_getter, min_column_value); | 370 column_getter, min_column_value); |
207 model->AddObserver(&observer); | |
208 observer.RunUntilSatisfied(); | 371 observer.RunUntilSatisfied(); |
209 model->RemoveObserver(&observer); | |
210 } | 372 } |
211 | 373 |
212 base::string16 MatchTab(const char* title) { | 374 base::string16 MatchTab(const char* title) { |
213 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX, | 375 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX, |
214 base::ASCIIToUTF16(title)); | 376 base::ASCIIToUTF16(title)); |
215 } | 377 } |
216 | 378 |
217 base::string16 MatchAnyTab() { return MatchTab("*"); } | 379 base::string16 MatchAnyTab() { return MatchTab("*"); } |
218 | 380 |
219 base::string16 MatchAboutBlankTab() { return MatchTab("about:blank"); } | 381 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) { | 428 base::string16 MatchUtility(const base::string16& title) { |
267 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_UTILITY_PREFIX, title); | 429 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_UTILITY_PREFIX, title); |
268 } | 430 } |
269 | 431 |
270 base::string16 MatchAnyUtility() { | 432 base::string16 MatchAnyUtility() { |
271 return MatchUtility(base::ASCIIToUTF16("*")); | 433 return MatchUtility(base::ASCIIToUTF16("*")); |
272 } | 434 } |
273 | 435 |
274 } // namespace browsertest_util | 436 } // namespace browsertest_util |
275 } // namespace task_manager | 437 } // namespace task_manager |
OLD | NEW |