Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / content / renderer / savable_resources_browsertest.cc
blobea991997b6f063c198c8c093ea8a07a0cec2a96e
1 // 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.
5 #include "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/files/file_path.h"
8 #include "content/public/browser/render_frame_host.h"
9 #include "content/public/browser/web_contents.h"
10 #include "content/public/common/content_switches.h"
11 #include "content/public/renderer/render_frame.h"
12 #include "content/public/test/content_browser_test.h"
13 #include "content/public/test/content_browser_test_utils.h"
14 #include "content/renderer/savable_resources.h"
15 #include "content/shell/browser/shell.h"
16 #include "net/base/filename_util.h"
17 #include "third_party/WebKit/public/web/WebLocalFrame.h"
19 namespace content {
21 class SavableResourcesTest : public ContentBrowserTest {
22 public:
23 void SetUpCommandLine(base::CommandLine* command_line) override {
24 command_line->AppendSwitch(switches::kSingleProcess);
25 #if defined(OS_WIN)
26 // Don't want to try to create a GPU process.
27 command_line->AppendSwitch(switches::kDisableGpu);
28 #endif
31 // Test function GetAllSavableResourceLinksForCurrentPage with a web page.
32 // We expect result of GetAllSavableResourceLinksForCurrentPage exactly
33 // matches expected_resources_set.
34 void GetSavableResourceLinksForPage(
35 const base::FilePath& page_file_path,
36 const std::set<GURL>& expected_resources_set) {
37 // Convert local file path to file URL.
38 GURL file_url = net::FilePathToFileURL(page_file_path);
39 // Load the test file.
40 NavigateToURL(shell(), file_url);
42 PostTaskToInProcessRendererAndWait(base::Bind(
43 &SavableResourcesTest::CheckResources, base::Unretained(this),
44 page_file_path, expected_resources_set, file_url,
45 shell()->web_contents()->GetMainFrame()->GetRoutingID()));
48 void CheckResources(const base::FilePath& page_file_path,
49 const std::set<GURL>& expected_resources_set,
50 const GURL& file_url,
51 int render_frame_routing_id) {
52 // Get all savable resource links for the page.
53 std::vector<GURL> resources_list;
54 std::vector<GURL> referrer_urls_list;
55 std::vector<blink::WebReferrerPolicy> referrer_policies_list;
56 SavableResourcesResult result(&resources_list, &referrer_urls_list,
57 &referrer_policies_list);
59 const char* savable_schemes[] = {
60 "http",
61 "https",
62 "file",
63 NULL
66 RenderFrame* render_frame =
67 RenderFrame::FromRoutingID(render_frame_routing_id);
69 ASSERT_TRUE(GetSavableResourceLinksForFrame(
70 render_frame->GetWebFrame(),
71 &result, savable_schemes));
73 // Check all links of sub-resource
74 for (const auto& resource : resources_list) {
75 ASSERT_TRUE(expected_resources_set.count(resource) != 0);
80 IN_PROC_BROWSER_TEST_F(SavableResourcesTest,
81 GetSavableResourceLinksWithPageHasValidStyleLink) {
82 const char* expected_sub_resource_links[] = {
83 "style.css"
86 const char* expected_frame_links[] = {
87 "simple_linked_stylesheet.html",
90 // Add all expected links of sub-resource to expected set.
91 std::set<GURL> expected_resources_set;
92 const base::FilePath expected_resource_url =
93 GetTestFilePath("dom_serializer", expected_sub_resource_links[0]);
94 expected_resources_set.insert(
95 net::FilePathToFileURL(expected_resource_url));
97 //expected frame set
98 const base::FilePath expected_frame_url =
99 GetTestFilePath("dom_serializer", expected_frame_links[0]);
100 expected_resources_set.insert(
101 net::FilePathToFileURL(expected_frame_url));
103 base::FilePath page_file_path =
104 GetTestFilePath("dom_serializer", "simple_linked_stylesheet.html");
105 GetSavableResourceLinksForPage(page_file_path, expected_resources_set);
108 // Test function GetAllSavableResourceLinksForCurrentPage with a web page
109 // which has valid savable resource links.
110 IN_PROC_BROWSER_TEST_F(SavableResourcesTest,
111 GetSavableResourceLinksWithPageHasValidLinks) {
112 std::set<GURL> expected_resources_set;
114 const char* expected_sub_resource_links[] = {
115 "file:///c:/yt/css/base_all-vfl36460.css",
116 "file:///c:/yt/js/base_all_with_bidi-vfl36451.js",
117 "file:///c:/yt/img/pixel-vfl73.gif"
119 const char* expected_frame_links[] = {
120 "youtube_1.htm",
121 "youtube_2.htm"
123 // Add all expected links of sub-resource to expected set.
124 for (size_t i = 0; i < arraysize(expected_sub_resource_links); ++i)
125 expected_resources_set.insert(GURL(expected_sub_resource_links[i]));
126 // Add all expected links of frame to expected set.
127 for (size_t i = 0; i < arraysize(expected_frame_links); ++i) {
128 const base::FilePath expected_frame_url =
129 GetTestFilePath("dom_serializer", expected_frame_links[i]);
130 expected_resources_set.insert(
131 net::FilePathToFileURL(expected_frame_url));
134 base::FilePath page_file_path =
135 GetTestFilePath("dom_serializer", "youtube_1.htm");
136 GetSavableResourceLinksForPage(page_file_path, expected_resources_set);
139 // Test function GetAllSavableResourceLinksForCurrentPage with a web page
140 // which does not have valid savable resource links.
141 IN_PROC_BROWSER_TEST_F(SavableResourcesTest,
142 GetSavableResourceLinksWithPageHasInvalidLinks) {
143 std::set<GURL> expected_resources_set;
145 const char* expected_frame_links[] = {
146 "youtube_2.htm"
148 // Add all expected links of frame to expected set.
149 for (size_t i = 0; i < arraysize(expected_frame_links); ++i) {
150 base::FilePath expected_frame_url =
151 GetTestFilePath("dom_serializer", expected_frame_links[i]);
152 expected_resources_set.insert(
153 net::FilePathToFileURL(expected_frame_url));
156 base::FilePath page_file_path =
157 GetTestFilePath("dom_serializer", "youtube_2.htm");
158 GetSavableResourceLinksForPage(page_file_path, expected_resources_set);
161 } // namespace content