779b42c9bd7d72b5b7af5cec5b52a7e504350c9f
[platform/framework/web/crosswalk.git] / src / chrome / browser / task_manager / task_manager_browsertest.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 "chrome/browser/task_manager/task_manager.h"
6
7 #include "base/files/file_path.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/devtools/devtools_window.h"
12 #include "chrome/browser/extensions/extension_browsertest.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
15 #include "chrome/browser/infobars/infobar.h"
16 #include "chrome/browser/infobars/infobar_service.h"
17 #include "chrome/browser/notifications/desktop_notification_service.h"
18 #include "chrome/browser/notifications/notification.h"
19 #include "chrome/browser/notifications/notification_test_util.h"
20 #include "chrome/browser/notifications/notification_ui_manager.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/task_manager/resource_provider.h"
23 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/browser_dialogs.h"
26 #include "chrome/browser/ui/browser_navigator.h"
27 #include "chrome/browser/ui/browser_window.h"
28 #include "chrome/browser/ui/panels/panel.h"
29 #include "chrome/browser/ui/panels/panel_manager.h"
30 #include "chrome/browser/ui/tabs/tab_strip_model.h"
31 #include "chrome/browser/web_applications/web_app.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/test/base/in_process_browser_test.h"
34 #include "chrome/test/base/ui_test_utils.h"
35 #include "content/public/browser/notification_service.h"
36 #include "content/public/common/content_switches.h"
37 #include "content/public/common/page_transition_types.h"
38 #include "content/public/test/browser_test_utils.h"
39 #include "extensions/browser/extension_system.h"
40 #include "extensions/common/extension.h"
41 #include "grit/generated_resources.h"
42 #include "net/dns/mock_host_resolver.h"
43 #include "net/test/embedded_test_server/embedded_test_server.h"
44 #include "testing/gtest/include/gtest/gtest.h"
45 #include "ui/base/l10n/l10n_util.h"
46
47 using content::WebContents;
48 using task_manager::browsertest_util::MatchAboutBlankTab;
49 using task_manager::browsertest_util::MatchAnyApp;
50 using task_manager::browsertest_util::MatchAnyExtension;
51 using task_manager::browsertest_util::MatchAnyTab;
52 using task_manager::browsertest_util::MatchApp;
53 using task_manager::browsertest_util::MatchExtension;
54 using task_manager::browsertest_util::MatchTab;
55 using task_manager::browsertest_util::WaitForTaskManagerRows;
56
57 namespace {
58
59 const base::FilePath::CharType* kTitle1File = FILE_PATH_LITERAL("title1.html");
60
61 }  // namespace
62
63 class TaskManagerBrowserTest : public ExtensionBrowserTest {
64  public:
65   TaskManagerBrowserTest() {}
66   virtual ~TaskManagerBrowserTest() {}
67
68   TaskManagerModel* model() const {
69     return TaskManager::GetInstance()->model();
70   }
71
72   void ShowTaskManager() {
73     EXPECT_EQ(0, model()->ResourceCount());
74
75     // Show the task manager. This populates the model, and helps with debugging
76     // (you see the task manager).
77     chrome::ShowTaskManager(browser());
78   }
79
80   void Refresh() {
81     model()->Refresh();
82   }
83
84   int GetUpdateTimeMs() {
85     return TaskManagerModel::kUpdateTimeMs;
86   }
87
88   GURL GetTestURL() {
89     return ui_test_utils::GetTestUrl(
90         base::FilePath(base::FilePath::kCurrentDirectory),
91         base::FilePath(kTitle1File));
92   }
93
94   int FindResourceIndex(const base::string16& title) {
95     for (int i = 0; i < model()->ResourceCount(); ++i) {
96       if (title == model()->GetResourceTitle(i))
97         return i;
98     }
99     return -1;
100   }
101
102  protected:
103   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
104     ExtensionBrowserTest::SetUpCommandLine(command_line);
105
106     // Do not launch device discovery process.
107     command_line->AppendSwitch(switches::kDisableDeviceDiscoveryNotifications);
108   }
109
110  private:
111   DISALLOW_COPY_AND_ASSIGN(TaskManagerBrowserTest);
112 };
113
114 #if defined(OS_MACOSX) || defined(OS_LINUX)
115 #define MAYBE_ShutdownWhileOpen DISABLED_ShutdownWhileOpen
116 #else
117 #define MAYBE_ShutdownWhileOpen ShutdownWhileOpen
118 #endif
119
120 // Regression test for http://crbug.com/13361
121 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, MAYBE_ShutdownWhileOpen) {
122   ShowTaskManager();
123 }
124
125 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeTabContentsChanges) {
126   ShowTaskManager();
127   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
128   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
129   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchTab("title1.html")));
130
131   // Open a new tab and make sure the task manager notices it.
132   AddTabAtIndex(0, GetTestURL(), content::PAGE_TRANSITION_TYPED);
133
134   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchTab("title1.html")));
135   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyTab()));
136
137   // Close the tab and verify that we notice.
138   browser()->tab_strip_model()->CloseWebContentsAt(0,
139                                                    TabStripModel::CLOSE_NONE);
140   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchTab("title1.html")));
141   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
142 }
143
144 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticePanel) {
145   ASSERT_TRUE(LoadExtension(
146       test_data_dir_.AppendASCII("good").AppendASCII("Extensions")
147                     .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
148                     .AppendASCII("1.0.0.0")));
149
150   // Open a new panel to an extension url.
151   GURL url(
152     "chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/french_sentence.html");
153   Panel* panel = PanelManager::GetInstance()->CreatePanel(
154       web_app::GenerateApplicationNameFromExtensionId(
155           last_loaded_extension_id()),
156       browser()->profile(),
157       url,
158       gfx::Rect(300, 400),
159       PanelManager::CREATE_AS_DOCKED);
160
161   // Make sure that a task manager model created after the panel shows the
162   // existence of the panel and the extension.
163   ShowTaskManager();
164   ASSERT_NO_FATAL_FAILURE(
165       WaitForTaskManagerRows(1, MatchExtension("My extension 1")));
166   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(
167       1,
168       MatchExtension(
169           "chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/"
170           "french_sentence.html")));
171   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyExtension()));
172   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
173   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
174
175   // Close the panel and verify that we notice.
176   panel->Close();
177   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyExtension()));
178   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(
179       0,
180       MatchExtension(
181           "chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/"
182           "french_sentence.html")));
183   ASSERT_NO_FATAL_FAILURE(
184       WaitForTaskManagerRows(1, MatchExtension("My extension 1")));
185 }
186
187 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticePanelChanges) {
188   ShowTaskManager();
189   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
190   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
191
192   ASSERT_TRUE(LoadExtension(
193       test_data_dir_.AppendASCII("good").AppendASCII("Extensions")
194                     .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
195                     .AppendASCII("1.0.0.0")));
196
197   // Browser, the New Tab Page and Extension background page.
198   ASSERT_NO_FATAL_FAILURE(
199       WaitForTaskManagerRows(1, MatchExtension("My extension 1")));
200   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyExtension()));
201   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
202
203   // Open a new panel to an extension url and make sure we notice that.
204   GURL url(
205     "chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/french_sentence.html");
206   Panel* panel = PanelManager::GetInstance()->CreatePanel(
207       web_app::GenerateApplicationNameFromExtensionId(
208           last_loaded_extension_id()),
209       browser()->profile(),
210       url,
211       gfx::Rect(300, 400),
212       PanelManager::CREATE_AS_DOCKED);
213   ASSERT_NO_FATAL_FAILURE(
214       WaitForTaskManagerRows(1, MatchExtension("My extension 1")));
215   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(
216       1,
217       MatchExtension(
218           "chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/"
219           "french_sentence.html")));
220   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyExtension()));
221   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
222   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
223
224   // Close the panel and verify that we notice.
225   panel->Close();
226   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyExtension()));
227   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(
228       0,
229       MatchExtension(
230           "chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/"
231           "french_sentence.html")));
232   ASSERT_NO_FATAL_FAILURE(
233       WaitForTaskManagerRows(1, MatchExtension("My extension 1")));
234
235   // Unload extension.
236   UnloadExtension(last_loaded_extension_id());
237   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyExtension()));
238   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
239 }
240
241 // Kills a process that has more than one task manager entry.
242 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, KillPanelViaExtensionResource) {
243   ShowTaskManager();
244   ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("good")
245                                 .AppendASCII("Extensions")
246                                 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
247                                 .AppendASCII("1.0.0.0")));
248
249   // Open a new panel to an extension url.
250   GURL url(
251       "chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/"
252       "french_sentence.html");
253   PanelManager::GetInstance()->CreatePanel(
254       web_app::GenerateApplicationNameFromExtensionId(
255           last_loaded_extension_id()),
256       browser()->profile(),
257       url,
258       gfx::Rect(300, 400),
259       PanelManager::CREATE_AS_DOCKED);
260
261   ASSERT_NO_FATAL_FAILURE(
262       WaitForTaskManagerRows(1, MatchExtension("My extension 1")));
263   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(
264       1,
265       MatchExtension(
266           "chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/"
267           "french_sentence.html")));
268   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyExtension()));
269   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
270   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
271
272   // Kill the process via the BACKGROUND PAGE (not the panel). Verify that both
273   // the background page and the panel go away from the task manager.
274   int background_page = FindResourceIndex(MatchExtension("My extension 1"));
275   ASSERT_NE(-1, background_page);
276   ASSERT_TRUE(model()->GetResourceWebContents(background_page) == NULL);
277   ASSERT_FALSE(model()->CanActivate(background_page));
278   TaskManager::GetInstance()->KillProcess(background_page);
279   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyExtension()));
280   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
281 }
282
283 // Kills a process that has more than one task manager entry. This test is the
284 // same as KillPanelViaExtensionResource except it does the kill via the other
285 // entry.
286 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, KillPanelViaPanelResource) {
287   ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("good")
288                                 .AppendASCII("Extensions")
289                                 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
290                                 .AppendASCII("1.0.0.0")));
291
292   // Open a new panel to an extension url.
293   GURL url(
294       "chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/"
295       "french_sentence.html");
296   PanelManager::GetInstance()->CreatePanel(
297       web_app::GenerateApplicationNameFromExtensionId(
298           last_loaded_extension_id()),
299       browser()->profile(),
300       url,
301       gfx::Rect(300, 400),
302       PanelManager::CREATE_AS_DOCKED);
303
304   ShowTaskManager();
305   ASSERT_NO_FATAL_FAILURE(
306       WaitForTaskManagerRows(1, MatchExtension("My extension 1")));
307   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(
308       1,
309       MatchExtension(
310           "chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/"
311           "french_sentence.html")));
312   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyExtension()));
313   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
314   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
315
316   int background_page = FindResourceIndex(MatchExtension("My extension 1"));
317   ASSERT_NE(-1, background_page);
318   ASSERT_TRUE(model()->GetResourceWebContents(background_page) == NULL);
319   ASSERT_FALSE(model()->CanActivate(background_page));
320
321   // Kill the process via the PANEL RESOURCE (not the background page). Verify
322   // that both the background page and the panel go away from the task manager.
323   int panel = FindResourceIndex(MatchExtension(
324       "chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/"
325       "french_sentence.html"));
326   ASSERT_NE(-1, panel);
327   ASSERT_TRUE(model()->GetResourceWebContents(panel) != NULL);
328   ASSERT_TRUE(model()->CanActivate(panel));
329   TaskManager::GetInstance()->KillProcess(panel);
330   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyExtension()));
331   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
332 }
333
334 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeExtensionTabChanges) {
335   ShowTaskManager();
336   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
337   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
338
339   ASSERT_TRUE(LoadExtension(
340       test_data_dir_.AppendASCII("good").AppendASCII("Extensions")
341                     .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
342                     .AppendASCII("1.0.0.0")));
343
344   // Browser, Extension background page, and the New Tab Page.
345   ASSERT_NO_FATAL_FAILURE(
346       WaitForTaskManagerRows(1, MatchExtension("My extension 1")));
347   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyExtension()));
348   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
349
350   // Open a new tab to an extension URL. Afterwards, the third entry (background
351   // page) should be an extension resource whose title starts with "Extension:".
352   // The fourth entry (page.html) is also of type extension and has both a
353   // WebContents and an extension. The title should start with "Extension:".
354   GURL url("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/page.html");
355   AddTabAtIndex(0, url, content::PAGE_TRANSITION_TYPED);
356   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchExtension("Foobar")));
357   ASSERT_NO_FATAL_FAILURE(
358       WaitForTaskManagerRows(1, MatchExtension("My extension 1")));
359   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyExtension()));
360   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
361   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
362
363   int extension_tab = FindResourceIndex(MatchExtension("Foobar"));
364   ASSERT_NE(-1, extension_tab);
365   ASSERT_TRUE(model()->GetResourceWebContents(extension_tab) != NULL);
366   ASSERT_TRUE(model()->CanActivate(extension_tab));
367
368   int background_page = FindResourceIndex(MatchExtension("My extension 1"));
369   ASSERT_NE(-1, background_page);
370   ASSERT_TRUE(model()->GetResourceWebContents(background_page) == NULL);
371   ASSERT_FALSE(model()->CanActivate(background_page));
372 }
373
374 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeExtensionTab) {
375   // With the task manager closed, open a new tab to an extension URL.
376   // Afterwards, when we open the task manager, the third entry (background
377   // page) should be an extension resource whose title starts with "Extension:".
378   // The fourth entry (page.html) is also of type extension and has both a
379   // WebContents and an extension. The title should start with "Extension:".
380   ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("good")
381                                 .AppendASCII("Extensions")
382                                 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
383                                 .AppendASCII("1.0.0.0")));
384   GURL url("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/page.html");
385   AddTabAtIndex(0, url, content::PAGE_TRANSITION_TYPED);
386
387   ShowTaskManager();
388   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchExtension("Foobar")));
389   ASSERT_NO_FATAL_FAILURE(
390       WaitForTaskManagerRows(1, MatchExtension("My extension 1")));
391   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyExtension()));
392   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
393   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
394
395   int extension_tab = FindResourceIndex(MatchExtension("Foobar"));
396   ASSERT_NE(-1, extension_tab);
397   ASSERT_TRUE(model()->GetResourceWebContents(extension_tab) != NULL);
398   ASSERT_TRUE(model()->CanActivate(extension_tab));
399
400   int background_page = FindResourceIndex(MatchExtension("My extension 1"));
401   ASSERT_NE(-1, background_page);
402   ASSERT_TRUE(model()->GetResourceWebContents(background_page) == NULL);
403   ASSERT_FALSE(model()->CanActivate(background_page));
404 }
405
406 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeAppTabChanges) {
407   ShowTaskManager();
408
409   ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("packaged_app")));
410   ExtensionService* service = extensions::ExtensionSystem::Get(
411                                   browser()->profile())->extension_service();
412   const extensions::Extension* extension =
413       service->GetExtensionById(last_loaded_extension_id(), false);
414
415   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
416   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
417   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyExtension()));
418   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyApp()));
419
420   // Open a new tab to the app's launch URL and make sure we notice that.
421   GURL url(extension->GetResourceURL("main.html"));
422   AddTabAtIndex(0, url, content::PAGE_TRANSITION_TYPED);
423
424   // There should be 1 "App: " tab and the original new tab page.
425   ASSERT_NO_FATAL_FAILURE(
426       WaitForTaskManagerRows(1, MatchApp("Packaged App Test")));
427   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyApp()));
428   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
429   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
430   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyExtension()));
431
432   // Check that the third entry (main.html) is of type extension and has both
433   // a tab contents and an extension.
434   int app_tab = FindResourceIndex(MatchApp("Packaged App Test"));
435   ASSERT_NE(-1, app_tab);
436   ASSERT_TRUE(model()->GetResourceWebContents(app_tab) != NULL);
437   ASSERT_TRUE(model()->CanActivate(app_tab));
438   ASSERT_EQ(task_manager::Resource::EXTENSION,
439             model()->GetResourceType(app_tab));
440   ASSERT_EQ(2, browser()->tab_strip_model()->count());
441
442   // Unload extension to make sure the tab goes away.
443   UnloadExtension(last_loaded_extension_id());
444
445   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
446   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyApp()));
447   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyExtension()));
448   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
449   ASSERT_EQ(1, browser()->tab_strip_model()->count());
450 }
451
452 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeAppTab) {
453   ASSERT_TRUE(LoadExtension(
454       test_data_dir_.AppendASCII("packaged_app")));
455   ExtensionService* service = extensions::ExtensionSystem::Get(
456       browser()->profile())->extension_service();
457   const extensions::Extension* extension =
458       service->GetExtensionById(last_loaded_extension_id(), false);
459
460   // Open a new tab to the app's launch URL and make sure we notice that.
461   GURL url(extension->GetResourceURL("main.html"));
462   AddTabAtIndex(0, url, content::PAGE_TRANSITION_TYPED);
463
464   ShowTaskManager();
465
466   ASSERT_NO_FATAL_FAILURE(
467       WaitForTaskManagerRows(1, MatchApp("Packaged App Test")));
468   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
469   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyExtension()));
470   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyApp()));
471
472   // Check that the third entry (main.html) is of type extension and has both
473   // a tab contents and an extension.
474   int app_tab = FindResourceIndex(MatchApp("Packaged App Test"));
475   ASSERT_NE(-1, app_tab);
476   ASSERT_TRUE(model()->GetResourceWebContents(app_tab) != NULL);
477   ASSERT_TRUE(model()->CanActivate(app_tab));
478   ASSERT_EQ(task_manager::Resource::EXTENSION,
479             model()->GetResourceType(app_tab));
480 }
481
482 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeHostedAppTabChanges) {
483   ShowTaskManager();
484
485   // The app under test acts on URLs whose host is "localhost",
486   // so the URLs we navigate to must have host "localhost".
487   host_resolver()->AddRule("*", "127.0.0.1");
488   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
489   GURL::Replacements replace_host;
490   std::string host_str("localhost");  // must stay in scope with replace_host
491   replace_host.SetHostStr(host_str);
492   GURL base_url = embedded_test_server()->GetURL(
493       "/extensions/api_test/app_process/");
494   base_url = base_url.ReplaceComponents(replace_host);
495
496   // Open a new tab to an app URL before the app is loaded.
497   GURL url(base_url.Resolve("path1/empty.html"));
498   content::WindowedNotificationObserver observer(
499       content::NOTIFICATION_NAV_ENTRY_COMMITTED,
500       content::NotificationService::AllSources());
501   AddTabAtIndex(0, url, content::PAGE_TRANSITION_TYPED);
502   observer.Wait();
503
504   // Check that the new entry's title starts with "Tab:".
505   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyTab()));
506
507   // Load the hosted app and make sure it still starts with "Tab:",
508   // since it hasn't changed to an app process yet.
509   ASSERT_TRUE(LoadExtension(
510       test_data_dir_.AppendASCII("api_test").AppendASCII("app_process")));
511   // Force the TaskManager to query the title.
512   Refresh();
513   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyTab()));
514   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
515   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchTab("Unmodified")));
516   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyExtension()));
517
518   // Now reload and check that the last entry's title now starts with "App:".
519   ui_test_utils::NavigateToURL(browser(), url);
520
521   // Force the TaskManager to query the title.
522   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
523   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
524   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyApp()));
525   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchApp("Unmodified")));
526   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyExtension()));
527
528   // Disable extension.
529   DisableExtension(last_loaded_extension_id());
530
531   // The hosted app should now show up as a normal "Tab: ".
532   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyTab()));
533   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
534   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchTab("Unmodified")));
535   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyExtension()));
536   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyApp()));
537
538   // Reload the page.
539   ui_test_utils::NavigateToURL(browser(), url);
540
541   // No change expected.
542   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyTab()));
543   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
544   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchTab("Unmodified")));
545   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyExtension()));
546   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyApp()));
547 }
548
549 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeHostedAppTabAfterReload) {
550   // The app under test acts on URLs whose host is "localhost",
551   // so the URLs we navigate to must have host "localhost".
552   host_resolver()->AddRule("*", "127.0.0.1");
553   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
554   GURL::Replacements replace_host;
555   std::string host_str("localhost");  // must stay in scope with replace_host
556   replace_host.SetHostStr(host_str);
557   GURL base_url =
558       embedded_test_server()->GetURL("/extensions/api_test/app_process/");
559   base_url = base_url.ReplaceComponents(replace_host);
560
561   // Open a new tab to an app URL before the app is loaded.
562   GURL url(base_url.Resolve("path1/empty.html"));
563   content::WindowedNotificationObserver observer(
564       content::NOTIFICATION_NAV_ENTRY_COMMITTED,
565       content::NotificationService::AllSources());
566   AddTabAtIndex(0, url, content::PAGE_TRANSITION_TYPED);
567   observer.Wait();
568
569   // Load the hosted app and make sure it still starts with "Tab:",
570   // since it hasn't changed to an app process yet.
571   ASSERT_TRUE(LoadExtension(
572       test_data_dir_.AppendASCII("api_test").AppendASCII("app_process")));
573
574   // Now reload, which should transition this tab to being an App.
575   ui_test_utils::NavigateToURL(browser(), url);
576
577   ShowTaskManager();
578
579   // The TaskManager should show this as an "App: "
580   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
581   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyApp()));
582   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyExtension()));
583 }
584
585 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeHostedAppTabBeforeReload) {
586   // The app under test acts on URLs whose host is "localhost",
587   // so the URLs we navigate to must have host "localhost".
588   host_resolver()->AddRule("*", "127.0.0.1");
589   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
590   GURL::Replacements replace_host;
591   std::string host_str("localhost");  // must stay in scope with replace_host
592   replace_host.SetHostStr(host_str);
593   GURL base_url =
594       embedded_test_server()->GetURL("/extensions/api_test/app_process/");
595   base_url = base_url.ReplaceComponents(replace_host);
596
597   // Open a new tab to an app URL before the app is loaded.
598   GURL url(base_url.Resolve("path1/empty.html"));
599   content::WindowedNotificationObserver observer(
600       content::NOTIFICATION_NAV_ENTRY_COMMITTED,
601       content::NotificationService::AllSources());
602   AddTabAtIndex(0, url, content::PAGE_TRANSITION_TYPED);
603   observer.Wait();
604
605   // Load the hosted app and make sure it still starts with "Tab:",
606   // since it hasn't changed to an app process yet.
607   ASSERT_TRUE(LoadExtension(
608       test_data_dir_.AppendASCII("api_test").AppendASCII("app_process")));
609
610   ShowTaskManager();
611
612   // The TaskManager should show this as a "Tab: " because the page hasn't been
613   // reloaded since the hosted app was installed.
614   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyTab()));
615   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyApp()));
616   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyExtension()));
617 }
618
619 // Regression test for http://crbug.com/18693.
620 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, ReloadExtension) {
621   ShowTaskManager();
622   ASSERT_TRUE(LoadExtension(
623       test_data_dir_.AppendASCII("common").AppendASCII("background_page")));
624
625   // Wait until we see the loaded extension in the task manager (the three
626   // resources are: the browser process, New Tab Page, and the extension).
627   ASSERT_NO_FATAL_FAILURE(
628       WaitForTaskManagerRows(1, MatchExtension("background_page")));
629
630   // Reload the extension a few times and make sure our resource count doesn't
631   // increase.
632   std::string extension_id = last_loaded_extension_id();
633   for (int i = 1; i <= 5; i++) {
634     SCOPED_TRACE(testing::Message() << "Reloading extension for the " << i
635                                     << "th time.");
636     ReloadExtension(extension_id);
637     ASSERT_NO_FATAL_FAILURE(
638         WaitForTaskManagerRows(1, MatchExtension("background_page")));
639   }
640 }
641
642 // Crashy, http://crbug.com/42301.
643 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest,
644                        DISABLED_PopulateWebCacheFields) {
645   ShowTaskManager();
646   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
647
648   int resource_count = TaskManager::GetInstance()->model()->ResourceCount();
649
650   // Open a new tab and make sure we notice that.
651   AddTabAtIndex(0, GetTestURL(), content::PAGE_TRANSITION_TYPED);
652   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyTab()));
653
654   // Check that we get some value for the cache columns.
655   DCHECK_NE(model()->GetResourceWebCoreImageCacheSize(resource_count),
656             l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT));
657   DCHECK_NE(model()->GetResourceWebCoreScriptsCacheSize(resource_count),
658             l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT));
659   DCHECK_NE(model()->GetResourceWebCoreCSSCacheSize(resource_count),
660             l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT));
661 }
662
663 // Checks that task manager counts a worker thread JS heap size.
664 // http://crbug.com/241066
665 // Flaky, http://crbug.com/259368
666 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, DISABLED_WebWorkerJSHeapMemory) {
667   ui_test_utils::NavigateToURL(browser(), GetTestURL());
668   const int extra_timeout_ms = 500;
669   size_t minimal_heap_size = 2 * 1024 * 1024 * sizeof(void*);
670   std::string test_js = base::StringPrintf(
671       "var blob = new Blob([\n"
672       "    'mem = new Array(%lu);',\n"
673       "    'for (var i = 0; i < mem.length; i += 16) mem[i] = i;',\n"
674       "    'postMessage();']);\n"
675       "blobURL = window.URL.createObjectURL(blob);\n"
676       "worker = new Worker(blobURL);\n"
677       "// Give the task manager few seconds to poll for JS heap sizes.\n"
678       "worker.onmessage = setTimeout.bind(\n"
679       "    this,\n"
680       "    function () { window.domAutomationController.send(true); },\n"
681       "    %d);\n"
682       "worker.postMessage();\n",
683       static_cast<unsigned long>(minimal_heap_size),
684       GetUpdateTimeMs() + extra_timeout_ms);
685   bool ok;
686   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
687       browser()->tab_strip_model()->GetActiveWebContents(), test_js, &ok));
688   ASSERT_TRUE(ok);
689
690   int resource_index = TaskManager::GetInstance()->model()->ResourceCount() - 1;
691   size_t result;
692
693   ASSERT_TRUE(model()->GetV8Memory(resource_index, &result));
694   LOG(INFO) << "Got V8 Heap Size " << result << " bytes";
695   EXPECT_GE(result, minimal_heap_size);
696
697   ASSERT_TRUE(model()->GetV8MemoryUsed(resource_index, &result));
698   LOG(INFO) << "Got V8 Used Heap Size " << result << " bytes";
699   EXPECT_GE(result, minimal_heap_size);
700 }
701
702 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, DevToolsNewDockedWindow) {
703   ShowTaskManager();  // Task manager shown BEFORE dev tools window.
704
705   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
706   DevToolsWindow::OpenDevToolsWindowForTest(
707       browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost(),
708       true);
709   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyTab()));
710   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyTab()));
711 }
712
713 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, DevToolsNewUndockedWindow) {
714   ShowTaskManager();  // Task manager shown BEFORE dev tools window.
715   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
716   DevToolsWindow::OpenDevToolsWindowForTest(
717       browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost(),
718       false);
719   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyTab()));
720   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyTab()));
721 }
722
723 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, DevToolsOldDockedWindow) {
724   DevToolsWindow::OpenDevToolsWindowForTest(
725       browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost(),
726       true);
727
728   ShowTaskManager();  // Task manager shown AFTER dev tools window.
729   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyTab()));
730   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyTab()));
731 }
732
733 IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, DevToolsOldUnockedWindow) {
734   DevToolsWindow::OpenDevToolsWindowForTest(
735       browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost(),
736       false);
737   ShowTaskManager();  // Task manager shown AFTER dev tools window.
738   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyTab()));
739   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(2, MatchAnyTab()));
740 }