- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / task_manager / task_manager_browsertest_util.cc
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.
4
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"
20
21 namespace {
22
23 int GetWebResourceCount(const TaskManagerModel* model) {
24   int count = 0;
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) {
34       continue;
35     }
36
37     count++;
38   }
39   return count;
40 }
41
42 class ResourceChangeObserver : public TaskManagerModelObserver {
43  public:
44   ResourceChangeObserver(const TaskManagerModel* model,
45                          int target_resource_count)
46       : model_(model),
47         target_resource_count_(target_resource_count) {
48   }
49
50   virtual void OnModelChanged() OVERRIDE {
51     OnResourceChange();
52   }
53
54   virtual void OnItemsChanged(int start, int length) OVERRIDE {
55     OnResourceChange();
56   }
57
58   virtual void OnItemsAdded(int start, int length) OVERRIDE {
59     OnResourceChange();
60   }
61
62   virtual void OnItemsRemoved(int start, int length) OVERRIDE {
63     OnResourceChange();
64   }
65
66  private:
67   void OnResourceChange() {
68     if (GetWebResourceCount(model_) == target_resource_count_)
69       base::MessageLoopForUI::current()->Quit();
70   }
71
72   const TaskManagerModel* model_;
73   const int target_resource_count_;
74 };
75
76 }  // namespace
77
78 // static
79 void TaskManagerBrowserTestUtil::WaitForWebResourceChange(int target_count) {
80   TaskManagerModel* model = TaskManager::GetInstance()->model();
81
82   ResourceChangeObserver observer(model, target_count);
83   model->AddObserver(&observer);
84
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);
90     return;
91   }
92
93   content::RunMessageLoop();
94   model->RemoveObserver(&observer);
95 }