File: desktop_android_extensions_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (478 lines) | stat: -rw-r--r-- 18,524 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/strings/stringprintf.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/extensions/chrome_test_extension_loader.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#include "chrome/test/base/android/android_browser_test.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/api/declarative_net_request/rules_monitor_service.h"
#include "extensions/browser/api/declarative_net_request/test_utils.h"
#include "extensions/browser/embedder_user_script_loader.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/user_script_manager.h"
#include "extensions/common/extension.h"
#include "extensions/common/file_util.h"
#include "extensions/common/manifest_handlers/content_scripts_handler.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "extensions/test/test_content_script_load_waiter.h"
#include "extensions/test/test_extension_dir.h"
#include "net/dns/mock_host_resolver.h"

namespace extensions {

class DesktopAndroidExtensionsBrowserTest : public AndroidBrowserTest {
 public:
  DesktopAndroidExtensionsBrowserTest() = default;
  ~DesktopAndroidExtensionsBrowserTest() override = default;

  content::WebContents* GetActiveWebContents() {
    return chrome_test_utils::GetActiveWebContents(this);
  }

  void SetUpOnMainThread() override {
    AndroidBrowserTest::SetUpOnMainThread();

    host_resolver()->AddRule("*", "127.0.0.1");
    ASSERT_TRUE(embedded_test_server()->Start());
  }

  // Returns the main `BrowserContext`.
  content::BrowserContext* GetBrowserContext() {
    return GetActiveWebContents()->GetBrowserContext();
  }

  // Attempts to parse and load an extension from the given `file_path` and add
  // it to the extensions system (which will also activate the extension).
  // Returns the extension on success; on failure, returns null.
  scoped_refptr<const Extension> LoadExtensionFromDirectory(
      const base::FilePath& file_path) {
    ChromeTestExtensionLoader loader(GetBrowserContext());
    return loader.LoadExtension(file_path);
  }
};

// The following is a simple test exercising a basic navigation and script
// injection. This doesn't exercise any extensions logic, but ensures Chrome
// successfully starts and can navigate the web.
IN_PROC_BROWSER_TEST_F(DesktopAndroidExtensionsBrowserTest, SanityCheck) {
  ASSERT_EQ(TabModelList::models().size(), 1u);

  EXPECT_TRUE(content::NavigateToURL(
      GetActiveWebContents(),
      embedded_test_server()->GetURL("example.com", "/title1.html")));

  EXPECT_EQ("This page has no title.",
            content::EvalJs(GetActiveWebContents(), "document.body.innerText"));
}

// Tests the ability to parse and validate a simple extension.
IN_PROC_BROWSER_TEST_F(DesktopAndroidExtensionsBrowserTest,
                       ParseAndValidateASimpleExtension) {
  static constexpr char kManifest[] =
      R"({
           "name": "Test Extension",
           "version": "0.1",
           "manifest_version": 3
         })";

  TestExtensionDir test_dir;
  test_dir.WriteManifest(kManifest);

  scoped_refptr<const Extension> extension =
      LoadExtensionFromDirectory(test_dir.UnpackedPath());
  ASSERT_TRUE(extension);

  // Validate the fields in the extension.
  EXPECT_EQ("Test Extension", extension->name());
  EXPECT_EQ("0.1", extension->version().GetString());
  EXPECT_EQ(3, extension->manifest_version());
}

// Tests the adding an extension to the registry and navigating to a
// corresponding page in the extension, verifying the expected content, and
// leveraging the chrome.test API to pass a result. The latter verifies the
// core extension bindings system and API handling works, including
// exercising custom bindings.
IN_PROC_BROWSER_TEST_F(DesktopAndroidExtensionsBrowserTest,
                       NavigateToExtensionPage) {
  static constexpr char kManifest[] =
      R"({
           "name": "Test Extension",
           "version": "0.1",
           "manifest_version": 3
         })";
  static constexpr char kPageHtml[] =
      R"(<html>
           Hello, world
           <script src="page.js"></script>
         </html>)";
  static constexpr char kPageJs[] =
      R"(chrome.test.runTests([
           function sanityCheck() {
             chrome.test.assertEq(2, 1 + 1);
             chrome.test.succeed();
           }
         ]);)";

  TestExtensionDir test_dir;
  test_dir.WriteManifest(kManifest);
  test_dir.WriteFile(FILE_PATH_LITERAL("page.html"), kPageHtml);
  test_dir.WriteFile(FILE_PATH_LITERAL("page.js"), kPageJs);

  scoped_refptr<const Extension> extension =
      LoadExtensionFromDirectory(test_dir.UnpackedPath());
  ASSERT_TRUE(extension);

  GURL extension_page = extension->ResolveExtensionURL("page.html");

  ResultCatcher result_catcher;
  EXPECT_TRUE(content::NavigateToURL(GetActiveWebContents(), extension_page));
  EXPECT_EQ(extension_page, GetActiveWebContents()->GetLastCommittedURL());
  EXPECT_EQ("Hello, world",
            content::EvalJs(GetActiveWebContents(), "document.body.innerText"));
  EXPECT_TRUE(result_catcher.GetNextResult()) << result_catcher.message();
}

