| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_management/task_manager_tester.h" |
| 6 | 6 |
| 7 #include "base/bind_helpers.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/location.h" | |
| 10 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/single_thread_task_runner.h" | |
| 13 #include "base/strings/pattern.h" | |
| 14 #include "base/strings/string16.h" | |
| 15 #include "base/strings/string_util.h" | |
| 16 #include "base/strings/utf_string_conversions.h" | |
| 17 #include "base/test/test_timeouts.h" | |
| 18 #include "base/thread_task_runner_handle.h" | |
| 19 #include "base/timer/timer.h" | |
| 20 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/sessions/session_tab_helper.h" | 10 #include "chrome/browser/sessions/session_tab_helper.h" |
| 23 #include "chrome/browser/task_management/task_manager_interface.h" | 11 #include "chrome/browser/task_management/task_manager_interface.h" |
| 24 #include "chrome/browser/task_manager/legacy_task_manager_tester.h" | 12 #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" | 13 #include "chrome/browser/ui/browser_dialogs.h" |
| 28 #include "chrome/browser/ui/task_manager/task_manager_table_model.h" | 14 #include "chrome/browser/ui/task_manager/task_manager_table_model.h" |
| 29 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 30 #include "chrome/grit/generated_resources.h" | 16 #include "chrome/grit/generated_resources.h" |
| 31 #include "extensions/strings/grit/extensions_strings.h" | |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 33 #include "ui/base/l10n/l10n_util.h" | |
| 34 #include "ui/base/models/table_model_observer.h" | 18 #include "ui/base/models/table_model_observer.h" |
| 35 | 19 |
| 36 namespace task_manager { | 20 namespace task_management { |
| 37 namespace browsertest_util { | |
| 38 | 21 |
| 39 namespace { | 22 namespace { |
| 40 | 23 |
| 41 // Returns whether chrome::ShowTaskManager() will, for the current platform and | 24 // Returns whether chrome::ShowTaskManager() will, for the current platform and |
| 42 // command line, show a view backed by a task_management::TaskManagerTableModel. | 25 // command line, show a view backed by a task_management::TaskManagerTableModel. |
| 43 bool IsNewTaskManagerViewEnabled() { | 26 bool IsNewTaskManagerViewEnabled() { |
| 44 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) |
| 45 if (!chrome::ToolkitViewsDialogsEnabled()) | 28 if (!chrome::ToolkitViewsDialogsEnabled()) |
| 46 return false; | 29 return false; |
| 47 #endif | 30 #endif |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 task_management::TaskManagerTableModel* result = | 161 task_management::TaskManagerTableModel* result = |
| 179 static_cast<task_management::TaskManagerTableModel*>( | 162 static_cast<task_management::TaskManagerTableModel*>( |
| 180 chrome::ShowTaskManager(nullptr)); | 163 chrome::ShowTaskManager(nullptr)); |
| 181 return result; | 164 return result; |
| 182 } | 165 } |
| 183 | 166 |
| 184 task_management::TaskManagerTableModel* model_; | 167 task_management::TaskManagerTableModel* model_; |
| 185 std::unique_ptr<ScopedInterceptTableModelObserver> interceptor_; | 168 std::unique_ptr<ScopedInterceptTableModelObserver> interceptor_; |
| 186 }; | 169 }; |
| 187 | 170 |
| 188 namespace { | 171 // static |
| 189 | 172 std::unique_ptr<TaskManagerTester> TaskManagerTester::Create( |
| 190 // Helper class to run a message loop until a TaskManagerTester is in an | 173 const base::Closure& callback) { |
| 191 // 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 | |
| 193 // output, to assist debugging. | |
| 194 class ResourceChangeObserver { | |
| 195 public: | |
| 196 ResourceChangeObserver(int required_count, | |
| 197 const base::string16& title_pattern, | |
| 198 ColumnSpecifier column_specifier, | |
| 199 size_t min_column_value) | |
| 200 : required_count_(required_count), | |
| 201 title_pattern_(title_pattern), | |
| 202 column_specifier_(column_specifier), | |
| 203 min_column_value_(min_column_value) { | |
| 204 base::Closure callback = base::Bind( | |
| 205 &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 } | |
| 212 | |
| 213 void RunUntilSatisfied() { | |
| 214 // See if the condition is satisfied without having to run the loop. This | |
| 215 // check has to be placed after the installation of the | |
| 216 // TaskManagerModelObserver, because resources may change before that. | |
| 217 if (IsSatisfied()) | |
| 218 return; | |
| 219 | |
| 220 timer_.Start(FROM_HERE, TestTimeouts::action_timeout(), this, | |
| 221 &ResourceChangeObserver::OnTimeout); | |
| 222 | |
| 223 run_loop_.Run(); | |
| 224 | |
| 225 // If we succeeded normally (no timeout), check our post condition again | |
| 226 // before returning control to the test. If it is no longer satisfied, the | |
| 227 // test is likely flaky: we were waiting for a state that was only achieved | |
| 228 // emphemerally), so treat this as a failure. | |
| 229 if (!IsSatisfied() && timer_.IsRunning()) { | |
| 230 FAIL() << "Wait condition satisfied only emphemerally. Likely test " | |
| 231 << "problem. Maybe wait instead for the state below?\n" | |
| 232 << DumpTaskManagerModel(); | |
| 233 } | |
| 234 | |
| 235 timer_.Stop(); | |
| 236 } | |
| 237 | |
| 238 private: | |
| 239 void OnResourceChange() { | |
| 240 if (!IsSatisfied()) | |
| 241 return; | |
| 242 | |
| 243 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 244 run_loop_.QuitClosure()); | |
| 245 } | |
| 246 | |
| 247 bool IsSatisfied() { return CountMatches() == required_count_; } | |
| 248 | |
| 249 int CountMatches() { | |
| 250 int match_count = 0; | |
| 251 for (int i = 0; i < task_manager_tester_->GetRowCount(); i++) { | |
| 252 if (!base::MatchPattern(task_manager_tester_->GetRowTitle(i), | |
| 253 title_pattern_)) | |
| 254 continue; | |
| 255 | |
| 256 if (GetColumnValue(i) < min_column_value_) | |
| 257 continue; | |
| 258 | |
| 259 match_count++; | |
| 260 } | |
| 261 return match_count; | |
| 262 } | |
| 263 | |
| 264 int64_t GetColumnValue(int index) { | |
| 265 return task_manager_tester_->GetColumnValue(column_specifier_, index); | |
| 266 } | |
| 267 | |
| 268 const char* GetColumnName() { | |
| 269 switch (column_specifier_) { | |
| 270 case ColumnSpecifier::COLUMN_NONE: | |
| 271 return "N/A"; | |
| 272 case ColumnSpecifier::V8_MEMORY: | |
| 273 return "V8 Memory"; | |
| 274 case ColumnSpecifier::V8_MEMORY_USED: | |
| 275 return "V8 Memory Used"; | |
| 276 case ColumnSpecifier::SQLITE_MEMORY_USED: | |
| 277 return "SQLite Memory Used"; | |
| 278 } | |
| 279 return "N/A"; | |
| 280 } | |
| 281 | |
| 282 void OnTimeout() { | |
| 283 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 284 run_loop_.QuitClosure()); | |
| 285 FAIL() << "Timed out.\n" << DumpTaskManagerModel(); | |
| 286 } | |
| 287 | |
| 288 testing::Message DumpTaskManagerModel() { | |
| 289 testing::Message task_manager_state_dump; | |
| 290 task_manager_state_dump << "Waiting for exactly " << required_count_ | |
| 291 << " matches of wildcard pattern \"" | |
| 292 << base::UTF16ToASCII(title_pattern_) << "\""; | |
| 293 if (min_column_value_ > 0) { | |
| 294 task_manager_state_dump << " && [" << GetColumnName() | |
| 295 << " >= " << min_column_value_ << "]"; | |
| 296 } | |
| 297 task_manager_state_dump << "\nCurrently there are " << CountMatches() | |
| 298 << " matches."; | |
| 299 task_manager_state_dump << "\nCurrent Task Manager Model is:"; | |
| 300 for (int i = 0; i < task_manager_tester_->GetRowCount(); i++) { | |
| 301 task_manager_state_dump | |
| 302 << "\n > " << std::setw(40) << std::left | |
| 303 << base::UTF16ToASCII(task_manager_tester_->GetRowTitle(i)); | |
| 304 if (min_column_value_ > 0) { | |
| 305 task_manager_state_dump << " [" << GetColumnName() | |
| 306 << " == " << GetColumnValue(i) << "]"; | |
| 307 } | |
| 308 } | |
| 309 return task_manager_state_dump; | |
| 310 } | |
| 311 | |
| 312 std::unique_ptr<TaskManagerTester> task_manager_tester_; | |
| 313 const int required_count_; | |
| 314 const base::string16 title_pattern_; | |
| 315 const ColumnSpecifier column_specifier_; | |
| 316 const int64_t min_column_value_; | |
| 317 base::RunLoop run_loop_; | |
| 318 base::OneShotTimer timer_; | |
| 319 }; | |
| 320 | |
| 321 } // namespace | |
| 322 | |
| 323 std::unique_ptr<TaskManagerTester> GetTaskManagerTester() { | |
| 324 if (IsNewTaskManagerViewEnabled()) | 174 if (IsNewTaskManagerViewEnabled()) |
| 325 return base::WrapUnique(new TaskManagerTesterImpl(base::Closure())); | 175 return base::WrapUnique(new TaskManagerTesterImpl(callback)); |
| 326 else | 176 else |
| 327 return CreateLegacyTaskManagerTester(base::Closure()); | 177 return task_manager::CreateLegacyTaskManagerTester(callback); |
| 328 } | 178 } |
| 329 | 179 |
| 330 void WaitForTaskManagerRows(int required_count, | 180 } // namespace task_management |
| 331 const base::string16& title_pattern) { | |
| 332 const int column_value_dont_care = 0; | |
| 333 ResourceChangeObserver observer(required_count, title_pattern, | |
| 334 ColumnSpecifier::COLUMN_NONE, | |
| 335 column_value_dont_care); | |
| 336 observer.RunUntilSatisfied(); | |
| 337 } | |
| 338 | |
| 339 void WaitForTaskManagerStatToExceed(const base::string16& title_pattern, | |
| 340 ColumnSpecifier column_getter, | |
| 341 size_t min_column_value) { | |
| 342 const int wait_for_one_match = 1; | |
| 343 ResourceChangeObserver observer(wait_for_one_match, title_pattern, | |
| 344 column_getter, min_column_value); | |
| 345 observer.RunUntilSatisfied(); | |
| 346 } | |
| 347 | |
| 348 base::string16 MatchTab(const char* title) { | |
| 349 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX, | |
| 350 base::ASCIIToUTF16(title)); | |
| 351 } | |
| 352 | |
| 353 base::string16 MatchAnyTab() { | |
| 354 return MatchTab("*"); | |
| 355 } | |
| 356 | |
| 357 base::string16 MatchAboutBlankTab() { | |
| 358 return MatchTab("about:blank"); | |
| 359 } | |
| 360 | |
| 361 base::string16 MatchExtension(const char* title) { | |
| 362 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_EXTENSION_PREFIX, | |
| 363 base::ASCIIToUTF16(title)); | |
| 364 } | |
| 365 | |
| 366 base::string16 MatchAnyExtension() { | |
| 367 return MatchExtension("*"); | |
| 368 } | |
| 369 | |
| 370 base::string16 MatchApp(const char* title) { | |
| 371 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_APP_PREFIX, | |
| 372 base::ASCIIToUTF16(title)); | |
| 373 } | |
| 374 | |
| 375 base::string16 MatchAnyApp() { | |
| 376 return MatchApp("*"); | |
| 377 } | |
| 378 | |
| 379 base::string16 MatchWebView(const char* title) { | |
| 380 return l10n_util::GetStringFUTF16( | |
| 381 IDS_EXTENSION_TASK_MANAGER_WEBVIEW_TAG_PREFIX, base::ASCIIToUTF16(title)); | |
| 382 } | |
| 383 | |
| 384 base::string16 MatchAnyWebView() { | |
| 385 return MatchWebView("*"); | |
| 386 } | |
| 387 | |
| 388 base::string16 MatchBackground(const char* title) { | |
| 389 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_BACKGROUND_PREFIX, | |
| 390 base::ASCIIToUTF16(title)); | |
| 391 } | |
| 392 | |
| 393 base::string16 MatchAnyBackground() { | |
| 394 return MatchBackground("*"); | |
| 395 } | |
| 396 | |
| 397 base::string16 MatchPrint(const char* title) { | |
| 398 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRINT_PREFIX, | |
| 399 base::ASCIIToUTF16(title)); | |
| 400 } | |
| 401 | |
| 402 base::string16 MatchAnyPrint() { | |
| 403 return MatchPrint("*"); | |
| 404 } | |
| 405 | |
| 406 base::string16 MatchSubframe(const char* title) { | |
| 407 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_SUBFRAME_PREFIX, | |
| 408 base::ASCIIToUTF16(title)); | |
| 409 } | |
| 410 | |
| 411 base::string16 MatchAnySubframe() { | |
| 412 return MatchSubframe("*"); | |
| 413 } | |
| 414 | |
| 415 base::string16 MatchUtility(const base::string16& title) { | |
| 416 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_UTILITY_PREFIX, title); | |
| 417 } | |
| 418 | |
| 419 base::string16 MatchAnyUtility() { | |
| 420 return MatchUtility(base::ASCIIToUTF16("*")); | |
| 421 } | |
| 422 | |
| 423 } // namespace browsertest_util | |
| 424 } // namespace task_manager | |
| OLD | NEW |