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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/apps/link_capturing/link_capturing_feature_test_support.h"
#include "chrome/browser/sharesheet/sharesheet_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/ui/web_applications/web_app_browsertest_base.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "url/gurl.h"
namespace {
content::EvalJsResult ReadTextContent(content::WebContents* web_contents,
const char* id) {
const std::string script =
base::StringPrintf("document.getElementById('%s').textContent", id);
return content::EvalJs(web_contents, script);
}
class ScopedSharesheetAppSelection {
public:
explicit ScopedSharesheetAppSelection(const std::string& app_id) {
SetSelectedSharesheetApp(app_id);
}
ScopedSharesheetAppSelection(const ScopedSharesheetAppSelection&) = delete;
ScopedSharesheetAppSelection& operator=(const ScopedSharesheetAppSelection&) =
delete;
~ScopedSharesheetAppSelection() { SetSelectedSharesheetApp(std::string()); }
private:
static void SetSelectedSharesheetApp(const std::string& app_id) {
sharesheet::SharesheetService::SetSelectedAppForTesting(
base::UTF8ToUTF16(app_id));
}
};
} // namespace
namespace web_app {
class ShareToTargetBrowserTest : public WebAppBrowserTestBase,
public testing::WithParamInterface<
apps::test::LinkCapturingFeatureVersion> {
public:
ShareToTargetBrowserTest() {
scoped_feature_list_.InitWithFeaturesAndParameters(
apps::test::GetFeaturesToEnableLinkCapturingUX(GetParam()), {});
}
std::string ExecuteShare(const std::string& script) {
const GURL url = https_server()->GetURL("/webshare/index.html");
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
content::WebContents* const contents =
browser()->tab_strip_model()->GetActiveWebContents();
return content::EvalJs(contents, script).ExtractString();
}
content::WebContents* ShareToTarget(const std::string& script) {
ui_test_utils::AllBrowserTabAddedWaiter waiter;
EXPECT_EQ("share succeeded", ExecuteShare(script));
content::WebContents* contents = waiter.Wait();
EXPECT_TRUE(content::WaitForLoadStop(contents));
// For Ash builds, we could verify no files have been added to Recent Files.
return contents;
}
void InstallWebAppFromManifest(const GURL& app_url) {
DCHECK(app_id_.empty());
app_id_ = web_app::InstallWebAppFromManifest(browser(), app_url);
// Enabling link capturing to ensure it doesn't interfere.
EXPECT_EQ(apps::test::EnableLinkCapturingByUser(profile(), app_id_),
base::ok());
}
const webapps::AppId& app_id() const { return app_id_; }
private:
// WebAppBrowserTestBase:
void TearDownOnMainThread() override {
if (!app_id_.empty()) {
CloseAppWindows(app_id_);
}
WebAppBrowserTestBase::TearDownOnMainThread();
}
static void CloseAppWindows(const webapps::AppId& app_id) {
for (Browser* browser : *BrowserList::GetInstance()) {
const AppBrowserController* app_controller = browser->app_controller();
if (app_controller && app_controller->app_id() == app_id) {
browser->window()->Close();
}
}
}
webapps::AppId app_id_;
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_P(ShareToTargetBrowserTest, ShareToPosterWebApp) {
const GURL app_url = https_server()->GetURL("/web_share_target/poster.html");
InstallWebAppFromManifest(app_url);
ScopedSharesheetAppSelection selection(app_id());
// Poster web app does not accept image shares.
EXPECT_EQ("share failed: AbortError: Share canceled",
ExecuteShare("share_single_file()"));
content::WebContents* web_contents = ShareToTarget("share_title()");
EXPECT_EQ("Subject", ReadTextContent(web_contents, "headline"));
web_contents = ShareToTarget("share_url()");
EXPECT_EQ("https://example.com/", ReadTextContent(web_contents, "link"));
}
IN_PROC_BROWSER_TEST_P(ShareToTargetBrowserTest, ShareToChartsWebApp) {
const GURL app_url = https_server()->GetURL("/web_share_target/charts.html");
InstallWebAppFromManifest(app_url);
ScopedSharesheetAppSelection selection(app_id());
content::WebContents* web_contents = ShareToTarget("share_single_file()");
EXPECT_EQ("************", ReadTextContent(web_contents, "notes"));
web_contents = ShareToTarget("share_url()");
EXPECT_EQ("https://example.com/", ReadTextContent(web_contents, "link"));
}
IN_PROC_BROWSER_TEST_P(ShareToTargetBrowserTest, ShareImage) {
const GURL app_url =
https_server()->GetURL("/web_share_target/multimedia.html");
InstallWebAppFromManifest(app_url);
ScopedSharesheetAppSelection selection(app_id());
content::WebContents* web_contents = ShareToTarget("share_single_file()");
EXPECT_EQ(std::string(12, '*'), ReadTextContent(web_contents, "image"));
EXPECT_EQ("sample.webp", ReadTextContent(web_contents, "image_filename"));
}
IN_PROC_BROWSER_TEST_P(ShareToTargetBrowserTest, ShareMultimedia) {
const GURL app_url =
https_server()->GetURL("/web_share_target/multimedia.html");
InstallWebAppFromManifest(app_url);
ScopedSharesheetAppSelection selection(app_id());
content::WebContents* web_contents = ShareToTarget("share_multiple_files()");
EXPECT_EQ(std::string(345, '*'), ReadTextContent(web_contents, "audio"));
EXPECT_EQ(std::string(67890, '*'), ReadTextContent(web_contents, "video"));
EXPECT_EQ(std::string(1, '*'), ReadTextContent(web_contents, "image"));
EXPECT_EQ("sam.ple.mp3", ReadTextContent(web_contents, "audio_filename"));
EXPECT_EQ("sample.mp4", ReadTextContent(web_contents, "video_filename"));
EXPECT_EQ("sam_ple.gif", ReadTextContent(web_contents, "image_filename"));
}
IN_PROC_BROWSER_TEST_P(ShareToTargetBrowserTest, ShareToPartialWild) {
const GURL app_url =
https_server()->GetURL("/web_share_target/partial-wild.html");
InstallWebAppFromManifest(app_url);
ScopedSharesheetAppSelection selection(app_id());
// Partial Wild does not accept text shares.
EXPECT_EQ("share failed: AbortError: Share canceled",
ExecuteShare("share_title()"));
content::WebContents* web_contents = ShareToTarget("share_single_file()");
EXPECT_EQ("************", ReadTextContent(web_contents, "graphs"));
}
INSTANTIATE_TEST_SUITE_P(
All,
ShareToTargetBrowserTest,
// Ensure share target still works with navigation capturing v2.
testing::Values(apps::test::LinkCapturingFeatureVersion::kV1DefaultOff,
apps::test::LinkCapturingFeatureVersion::kV2DefaultOff,
apps::test::LinkCapturingFeatureVersion::
kV2DefaultOffCaptureExistingFrames),
apps::test::LinkCapturingVersionToString);
} // namespace web_app
|