[email protected] | 9b0a263 | 2012-01-28 19:01:51 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 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/command_line.h" |
Owen Min | 99f20ff | 2019-11-26 23:45:56 | [diff] [blame] | 6 | #include "build/build_config.h" |
afakhry | 9824183 | 2016-03-11 19:27:47 | [diff] [blame] | 7 | #include "chrome/browser/extensions/api/processes/processes_api.h" |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 8 | #include "chrome/browser/extensions/extension_apitest.h" |
avi | 24d693f | 2016-08-06 18:03:52 | [diff] [blame] | 9 | #include "chrome/browser/task_manager/task_manager_interface.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 10 | #include "chrome/browser/ui/browser.h" |
[email protected] | 060dc0f8 | 2013-03-21 12:24:33 | [diff] [blame] | 11 | #include "chrome/browser/ui/browser_dialogs.h" |
[email protected] | 00070c73 | 2011-04-09 15:31:33 | [diff] [blame] | 12 | #include "chrome/browser/ui/browser_window.h" |
Ahmed Fakhry | 917a413 | 2018-01-19 21:00:14 | [diff] [blame] | 13 | #include "chrome/common/extensions/api/processes.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 14 | #include "content/public/test/browser_test.h" |
[email protected] | c8d0299 | 2013-07-31 22:16:51 | [diff] [blame] | 15 | #include "extensions/common/switches.h" |
lfg | 910f2f9 | 2014-09-19 05:31:09 | [diff] [blame] | 16 | #include "extensions/test/extension_test_message_listener.h" |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 17 | |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 18 | class ProcessesApiTest : public extensions::ExtensionApiTest { |
afakhry | 9824183 | 2016-03-11 19:27:47 | [diff] [blame] | 19 | public: |
| 20 | ProcessesApiTest() {} |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame^] | 21 | |
| 22 | ProcessesApiTest(const ProcessesApiTest&) = delete; |
| 23 | ProcessesApiTest& operator=(const ProcessesApiTest&) = delete; |
| 24 | |
afakhry | 9824183 | 2016-03-11 19:27:47 | [diff] [blame] | 25 | ~ProcessesApiTest() override {} |
afakhry | 240b0517 | 2016-03-02 22:08:30 | [diff] [blame] | 26 | |
afakhry | 9824183 | 2016-03-11 19:27:47 | [diff] [blame] | 27 | int GetListenersCount() { |
| 28 | return extensions::ProcessesAPI::Get(profile())-> |
| 29 | processes_event_router()->listeners_; |
| 30 | } |
afakhry | 9824183 | 2016-03-11 19:27:47 | [diff] [blame] | 31 | }; |
| 32 | |
sorin | 9123699 | 2016-03-28 23:42:10 | [diff] [blame] | 33 | |
dbeam | 48a315b | 2016-08-05 00:24:00 | [diff] [blame] | 34 | // This test is flaky. https://crbug.com/598445 |
| 35 | IN_PROC_BROWSER_TEST_F(ProcessesApiTest, DISABLED_Processes) { |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 36 | ASSERT_TRUE(RunExtensionTest("processes/api")) << message_; |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 37 | } |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 38 | |
Andy Paicu | 672e4bd | 2019-03-14 14:06:41 | [diff] [blame] | 39 | IN_PROC_BROWSER_TEST_F(ProcessesApiTest, DISABLED_ProcessesApiListeners) { |
afakhry | 9824183 | 2016-03-11 19:27:47 | [diff] [blame] | 40 | EXPECT_EQ(0, GetListenersCount()); |
afakhry | 35c987ef | 2015-08-15 03:01:01 | [diff] [blame] | 41 | |
afakhry | 9824183 | 2016-03-11 19:27:47 | [diff] [blame] | 42 | // 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] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 48 | |
afakhry | 9824183 | 2016-03-11 19:27:47 | [diff] [blame] | 49 | // 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] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 53 | |
afakhry | 9824183 | 2016-03-11 19:27:47 | [diff] [blame] | 54 | // 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] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 61 | |
afakhry | 9824183 | 2016-03-11 19:27:47 | [diff] [blame] | 62 | // The memory refresh type must be enabled now. |
avi | 24d693f | 2016-08-06 18:03:52 | [diff] [blame] | 63 | const task_manager::TaskManagerInterface* task_manager = |
| 64 | task_manager::TaskManagerInterface::GetTaskManager(); |
afakhry | 9824183 | 2016-03-11 19:27:47 | [diff] [blame] | 65 | EXPECT_EQ(2, GetListenersCount()); |
| 66 | EXPECT_TRUE(task_manager->IsResourceRefreshEnabled( |
erikchen | e7bba45 | 2018-03-16 16:50:18 | [diff] [blame] | 67 | task_manager::REFRESH_TYPE_MEMORY_FOOTPRINT)); |
[email protected] | 8b2e9f39 | 2011-08-05 04:00:47 | [diff] [blame] | 68 | |
afakhry | 9824183 | 2016-03-11 19:27:47 | [diff] [blame] | 69 | // 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] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 74 | } |
kalman | c2293da | 2015-09-22 21:56:31 | [diff] [blame] | 75 | |
Ahmed Fakhry | 917a413 | 2018-01-19 21:00:14 | [diff] [blame] | 76 | IN_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( |
erikchen | e7bba45 | 2018-03-16 16:50:18 | [diff] [blame] | 98 | task_manager::REFRESH_TYPE_MEMORY_FOOTPRINT)); |
Ahmed Fakhry | 917a413 | 2018-01-19 21:00:14 | [diff] [blame] | 99 | |
| 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 McAllister | 4a991b97 | 2020-08-19 17:38:51 | [diff] [blame] | 118 | // 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 Min | 99f20ff | 2019-11-26 23:45:56 | [diff] [blame] | 120 | #define MAYBE_CannotTerminateBrowserProcess \ |
| 121 | DISABLED_CannotTerminateBrowserProcess |
| 122 | #else |
| 123 | #define MAYBE_CannotTerminateBrowserProcess CannotTerminateBrowserProcess |
| 124 | #endif |
| 125 | IN_PROC_BROWSER_TEST_F(ProcessesApiTest, MAYBE_CannotTerminateBrowserProcess) { |
kalman | c2293da | 2015-09-22 21:56:31 | [diff] [blame] | 126 | ASSERT_TRUE(RunExtensionTest("processes/terminate-browser-process")) |
| 127 | << message_; |
| 128 | } |