OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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.h" | 5 #include "chrome/browser/task_manager/task_manager.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/background_contents_service.h" | 10 #include "chrome/browser/background_contents_service.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "chrome/common/page_transition_types.h" | 27 #include "chrome/common/page_transition_types.h" |
28 #include "chrome/test/in_process_browser_test.h" | 28 #include "chrome/test/in_process_browser_test.h" |
29 #include "chrome/test/ui_test_utils.h" | 29 #include "chrome/test/ui_test_utils.h" |
30 #include "grit/generated_resources.h" | 30 #include "grit/generated_resources.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
32 | 32 |
33 namespace { | 33 namespace { |
34 | 34 |
35 const FilePath::CharType* kTitle1File = FILE_PATH_LITERAL("title1.html"); | 35 const FilePath::CharType* kTitle1File = FILE_PATH_LITERAL("title1.html"); |
36 | 36 |
37 class ResourceChangeObserver | 37 class ResourceChangeObserver : public TaskManagerModelObserver { |
38 : public TaskManagerModelObserver, | |
39 public base::RefCountedThreadSafe<ResourceChangeObserver> { | |
40 public: | 38 public: |
41 ResourceChangeObserver(const TaskManagerModel* model, | 39 ResourceChangeObserver(const TaskManagerModel* model, |
42 int target_resource_count) | 40 int target_resource_count) |
43 : model_(model), | 41 : model_(model), |
44 target_resource_count_(target_resource_count), | 42 target_resource_count_(target_resource_count) { |
45 check_pending_(false) { | |
46 } | 43 } |
47 | 44 |
48 virtual void OnModelChanged() { | 45 virtual void OnModelChanged() { |
49 OnResourceChange(); | 46 OnResourceChange(); |
50 } | 47 } |
51 | 48 |
52 virtual void OnItemsChanged(int start, int length) { | 49 virtual void OnItemsChanged(int start, int length) { |
53 OnResourceChange(); | 50 OnResourceChange(); |
54 } | 51 } |
55 | 52 |
56 virtual void OnItemsAdded(int start, int length) { | 53 virtual void OnItemsAdded(int start, int length) { |
57 OnResourceChange(); | 54 OnResourceChange(); |
58 } | 55 } |
59 | 56 |
60 virtual void OnItemsRemoved(int start, int length) { | 57 virtual void OnItemsRemoved(int start, int length) { |
61 OnResourceChange(); | 58 OnResourceChange(); |
62 } | 59 } |
63 | 60 |
64 private: | 61 private: |
65 friend class base::RefCountedThreadSafe<ResourceChangeObserver>; | |
66 void OnResourceChange() { | 62 void OnResourceChange() { |
67 // The task manager can churn resources (for example, when a | |
68 // BackgroundContents navigates, we remove and re-add the resource to | |
69 // allow the UI to update properly). So check the resource count via | |
70 // a task to make sure we aren't triggered by a transient change. | |
71 if (check_pending_) | |
72 return; | |
73 check_pending_ = true; | |
74 MessageLoopForUI::current()->PostTask( | |
75 FROM_HERE, | |
76 NewRunnableMethod(this, &ResourceChangeObserver::CheckResourceCount)); | |
77 } | |
78 | |
79 void CheckResourceCount() { | |
80 if (model_->ResourceCount() == target_resource_count_) | 63 if (model_->ResourceCount() == target_resource_count_) |
81 MessageLoopForUI::current()->Quit(); | 64 MessageLoopForUI::current()->Quit(); |
82 } | 65 } |
83 | 66 |
84 const TaskManagerModel* model_; | 67 const TaskManagerModel* model_; |
85 const int target_resource_count_; | 68 const int target_resource_count_; |
86 bool check_pending_; | |
87 }; | |
88 | |
89 // Helper class used to wait for a BackgroundContents to finish loading. | |
90 class BackgroundContentsListener : public NotificationObserver { | |
91 public: | |
92 explicit BackgroundContentsListener(Profile* profile) { | |
93 registrar_.Add(this, NotificationType::BACKGROUND_CONTENTS_NAVIGATED, | |
94 Source<Profile>(profile)); | |
95 } | |
96 virtual void Observe(NotificationType type, | |
97 const NotificationSource& source, | |
98 const NotificationDetails& details) { | |
99 // Quit once the BackgroundContents has been loaded. | |
100 if (type.value == NotificationType::BACKGROUND_CONTENTS_NAVIGATED) | |
101 MessageLoopForUI::current()->Quit(); | |
102 } | |
103 private: | |
104 NotificationRegistrar registrar_; | |
105 }; | 69 }; |
106 | 70 |
107 } // namespace | 71 } // namespace |
108 | 72 |
109 class TaskManagerBrowserTest : public ExtensionBrowserTest { | 73 class TaskManagerBrowserTest : public ExtensionBrowserTest { |
110 public: | 74 public: |
111 TaskManagerModel* model() const { | 75 TaskManagerModel* model() const { |
112 return TaskManager::GetInstance()->model(); | 76 return TaskManager::GetInstance()->model(); |
113 } | 77 } |
114 | 78 |
115 void WaitForResourceChange(int target_count) { | 79 void WaitForResourceChange(int target_count) { |
116 if (model()->ResourceCount() == target_count) | 80 if (model()->ResourceCount() == target_count) |
117 return; | 81 return; |
118 scoped_refptr<ResourceChangeObserver> observer( | 82 ResourceChangeObserver observer(model(), target_count); |
119 new ResourceChangeObserver(model(), target_count)); | 83 model()->AddObserver(&observer); |
120 model()->AddObserver(observer.get()); | |
121 ui_test_utils::RunMessageLoop(); | 84 ui_test_utils::RunMessageLoop(); |
122 model()->RemoveObserver(observer.get()); | 85 model()->RemoveObserver(&observer); |
123 } | |
124 | |
125 // Wait for any pending BackgroundContents to finish starting up. | |
126 void WaitForBackgroundContents() { | |
127 BackgroundContentsListener listener(browser()->profile()); | |
128 ui_test_utils::RunMessageLoop(); | |
129 } | 86 } |
130 }; | 87 }; |
131 | 88 |
132 // Regression test for http://crbug.com/13361 | 89 // Regression test for http://crbug.com/13361 |
133 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, ShutdownWhileOpen) { | 90 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, ShutdownWhileOpen) { |
134 browser()->window()->ShowTaskManager(); | 91 browser()->window()->ShowTaskManager(); |
135 } | 92 } |
136 | 93 |
137 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeTabContentsChanges) { | 94 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeTabContentsChanges) { |
138 EXPECT_EQ(0, model()->ResourceCount()); | 95 EXPECT_EQ(0, model()->ResourceCount()); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 url, | 142 url, |
186 ASCIIToUTF16("background_page"), | 143 ASCIIToUTF16("background_page"), |
187 application_id); | 144 application_id); |
188 WaitForResourceChange(3); | 145 WaitForResourceChange(3); |
189 | 146 |
190 // Close the background contents and verify that we notice. | 147 // Close the background contents and verify that we notice. |
191 service->ShutdownAssociatedBackgroundContents(application_id); | 148 service->ShutdownAssociatedBackgroundContents(application_id); |
192 WaitForResourceChange(2); | 149 WaitForResourceChange(2); |
193 } | 150 } |
194 | 151 |
195 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, KillBGContents) { | |
196 EXPECT_EQ(0, model()->ResourceCount()); | |
197 | |
198 // Show the task manager. This populates the model, and helps with debugging | |
199 // (you see the task manager). | |
200 browser()->window()->ShowTaskManager(); | |
201 | |
202 // Browser and the New Tab Page. | |
203 WaitForResourceChange(2); | |
204 | |
205 // Open a new background contents and make sure we notice that. | |
206 GURL url(ui_test_utils::GetTestUrl(FilePath(FilePath::kCurrentDirectory), | |
207 FilePath(kTitle1File))); | |
208 | |
209 BackgroundContentsService* service = | |
210 browser()->profile()->GetBackgroundContentsService(); | |
211 string16 application_id(ASCIIToUTF16("test_app_id")); | |
212 service->LoadBackgroundContents(browser()->profile(), | |
213 url, | |
214 ASCIIToUTF16("background_page"), | |
215 application_id); | |
216 // Wait for the background contents process to finish loading. | |
217 WaitForBackgroundContents(); | |
218 EXPECT_EQ(3, model()->ResourceCount()); | |
219 | |
220 // Kill the background contents process and verify that it disappears from the | |
221 // model. | |
222 bool found = false; | |
223 for (int i = 0; i < model()->ResourceCount(); ++i) { | |
224 if (model()->IsBackgroundResource(i)) { | |
225 TaskManager::GetInstance()->KillProcess(i); | |
226 found = true; | |
227 break; | |
228 } | |
229 } | |
230 ASSERT_TRUE(found); | |
231 WaitForResourceChange(2); | |
232 } | |
233 | |
234 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeExtensionChanges) { | 152 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeExtensionChanges) { |
235 EXPECT_EQ(0, model()->ResourceCount()); | 153 EXPECT_EQ(0, model()->ResourceCount()); |
236 | 154 |
237 // Show the task manager. This populates the model, and helps with debugging | 155 // Show the task manager. This populates the model, and helps with debugging |
238 // (you see the task manager). | 156 // (you see the task manager). |
239 browser()->window()->ShowTaskManager(); | 157 browser()->window()->ShowTaskManager(); |
240 | 158 |
241 // Browser and the New Tab Page. | 159 // Browser and the New Tab Page. |
242 WaitForResourceChange(2); | 160 WaitForResourceChange(2); |
243 | 161 |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 WaitForResourceChange(3); | 393 WaitForResourceChange(3); |
476 | 394 |
477 // Check that we get some value for the cache columns. | 395 // Check that we get some value for the cache columns. |
478 DCHECK_NE(model()->GetResourceWebCoreImageCacheSize(2), | 396 DCHECK_NE(model()->GetResourceWebCoreImageCacheSize(2), |
479 l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT)); | 397 l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT)); |
480 DCHECK_NE(model()->GetResourceWebCoreScriptsCacheSize(2), | 398 DCHECK_NE(model()->GetResourceWebCoreScriptsCacheSize(2), |
481 l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT)); | 399 l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT)); |
482 DCHECK_NE(model()->GetResourceWebCoreCSSCacheSize(2), | 400 DCHECK_NE(model()->GetResourceWebCoreCSSCacheSize(2), |
483 l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT)); | 401 l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT)); |
484 } | 402 } |
OLD | NEW |