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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
// Copyright 2020 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/apps/app_service/launch_utils.h"
#include "build/build_config.h"
#include "chrome/browser/apps/app_service/app_launch_params.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/testing_profile.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "components/services/app_service/public/cpp/intent.h"
#include "components/services/app_service/public/cpp/intent_util.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/display/types/display_constants.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "ash/public/cpp/test/test_new_window_delegate.h"
#include "chrome/browser/apps/app_service/publishers/app_publisher.h"
#include "chrome/browser/apps/link_capturing/link_capturing_feature_test_support.h"
#include "components/services/app_service/public/cpp/intent_filter_util.h"
#endif // BUILDFLAG(IS_CHROMEOS)
namespace apps {
class LaunchUtilsTest : public testing::Test {
protected:
apps::AppLaunchParams CreateLaunchParams(
apps::LaunchContainer container,
WindowOpenDisposition disposition,
bool preferred_container,
int64_t display_id = display::kInvalidDisplayId,
apps::LaunchContainer fallback_container =
apps::LaunchContainer::kLaunchContainerNone) {
return apps::CreateAppIdLaunchParamsWithEventFlags(
app_id, apps::GetEventFlags(disposition, preferred_container),
apps::LaunchSource::kFromChromeInternal, display_id,
fallback_container);
}
std::string app_id = "aaa";
content::BrowserTaskEnvironment task_environment_;
TestingProfile profile_;
};
TEST_F(LaunchUtilsTest, WindowContainerAndWindowDisposition) {
auto container = apps::LaunchContainer::kLaunchContainerWindow;
auto disposition = WindowOpenDisposition::NEW_WINDOW;
auto params = CreateLaunchParams(container, disposition, false);
EXPECT_EQ(container, params.container);
EXPECT_EQ(disposition, params.disposition);
}
TEST_F(LaunchUtilsTest, TabContainerAndForegoundTabDisposition) {
auto container = apps::LaunchContainer::kLaunchContainerTab;
auto disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
auto params = CreateLaunchParams(container, disposition, false);
EXPECT_EQ(container, params.container);
EXPECT_EQ(disposition, params.disposition);
}
TEST_F(LaunchUtilsTest, TabContainerAndBackgoundTabDisposition) {
auto container = apps::LaunchContainer::kLaunchContainerTab;
auto disposition = WindowOpenDisposition::NEW_BACKGROUND_TAB;
auto params = CreateLaunchParams(container, disposition, false);
EXPECT_EQ(container, params.container);
EXPECT_EQ(disposition, params.disposition);
}
TEST_F(LaunchUtilsTest, PreferContainerWithTab) {
auto container = apps::LaunchContainer::kLaunchContainerNone;
auto disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
auto preferred_container = apps::LaunchContainer::kLaunchContainerWindow;
auto params =
CreateLaunchParams(container, disposition, true,
display::kInvalidDisplayId, preferred_container);
EXPECT_EQ(preferred_container, params.container);
EXPECT_EQ(disposition, params.disposition);
}
TEST_F(LaunchUtilsTest, PreferContainerWithWindow) {
auto container = apps::LaunchContainer::kLaunchContainerNone;
auto disposition = WindowOpenDisposition::NEW_WINDOW;
auto preferred_container = apps::LaunchContainer::kLaunchContainerWindow;
auto params =
CreateLaunchParams(container, disposition, true,
display::kInvalidDisplayId, preferred_container);
EXPECT_EQ(preferred_container, params.container);
EXPECT_EQ(WindowOpenDisposition::NEW_FOREGROUND_TAB, params.disposition);
}
TEST_F(LaunchUtilsTest, UseIntentFullUrlInLaunchParams) {
auto disposition = WindowOpenDisposition::NEW_WINDOW;
const GURL url = GURL("https://example.com/?query=1#frag");
auto intent =
std::make_unique<apps::Intent>(apps_util::kIntentActionView, url);
auto params = apps::CreateAppLaunchParamsForIntent(
app_id, apps::GetEventFlags(disposition, true),
apps::LaunchSource::kFromChromeInternal, display::kInvalidDisplayId,
apps::LaunchContainer::kLaunchContainerWindow, std::move(intent),
&profile_);
EXPECT_EQ(url, params.override_url);
}
TEST_F(LaunchUtilsTest, IntentFilesAreCopiedToLaunchParams) {
auto disposition = WindowOpenDisposition::NEW_WINDOW;
std::vector<apps::IntentFilePtr> files;
std::string file_path = "filesystem:http://foo.com/test/foo.txt";
auto file = std::make_unique<apps::IntentFile>(GURL(file_path));
EXPECT_TRUE(file->url.is_valid());
file->mime_type = "text/plain";
files.push_back(std::move(file));
auto intent = std::make_unique<apps::Intent>(apps_util::kIntentActionView,
std::move(files));
auto params = apps::CreateAppLaunchParamsForIntent(
app_id, apps::GetEventFlags(disposition, true),
apps::LaunchSource::kFromChromeInternal, display::kInvalidDisplayId,
apps::LaunchContainer::kLaunchContainerWindow, std::move(intent),
&profile_);
#if BUILDFLAG(IS_CHROMEOS)
ASSERT_EQ(params.launch_files.size(), 1U);
EXPECT_EQ("foo.txt", params.launch_files[0].MaybeAsASCII());
#else
ASSERT_EQ(params.launch_files.size(), 0U);
#endif // BUILDFLAG(IS_CHROMEOS)
}
TEST_F(LaunchUtilsTest, GetLaunchFilesFromCommandLine_NoAppID) {
// Validate an empty vector is returned if there is
// no AppID specified on the command line.
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
std::vector<base::FilePath> launch_files =
apps::GetLaunchFilesFromCommandLine(command_line);
EXPECT_EQ(launch_files.size(), 0U);
}
TEST_F(LaunchUtilsTest, GetLaunchFilesFromCommandLine_NoFiles) {
// Validate an empty vector is returned if there are
// no files specified on the command line.
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitchASCII(switches::kAppId, "test");
std::vector<base::FilePath> launch_files =
apps::GetLaunchFilesFromCommandLine(command_line);
EXPECT_EQ(launch_files.size(), 0U);
}
TEST_F(LaunchUtilsTest, GetLaunchFilesFromCommandLine_SingleFile) {
// Validate a vector with size 1 is returned, and the
// contents match the command line parameter.
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitchASCII(switches::kAppId, "test");
command_line.AppendArg("filename");
std::vector<base::FilePath> launch_files =
apps::GetLaunchFilesFromCommandLine(command_line);
ASSERT_EQ(launch_files.size(), 1U);
EXPECT_EQ(launch_files[0], base::FilePath(FILE_PATH_LITERAL("filename")));
}
TEST_F(LaunchUtilsTest, GetLaunchFilesFromCommandLine_MultipleFiles) {
// Validate a vector with size 2 is returned, and the
// contents match the command line parameter.
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitchASCII(switches::kAppId, "test");
command_line.AppendArg("filename");
command_line.AppendArg("filename2");
std::vector<base::FilePath> launch_files =
apps::GetLaunchFilesFromCommandLine(command_line);
ASSERT_EQ(launch_files.size(), 2U);
EXPECT_EQ(launch_files[0], base::FilePath(FILE_PATH_LITERAL("filename")));
EXPECT_EQ(launch_files[1], base::FilePath(FILE_PATH_LITERAL("filename2")));
}
TEST_F(LaunchUtilsTest, GetLaunchFilesFromCommandLine_FileProtocol) {
// Validate a vector with size 1 is returned, and the
// contents match the command line parameter. This uses
// the file protocol to reference the file.
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitchASCII(switches::kAppId, "test");
command_line.AppendArg("file://filename");
std::vector<base::FilePath> launch_files =
apps::GetLaunchFilesFromCommandLine(command_line);
ASSERT_EQ(launch_files.size(), 1U);
EXPECT_EQ(launch_files[0],
base::FilePath(FILE_PATH_LITERAL("file://filename")));
}
// Verifies that a non-file protocol is not treated as a filename.
TEST_F(LaunchUtilsTest, GetLaunchFilesFromCommandLine_CustomProtocol) {
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitchASCII(switches::kAppId, "test");
command_line.AppendArg("web+test://filename");
std::vector<base::FilePath> launch_files =
apps::GetLaunchFilesFromCommandLine(command_line);
EXPECT_EQ(0U, launch_files.size());
}
#if BUILDFLAG(IS_CHROMEOS)
// Fake AppPublisher for tracking app launches.
class FakePublisher : public AppPublisher {
public:
explicit FakePublisher(AppServiceProxy* proxy) : AppPublisher(proxy) {
RegisterPublisher(AppType::kWeb);
}
void PublishAppWithUrlScope(std::string app_id, GURL scope) {
AppPtr app = std::make_unique<App>(AppType::kWeb, app_id);
app->readiness = Readiness::kReady;
app->handles_intents = true;
app->intent_filters.push_back(apps_util::MakeIntentFilterForUrlScope(
scope, /*omit_port_for_testing=*/true));
std::vector<AppPtr> apps;
apps.push_back(std::move(app));
AppPublisher::Publish(std::move(apps), AppType::kWeb,
/*should_notify_initialized=*/true);
}
const std::string& last_launched_app() { return last_launched_app_; }
// AppPublisher:
void Launch(const std::string& app_id,
int32_t event_flags,
LaunchSource launch_source,
WindowInfoPtr window_info) override {}
void LaunchAppWithParams(AppLaunchParams&& params,
LaunchCallback callback) override {}
void LaunchAppWithIntent(const std::string& app_id,
int32_t event_flags,
IntentPtr intent,
LaunchSource launch_source,
WindowInfoPtr window_info,
LaunchCallback callback) override {
last_launched_app_ = app_id;
}
private:
std::string last_launched_app_;
};
class MockNewWindowDelegate
: public testing::NiceMock<ash::TestNewWindowDelegate> {
public:
// TestNewWindowDelegate:
MOCK_METHOD(void,
OpenUrl,
(const GURL& url, OpenUrlFrom from, Disposition disposition),
(override));
};
class LaunchUtilsNewWindowTest : public LaunchUtilsTest {
public:
MockNewWindowDelegate& new_window_delegate() { return new_window_delegate_; }
private:
MockNewWindowDelegate new_window_delegate_;
};
TEST_F(LaunchUtilsNewWindowTest,
MaybeLaunchPreferredAppForUrl_LaunchesPreferred) {
FakePublisher publisher(AppServiceProxyFactory::GetForProfile(&profile_));
publisher.PublishAppWithUrlScope("abc", GURL("https://www.example.com/"));
ASSERT_EQ(test::EnableLinkCapturingByUser(&profile_, "abc"), base::ok());
MaybeLaunchPreferredAppForUrl(&profile_, GURL("https://www.example.com/foo/"),
LaunchSource::kFromTest);
ASSERT_EQ(publisher.last_launched_app(), "abc");
}
TEST_F(LaunchUtilsNewWindowTest,
MaybeLaunchPreferredAppForUrl_LaunchesBrowserIfNoPreferredApp) {
FakePublisher publisher(AppServiceProxyFactory::GetForProfile(&profile_));
// Do not mark this app as preferred. The browser should be opened instead.
publisher.PublishAppWithUrlScope("abc", GURL("https://www.example.com/"));
EXPECT_CALL(
new_window_delegate(),
OpenUrl(GURL("https://www.example.com/foo/"), testing::_, testing::_));
MaybeLaunchPreferredAppForUrl(&profile_, GURL("https://www.example.com/foo/"),
LaunchSource::kFromTest);
}
TEST_F(LaunchUtilsNewWindowTest, LaunchUrlInInstalledAppOrBrowser_OneApp) {
FakePublisher publisher(AppServiceProxyFactory::GetForProfile(&profile_));
publisher.PublishAppWithUrlScope("abc", GURL("https://www.example.com/"));
LaunchUrlInInstalledAppOrBrowser(
&profile_, GURL("https://www.example.com/foo"), LaunchSource::kFromTest);
ASSERT_EQ(publisher.last_launched_app(), "abc");
}
TEST_F(LaunchUtilsNewWindowTest,
LaunchUrlInInstalledAppOrBrowser_TwoAppsNoPreferred) {
FakePublisher publisher(AppServiceProxyFactory::GetForProfile(&profile_));
publisher.PublishAppWithUrlScope("abc", GURL("https://www.example.com/"));
publisher.PublishAppWithUrlScope("def", GURL("https://www.example.com/app/"));
EXPECT_CALL(
new_window_delegate(),
OpenUrl(GURL("https://www.example.com/app/"), testing::_, testing::_));
LaunchUrlInInstalledAppOrBrowser(
&profile_, GURL("https://www.example.com/app/"), LaunchSource::kFromTest);
}
TEST_F(LaunchUtilsNewWindowTest,
LaunchUrlInInstalledAppOrBrowser_MultipleApps_LaunchesPreferred) {
FakePublisher publisher(AppServiceProxyFactory::GetForProfile(&profile_));
publisher.PublishAppWithUrlScope("abc", GURL("https://www.example.com/"));
publisher.PublishAppWithUrlScope("def", GURL("https://www.example.com/app/"));
// Enable the link capturing preference for one of the apps. This app should
// be launched by `LaunchUrlInInstalledAppOrBrowser`.
ASSERT_EQ(test::EnableLinkCapturingByUser(&profile_, "def"), base::ok());
LaunchUrlInInstalledAppOrBrowser(
&profile_, GURL("https://www.example.com/app/"), LaunchSource::kFromTest);
ASSERT_EQ(publisher.last_launched_app(), "def");
}
TEST_F(LaunchUtilsNewWindowTest,
LaunchUrlInInstalledAppOrBrowser_NoApp_LaunchesInBrowser) {
EXPECT_CALL(new_window_delegate(),
OpenUrl(GURL("https://www.example.com"), testing::_, testing::_));
LaunchUrlInInstalledAppOrBrowser(&profile_, GURL("https://www.example.com/"),
LaunchSource::kFromTest);
}
#endif // BUILDFLAG(IS_CHROMEOS)
} // namespace apps
|