|
4 | 4 |
|
5 | 5 | #include "base/base_switches.h"
|
6 | 6 | #include "base/command_line.h"
|
| 7 | +#include "base/files/file_path.h" |
7 | 8 | #include "base/macros.h"
|
| 9 | +#include "base/path_service.h" |
8 | 10 | #include "base/run_loop.h"
|
| 11 | +#include "base/strings/string_piece.h" |
| 12 | +#include "base/strings/stringprintf.h" |
9 | 13 | #include "build/build_config.h"
|
| 14 | +#include "build/buildflag.h" |
| 15 | +#include "chrome/browser/profiles/profile.h" |
10 | 16 | #include "chrome/browser/ui/browser.h"
|
11 | 17 | #include "chrome/browser/ui/tabs/tab_strip_model.h"
|
| 18 | +#include "chrome/common/chrome_paths.h" |
12 | 19 | #include "chrome/test/base/in_process_browser_test.h"
|
| 20 | +#include "chrome/test/base/ui_test_utils.h" |
13 | 21 | #include "content/public/browser/web_contents.h"
|
14 | 22 | #include "content/public/browser/web_contents_observer.h"
|
| 23 | +#include "content/public/test/browser_test_utils.h" |
15 | 24 | #include "content/public/test/test_utils.h"
|
| 25 | +#include "net/dns/mock_host_resolver.h" |
16 | 26 | #include "testing/gtest/include/gtest/gtest.h"
|
17 | 27 |
|
| 28 | +#if BUILDFLAG(ENABLE_EXTENSIONS) |
| 29 | +#include "chrome/browser/extensions/unpacked_installer.h" |
| 30 | +#include "extensions/browser/extension_registry.h" |
| 31 | +#include "extensions/browser/extension_system.h" |
| 32 | +#include "extensions/browser/test_extension_registry_observer.h" |
| 33 | +#include "extensions/common/extension.h" |
| 34 | +#endif |
| 35 | + |
18 | 36 | namespace {
|
19 | 37 |
|
20 | 38 | class RunLoopUntilLoadedAndPainted : public content::WebContentsObserver {
|
@@ -63,11 +81,25 @@ class NoBestEffortTasksTest : public InProcessBrowserTest {
|
63 | 81 | private:
|
64 | 82 | void SetUpCommandLine(base::CommandLine* command_line) override {
|
65 | 83 | command_line->AppendSwitch(switches::kDisableBackgroundTasks);
|
| 84 | + InProcessBrowserTest::SetUpCommandLine(command_line); |
| 85 | + } |
| 86 | + |
| 87 | + void SetUpOnMainThread() override { |
| 88 | + // Redirect all DNS requests back to localhost (to the embedded test |
| 89 | + // server). |
| 90 | + host_resolver()->AddRule("*", "127.0.0.1"); |
| 91 | + InProcessBrowserTest::SetUpOnMainThread(); |
66 | 92 | }
|
67 | 93 |
|
68 | 94 | DISALLOW_COPY_AND_ASSIGN(NoBestEffortTasksTest);
|
69 | 95 | };
|
70 | 96 |
|
| 97 | +#if BUILDFLAG(ENABLE_EXTENSIONS) |
| 98 | +constexpr base::StringPiece kExtensionId = "ddchlicdkolnonkihahngkmmmjnjlkkf"; |
| 99 | +constexpr base::TimeDelta kSendMessageRetryPeriod = |
| 100 | + base::TimeDelta::FromMilliseconds(250); |
| 101 | +#endif |
| 102 | + |
71 | 103 | } // namespace
|
72 | 104 |
|
73 | 105 | // Verify that it is possible to load and paint the initial about:blank page
|
@@ -106,3 +138,74 @@ IN_PROC_BROWSER_TEST_F(NoBestEffortTasksTest, LoadAndPaintFromNetwork) {
|
106 | 138 | RunLoopUntilLoadedAndPainted run_until_loaded_and_painted(web_contents);
|
107 | 139 | run_until_loaded_and_painted.Run();
|
108 | 140 | }
|
| 141 | + |
| 142 | +// Verify that an extension can be loaded and perform basic messaging without |
| 143 | +// running BEST_EFFORT tasks. Regression test for http://crbug.com/177163#c112. |
| 144 | +#if BUILDFLAG(ENABLE_EXTENSIONS) |
| 145 | +IN_PROC_BROWSER_TEST_F(NoBestEffortTasksTest, LoadExtensionAndSendMessages) { |
| 146 | + ASSERT_TRUE(embedded_test_server()->Start()); |
| 147 | + |
| 148 | + // Load the extension, waiting until the ExtensionRegistry reports that its |
| 149 | + // renderer has been started. |
| 150 | + base::FilePath extension_dir; |
| 151 | + const bool have_test_data_dir = |
| 152 | + base::PathService::Get(chrome::DIR_TEST_DATA, &extension_dir); |
| 153 | + ASSERT_TRUE(have_test_data_dir); |
| 154 | + extension_dir = extension_dir.AppendASCII("extensions") |
| 155 | + .AppendASCII("no_best_effort_tasks_test_extension"); |
| 156 | + extensions::UnpackedInstaller::Create( |
| 157 | + extensions::ExtensionSystem::Get(browser()->profile()) |
| 158 | + ->extension_service()) |
| 159 | + ->Load(extension_dir); |
| 160 | + auto* const extension = |
| 161 | + extensions::TestExtensionRegistryObserver( |
| 162 | + extensions::ExtensionRegistry::Get(browser()->profile())) |
| 163 | + .WaitForExtensionReady(); |
| 164 | + ASSERT_TRUE(extension); |
| 165 | + ASSERT_EQ(kExtensionId, extension->id()); |
| 166 | + |
| 167 | + // Navigate to a test page, waiting until complete. Note that the hostname |
| 168 | + // here must match the pattern found in the extension's manifest file, or it |
| 169 | + // will not be able to send/receive messaging from the test web page (due to |
| 170 | + // extension permissions). |
| 171 | + ui_test_utils::NavigateToURL( |
| 172 | + browser(), |
| 173 | + embedded_test_server()->GetURL("fake.chromium.org", "/empty.html")); |
| 174 | + |
| 175 | + // Execute JavaScript in the test page, to send a ping message to the |
| 176 | + // extension and await the reply. The chrome.runtime.sendMessage() operation |
| 177 | + // can fail if the extension's background page hasn't finished running yet |
| 178 | + // (i.e., there is no message listener yet). Thus, use a retry loop. |
| 179 | + const std::string request_reply_javascript = base::StringPrintf( |
| 180 | + "new Promise((resolve, reject) => {\n" |
| 181 | + " chrome.runtime.sendMessage(\n" |
| 182 | + " '%s',\n" |
| 183 | + " {ping: true},\n" |
| 184 | + " response => {\n" |
| 185 | + " if (response) {\n" |
| 186 | + " resolve(response);\n" |
| 187 | + " } else {\n" |
| 188 | + " reject(chrome.runtime.lastError.message);\n" |
| 189 | + " }\n" |
| 190 | + " });\n" |
| 191 | + "})", |
| 192 | + extension->id().c_str()); |
| 193 | + for (;;) { |
| 194 | + const auto result = |
| 195 | + content::EvalJs(browser()->tab_strip_model()->GetActiveWebContents(), |
| 196 | + request_reply_javascript); |
| 197 | + if (result.error.empty()) { |
| 198 | + LOG(INFO) << "Got a response from the extension."; |
| 199 | + EXPECT_TRUE(result.value.FindBoolKey("pong").value_or(false)); |
| 200 | + break; |
| 201 | + } |
| 202 | + // An error indicates the extension's message listener isn't up yet. Wait a |
| 203 | + // little before trying again. |
| 204 | + LOG(INFO) << "Waiting for the extension's message listener..."; |
| 205 | + base::RunLoop run_loop; |
| 206 | + base::SequencedTaskRunnerHandle::Get()->PostDelayedTask( |
| 207 | + FROM_HERE, run_loop.QuitClosure(), kSendMessageRetryPeriod); |
| 208 | + run_loop.Run(); |
| 209 | + } |
| 210 | +} |
| 211 | +#endif // BUILDFLAG(ENABLE_EXTENSIONS) |
0 commit comments