Commit 8aa42978 authored by Lukasz Anforowicz's avatar Lukasz Anforowicz Committed by Commit Bot

Avoid using LoadDataWithBaseURL in browser tests.

LoadDataWithBaseURL should be avoided in browser tests, because
1. It doesn't fully, correctly simulate navigating to a real website.
2. It is only used in production in Android WebView and in GuestView
   (both of which don't run with site-per-process, which is the default
    mode on desktop platforms).

Cleaning up test usage of LoadDataWithBaseURL will help proceed with
enforcement of CanCommit origin checks - LoadDataWithBaseURL is
problematic because it commits https://foo.com origin in a SiteInstance
associated with data: site URL.

Bug: 770239
Change-Id: Ic68697be28cace0d8ff8abc0951e12cb5f81ac68
Reviewed-on: https://chromium-review.googlesource.com/c/1385471Reviewed-by: default avatarNasko Oskov <nasko@chromium.org>
Commit-Queue: Łukasz Anforowicz <lukasza@chromium.org>
Cr-Commit-Position: refs/heads/master@{#618336}
parent acb62c29
......@@ -2,7 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/browser/display_cutout/display_cutout_constants.h"
......@@ -19,6 +22,7 @@
#include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/mojom/page/display_cutout.mojom.h"
......@@ -99,7 +103,7 @@ class DisplayCutoutWebContentsDelegate : public WebContentsDelegate {
const char kTestHTML[] =
"<!DOCTYPE html>"
"<style>"
" %23target {"
" #target {"
" margin-top: env(safe-area-inset-top);"
" margin-left: env(safe-area-inset-left);"
" margin-bottom: env(safe-area-inset-bottom);"
......@@ -119,6 +123,15 @@ class DisplayCutoutBrowserTest : public ContentBrowserTest {
"DisplayCutoutAPI");
}
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
embedded_test_server()->ServeFilesFromDirectory(temp_dir_.GetPath());
ASSERT_TRUE(embedded_test_server()->Start());
ContentBrowserTest::SetUp();
}
void LoadTestPageWithViewportFitFromMeta(const std::string& value) {
LoadTestPageWithData(
"<!DOCTYPE html>"
......@@ -135,14 +148,8 @@ class DisplayCutoutBrowserTest : public ContentBrowserTest {
FrameTreeNode* root = web_contents_impl()->GetFrameTree()->root();
FrameTreeNode* child = root->child_at(0);
TestFrameNavigationObserver observer(child);
NavigationController::LoadURLParams params(GURL::EmptyGURL());
params.url = GURL(data);
params.frame_tree_node_id = child->frame_tree_node_id();
params.load_type = NavigationController::LOAD_TYPE_DATA;
web_contents_impl()->GetController().LoadURLWithParams(params);
ASSERT_TRUE(NavigateToURLFromRenderer(child, GURL(data)));
web_contents_impl()->Focus();
observer.Wait();
}
bool ClearViewportFitTag() {
......@@ -175,16 +182,22 @@ class DisplayCutoutBrowserTest : public ContentBrowserTest {
}
void LoadTestPageWithData(const std::string& data) {
GURL url("https://www.example.com");
ResetUKM();
// Write |data| to a temporary file that can be later reached at
// http://127.0.0.1/test_file_*.html.
static int s_test_file_number = 1;
base::FilePath file_path = temp_dir_.GetPath().AppendASCII(
base::StringPrintf("test_file_%d.html", s_test_file_number++));
{
base::ScopedAllowBlockingForTesting allow_temp_file_writing;
ASSERT_EQ(static_cast<int>(data.length()),
base::WriteFile(file_path, data.c_str(), data.length()));
}
GURL url = embedded_test_server()->GetURL(
"/" + file_path.BaseName().AsUTF8Unsafe());
TestNavigationObserver same_tab_observer(shell()->web_contents(), 1);
#if defined(OS_ANDROID)
shell()->LoadDataAsStringWithBaseURL(url, data, url);
#else
shell()->LoadDataWithBaseURL(url, data, url);
#endif
same_tab_observer.Wait();
// Reset UKM and navigate to the html file created above.
ResetUKM();
ASSERT_TRUE(NavigateToURL(shell(), url));
}
void SimulateFullscreenStateChanged(RenderFrameHost* frame,
......@@ -245,6 +258,7 @@ class DisplayCutoutBrowserTest : public ContentBrowserTest {
test_ukm_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>();
}
base::ScopedTempDir temp_dir_;
std::unique_ptr<ukm::TestUkmRecorder> test_ukm_recorder_;
DISALLOW_COPY_AND_ASSIGN(DisplayCutoutBrowserTest);
......
......@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/scoped_temp_dir.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread_restrictions.h"
......@@ -9,6 +10,7 @@
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/blink/public/common/features.h"
#include "ui/shell_dialogs/select_file_dialog.h"
#include "ui/shell_dialogs/select_file_dialog_factory.h"
......@@ -24,6 +26,9 @@ class FileSystemChooserBrowserTest : public ContentBrowserTest {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
scoped_feature_list_.InitAndEnableFeature(
blink::features::kWritableFilesAPI);
ASSERT_TRUE(embedded_test_server()->Start());
ContentBrowserTest::SetUp();
}
......@@ -50,10 +55,6 @@ class FileSystemChooserBrowserTest : public ContentBrowserTest {
return result;
}
protected:
const std::string kBlankHtml = "<!DOCTYPE html><html><body>";
const GURL kTestUrl = GURL("https://foobar.com/");
private:
base::test::ScopedFeatureList scoped_feature_list_;
base::ScopedTempDir temp_dir_;
......@@ -61,7 +62,8 @@ class FileSystemChooserBrowserTest : public ContentBrowserTest {
IN_PROC_BROWSER_TEST_F(FileSystemChooserBrowserTest, CancelDialog) {
ui::SelectFileDialog::SetFactory(new CancellingSelectFileDialogFactory);
LoadDataWithBaseURL(shell(), kTestUrl, kBlankHtml, kTestUrl);
ASSERT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")));
auto result = EvalJs(shell(), "self.chooseFileSystemEntries()");
EXPECT_TRUE(result.error.find("AbortError") != std::string::npos)
<< result.error;
......@@ -73,7 +75,8 @@ IN_PROC_BROWSER_TEST_F(FileSystemChooserBrowserTest, OpenFile) {
SelectFileDialogParams dialog_params;
ui::SelectFileDialog::SetFactory(
new FakeSelectFileDialogFactory({test_file}, &dialog_params));
LoadDataWithBaseURL(shell(), kTestUrl, kBlankHtml, kTestUrl);
ASSERT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")));
EXPECT_EQ(test_file.BaseName().AsUTF8Unsafe(),
EvalJs(shell(),
"(async () => {"
......@@ -100,7 +103,8 @@ IN_PROC_BROWSER_TEST_F(FileSystemChooserBrowserTest, SaveFile) {
SelectFileDialogParams dialog_params;
ui::SelectFileDialog::SetFactory(
new FakeSelectFileDialogFactory({test_file}, &dialog_params));
LoadDataWithBaseURL(shell(), kTestUrl, kBlankHtml, kTestUrl);
ASSERT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")));
EXPECT_EQ(test_file.BaseName().AsUTF8Unsafe(),
EvalJs(shell(),
"(async () => {"
......@@ -130,7 +134,8 @@ IN_PROC_BROWSER_TEST_F(FileSystemChooserBrowserTest, OpenMultipleFiles) {
SelectFileDialogParams dialog_params;
ui::SelectFileDialog::SetFactory(new FakeSelectFileDialogFactory(
{test_file1, test_file2}, &dialog_params));
LoadDataWithBaseURL(shell(), kTestUrl, kBlankHtml, kTestUrl);
ASSERT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")));
EXPECT_EQ(ListValueOf(test_file1.BaseName().AsUTF8Unsafe(),
test_file2.BaseName().AsUTF8Unsafe()),
EvalJs(shell(),
......@@ -146,7 +151,8 @@ IN_PROC_BROWSER_TEST_F(FileSystemChooserBrowserTest, OpenDirectory) {
SelectFileDialogParams dialog_params;
ui::SelectFileDialog::SetFactory(
new FakeSelectFileDialogFactory({test_dir}, &dialog_params));
LoadDataWithBaseURL(shell(), kTestUrl, kBlankHtml, kTestUrl);
ASSERT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")));
EXPECT_EQ(test_dir.BaseName().AsUTF8Unsafe(),
EvalJs(shell(),
"(async () => {"
......@@ -161,7 +167,8 @@ IN_PROC_BROWSER_TEST_F(FileSystemChooserBrowserTest, AcceptsOptions) {
SelectFileDialogParams dialog_params;
ui::SelectFileDialog::SetFactory(
new CancellingSelectFileDialogFactory(&dialog_params));
LoadDataWithBaseURL(shell(), kTestUrl, kBlankHtml, kTestUrl);
ASSERT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")));
auto result = EvalJs(shell(),
"self.chooseFileSystemEntries({accepts: ["
" {description: 'no-extensions'},"
......@@ -192,4 +199,4 @@ IN_PROC_BROWSER_TEST_F(FileSystemChooserBrowserTest, AcceptsOptions) {
dialog_params.file_types->extension_description_overrides[1]);
}
} // namespace content
\ No newline at end of file
} // namespace content
......@@ -23,25 +23,34 @@ class MessagePortProviderBrowserTest : public ContentBrowserTest {
// Verify that messages can be posted to main frame.
IN_PROC_BROWSER_TEST_F(MessagePortProviderBrowserTest, PostMessage) {
const std::string data =
"<!DOCTYPE html><html><body>"
" <script type=\"text/javascript\">"
" onmessage = function (e) { document.title = e.data; }"
" </script>"
"</body></html>";
const base::string16 target_origin(base::UTF8ToUTF16("http://baseurl"));
const GURL base_url(target_origin);
const GURL history_url;
// Load data. Blocks until it is done.
content::LoadDataWithBaseURL(shell(), history_url, data, base_url);
const base::string16 source_origin(base::UTF8ToUTF16("source"));
const base::string16 message(base::UTF8ToUTF16("success"));
content::TitleWatcher title_watcher(shell()->web_contents(), message);
MessagePortProvider::PostMessageToFrame(shell()->web_contents(),
source_origin,
target_origin,
message);
EXPECT_EQ(message, title_watcher.WaitAndGetTitle());
// Listen for a message.
ASSERT_TRUE(embedded_test_server()->Start());
GURL url(embedded_test_server()->GetURL("/title1.html"));
EXPECT_TRUE(NavigateToURL(shell(), url));
EXPECT_TRUE(ExecuteScript(shell(), R"(
onmessage = function(e) {
domAutomationController.send(e.origin + ':' + e.data);
} )"));
// Post a message.
const std::string target_origin(url.GetOrigin().spec());
const std::string source_origin("https://source.origin.com");
const std::string message("success");
DOMMessageQueue msg_queue;
MessagePortProvider::PostMessageToFrame(
shell()->web_contents(), base::UTF8ToUTF16(source_origin),
base::UTF8ToUTF16(target_origin), base::UTF8ToUTF16(message));
// Verify that the message was received (and had the expected payload).
std::string expected_test_reply;
expected_test_reply += '"';
expected_test_reply += source_origin;
expected_test_reply += ':';
expected_test_reply += message;
expected_test_reply += '"';
std::string actual_test_reply;
EXPECT_TRUE(msg_queue.WaitForMessage(&actual_test_reply));
EXPECT_EQ(expected_test_reply, actual_test_reply);
}
} // namespace content
......@@ -74,17 +74,6 @@ void ReloadBypassingCacheBlockUntilNavigationsComplete(
same_tab_observer.Wait();
}
void LoadDataWithBaseURL(Shell* window,
const GURL& url,
const std::string& data,
const GURL& base_url) {
WaitForLoadStop(window->web_contents());
TestNavigationObserver same_tab_observer(window->web_contents(), 1);
window->LoadDataWithBaseURL(url, data, base_url);
same_tab_observer.Wait();
}
bool NavigateToURL(Shell* window, const GURL& url) {
return NavigateToURL(window->web_contents(), url);
}
......@@ -93,7 +82,7 @@ bool NavigateToURLFromRenderer(const ToRenderFrameHost& adapter,
const GURL& url) {
RenderFrameHost* rfh = adapter.render_frame_host();
TestFrameNavigationObserver nav_observer(rfh);
if (!ExecuteScript(rfh, "location = '" + url.spec() + "';"))
if (!ExecJs(rfh, JsReplace("location = $1", url)))
return false;
nav_observer.Wait();
return nav_observer.last_committed_url() == url;
......@@ -104,8 +93,8 @@ bool NavigateToURLFromRendererWithoutUserGesture(
const GURL& url) {
RenderFrameHost* rfh = adapter.render_frame_host();
TestFrameNavigationObserver nav_observer(rfh);
if (!ExecuteScriptWithoutUserGesture(rfh,
"location = '" + url.spec() + "';")) {
if (!ExecJs(rfh, JsReplace("location = $1", url),
EXECUTE_SCRIPT_NO_USER_GESTURE)) {
return false;
}
nav_observer.Wait();
......
......@@ -80,11 +80,6 @@ WARN_UNUSED_RESULT bool NavigateToURLFromRendererWithoutUserGesture(
const ToRenderFrameHost& adapter,
const GURL& url);
void LoadDataWithBaseURL(Shell* window,
const GURL& url,
const std::string& data,
const GURL& base_url);
// Navigates |window| to |url|, blocking until the given number of navigations
// finishes.
void NavigateToURLBlockUntilNavigationsComplete(Shell* window,
......
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