Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(301)

Side by Side Diff: chrome/browser/task_manager/task_manager_browsertest.cc

Issue 6226002: Blow away BackgroundContents when RenderView goes away. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/chrome
Patch Set: More style cleanups. Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/tab_contents/background_contents.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 : public TaskManagerModelObserver { 37 class ResourceChangeObserver
38 : public TaskManagerModelObserver,
39 public base::RefCountedThreadSafe<ResourceChangeObserver> {
38 public: 40 public:
39 ResourceChangeObserver(const TaskManagerModel* model, 41 ResourceChangeObserver(const TaskManagerModel* model,
40 int target_resource_count) 42 int target_resource_count)
41 : model_(model), 43 : model_(model),
42 target_resource_count_(target_resource_count) { 44 target_resource_count_(target_resource_count),
45 check_pending_(false) {
43 } 46 }
44 47
45 virtual void OnModelChanged() { 48 virtual void OnModelChanged() {
46 OnResourceChange(); 49 OnResourceChange();
47 } 50 }
48 51
49 virtual void OnItemsChanged(int start, int length) { 52 virtual void OnItemsChanged(int start, int length) {
50 OnResourceChange(); 53 OnResourceChange();
51 } 54 }
52 55
53 virtual void OnItemsAdded(int start, int length) { 56 virtual void OnItemsAdded(int start, int length) {
54 OnResourceChange(); 57 OnResourceChange();
55 } 58 }
56 59
57 virtual void OnItemsRemoved(int start, int length) { 60 virtual void OnItemsRemoved(int start, int length) {
58 OnResourceChange(); 61 OnResourceChange();
59 } 62 }
60 63
61 private: 64 private:
65 friend class base::RefCountedThreadSafe<ResourceChangeObserver>;
62 void OnResourceChange() { 66 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.
rafaelw 2011/01/14 01:04:46 I'm not familiar with the TaskManager or this test
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() {
63 if (model_->ResourceCount() == target_resource_count_) 80 if (model_->ResourceCount() == target_resource_count_)
64 MessageLoopForUI::current()->Quit(); 81 MessageLoopForUI::current()->Quit();
65 } 82 }
66 83
67 const TaskManagerModel* model_; 84 const TaskManagerModel* model_;
68 const int target_resource_count_; 85 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_;
69 }; 105 };
70 106
71 } // namespace 107 } // namespace
72 108
73 class TaskManagerBrowserTest : public ExtensionBrowserTest { 109 class TaskManagerBrowserTest : public ExtensionBrowserTest {
74 public: 110 public:
75 TaskManagerModel* model() const { 111 TaskManagerModel* model() const {
76 return TaskManager::GetInstance()->model(); 112 return TaskManager::GetInstance()->model();
77 } 113 }
78 114
79 void WaitForResourceChange(int target_count) { 115 void WaitForResourceChange(int target_count) {
80 if (model()->ResourceCount() == target_count) 116 if (model()->ResourceCount() == target_count)
81 return; 117 return;
82 ResourceChangeObserver observer(model(), target_count); 118 scoped_refptr<ResourceChangeObserver> observer(
83 model()->AddObserver(&observer); 119 new ResourceChangeObserver(model(), target_count));
120 model()->AddObserver(observer.get());
84 ui_test_utils::RunMessageLoop(); 121 ui_test_utils::RunMessageLoop();
85 model()->RemoveObserver(&observer); 122 model()->RemoveObserver(observer.get());
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();
86 } 129 }
87 }; 130 };
88 131
89 // Regression test for http://crbug.com/13361 132 // Regression test for http://crbug.com/13361
90 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, ShutdownWhileOpen) { 133 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, ShutdownWhileOpen) {
91 browser()->window()->ShowTaskManager(); 134 browser()->window()->ShowTaskManager();
92 } 135 }
93 136
94 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeTabContentsChanges) { 137 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeTabContentsChanges) {
95 EXPECT_EQ(0, model()->ResourceCount()); 138 EXPECT_EQ(0, model()->ResourceCount());
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 url, 185 url,
143 ASCIIToUTF16("background_page"), 186 ASCIIToUTF16("background_page"),
144 application_id); 187 application_id);
145 WaitForResourceChange(3); 188 WaitForResourceChange(3);
146 189
147 // Close the background contents and verify that we notice. 190 // Close the background contents and verify that we notice.
148 service->ShutdownAssociatedBackgroundContents(application_id); 191 service->ShutdownAssociatedBackgroundContents(application_id);
149 WaitForResourceChange(2); 192 WaitForResourceChange(2);
150 } 193 }
151 194
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
152 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeExtensionChanges) { 234 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeExtensionChanges) {
153 EXPECT_EQ(0, model()->ResourceCount()); 235 EXPECT_EQ(0, model()->ResourceCount());
154 236
155 // Show the task manager. This populates the model, and helps with debugging 237 // Show the task manager. This populates the model, and helps with debugging
156 // (you see the task manager). 238 // (you see the task manager).
157 browser()->window()->ShowTaskManager(); 239 browser()->window()->ShowTaskManager();
158 240
159 // Browser and the New Tab Page. 241 // Browser and the New Tab Page.
160 WaitForResourceChange(2); 242 WaitForResourceChange(2);
161 243
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 WaitForResourceChange(3); 475 WaitForResourceChange(3);
394 476
395 // Check that we get some value for the cache columns. 477 // Check that we get some value for the cache columns.
396 DCHECK_NE(model()->GetResourceWebCoreImageCacheSize(2), 478 DCHECK_NE(model()->GetResourceWebCoreImageCacheSize(2),
397 l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT)); 479 l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT));
398 DCHECK_NE(model()->GetResourceWebCoreScriptsCacheSize(2), 480 DCHECK_NE(model()->GetResourceWebCoreScriptsCacheSize(2),
399 l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT)); 481 l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT));
400 DCHECK_NE(model()->GetResourceWebCoreCSSCacheSize(2), 482 DCHECK_NE(model()->GetResourceWebCoreCSSCacheSize(2),
401 l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT)); 483 l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT));
402 } 484 }
OLDNEW
« no previous file with comments | « chrome/browser/tab_contents/background_contents.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698