File: extension_installed_waiter_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (161 lines) | stat: -rw-r--r-- 5,313 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
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/extensions/extension_installed_waiter.h"

#include "base/command_line.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/extensions/load_error_reporter.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension_builder.h"
#include "testing/gtest/include/gtest/gtest.h"

using extensions::Extension;

class ExtensionInstalledWaiterTest : public BrowserWithTestWindowTest {
 public:
  ExtensionInstalledWaiterTest()
      : BrowserWithTestWindowTest(
            base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  ~ExtensionInstalledWaiterTest() override = default;

  void SetUp() override {
    BrowserWithTestWindowTest::SetUp();
    extensions::LoadErrorReporter::Init(false);
    extensions::TestExtensionSystem* extension_system =
        static_cast<extensions::TestExtensionSystem*>(
            extensions::ExtensionSystem::Get(profile()));
    extension_system->CreateExtensionService(
        base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
  }

  void TearDown() override {
    ExtensionInstalledWaiter::SetGivingUpCallbackForTesting(
        base::NullCallback());
    BrowserWithTestWindowTest::TearDown();
  }

  void WaitFor(scoped_refptr<const Extension> extension,
               Browser* test_browser = nullptr) {
    ExtensionInstalledWaiter::SetGivingUpCallbackForTesting(base::BindRepeating(
        &ExtensionInstalledWaiterTest::GivingUp, base::Unretained(this)));
    if (!test_browser) {
      test_browser = browser();
    }
    ExtensionInstalledWaiter::WaitForInstall(
        extension, test_browser,
        base::BindOnce(&ExtensionInstalledWaiterTest::Done,
                       base::Unretained(this)));
  }

  void Done() { done_called_++; }
  void GivingUp() { giving_up_called_++; }

 protected:
  int done_called_ = 0;
  int giving_up_called_ = 0;

  scoped_refptr<const Extension> MakeExtensionNamed(const std::string& name) {
    return extensions::ExtensionBuilder(name).Build();
  }

  extensions::ExtensionRegistrar* extension_registrar() {
    return extensions::ExtensionRegistrar::Get(profile());
  }
};

TEST_F(ExtensionInstalledWaiterTest, ExtensionIsAlreadyInstalled) {
  auto extension = MakeExtensionNamed("foo");
  extension_registrar()->AddExtension(extension);

  WaitFor(extension);
  EXPECT_EQ(1, done_called_);
}

TEST_F(ExtensionInstalledWaiterTest, ExtensionInstall) {
  auto extension = MakeExtensionNamed("foo");

  WaitFor(extension);
  EXPECT_EQ(0, done_called_);

  extension_registrar()->AddExtension(extension);

  // ExtensionInstalledWaiter must *not* call the done callback on the same
  // runloop cycle as the extension installation, to allow all the other
  // observers to run.
  EXPECT_FALSE(task_environment()->MainThreadIsIdle());
  EXPECT_EQ(0, done_called_);

  task_environment()->RunUntilIdle();
  EXPECT_EQ(1, done_called_);
}

TEST_F(ExtensionInstalledWaiterTest, NotTheExtensionYouAreLookingFor) {
  auto foo = MakeExtensionNamed("foo");
  auto bar = MakeExtensionNamed("bar");

  WaitFor(foo);
  EXPECT_EQ(0, done_called_);

  extension_registrar()->AddExtension(bar);
  task_environment()->RunUntilIdle();
  EXPECT_EQ(0, done_called_);

  extension_registrar()->AddExtension(foo);
  task_environment()->RunUntilIdle();
  EXPECT_EQ(1, done_called_);
}

TEST_F(ExtensionInstalledWaiterTest, ExtensionUninstalledWhileWaiting) {
  auto extension = MakeExtensionNamed("foo");

  WaitFor(extension);
  EXPECT_EQ(0, done_called_);

  extension_registrar()->AddExtension(extension);
  extension_registrar()->RemoveExtension(
      extension->id(), extensions::UnloadedExtensionReason::UNINSTALL);
  EXPECT_EQ(1, giving_up_called_);

  task_environment()->RunUntilIdle();
  EXPECT_EQ(0, done_called_);
}

TEST_F(ExtensionInstalledWaiterTest, BrowserShutdownWhileWaiting) {
  std::unique_ptr<BrowserWindow> window = CreateBrowserWindow();
  std::unique_ptr<Browser> browser =
      CreateBrowser(profile(), Browser::TYPE_NORMAL, false, window.get());

  auto foo = MakeExtensionNamed("foo");
  WaitFor(foo, browser.get());

  browser.reset();
  EXPECT_EQ(1, giving_up_called_);
  EXPECT_EQ(0, done_called_);
}

// Regression test for https://crbug.com/1049190.
TEST_F(ExtensionInstalledWaiterTest, BrowserShutdownWhileWaitingDoesntCrash) {
  std::unique_ptr<BrowserWindow> window = CreateBrowserWindow();
  std::unique_ptr<Browser> browser =
      CreateBrowser(profile(), Browser::TYPE_NORMAL, false, window.get());

  auto foo = MakeExtensionNamed("foo");
  WaitFor(foo, browser.get());

  // Null out the giving-up callback, which is how the class is actually used in
  // production.
  ExtensionInstalledWaiter::SetGivingUpCallbackForTesting({});

  // If the fix for https://crbug.com/1049190 regresses, this will crash:
  browser->OnWindowClosing();

  EXPECT_EQ(0, giving_up_called_);
  EXPECT_EQ(0, done_called_);
}