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.
6 #include "base/command_line.h"
7 #include "base/files/file_path.h"
8 #include "content/public/browser/web_contents.h"
9 #include "content/public/common/content_switches.h"
10 #include "content/public/renderer/render_view.h"
11 #include "content/public/test/content_browser_test.h"
12 #include "content/public/test/content_browser_test_utils.h"
13 #include "content/renderer/savable_resources.h"
14 #include "content/shell/browser/shell.h"
15 #include "net/base/filename_util.h"
19 class SavableResourcesTest
: public ContentBrowserTest
{
21 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
22 command_line
->AppendSwitch(switches::kSingleProcess
);
24 // Don't want to try to create a GPU process.
25 command_line
->AppendSwitch(switches::kDisableGpu
);
29 // Test function GetAllSavableResourceLinksForCurrentPage with a web page.
30 // We expect result of GetAllSavableResourceLinksForCurrentPage exactly
31 // matches expected_resources_set.
32 void GetSavableResourceLinksForPage(
33 const base::FilePath
& page_file_path
,
34 const std::set
<GURL
>& expected_resources_set
) {
35 // Convert local file path to file URL.
36 GURL file_url
= net::FilePathToFileURL(page_file_path
);
37 // Load the test file.
38 NavigateToURL(shell(), file_url
);
40 PostTaskToInProcessRendererAndWait(
41 base::Bind(&SavableResourcesTest::CheckResources
,
42 base::Unretained(this),
44 expected_resources_set
,
46 shell()->web_contents()->GetRoutingID()));
49 void CheckResources(const base::FilePath
& page_file_path
,
50 const std::set
<GURL
>& expected_resources_set
,
53 // Get all savable resource links for the page.
54 std::vector
<GURL
> resources_list
;
55 std::vector
<GURL
> referrer_urls_list
;
56 std::vector
<blink::WebReferrerPolicy
> referrer_policies_list
;
57 std::vector
<GURL
> frames_list
;
58 SavableResourcesResult
result(&resources_list
,
60 &referrer_policies_list
,
63 const char* savable_schemes
[] = {
70 RenderView
* render_view
= RenderView::FromRoutingID(render_view_id
);
72 ASSERT_TRUE(GetAllSavableResourceLinksForCurrentPage(
73 render_view
->GetWebView(), file_url
, &result
, savable_schemes
));
75 // Check all links of sub-resource
76 for (std::vector
<GURL
>::const_iterator cit
= resources_list
.begin();
77 cit
!= resources_list
.end(); ++cit
) {
78 ASSERT_TRUE(expected_resources_set
.find(*cit
) !=
79 expected_resources_set
.end());
82 // Check all links of frame.
83 for (std::vector
<GURL
>::const_iterator cit
= frames_list
.begin();
84 cit
!= frames_list
.end(); ++cit
) {
85 ASSERT_TRUE(expected_resources_set
.find(*cit
) !=
86 expected_resources_set
.end());
91 IN_PROC_BROWSER_TEST_F(SavableResourcesTest
,
92 GetSavableResourceLinksWithPageHasValidStyleLink
) {
93 const char* expected_sub_resource_links
[] = {
97 const char* expected_frame_links
[] = {
98 "simple_linked_stylesheet.html",
101 // Add all expected links of sub-resource to expected set.
102 std::set
<GURL
> expected_resources_set
;
103 const base::FilePath expected_resource_url
=
104 GetTestFilePath("dom_serializer", expected_sub_resource_links
[0]);
105 expected_resources_set
.insert(
106 net::FilePathToFileURL(expected_resource_url
));
109 const base::FilePath expected_frame_url
=
110 GetTestFilePath("dom_serializer", expected_frame_links
[0]);
111 expected_resources_set
.insert(
112 net::FilePathToFileURL(expected_frame_url
));
114 base::FilePath page_file_path
=
115 GetTestFilePath("dom_serializer", "simple_linked_stylesheet.html");
116 GetSavableResourceLinksForPage(page_file_path
, expected_resources_set
);
119 // Test function GetAllSavableResourceLinksForCurrentPage with a web page
120 // which has valid savable resource links.
121 IN_PROC_BROWSER_TEST_F(SavableResourcesTest
,
122 GetSavableResourceLinksWithPageHasValidLinks
) {
123 std::set
<GURL
> expected_resources_set
;
125 const char* expected_sub_resource_links
[] = {
126 "file:///c:/yt/css/base_all-vfl36460.css",
127 "file:///c:/yt/js/base_all_with_bidi-vfl36451.js",
128 "file:///c:/yt/img/pixel-vfl73.gif"
130 const char* expected_frame_links
[] = {
134 // Add all expected links of sub-resource to expected set.
135 for (size_t i
= 0; i
< arraysize(expected_sub_resource_links
); ++i
)
136 expected_resources_set
.insert(GURL(expected_sub_resource_links
[i
]));
137 // Add all expected links of frame to expected set.
138 for (size_t i
= 0; i
< arraysize(expected_frame_links
); ++i
) {
139 const base::FilePath expected_frame_url
=
140 GetTestFilePath("dom_serializer", expected_frame_links
[i
]);
141 expected_resources_set
.insert(
142 net::FilePathToFileURL(expected_frame_url
));
145 base::FilePath page_file_path
=
146 GetTestFilePath("dom_serializer", "youtube_1.htm");
147 GetSavableResourceLinksForPage(page_file_path
, expected_resources_set
);
150 // Test function GetAllSavableResourceLinksForCurrentPage with a web page
151 // which does not have valid savable resource links.
152 IN_PROC_BROWSER_TEST_F(SavableResourcesTest
,
153 GetSavableResourceLinksWithPageHasInvalidLinks
) {
154 std::set
<GURL
> expected_resources_set
;
156 const char* expected_frame_links
[] = {
159 // Add all expected links of frame to expected set.
160 for (size_t i
= 0; i
< arraysize(expected_frame_links
); ++i
) {
161 base::FilePath expected_frame_url
=
162 GetTestFilePath("dom_serializer", expected_frame_links
[i
]);
163 expected_resources_set
.insert(
164 net::FilePathToFileURL(expected_frame_url
));
167 base::FilePath page_file_path
=
168 GetTestFilePath("dom_serializer", "youtube_2.htm");
169 GetSavableResourceLinksForPage(page_file_path
, expected_resources_set
);
172 } // namespace content