File: extension_installed_waiter_browsertest.cc

package info (click to toggle)
chromium 143.0.7499.109-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 5,786,824 kB
  • sloc: cpp: 35,783,839; ansic: 7,477,365; javascript: 3,962,116; python: 1,480,521; xml: 764,832; asm: 710,816; pascal: 188,028; sh: 88,717; perl: 88,692; objc: 79,984; sql: 57,625; cs: 42,265; fortran: 24,101; makefile: 22,509; tcl: 15,277; php: 14,018; yacc: 9,043; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (142 lines) | stat: -rw-r--r-- 4,659 bytes parent folder | download | duplicates (6)
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
// 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/functional/callback_helpers.h"
#include "base/test/run_until.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/common/extension_builder.h"
#include "testing/gtest/include/gtest/gtest.h"

using extensions::Extension;

class ExtensionInstalledWaiterTest : public extensions::ExtensionBrowserTest {
 public:
  void TearDownOnMainThread() override {
    ExtensionInstalledWaiter::SetGivingUpCallbackForTesting(
        base::NullCallback());
    extensions::ExtensionBrowserTest::TearDownOnMainThread();
  }

  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());
  }
};

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

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

IN_PROC_BROWSER_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_EQ(0, done_called_);

  ASSERT_TRUE(base::test::RunUntil([&] { return done_called_ >= 1; }));
  EXPECT_EQ(1, done_called_);
}

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

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

  extension_registrar()->AddExtension(bar);
  extension_registrar()->AddExtension(foo);

  ASSERT_TRUE(base::test::RunUntil([&] { return done_called_ >= 1; }));
  EXPECT_EQ(1, done_called_);
}

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

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

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

  ASSERT_TRUE(base::test::RunUntil([&] { return giving_up_called_ >= 1; }));
  EXPECT_EQ(1, giving_up_called_);
  EXPECT_EQ(0, done_called_);
}

IN_PROC_BROWSER_TEST_F(ExtensionInstalledWaiterTest,
                       BrowserShutdownWhileWaiting) {
  auto foo = MakeExtensionNamed("foo");
  WaitFor(foo, browser());

  CloseBrowserSynchronously(browser());
  EXPECT_EQ(1, giving_up_called_);
  EXPECT_EQ(0, done_called_);
}

// Regression test for https://crbug.com/1049190.
IN_PROC_BROWSER_TEST_F(ExtensionInstalledWaiterTest,
                       BrowserShutdownWhileWaitingDoesntCrash) {
  auto foo = MakeExtensionNamed("foo");
  WaitFor(foo, browser());

  // 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:
  chrome::CloseWindow(browser());
  ui_test_utils::WaitForBrowserToClose(browser());

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