Commit b8324419 authored by Alexey Baskakov's avatar Alexey Baskakov Committed by Commit Bot

WebApp: Refactor testing for BookmarkAppInstallFinalizer.

Create SetCrxInstallerFactory test-only method which enables tests
to inject a CrxInstaller mock.

This change will allow us to support parallel installs (see next CLs).

Bug: 915043
Change-Id: I2872a815e1d6255488e4c0af182f26f776b042aa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1498259Reviewed-by: default avatarGiovanni Ortuño Urquidi <ortuno@chromium.org>
Commit-Queue: Alexey Baskakov <loyso@chromium.org>
Cr-Commit-Position: refs/heads/master@{#637567}
parent 54329c1d
......@@ -7,7 +7,6 @@
#include <utility>
#include "base/bind.h"
#include "base/callback.h"
#include "chrome/browser/extensions/bookmark_app_extension_util.h"
#include "chrome/browser/extensions/crx_installer.h"
#include "chrome/browser/extensions/launch_util.h"
......@@ -42,19 +41,24 @@ const Extension* GetExtensionById(Profile* profile,
} // namespace
BookmarkAppInstallFinalizer::BookmarkAppInstallFinalizer(Profile* profile)
: profile_(profile) {}
: profile_(profile) {
crx_installer_factory_ = base::BindRepeating([](Profile* profile) {
ExtensionService* extension_service =
ExtensionSystem::Get(profile)->extension_service();
DCHECK(extension_service);
return CrxInstaller::CreateSilent(extension_service);
});
}
BookmarkAppInstallFinalizer::~BookmarkAppInstallFinalizer() = default;
void BookmarkAppInstallFinalizer::FinalizeInstall(
const WebApplicationInfo& web_app_info,
InstallFinalizedCallback callback) {
if (!crx_installer_) {
ExtensionService* extension_service =
ExtensionSystem::Get(profile_)->extension_service();
DCHECK(extension_service);
crx_installer_ = CrxInstaller::CreateSilent(extension_service);
}
DCHECK(!crx_installer_);
crx_installer_ = crx_installer_factory_.Run(profile_);
DCHECK(crx_installer_);
crx_installer_->set_installer_callback(base::BindOnce(
&BookmarkAppInstallFinalizer::OnExtensionInstalled,
......@@ -63,22 +67,19 @@ void BookmarkAppInstallFinalizer::FinalizeInstall(
crx_installer_->InstallWebApp(web_app_info);
}
void BookmarkAppInstallFinalizer::SetCrxInstallerForTesting(
scoped_refptr<CrxInstaller> crx_installer) {
crx_installer_ = crx_installer;
}
void BookmarkAppInstallFinalizer::OnExtensionInstalled(
std::unique_ptr<WebApplicationInfo> web_app_info,
InstallFinalizedCallback callback,
const base::Optional<CrxInstallError>& error) {
const Extension* extension = crx_installer_->extension();
crx_installer_.reset();
if (error) {
std::move(callback).Run(web_app::AppId(),
web_app::InstallResultCode::kFailedUnknownReason);
return;
}
auto* extension = crx_installer_->extension();
DCHECK(extension);
DCHECK_EQ(AppLaunchInfo::GetLaunchWebURL(extension), web_app_info->app_url);
......@@ -137,4 +138,9 @@ void BookmarkAppInstallFinalizer::RevealAppShim(const web_app::AppId& app_id) {
BookmarkAppRevealAppShim(profile_, app);
}
void BookmarkAppInstallFinalizer::SetCrxInstallerFactoryForTesting(
CrxInstallerFactory crx_installer_factory) {
crx_installer_factory_ = crx_installer_factory;
}
} // namespace extensions
......@@ -5,7 +5,7 @@
#ifndef CHROME_BROWSER_WEB_APPLICATIONS_EXTENSIONS_BOOKMARK_APP_INSTALL_FINALIZER_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_EXTENSIONS_BOOKMARK_APP_INSTALL_FINALIZER_H_
#include "base/callback_forward.h"
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
......@@ -47,7 +47,10 @@ class BookmarkAppInstallFinalizer : public web_app::InstallFinalizer {
bool CanRevealAppShim() const override;
void RevealAppShim(const web_app::AppId& app_id) override;
void SetCrxInstallerForTesting(scoped_refptr<CrxInstaller> crx_installer);
using CrxInstallerFactory =
base::RepeatingCallback<scoped_refptr<CrxInstaller>(Profile*)>;
void SetCrxInstallerFactoryForTesting(
CrxInstallerFactory crx_installer_factory);
private:
void OnExtensionInstalled(std::unique_ptr<WebApplicationInfo> web_app_info,
......@@ -55,6 +58,7 @@ class BookmarkAppInstallFinalizer : public web_app::InstallFinalizer {
const base::Optional<CrxInstallError>& error);
scoped_refptr<CrxInstaller> crx_installer_;
CrxInstallerFactory crx_installer_factory_;
Profile* profile_;
// We need a WeakPtr because CrxInstaller is refcounted and it can run its
......
......@@ -120,7 +120,12 @@ TEST_F(BookmarkAppInstallFinalizerTest, BasicInstallFails) {
auto fake_crx_installer =
base::MakeRefCounted<BookmarkAppInstallFinalizerTest::FakeCrxInstaller>(
profile());
installer.SetCrxInstallerForTesting(fake_crx_installer);
installer.SetCrxInstallerFactoryForTesting(
base::BindLambdaForTesting([&](Profile* profile) {
scoped_refptr<CrxInstaller> crx_installer = fake_crx_installer;
return crx_installer;
}));
auto info = std::make_unique<WebApplicationInfo>();
info->app_url = GURL(kWebAppUrl);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment