blob: ceade9d4c24b5192ec410089b731883f4fd33cb9 [file] [log] [blame]
[email protected]c1978abe2013-04-23 03:08:121// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/bind.h"
6#include "base/command_line.h"
7#include "base/files/file_path.h"
avi1023d012015-12-25 02:39:148#include "build/build_config.h"
lukasza6af746b72015-09-18 23:37:229#include "content/public/browser/render_frame_host.h"
[email protected]c1978abe2013-04-23 03:08:1210#include "content/public/browser/web_contents.h"
11#include "content/public/common/content_switches.h"
lukasza6af746b72015-09-18 23:37:2212#include "content/public/renderer/render_frame.h"
[email protected]6e9def12014-03-27 20:23:2813#include "content/public/test/content_browser_test.h"
14#include "content/public/test/content_browser_test_utils.h"
[email protected]12a936d2013-05-15 04:55:4915#include "content/renderer/savable_resources.h"
[email protected]de7d61ff2013-08-20 11:30:4116#include "content/shell/browser/shell.h"
[email protected]d96cf752014-04-09 04:05:2817#include "net/base/filename_util.h"
lukaszafe911df2015-10-21 21:06:1918#include "testing/gmock/include/gmock/gmock.h"
19#include "testing/gtest/include/gtest/gtest.h"
Blink Reformata30d4232018-04-07 15:31:0620#include "third_party/blink/public/web/web_frame.h"
21#include "third_party/blink/public/web/web_local_frame.h"
[email protected]c1978abe2013-04-23 03:08:1222
23namespace content {
24
[email protected]12a936d2013-05-15 04:55:4925class SavableResourcesTest : public ContentBrowserTest {
[email protected]c1978abe2013-04-23 03:08:1226 public:
lukaszafe911df2015-10-21 21:06:1927 using UrlVectorMatcher = testing::Matcher<std::vector<GURL>>;
28
avi83883c82014-12-23 00:08:4929 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]c1978abe2013-04-23 03:08:1230 command_line->AppendSwitch(switches::kSingleProcess);
[email protected]0c2c0d12014-02-04 17:24:4331#if defined(OS_WIN)
[email protected]5d97f762013-04-23 06:15:4932 // Don't want to try to create a GPU process.
[email protected]7af65ca2014-04-16 20:25:5633 command_line->AppendSwitch(switches::kDisableGpu);
[email protected]5d97f762013-04-23 06:15:4934#endif
[email protected]c1978abe2013-04-23 03:08:1235 }
36
37 // Test function GetAllSavableResourceLinksForCurrentPage with a web page.
38 // We expect result of GetAllSavableResourceLinksForCurrentPage exactly
39 // matches expected_resources_set.
40 void GetSavableResourceLinksForPage(
41 const base::FilePath& page_file_path,
lukasza87f4e2ab2015-11-04 16:03:0342 const UrlVectorMatcher& expected_resources_matcher,
43 const UrlVectorMatcher& expected_subframe_urls_matcher) {
[email protected]c1978abe2013-04-23 03:08:1244 // Convert local file path to file URL.
45 GURL file_url = net::FilePathToFileURL(page_file_path);
46 // Load the test file.
Alex Moshchukaeb20fe32019-09-25 17:40:0147 EXPECT_TRUE(NavigateToURL(shell(), file_url));
[email protected]c1978abe2013-04-23 03:08:1248
danakjab3475802019-05-22 19:05:1749 PostTaskToInProcessRendererAndWait(base::BindOnce(
lukasza6af746b72015-09-18 23:37:2250 &SavableResourcesTest::CheckResources, base::Unretained(this),
lukasza87f4e2ab2015-11-04 16:03:0351 page_file_path, expected_resources_matcher,
52 expected_subframe_urls_matcher, file_url,
lukasza6af746b72015-09-18 23:37:2253 shell()->web_contents()->GetMainFrame()->GetRoutingID()));
[email protected]c1978abe2013-04-23 03:08:1254 }
55
56 void CheckResources(const base::FilePath& page_file_path,
lukaszafe911df2015-10-21 21:06:1957 const UrlVectorMatcher& expected_resources_matcher,
lukasza87f4e2ab2015-11-04 16:03:0358 const UrlVectorMatcher& expected_subframe_urls_matcher,
[email protected]c1978abe2013-04-23 03:08:1259 const GURL& file_url,
lukasza6af746b72015-09-18 23:37:2260 int render_frame_routing_id) {
[email protected]c1978abe2013-04-23 03:08:1261 // Get all savable resource links for the page.
62 std::vector<GURL> resources_list;
lukasza779a08f82015-11-04 18:27:0463 std::vector<SavableSubframe> subframes;
64 SavableResourcesResult result(&resources_list, &subframes);
[email protected]c1978abe2013-04-23 03:08:1265
lukasza6af746b72015-09-18 23:37:2266 RenderFrame* render_frame =
67 RenderFrame::FromRoutingID(render_frame_routing_id);
[email protected]c1978abe2013-04-23 03:08:1268
lukasza6af746b72015-09-18 23:37:2269 ASSERT_TRUE(GetSavableResourceLinksForFrame(
jame0dcd982017-01-11 03:13:4570 render_frame->GetWebFrame(), &result));
samahto99fcc582015-06-15 17:13:2771
lukaszafe911df2015-10-21 21:06:1972 EXPECT_THAT(resources_list, expected_resources_matcher);
lukasza87f4e2ab2015-11-04 16:03:0373
lukasza779a08f82015-11-04 18:27:0474 std::vector<GURL> subframe_original_urls;
75 for (const SavableSubframe& subframe : subframes) {
76 subframe_original_urls.push_back(subframe.original_url);
77 }
lukasza87f4e2ab2015-11-04 16:03:0378 EXPECT_THAT(subframe_original_urls, expected_subframe_urls_matcher);
[email protected]c1978abe2013-04-23 03:08:1279 }
80};
81
samahto99fcc582015-06-15 17:13:2782IN_PROC_BROWSER_TEST_F(SavableResourcesTest,
83 GetSavableResourceLinksWithPageHasValidStyleLink) {
samahto99fcc582015-06-15 17:13:2784 base::FilePath page_file_path =
85 GetTestFilePath("dom_serializer", "simple_linked_stylesheet.html");
lukaszafe911df2015-10-21 21:06:1986
87 auto expected_subresources_matcher = testing::UnorderedElementsAre(
88 net::FilePathToFileURL(GetTestFilePath("dom_serializer", "style.css")));
89
lukasza87f4e2ab2015-11-04 16:03:0390 auto expected_subframe_urls_matcher = testing::IsEmpty();
91
92 GetSavableResourceLinksForPage(page_file_path, expected_subresources_matcher,
93 expected_subframe_urls_matcher);
samahto99fcc582015-06-15 17:13:2794}
95
[email protected]c1978abe2013-04-23 03:08:1296// Test function GetAllSavableResourceLinksForCurrentPage with a web page
97// which has valid savable resource links.
[email protected]12a936d2013-05-15 04:55:4998IN_PROC_BROWSER_TEST_F(SavableResourcesTest,
[email protected]c1978abe2013-04-23 03:08:1299 GetSavableResourceLinksWithPageHasValidLinks) {
[email protected]c1978abe2013-04-23 03:08:12100 base::FilePath page_file_path =
101 GetTestFilePath("dom_serializer", "youtube_1.htm");
lukaszafe911df2015-10-21 21:06:19102
103 auto expected_subresources_matcher = testing::UnorderedElementsAre(
104 GURL("file:///c:/yt/css/base_all-vfl36460.css"),
105 GURL("file:///c:/yt/js/base_all_with_bidi-vfl36451.js"),
106 GURL("file:///c:/yt/img/pixel-vfl73.gif"));
107
lukasza87f4e2ab2015-11-04 16:03:03108 auto expected_subframe_urls_matcher =
109 testing::UnorderedElementsAre(net::FilePathToFileURL(
110 GetTestFilePath("dom_serializer", "youtube_2.htm")));
111
112 GetSavableResourceLinksForPage(page_file_path, expected_subresources_matcher,
113 expected_subframe_urls_matcher);
[email protected]c1978abe2013-04-23 03:08:12114}
115
116// Test function GetAllSavableResourceLinksForCurrentPage with a web page
117// which does not have valid savable resource links.
[email protected]12a936d2013-05-15 04:55:49118IN_PROC_BROWSER_TEST_F(SavableResourcesTest,
[email protected]c1978abe2013-04-23 03:08:12119 GetSavableResourceLinksWithPageHasInvalidLinks) {
[email protected]c1978abe2013-04-23 03:08:12120 base::FilePath page_file_path =
121 GetTestFilePath("dom_serializer", "youtube_2.htm");
lukaszafe911df2015-10-21 21:06:19122
123 auto expected_subresources_matcher = testing::IsEmpty();
lukasza87f4e2ab2015-11-04 16:03:03124
125 auto expected_subframe_urls_matcher = testing::IsEmpty();
126
127 GetSavableResourceLinksForPage(page_file_path, expected_subresources_matcher,
128 expected_subframe_urls_matcher);
[email protected]c1978abe2013-04-23 03:08:12129}
130
[email protected]c1978abe2013-04-23 03:08:12131} // namespace content