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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "build/build_config.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "net/base/filename_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/mojom/frame/frame.mojom.h"
#include "third_party/blink/public/platform/web_url.h"
#include "third_party/blink/public/web/web_frame.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_savable_resources_test_support.h"
namespace content {
class SavableResourcesTest : public ContentBrowserTest {
public:
using UrlVectorMatcher = testing::Matcher<std::vector<GURL>>;
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(switches::kSingleProcess);
#if BUILDFLAG(IS_WIN)
// Don't want to try to create a GPU process.
command_line->AppendSwitch(switches::kDisableGpu);
#endif
}
// Test function GetAllSavableResourceLinksForCurrentPage with a web page.
// We expect result of GetAllSavableResourceLinksForCurrentPage exactly
// matches expected_resources_set.
void GetSavableResourceLinksForPage(
const base::FilePath& page_file_path,
const UrlVectorMatcher& expected_resources_matcher,
const UrlVectorMatcher& expected_subframe_urls_matcher) {
// Convert local file path to file URL.
GURL file_url = net::FilePathToFileURL(page_file_path);
// Load the test file.
EXPECT_TRUE(NavigateToURL(shell(), file_url));
PostTaskToInProcessRendererAndWait(base::BindOnce(
&SavableResourcesTest::CheckResources, base::Unretained(this),
page_file_path, expected_resources_matcher,
expected_subframe_urls_matcher, file_url,
shell()->web_contents()->GetPrimaryMainFrame()->GetRoutingID()));
}
void CheckResources(const base::FilePath& page_file_path,
const UrlVectorMatcher& expected_resources_matcher,
const UrlVectorMatcher& expected_subframe_urls_matcher,
const GURL& file_url,
int render_frame_routing_id) {
RenderFrame* render_frame =
RenderFrame::FromRoutingID(render_frame_routing_id);
mojo::AssociatedRemote<blink::mojom::LocalFrame> local_frame;
render_frame->GetRemoteAssociatedInterfaces()->GetInterface(
local_frame.BindNewEndpointAndPassReceiver());
local_frame->GetSavableResourceLinks(
base::BindOnce(&SavableResourcesTest::GetSavableResourceLinksCallback,
base::Unretained(this), expected_resources_matcher,
expected_subframe_urls_matcher));
}
void GetSavableResourceLinksCallback(
const UrlVectorMatcher& expected_resources_matcher,
const UrlVectorMatcher& expected_subframe_urls_matcher,
blink::mojom::GetSavableResourceLinksReplyPtr reply) {
if (!reply) {
DCHECK(false)
<< "blink::mojom::GetSavableResourceLinks returnes nullptr.";
return;
}
std::vector<GURL> resources_list;
for (auto url : reply->resources_list)
resources_list.push_back(url);
EXPECT_THAT(resources_list, expected_resources_matcher);
std::vector<GURL> subframe_original_urls;
for (auto& subframe : reply->subframes) {
subframe_original_urls.push_back(subframe->original_url);
}
EXPECT_THAT(subframe_original_urls, expected_subframe_urls_matcher);
}
};
// Flaky on Linux MSan. See crbug.com/1423060.
#if BUILDFLAG(IS_LINUX) && defined(MEMORY_SANITIZER)
#define MAYBE_GetSavableResourceLinksWithPageHasValidStyleLink \
DISABLED_GetSavableResourceLinksWithPageHasValidStyleLink
#else
#define MAYBE_GetSavableResourceLinksWithPageHasValidStyleLink \
GetSavableResourceLinksWithPageHasValidStyleLink
#endif
IN_PROC_BROWSER_TEST_F(SavableResourcesTest,
MAYBE_GetSavableResourceLinksWithPageHasValidStyleLink) {
base::FilePath page_file_path =
GetTestFilePath("dom_serializer", "simple_linked_stylesheet.html");
auto expected_subresources_matcher = testing::UnorderedElementsAre(
net::FilePathToFileURL(GetTestFilePath("dom_serializer", "style.css")));
auto expected_subframe_urls_matcher = testing::IsEmpty();
GetSavableResourceLinksForPage(page_file_path, expected_subresources_matcher,
expected_subframe_urls_matcher);
}
// Test function GetAllSavableResourceLinksForCurrentPage with a web page
// which has valid savable resource links.
// Flaky on Linux MSan. See crbug.com/1423060.
#if BUILDFLAG(IS_LINUX) && defined(MEMORY_SANITIZER)
#define MAYBE_GetSavableResourceLinksWithPageHasValidLinks \
DISABLED_GetSavableResourceLinksWithPageHasValidLinks
#else
#define MAYBE_GetSavableResourceLinksWithPageHasValidLinks \
GetSavableResourceLinksWithPageHasValidLinks
#endif
IN_PROC_BROWSER_TEST_F(SavableResourcesTest,
MAYBE_GetSavableResourceLinksWithPageHasValidLinks) {
base::FilePath page_file_path =
GetTestFilePath("dom_serializer", "youtube_1.htm");
auto expected_subresources_matcher = testing::UnorderedElementsAre(
GURL("file:///c:/yt/css/base_all-vfl36460.css"),
GURL("file:///c:/yt/js/base_all_with_bidi-vfl36451.js"),
GURL("file:///c:/yt/img/pixel-vfl73.gif"));
auto expected_subframe_urls_matcher =
testing::UnorderedElementsAre(net::FilePathToFileURL(
GetTestFilePath("dom_serializer", "youtube_2.htm")));
GetSavableResourceLinksForPage(page_file_path, expected_subresources_matcher,
expected_subframe_urls_matcher);
}
// Test function GetAllSavableResourceLinksForCurrentPage with a web page
// which does not have valid savable resource links.
// Flaky on Linux MSan. See crbug.com/1423060.
#if BUILDFLAG(IS_LINUX) && defined(MEMORY_SANITIZER)
#define MAYBE_GetSavableResourceLinksWithPageHasInvalidLinks \
DISABLED_GetSavableResourceLinksWithPageHasInvalidLinks
#else
#define MAYBE_GetSavableResourceLinksWithPageHasInvalidLinks \
GetSavableResourceLinksWithPageHasInvalidLinks
#endif
IN_PROC_BROWSER_TEST_F(SavableResourcesTest,
MAYBE_GetSavableResourceLinksWithPageHasInvalidLinks) {
base::FilePath page_file_path =
GetTestFilePath("dom_serializer", "youtube_2.htm");
auto expected_subresources_matcher = testing::IsEmpty();
auto expected_subframe_urls_matcher = testing::IsEmpty();
GetSavableResourceLinksForPage(page_file_path, expected_subresources_matcher,
expected_subframe_urls_matcher);
}
} // namespace content
|