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
|
// 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/web_applications/test/fake_web_app_ui_manager.h"
#include <optional>
#include <utility>
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/notimplemented.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "chrome/browser/ui/web_applications/web_app_run_on_os_login_notification.h"
#include "chrome/browser/web_applications/web_app_callback_app_identity.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "components/webapps/browser/install_result_code.h"
#include "components/webapps/browser/uninstall_result_code.h"
namespace web_app {
FakeWebAppUiManager::FakeWebAppUiManager() = default;
FakeWebAppUiManager::~FakeWebAppUiManager() = default;
void FakeWebAppUiManager::Start() {}
void FakeWebAppUiManager::Shutdown() {}
void FakeWebAppUiManager::SetNumWindowsForApp(const webapps::AppId& app_id,
size_t num_windows_for_app) {
app_id_to_num_windows_map_[app_id] = num_windows_for_app;
if (num_windows_for_app != 0) {
return;
}
auto it = windows_closed_requests_map_.find(app_id);
if (it == windows_closed_requests_map_.end()) {
return;
}
for (auto& callback : it->second) {
std::move(callback).Run();
}
windows_closed_requests_map_.erase(it);
}
void FakeWebAppUiManager::SetOnNotifyOnAllAppWindowsClosedCallback(
base::RepeatingCallback<void(webapps::AppId)> callback) {
notify_on_all_app_windows_closed_callback_ = std::move(callback);
}
void FakeWebAppUiManager::SetOnLaunchWebAppCallback(
OnLaunchWebAppCallback callback) {
on_launch_web_app_callback_ = std::move(callback);
}
WebAppUiManagerImpl* FakeWebAppUiManager::AsImpl() {
return nullptr;
}
size_t FakeWebAppUiManager::GetNumWindowsForApp(const webapps::AppId& app_id) {
if (!app_id_to_num_windows_map_.contains(app_id)) {
return 0;
}
return app_id_to_num_windows_map_[app_id];
}
void FakeWebAppUiManager::NotifyOnAllAppWindowsClosed(
const webapps::AppId& app_id,
base::OnceClosure callback) {
notify_on_all_app_windows_closed_callback_.Run(app_id);
if (GetNumWindowsForApp(app_id) == 0) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
return;
}
windows_closed_requests_map_[app_id].push_back(std::move(callback));
}
bool FakeWebAppUiManager::CanAddAppToQuickLaunchBar() const {
return false;
}
void FakeWebAppUiManager::AddAppToQuickLaunchBar(const webapps::AppId& app_id) {
}
bool FakeWebAppUiManager::IsAppInQuickLaunchBar(
const webapps::AppId& app_id) const {
return false;
}
bool FakeWebAppUiManager::CanReparentAppTabToWindow(
const webapps::AppId& app_id,
bool shortcut_created,
content::WebContents* web_contents) const {
return true;
}
Browser* FakeWebAppUiManager::ReparentAppTabToWindow(
content::WebContents* contents,
const webapps::AppId& app_id,
bool shortcut_created) {
++num_reparent_tab_calls_;
return nullptr;
}
Browser* FakeWebAppUiManager::ReparentAppTabToWindow(
content::WebContents* contents,
const webapps::AppId& app_id,
base::OnceCallback<void(content::WebContents*)> completion_callback) {
++num_reparent_tab_calls_;
std::move(completion_callback).Run(contents);
return nullptr;
}
void FakeWebAppUiManager::ShowWebAppIdentityUpdateDialog(
const std::string& app_id,
bool title_change,
bool icon_change,
const std::u16string& old_title,
const std::u16string& new_title,
const SkBitmap& old_icon,
const SkBitmap& new_icon,
content::WebContents* web_contents,
AppIdentityDialogCallback callback) {
auto identity_update_dialog_action_for_testing =
GetIdentityUpdateDialogActionForTesting();
if (!identity_update_dialog_action_for_testing) {
return;
}
std::move(callback).Run(identity_update_dialog_action_for_testing.value());
}
void FakeWebAppUiManager::LaunchWebApp(apps::AppLaunchParams params,
LaunchWebAppWindowSetting launch_setting,
Profile& profile,
LaunchWebAppDebugValueCallback callback,
WithAppResources& lock) {
// Due to this sometimes causing confusion in tests, print that a launch has
// been faked. To have launches create real WebContents in unit_tests (which
// will be non-functional anyways), populate the WebAppUiManagerImpl in the
// FakeWebAppProvider during startup.
LOG(INFO) << "Pretending to launch web app " << params.app_id;
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), /*browser=*/nullptr,
/*web_contents=*/nullptr, params.container,
base::Value("FakeWebAppUiManager::LaunchWebApp")));
if (on_launch_web_app_callback_) {
on_launch_web_app_callback_.Run(std::move(params),
std::move(launch_setting));
}
}
void FakeWebAppUiManager::WaitForFirstRunService(
Profile& profile,
FirstRunServiceCompletedCallback callback) {
std::move(callback).Run(/*success=*/true);
}
#if BUILDFLAG(IS_CHROMEOS)
void FakeWebAppUiManager::MigrateLauncherState(
const webapps::AppId& from_app_id,
const webapps::AppId& to_app_id,
base::OnceClosure callback) {
std::move(callback).Run();
}
void FakeWebAppUiManager::DisplayRunOnOsLoginNotification(
const base::flat_map<webapps::AppId,
WebAppUiManager::RoolNotificationBehavior>& apps,
base::WeakPtr<Profile> profile) {
// Still show the notification so it can be tested using the
// NotificationDisplayServiceTester
web_app::DisplayRunOnOsLoginNotification(apps, std::move(profile));
}
#endif // BUILDFLAG(IS_CHROMEOS)
void FakeWebAppUiManager::NotifyAppRelaunchState(
const webapps::AppId& placeholder_app_id,
const webapps::AppId& final_app_id,
const std::u16string& final_app_name,
base::WeakPtr<Profile> profile,
AppRelaunchState relaunch_state) {}
content::WebContents* FakeWebAppUiManager::CreateNewTab() {
return nullptr;
}
bool FakeWebAppUiManager::IsWebContentsActiveTabInBrowser(
content::WebContents* web_contents) {
return true;
}
void FakeWebAppUiManager::TriggerInstallDialog(
content::WebContents* web_contents,
webapps::WebappInstallSource source,
InstallCallback callback) {
std::move(callback).Run("",
webapps::InstallResultCode::kWebAppProviderNotReady);
}
void FakeWebAppUiManager::TriggerInstallDialogForBackgroundInstall(
content::WebContents* initiating_web_contents,
std::unique_ptr<webapps::MlInstallOperationTracker> tracker,
const GURL& install_url,
const std::optional<GURL>& manifest_id,
InstallCallback callback) {
NOTIMPLEMENTED();
}
void FakeWebAppUiManager::TriggerLaunchDialogForBackgroundInstall(
content::WebContents* initiating_web_contents,
const webapps::AppId& app_id,
Profile* profile,
const std::string& app_name,
base::OnceCallback<void(bool accepted)> callback) {
NOTIMPLEMENTED();
}
void FakeWebAppUiManager::PresentUserUninstallDialog(
const webapps::AppId& app_id,
webapps::WebappUninstallSource uninstall_source,
BrowserWindow* parent_window,
UninstallCompleteCallback callback) {
std::move(callback).Run(webapps::UninstallResultCode::kAppRemoved);
}
void FakeWebAppUiManager::PresentUserUninstallDialog(
const webapps::AppId& app_id,
webapps::WebappUninstallSource uninstall_source,
gfx::NativeWindow parent_window,
UninstallCompleteCallback callback) {
std::move(callback).Run(webapps::UninstallResultCode::kAppRemoved);
}
void FakeWebAppUiManager::PresentUserUninstallDialog(
const webapps::AppId& app_id,
webapps::WebappUninstallSource uninstall_source,
gfx::NativeWindow parent_window,
UninstallCompleteCallback callback,
UninstallScheduledCallback scheduled_callback) {
std::move(scheduled_callback).Run(/*uninstall_scheduled=*/true);
std::move(callback).Run(webapps::UninstallResultCode::kAppRemoved);
}
void FakeWebAppUiManager::ShowIntentPicker(
const GURL& url,
content::WebContents* web_contents,
ShowIntentPickerBubbleCallback callback) {}
void FakeWebAppUiManager::LaunchOrFocusIsolatedWebAppInstaller(
const base::FilePath& bundle_path) {}
void FakeWebAppUiManager::MaybeCreateEnableSupportedLinksInfobar(
content::WebContents* web_contents,
const std::string& launch_name) {}
void FakeWebAppUiManager::MaybeShowIPHPromoForAppsLaunchedViaLinkCapturing(
Browser* browser,
Profile* profile,
const std::string& app_id) {}
} // namespace web_app
|