OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "base/file_util.h" | |
6 #include "base/path_service.h" | |
7 #include "base/string_util.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "net/base/net_util.h" | |
10 #include "net/url_request/url_request_context.h" | |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
12 #include "webkit/glue/dom_operations.h" | |
13 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" | |
14 #include "webkit/tools/test_shell/test_shell_test.h" | |
15 | |
16 namespace { | |
17 | |
18 class DomOperationsTests : public TestShellTest { | |
19 public: | |
20 // Test function GetAllSavableResourceLinksForCurrentPage with a web page. | |
21 // We expect result of GetAllSavableResourceLinksForCurrentPage exactly | |
22 // matches expected_resources_set. | |
23 void GetSavableResourceLinksForPage(const base::FilePath& page_file_path, | |
24 const std::set<GURL>& expected_resources_set); | |
25 | |
26 protected: | |
27 // testing::Test | |
28 virtual void SetUp() { | |
29 TestShellTest::SetUp(); | |
30 } | |
31 | |
32 virtual void TearDown() { | |
33 TestShellTest::TearDown(); | |
34 } | |
35 }; | |
36 | |
37 | |
38 void DomOperationsTests::GetSavableResourceLinksForPage( | |
39 const base::FilePath& page_file_path, | |
40 const std::set<GURL>& expected_resources_set) { | |
41 // Convert local file path to file URL. | |
42 GURL file_url = net::FilePathToFileURL(page_file_path); | |
43 // Load the test file. | |
44 test_shell_->ResetTestController(); | |
45 test_shell_->LoadURL(file_url); | |
46 test_shell_->WaitTestFinished(); | |
47 // Get all savable resource links for the page. | |
48 std::vector<GURL> resources_list; | |
49 std::vector<GURL> referrer_urls_list; | |
50 std::vector<WebKit::WebReferrerPolicy> referrer_policies_list; | |
51 std::vector<GURL> frames_list; | |
52 webkit_glue::SavableResourcesResult result(&resources_list, | |
53 &referrer_urls_list, | |
54 &referrer_policies_list, | |
55 &frames_list); | |
56 | |
57 const char* savable_schemes[] = { | |
58 "http", | |
59 "https", | |
60 "file", | |
61 NULL | |
62 }; | |
63 | |
64 ASSERT_TRUE(webkit_glue::GetAllSavableResourceLinksForCurrentPage( | |
65 test_shell_->webView(), file_url, &result, savable_schemes)); | |
66 // Check all links of sub-resource | |
67 for (std::vector<GURL>::const_iterator cit = resources_list.begin(); | |
68 cit != resources_list.end(); ++cit) { | |
69 ASSERT_TRUE(expected_resources_set.find(*cit) != | |
70 expected_resources_set.end()); | |
71 } | |
72 // Check all links of frame. | |
73 for (std::vector<GURL>::const_iterator cit = frames_list.begin(); | |
74 cit != frames_list.end(); ++cit) { | |
75 ASSERT_TRUE(expected_resources_set.find(*cit) != | |
76 expected_resources_set.end()); | |
77 } | |
78 } | |
79 | |
80 // Test function GetAllSavableResourceLinksForCurrentPage with a web page | |
81 // which has valid savable resource links. | |
82 TEST_F(DomOperationsTests, GetSavableResourceLinksWithPageHasValidLinks) { | |
83 std::set<GURL> expected_resources_set; | |
84 // Set directory of test data. | |
85 base::FilePath page_file_path = data_dir_.AppendASCII("dom_serializer"); | |
86 | |
87 const char* expected_sub_resource_links[] = { | |
88 "file:///c:/yt/css/base_all-vfl36460.css", | |
89 "file:///c:/yt/js/base_all_with_bidi-vfl36451.js", | |
90 "file:///c:/yt/img/pixel-vfl73.gif" | |
91 }; | |
92 const char* expected_frame_links[] = { | |
93 "youtube_1.htm", | |
94 "youtube_2.htm" | |
95 }; | |
96 // Add all expected links of sub-resource to expected set. | |
97 for (size_t i = 0; i < arraysize(expected_sub_resource_links); ++i) | |
98 expected_resources_set.insert(GURL(expected_sub_resource_links[i])); | |
99 // Add all expected links of frame to expected set. | |
100 for (size_t i = 0; i < arraysize(expected_frame_links); ++i) { | |
101 const base::FilePath expected_frame_url = | |
102 page_file_path.AppendASCII(expected_frame_links[i]); | |
103 expected_resources_set.insert( | |
104 net::FilePathToFileURL(expected_frame_url)); | |
105 } | |
106 | |
107 page_file_path = page_file_path.AppendASCII("youtube_1.htm"); | |
108 GetSavableResourceLinksForPage(page_file_path, expected_resources_set); | |
109 } | |
110 | |
111 // Test function GetAllSavableResourceLinksForCurrentPage with a web page | |
112 // which does not have valid savable resource links. | |
113 TEST_F(DomOperationsTests, GetSavableResourceLinksWithPageHasInvalidLinks) { | |
114 std::set<GURL> expected_resources_set; | |
115 // Set directory of test data. | |
116 base::FilePath page_file_path = data_dir_.AppendASCII("dom_serializer"); | |
117 | |
118 const char* expected_frame_links[] = { | |
119 "youtube_2.htm" | |
120 }; | |
121 // Add all expected links of frame to expected set. | |
122 for (size_t i = 0; i < arraysize(expected_frame_links); ++i) { | |
123 base::FilePath expected_frame_url = | |
124 page_file_path.AppendASCII(expected_frame_links[i]); | |
125 expected_resources_set.insert( | |
126 net::FilePathToFileURL(expected_frame_url)); | |
127 } | |
128 | |
129 page_file_path = page_file_path.AppendASCII("youtube_2.htm"); | |
130 GetSavableResourceLinksForPage(page_file_path, expected_resources_set); | |
131 } | |
132 | |
133 } // namespace | |
OLD | NEW |