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/shell/shell.h"
12 #include "content/test/content_browser_test.h"
13 #include "content/test/content_browser_test_utils.h"
14 #include "net/base/net_util.h"
15 #include "webkit/glue/dom_operations.h"
19 class DomOperationsTests
: public ContentBrowserTest
{
21 virtual void SetUpCommandLine(CommandLine
* command_line
) {
22 command_line
->AppendSwitch(switches::kSingleProcess
);
23 #if defined(OS_WIN) && defined(USE_AURA)
24 // Don't want to try to create a GPU process.
25 command_line
->AppendSwitch(switches::kDisableAcceleratedCompositing
);
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(&DomOperationsTests::CheckResources
, base::Unretained(this),
42 page_file_path
, expected_resources_set
, file_url
,
43 shell()->web_contents()->GetRoutingID()));
46 void CheckResources(const base::FilePath
& page_file_path
,
47 const std::set
<GURL
>& expected_resources_set
,
50 // Get all savable resource links for the page.
51 std::vector
<GURL
> resources_list
;
52 std::vector
<GURL
> referrer_urls_list
;
53 std::vector
<WebKit::WebReferrerPolicy
> referrer_policies_list
;
54 std::vector
<GURL
> frames_list
;
55 webkit_glue::SavableResourcesResult
result(&resources_list
,
57 &referrer_policies_list
,
60 const char* savable_schemes
[] = {
67 RenderView
* render_view
= RenderView::FromRoutingID(render_view_id
);
69 ASSERT_TRUE(webkit_glue::GetAllSavableResourceLinksForCurrentPage(
70 render_view
->GetWebView(), file_url
, &result
, savable_schemes
));
71 // Check all links of sub-resource
72 for (std::vector
<GURL
>::const_iterator cit
= resources_list
.begin();
73 cit
!= resources_list
.end(); ++cit
) {
74 ASSERT_TRUE(expected_resources_set
.find(*cit
) !=
75 expected_resources_set
.end());
77 // Check all links of frame.
78 for (std::vector
<GURL
>::const_iterator cit
= frames_list
.begin();
79 cit
!= frames_list
.end(); ++cit
) {
80 ASSERT_TRUE(expected_resources_set
.find(*cit
) !=
81 expected_resources_set
.end());
86 // Test function GetAllSavableResourceLinksForCurrentPage with a web page
87 // which has valid savable resource links.
88 IN_PROC_BROWSER_TEST_F(DomOperationsTests
,
89 GetSavableResourceLinksWithPageHasValidLinks
) {
90 std::set
<GURL
> expected_resources_set
;
92 const char* expected_sub_resource_links
[] = {
93 "file:///c:/yt/css/base_all-vfl36460.css",
94 "file:///c:/yt/js/base_all_with_bidi-vfl36451.js",
95 "file:///c:/yt/img/pixel-vfl73.gif"
97 const char* expected_frame_links
[] = {
101 // Add all expected links of sub-resource to expected set.
102 for (size_t i
= 0; i
< arraysize(expected_sub_resource_links
); ++i
)
103 expected_resources_set
.insert(GURL(expected_sub_resource_links
[i
]));
104 // Add all expected links of frame to expected set.
105 for (size_t i
= 0; i
< arraysize(expected_frame_links
); ++i
) {
106 const base::FilePath expected_frame_url
=
107 GetTestFilePath("dom_serializer", expected_frame_links
[i
]);
108 expected_resources_set
.insert(
109 net::FilePathToFileURL(expected_frame_url
));
112 base::FilePath page_file_path
=
113 GetTestFilePath("dom_serializer", "youtube_1.htm");
114 GetSavableResourceLinksForPage(page_file_path
, expected_resources_set
);
117 // Test function GetAllSavableResourceLinksForCurrentPage with a web page
118 // which does not have valid savable resource links.
119 IN_PROC_BROWSER_TEST_F(DomOperationsTests
,
120 GetSavableResourceLinksWithPageHasInvalidLinks
) {
121 std::set
<GURL
> expected_resources_set
;
123 const char* expected_frame_links
[] = {
126 // Add all expected links of frame to expected set.
127 for (size_t i
= 0; i
< arraysize(expected_frame_links
); ++i
) {
128 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_2.htm");
136 GetSavableResourceLinksForPage(page_file_path
, expected_resources_set
);
139 } // namespace content