// Test service worker-based extensions properly load and have the service
// worker initialize and run.
IN_PROC_BROWSER_TEST_F(DesktopAndroidExtensionsBrowserTest,
                       ServiceWorkerBasedExtension) {
  static constexpr char kManifest[] =
      R"({
           "name": "Test Extension",
           "version": "0.1",
           "manifest_version": 3,
           "background": {"service_worker": "background.js"}
         })";
  static constexpr char kBackgroundJs[] =
      R"(chrome.test.runTests([
           function sanityCheck() {
             chrome.test.assertEq(2, 1 + 1);
             chrome.test.succeed();
           }
         ]);)";
  TestExtensionDir test_dir;
  test_dir.WriteManifest(kManifest);
  test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundJs);

  ResultCatcher result_catcher;
  scoped_refptr<const Extension> extension =
      LoadExtensionFromDirectory(test_dir.UnpackedPath());
  ASSERT_TRUE(extension);
  ASSERT_TRUE(result_catcher.GetNextResult()) << result_catcher.message();
}

// Tests the declarative net request API in extension service workers.
IN_PROC_BROWSER_TEST_F(DesktopAndroidExtensionsBrowserTest,
                       DeclarativeNetRequestSupport) {
  // Load a simple extension that redirects from example.com -> google.com.
  static constexpr char kManifest[] =
      R"({
           "name": "My Test Extension",
           "manifest_version": 3,
           "version": "0.1",
           "declarative_net_request": {
             "rule_resources": [{
               "id": "ruleset",
               "enabled": true,
               "path": "rules.json"
             }]
           },
           "permissions": ["declarativeNetRequest"],
           "host_permissions": ["*://example.com/*"]
         })";

  // One rule, which is a redirect from example.com to
  // http://google.com:<portid>/title2.html.
  static constexpr char kRulesJson[] =
      R"([{
           "id": 1,
           "priority": 1,
           "action": {
             "type": "redirect",
             "redirect": { "url": "http://google.com:%d/title2.html" }
           },
           "condition": {
             "urlFilter": "example.com",
             "resourceTypes": ["main_frame"]
           }
         }])";

  TestExtensionDir test_dir;
  test_dir.WriteManifest(kManifest);
  test_dir.WriteFile(
      FILE_PATH_LITERAL("rules.json"),
      base::StringPrintf(kRulesJson, embedded_test_server()->port()));

  declarative_net_request::RulesetManagerObserver ruleset_manager_observer(
      declarative_net_request::RulesMonitorService::Get(GetBrowserContext())
          ->ruleset_manager());

  scoped_refptr<const Extension> extension =
      LoadExtensionFromDirectory(test_dir.UnpackedPath());
  ASSERT_TRUE(extension);

  ruleset_manager_observer.WaitForExtensionsWithRulesetsCount(1);

  GURL url = embedded_test_server()->GetURL("example.com", "/title1.html");
  GURL expected_url =
      embedded_test_server()->GetURL("google.com", "/title2.html");

  EXPECT_TRUE(
      content::NavigateToURL(GetActiveWebContents(), url, expected_url));
  EXPECT_EQ(expected_url, GetActiveWebContents()->GetLastCommittedURL());
  EXPECT_EQ("This page has a title.",
            content::EvalJs(GetActiveWebContents(), "document.body.innerText"));
}

