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
3 // found in the LICENSE file.
5 #include "base/message_loop/message_loop.h"
6 #include "base/strings/stringprintf.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/task_manager/resource_provider.h"
12 #include "chrome/browser/task_manager/task_manager.h"
13 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_dialogs.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "content/public/browser/notification_source.h"
19 #include "content/public/browser/web_contents.h"
23 int GetWebResourceCount(const TaskManagerModel
* model
) {
25 for (int i
= 0; i
< model
->ResourceCount(); i
++) {
26 task_manager::Resource::Type type
= model
->GetResourceType(i
);
27 // Skip system infrastructure resources.
28 if (type
== task_manager::Resource::BROWSER
||
29 type
== task_manager::Resource::NACL
||
30 type
== task_manager::Resource::GPU
||
31 type
== task_manager::Resource::UTILITY
||
32 type
== task_manager::Resource::ZYGOTE
||
33 type
== task_manager::Resource::SANDBOX_HELPER
) {
42 class ResourceChangeObserver
: public TaskManagerModelObserver
{
44 ResourceChangeObserver(const TaskManagerModel
* model
,
45 int target_resource_count
)
47 target_resource_count_(target_resource_count
) {
50 virtual void OnModelChanged() OVERRIDE
{
54 virtual void OnItemsChanged(int start
, int length
) OVERRIDE
{
58 virtual void OnItemsAdded(int start
, int length
) OVERRIDE
{
62 virtual void OnItemsRemoved(int start
, int length
) OVERRIDE
{
67 void OnResourceChange() {
68 if (GetWebResourceCount(model_
) == target_resource_count_
)
69 base::MessageLoopForUI::current()->Quit();
72 const TaskManagerModel
* model_
;
73 const int target_resource_count_
;
79 void TaskManagerBrowserTestUtil::WaitForWebResourceChange(int target_count
) {
80 TaskManagerModel
* model
= TaskManager::GetInstance()->model();
82 ResourceChangeObserver
observer(model
, target_count
);
83 model
->AddObserver(&observer
);
85 // Checks that the condition has not been satisfied yet.
86 // This check has to be placed after the installation of the observer,
87 // because resources may change before that.
88 if (GetWebResourceCount(model
) == target_count
) {
89 model
->RemoveObserver(&observer
);
93 content::RunMessageLoop();
94 model
->RemoveObserver(&observer
);