blob: 7e52926901921a5670ae3ad7a3d37b9239dbeacd [file] [log] [blame]
[email protected]9b0a2632012-01-28 19:01:511// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]381162b2010-01-28 17:29:352// 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/command_line.h"
Owen Min99f20ff2019-11-26 23:45:566#include "build/build_config.h"
afakhry98241832016-03-11 19:27:477#include "chrome/browser/extensions/api/processes/processes_api.h"
[email protected]381162b2010-01-28 17:29:358#include "chrome/browser/extensions/extension_apitest.h"
avi24d693f2016-08-06 18:03:529#include "chrome/browser/task_manager/task_manager_interface.h"
[email protected]7b5dc002010-11-16 23:08:1010#include "chrome/browser/ui/browser.h"
[email protected]060dc0f82013-03-21 12:24:3311#include "chrome/browser/ui/browser_dialogs.h"
[email protected]00070c732011-04-09 15:31:3312#include "chrome/browser/ui/browser_window.h"
Ahmed Fakhry917a4132018-01-19 21:00:1413#include "chrome/common/extensions/api/processes.h"
Peter Kasting919ce652020-05-07 10:22:3614#include "content/public/test/browser_test.h"
[email protected]c8d02992013-07-31 22:16:5115#include "extensions/common/switches.h"
lfg910f2f92014-09-19 05:31:0916#include "extensions/test/extension_test_message_listener.h"
[email protected]381162b2010-01-28 17:29:3517
Devlin Croninef3e37e2018-05-14 23:47:2418class ProcessesApiTest : public extensions::ExtensionApiTest {
afakhry98241832016-03-11 19:27:4719 public:
20 ProcessesApiTest() {}
Peter Boström53c6c5952021-09-17 09:41:2621
22 ProcessesApiTest(const ProcessesApiTest&) = delete;
23 ProcessesApiTest& operator=(const ProcessesApiTest&) = delete;
24
afakhry98241832016-03-11 19:27:4725 ~ProcessesApiTest() override {}
afakhry240b05172016-03-02 22:08:3026
afakhry98241832016-03-11 19:27:4727 int GetListenersCount() {
28 return extensions::ProcessesAPI::Get(profile())->
29 processes_event_router()->listeners_;
30 }
afakhry98241832016-03-11 19:27:4731};
32
sorin91236992016-03-28 23:42:1033
dbeam48a315b2016-08-05 00:24:0034// This test is flaky. https://crbug.com/598445
35IN_PROC_BROWSER_TEST_F(ProcessesApiTest, DISABLED_Processes) {
[email protected]8a661f82010-10-19 21:47:1136 ASSERT_TRUE(RunExtensionTest("processes/api")) << message_;
[email protected]381162b2010-01-28 17:29:3537}
[email protected]8a661f82010-10-19 21:47:1138
Andy Paicu672e4bd2019-03-14 14:06:4139IN_PROC_BROWSER_TEST_F(ProcessesApiTest, DISABLED_ProcessesApiListeners) {
afakhry98241832016-03-11 19:27:4740 EXPECT_EQ(0, GetListenersCount());
afakhry35c987ef2015-08-15 03:01:0141
afakhry98241832016-03-11 19:27:4742 // Load extension that adds a listener in background page
43 ExtensionTestMessageListener listener1("ready", false /* will_reply */);
44 const extensions::Extension* extension1 = LoadExtension(
45 test_data_dir_.AppendASCII("processes").AppendASCII("onupdated"));
46 ASSERT_TRUE(extension1);
47 ASSERT_TRUE(listener1.WaitUntilSatisfied());
[email protected]8a661f82010-10-19 21:47:1148
afakhry98241832016-03-11 19:27:4749 // The memory refresh type of the task manager may or may not be enabled by
50 // now depending on the presence of other task manager observers.
51 // Ensure the listeners count has changed.
52 EXPECT_EQ(1, GetListenersCount());
[email protected]8a661f82010-10-19 21:47:1153
afakhry98241832016-03-11 19:27:4754 // Load another extension that listen to the onUpdatedWithMemory.
55 ExtensionTestMessageListener listener2("ready", false /* will_reply */);
56 const extensions::Extension* extension2 = LoadExtension(
57 test_data_dir_.AppendASCII("processes").AppendASCII(
58 "onupdated_with_memory"));
59 ASSERT_TRUE(extension2);
60 ASSERT_TRUE(listener2.WaitUntilSatisfied());
[email protected]8a661f82010-10-19 21:47:1161
afakhry98241832016-03-11 19:27:4762 // The memory refresh type must be enabled now.
avi24d693f2016-08-06 18:03:5263 const task_manager::TaskManagerInterface* task_manager =
64 task_manager::TaskManagerInterface::GetTaskManager();
afakhry98241832016-03-11 19:27:4765 EXPECT_EQ(2, GetListenersCount());
66 EXPECT_TRUE(task_manager->IsResourceRefreshEnabled(
erikchene7bba452018-03-16 16:50:1867 task_manager::REFRESH_TYPE_MEMORY_FOOTPRINT));
[email protected]8b2e9f392011-08-05 04:00:4768
afakhry98241832016-03-11 19:27:4769 // Unload the extensions and make sure the listeners count is updated.
70 UnloadExtension(extension2->id());
71 EXPECT_EQ(1, GetListenersCount());
72 UnloadExtension(extension1->id());
73 EXPECT_EQ(0, GetListenersCount());
[email protected]8a661f82010-10-19 21:47:1174}
kalmanc2293da2015-09-22 21:56:3175
Ahmed Fakhry917a4132018-01-19 21:00:1476IN_PROC_BROWSER_TEST_F(ProcessesApiTest, OnUpdatedWithMemoryRefreshTypes) {
77 EXPECT_EQ(0, GetListenersCount());
78
79 // Load an extension that listen to the onUpdatedWithMemory.
80 ExtensionTestMessageListener listener("ready", false /* will_reply */);
81 const extensions::Extension* extension =
82 LoadExtension(test_data_dir_.AppendASCII("processes")
83 .AppendASCII("onupdated_with_memory"));
84 ASSERT_TRUE(extension);
85 ASSERT_TRUE(listener.WaitUntilSatisfied());
86
87 // The memory refresh type must be enabled now.
88 const task_manager::TaskManagerInterface* task_manager =
89 task_manager::TaskManagerInterface::GetTaskManager();
90 EXPECT_EQ(1, GetListenersCount());
91 extensions::EventRouter* event_router =
92 extensions::EventRouter::Get(profile());
93 EXPECT_TRUE(event_router->HasEventListener(
94 extensions::api::processes::OnUpdatedWithMemory::kEventName));
95 EXPECT_FALSE(event_router->HasEventListener(
96 extensions::api::processes::OnUpdated::kEventName));
97 EXPECT_TRUE(task_manager->IsResourceRefreshEnabled(
erikchene7bba452018-03-16 16:50:1898 task_manager::REFRESH_TYPE_MEMORY_FOOTPRINT));
Ahmed Fakhry917a4132018-01-19 21:00:1499
100 // Despite the fact that there are no onUpdated listeners, refresh types for
101 // CPU, Network, SQLite, V8 memory, and webcache stats should be enabled.
102 constexpr task_manager::RefreshType kOnUpdatedRefreshTypes[] = {
103 task_manager::REFRESH_TYPE_CPU,
104 task_manager::REFRESH_TYPE_NETWORK_USAGE,
105 task_manager::REFRESH_TYPE_SQLITE_MEMORY,
106 task_manager::REFRESH_TYPE_V8_MEMORY,
107 task_manager::REFRESH_TYPE_WEBCACHE_STATS,
108 };
109
110 for (const auto& type : kOnUpdatedRefreshTypes)
111 EXPECT_TRUE(task_manager->IsResourceRefreshEnabled(type));
112
113 // Unload the extensions and make sure the listeners count is updated.
114 UnloadExtension(extension->id());
115 EXPECT_EQ(0, GetListenersCount());
116}
117
Sean McAllister4a991b972020-08-19 17:38:51118// This test is flaky on Linux and ChromeOS ASan LSan Tests bot. https://crbug.com/1028778
119#if (defined(OS_LINUX) || defined(OS_CHROMEOS)) && defined(ADDRESS_SANITIZER)
Owen Min99f20ff2019-11-26 23:45:56120#define MAYBE_CannotTerminateBrowserProcess \
121 DISABLED_CannotTerminateBrowserProcess
122#else
123#define MAYBE_CannotTerminateBrowserProcess CannotTerminateBrowserProcess
124#endif
125IN_PROC_BROWSER_TEST_F(ProcessesApiTest, MAYBE_CannotTerminateBrowserProcess) {
kalmanc2293da2015-09-22 21:56:31126 ASSERT_TRUE(RunExtensionTest("processes/terminate-browser-process"))
127 << message_;
128}