// Verifies content scripts are properly injected.
IN_PROC_BROWSER_TEST_F(DesktopAndroidExtensionsBrowserTest,
                       ContentScriptInjection) {
  // An extension that injects a simple script on match.test sites.
  static constexpr char kManifest[] =
      R"({
           "name": "Content Script Extension",
           "manifest_version": 3,
           "version": "0.1",
           "content_scripts": [{
             "matches": ["*://match.test/*"],
             "js": ["content_script.js"],
             "run_at": "document_idle"
           }]
         })";
  // The script just appends a span to indicate it injected.
  static constexpr char kContentScriptJs[] =
      R"(let span = document.createElement('span');
         span.textContent = 'content script injected';
         document.body.appendChild(span);)";

  TestExtensionDir test_dir;
  test_dir.WriteManifest(kManifest);
  test_dir.WriteFile(FILE_PATH_LITERAL("content_script.js"), kContentScriptJs);

  scoped_refptr<const Extension> extension =
      LoadExtensionFromDirectory(test_dir.UnpackedPath());
  ASSERT_TRUE(extension);

  // Verify scripts were properly parsed.
  const UserScriptList& content_scripts =
      ContentScriptsInfo::GetContentScripts(extension.get());
  ASSERT_EQ(1u, content_scripts.size());

  // Wait for scripts to load (if they haven't already).
  UserScriptManager* user_script_manager =
      ExtensionSystem::Get(GetBrowserContext())->user_script_manager();
  ExtensionUserScriptLoader* user_script_loader =
      user_script_manager->GetUserScriptLoaderForExtension(extension->id());
  if (!user_script_loader->HasLoadedScripts()) {
    ContentScriptLoadWaiter waiter(user_script_loader);
    waiter.Wait();
  }

  // First, navigate to a site that *doesn't* match the extension. The script
  // should not inject.
  const GURL no_match_test =
      embedded_test_server()->GetURL("no-match.test", "/title1.html");
  EXPECT_TRUE(content::NavigateToURL(GetActiveWebContents(), no_match_test));
  EXPECT_EQ(no_match_test, GetActiveWebContents()->GetLastCommittedURL());
  EXPECT_EQ("This page has no title.",
            content::EvalJs(GetActiveWebContents(), "document.body.innerText"));

  // Next, navigate to a site that *does* match. The script should inject.
  const GURL match_test =
      embedded_test_server()->GetURL("match.test", "/title1.html");
  EXPECT_TRUE(content::NavigateToURL(GetActiveWebContents(), match_test));
  EXPECT_EQ(match_test, GetActiveWebContents()->GetLastCommittedURL());
  EXPECT_EQ("This page has no title. content script injected",
            content::EvalJs(GetActiveWebContents(), "document.body.innerText"));
}

// Tests Storage API for StorageArea.local.
IN_PROC_BROWSER_TEST_F(DesktopAndroidExtensionsBrowserTest,
                       StorageApiTestStorageAreaLocal) {
  static constexpr char kManifest[] =
      R"({
           "name": "StorageArea.OnChanged",
           "version": "0.1",
           "manifest_version": 3,
           "permissions": ["storage"],
           "background": {"service_worker": "background.js"}
         })";
  static constexpr char kBackgroundJs[] =
      R"(chrome.test.runTests([
           function storageAreaOnChanged() {
              var localStorageArea = chrome.storage.local;
              chrome.test.listenOnce(localStorageArea.onChanged,
                function(changes) {
                  chrome.test.assertEq({key:{newValue:'value'}}, changes);
                });

              chrome.test.listenOnce(chrome.storage.onChanged,
                function(changes, namespace) {
                  chrome.test.assertEq({key:{newValue:'value'}}, changes);
                  chrome.test.assertEq('local', namespace);
                });

              chrome.storage.session.onChanged.addListener(
                function(changes, namespace) {
                  chrome.test.notifyFail(
                    'session.onChanged should not be called when local storage update');
              });

              localStorageArea.set({key: 'value'});
           }
         ]);)";
  TestExtensionDir test_dir;
  test_dir.WriteManifest(kManifest);
  test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundJs);

  ResultCatcher result_catcher;
  scoped_refptr<const Extension> extension =
      LoadExtensionFromDirectory(test_dir.UnpackedPath());
  ASSERT_TRUE(extension);
  ASSERT_TRUE(result_catcher.GetNextResult()) << result_catcher.message();
}

