File: component_extension_browsertest.cc

package info (click to toggle)
chromium 140.0.7339.80-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,201,348 kB
  • sloc: cpp: 35,092,378; ansic: 7,161,671; javascript: 4,199,703; python: 1,441,798; asm: 949,904; xml: 747,409; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,119; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (126 lines) | stat: -rw-r--r-- 4,809 bytes parent folder | download | duplicates (4)
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
// Copyright 2025 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 "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/test/browser_test.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "extensions/test/test_extension_dir.h"

namespace extensions {

using ComponentExtensionBrowserTest = ExtensionBrowserTest;

#if BUILDFLAG(IS_CHROMEOS)
// Tests that MojoJS is enabled for component extensions that need it.
// Note the test currently only runs for ChromeOS because the test extension
// uses `mojoPrivate` to test and `mojoPrivate` is ChromeOS only.
IN_PROC_BROWSER_TEST_F(ComponentExtensionBrowserTest, MojoJS) {
  ResultCatcher result_catcher;

  auto* extension =
      LoadExtension(test_data_dir_.AppendASCII("service_worker/mojo"),
                    {.load_as_component = true});
  ASSERT_TRUE(extension);

  ASSERT_TRUE(result_catcher.GetNextResult());
}
#endif  // BUILDFLAG(IS_CHROMEOS)

const ExtensionId kExtensionId = "iegclhlplifhodhkoafiokenjoapiobj";
constexpr char kExtensionKey[] =
    "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjzv7dI7Ygyh67VHE1DdidudpYf8P"
    "Ffv8iucWvzO+3xpF/Dm5xNo7aQhPNiEaNfHwJQ7lsp4gc+C+4bbaVewBFspTruoSJhZc5uEf"
    "qxwovJwN+v1/SUFXTXQmQBv6gs0qZB4gBbl4caNQBlqrFwAMNisnu1V6UROna8rOJQ90D7Nv"
    "7TCwoVPKBfVshpFjdDOTeBg4iLctO3S/06QYqaTDrwVceSyHkVkvzBY6tc6mnYX0RZu78J9i"
    "L8bdqwfllOhs69cqoHHgrLdI6JdOyiuh6pBP6vxMlzSKWJ3YTNjaQTPwfOYaLMuzdl0v+Ydz"
    "afIzV9zwe4Xiskk+5JNGt8b2rQIDAQAB";

// Tests updating a Service Worker-based component extension across a restart.
// This simulates a browser update where a component extension might change.
class ComponentExtensionServiceWorkerUpdateBrowserTest
    : public ComponentExtensionBrowserTest {
 public:
  void WriteExtension(TestExtensionDir* dir, int version) {
    constexpr char kManifestTemplate[] =
        R"({
         "name": "Component SW Update Test",
         "manifest_version": 3,
         "version": "%d",
         "background": {"service_worker": "sw.js"},
         "key": "%s"
       })";
    dir->WriteManifest(
        base::StringPrintf(kManifestTemplate, version, kExtensionKey));

    constexpr char kBackgroundScriptTemplate[] =
        R"(self.version = %d;
       chrome.test.sendMessage(`v${self.version} ready`);)";
    dir->WriteFile(FILE_PATH_LITERAL("sw.js"),
                   base::StringPrintf(kBackgroundScriptTemplate, version));
  }

  int GetWorkerVersion(const ExtensionId& id) {
    constexpr char kGetVersionScript[] =
        "chrome.test.sendScriptResult(self.version);";
    base::Value version = ExecuteScriptInBackgroundPage(id, kGetVersionScript);
    if (!version.is_int()) {
      ADD_FAILURE() << "Script did not return an integer. Value: " << version;
      return -1;
    }
    return version.GetInt();
  }

  TestExtensionDir test_dir_v1_;
  TestExtensionDir test_dir_v2_;
};

// PRE_ test: Installs V1 of the component extension. Verifies it runs.
IN_PROC_BROWSER_TEST_F(ComponentExtensionServiceWorkerUpdateBrowserTest,
                       PRE_Update) {
  WriteExtension(&test_dir_v1_, 1);

  // Load V1 of the component extension.
  ExtensionTestMessageListener v1_ready("v1 ready");
  const Extension* extension_v1 =
      LoadExtension(test_dir_v1_.UnpackedPath(), {.load_as_component = true});
  ASSERT_TRUE(extension_v1);

  // Ensure it has the correct ID and wait for it to start.
  const ExtensionId id = extension_v1->id();
  ASSERT_EQ(kExtensionId, id);
  ASSERT_TRUE(v1_ready.WaitUntilSatisfied());

  // Check service worker version.
  EXPECT_EQ("1", extension_v1->version().GetString());
  EXPECT_EQ(1, GetWorkerVersion(id));
}

// Main test: Installs V2 of the component extension. Verifies V2 runs.
IN_PROC_BROWSER_TEST_F(ComponentExtensionServiceWorkerUpdateBrowserTest,
                       Update) {
  ASSERT_FALSE(
      extension_registry()->enabled_extensions().GetByID(kExtensionId));
  WriteExtension(&test_dir_v2_, 2);

  // Load V2 of the component extension.
  ExtensionTestMessageListener v2_ready("v2 ready");
  const Extension* extension_v2 =
      LoadExtension(test_dir_v2_.UnpackedPath(), {.load_as_component = true});
  ASSERT_TRUE(extension_v2);

  // Ensure it has the correct ID (same as V1) and wait for it to start.
  const ExtensionId id = extension_v2->id();
  EXPECT_EQ(kExtensionId, id);
  ASSERT_TRUE(v2_ready.WaitUntilSatisfied());

  // Check service worker version.
  EXPECT_EQ("2", extension_v2->version().GetString());
  EXPECT_EQ(2, GetWorkerVersion(id));
}

}  // namespace extensions