// Tests Storage API for StorageArea.session.
IN_PROC_BROWSER_TEST_F(DesktopAndroidExtensionsBrowserTest,
                       StorageApiTestStorageAreaSession) {
  static constexpr char kManifest[] =
      R"({
           "name": "StorageArea.OnChanged",
           "version": "0.1",
           "manifest_version": 3,
           "permissions": ["storage"],
           "background": {"service_worker": "background.js"}
         })";
  static constexpr char kBackgroundJs[] =
      R"(chrome.test.runTests([
           function storageAreaOnChanged() {
              var sessionStorageArea = chrome.storage.session;

              chrome.test.listenOnce(sessionStorageArea.onChanged,
                function(changes) {
                  chrome.test.assertEq({key:{newValue:'value'}}, changes);
                });

              chrome.test.listenOnce(chrome.storage.onChanged,
                function(changes, namespace) {
                  chrome.test.assertEq({key:{newValue:'value'}}, changes);
                  chrome.test.assertEq('session', namespace);
                });

              chrome.storage.local.onChanged.addListener(
                function(changes, namespace) {
                  chrome.test.notifyFail(
                    'local.onChanged should not be called when session storage update');
              });

              sessionStorageArea.set({key: 'value'});
           }
         ]);)";
  TestExtensionDir test_dir;
  test_dir.WriteManifest(kManifest);
  test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundJs);

  ResultCatcher result_catcher;
  scoped_refptr<const Extension> extension =
      LoadExtensionFromDirectory(test_dir.UnpackedPath());
  ASSERT_TRUE(extension);
  ASSERT_TRUE(result_catcher.GetNextResult()) << result_catcher.message();
}

// Tests reading a chrome.extension.* property.
IN_PROC_BROWSER_TEST_F(DesktopAndroidExtensionsBrowserTest,
                       ExtensionPropertyRead) {
  static constexpr char kManifest[] =
      R"({
           "name": "chrome.extension property read",
           "version": "0.1",
           "manifest_version": 3,
           "background": {"service_worker": "background.js"}
         })";
  static constexpr char kBackgroundJs[] =
      R"(chrome.test.runTests([
           function readProperty() {
              // Test that we can read a property.
              let incognito = chrome.extension.inIncognitoContext;
              chrome.test.assertFalse(incognito);
              chrome.test.succeed();
           }
         ]);)";
  TestExtensionDir test_dir;
  test_dir.WriteManifest(kManifest);
  test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundJs);

  ResultCatcher result_catcher;
  scoped_refptr<const Extension> extension =
      LoadExtensionFromDirectory(test_dir.UnpackedPath());
  ASSERT_TRUE(extension);
  ASSERT_TRUE(result_catcher.GetNextResult()) << result_catcher.message();
}

// Tests passing a basic message between extension contexts.
IN_PROC_BROWSER_TEST_F(DesktopAndroidExtensionsBrowserTest, MessagePassing) {
  static constexpr char kManifest[] =
      R"({
           "name": "Message passing",
           "version": "0.1",
           "manifest_version": 3,
           "background": {"service_worker": "background.js"}
         })";

  // A service worker that will send a message and wait for a reply.
  static constexpr char kBackgroundJs[] =
      R"(chrome.test.runTests([
           async function sendMessage() {
             // We wait for the C++ side to be ready; this allows us to open a
             // tab to listen to the message we'll send.
             await chrome.test.sendMessage('ready');

             const reply = await chrome.runtime.sendMessage('ping');
             chrome.test.assertEq('pong', reply);
             chrome.test.succeed();
           },
         ]);)";
  // This is a basic page that will reply to an incoming message.
  static constexpr char kPageHtml[] =
      R"(<html><script src="page.js"></script></html>)";
  static constexpr char kPageJs[] =
      R"(chrome.runtime.onMessage.addListener(async(msg, sender, sendReply) => {
           chrome.test.assertEq(msg, 'ping');
           sendReply('pong');
         });)";

  TestExtensionDir test_dir;
  test_dir.WriteManifest(kManifest);
  test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundJs);
  test_dir.WriteFile(FILE_PATH_LITERAL("page.js"), kPageJs);
  test_dir.WriteFile(FILE_PATH_LITERAL("page.html"), kPageHtml);

  ResultCatcher result_catcher;
  ExtensionTestMessageListener listener("ready", ReplyBehavior::kWillReply);
  scoped_refptr<const Extension> extension =
      LoadExtensionFromDirectory(test_dir.UnpackedPath());
  ASSERT_TRUE(extension);

  ASSERT_TRUE(listener.WaitUntilSatisfied());

  // Open a tab to the listening page, and reply to the extension when it's
  // loaded.
  GURL extension_page = extension->ResolveExtensionURL("page.html");
  EXPECT_TRUE(content::NavigateToURL(GetActiveWebContents(), extension_page));
  EXPECT_TRUE(content::WaitForLoadStop(GetActiveWebContents()));
  EXPECT_EQ(extension_page, GetActiveWebContents()->GetLastCommittedURL());

  listener.Reply("done");

  ASSERT_TRUE(result_catcher.GetNextResult()) << result_catcher.message();
}

}  // namespace extensions