blob: 0ebf1b477f4fc30fcf923210fbec1c45cbc07f5b [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commitf5b16fe2008-07-27 00:20:514
5#include "base/file_util.h"
6#include "base/path_service.h"
7#include "base/string_util.h"
8#include "net/base/net_util.h"
9#include "net/url_request/url_request_context.h"
10#include "webkit/glue/dom_operations.h"
11#include "webkit/glue/webview.h"
12#include "webkit/glue/webframe.h"
13#include "webkit/tools/test_shell/simple_resource_loader_bridge.h"
14#include "webkit/tools/test_shell/test_shell_test.h"
15
16namespace {
17
18class 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.
[email protected]72cbd322009-04-07 10:17:1223 void GetSavableResourceLinksForPage(const FilePath& page_file_path,
initial.commitf5b16fe2008-07-27 00:20:5124 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
initial.commitf5b16fe2008-07-27 00:20:5137
38void DomOperationsTests::GetSavableResourceLinksForPage(
[email protected]72cbd322009-04-07 10:17:1239 const FilePath& page_file_path,
initial.commitf5b16fe2008-07-27 00:20:5140 const std::set<GURL>& expected_resources_set) {
41 // Convert local file path to file URL.
[email protected]8ac1a752008-07-31 19:40:3742 GURL file_url = net::FilePathToFileURL(page_file_path);
initial.commitf5b16fe2008-07-27 00:20:5143 // Load the test file.
44 test_shell_->ResetTestController();
45 std::wstring file_wurl = ASCIIToWide(file_url.spec());
46 test_shell_->LoadURL(file_wurl.c_str());
47 test_shell_->WaitTestFinished();
48 // Get all savable resource links for the page.
49 std::vector<GURL> resources_list;
50 std::vector<GURL> referrers_list;
51 std::vector<GURL> frames_list;
52 webkit_glue::SavableResourcesResult result(&resources_list,
53 &referrers_list,
54 &frames_list);
55
[email protected]a48146712008-11-06 17:51:2056 GURL main_page_gurl(WideToUTF8(file_wurl));
initial.commitf5b16fe2008-07-27 00:20:5157 ASSERT_TRUE(webkit_glue::GetAllSavableResourceLinksForCurrentPage(
58 test_shell_->webView(), main_page_gurl, &result));
59 // Check all links of sub-resource
60 for (std::vector<GURL>::const_iterator cit = resources_list.begin();
61 cit != resources_list.end(); ++cit) {
62 ASSERT_TRUE(expected_resources_set.find(*cit) !=
63 expected_resources_set.end());
64 }
65 // Check all links of frame.
66 for (std::vector<GURL>::const_iterator cit = frames_list.begin();
67 cit != frames_list.end(); ++cit) {
68 ASSERT_TRUE(expected_resources_set.find(*cit) !=
69 expected_resources_set.end());
70 }
71}
72
73// Test function GetAllSavableResourceLinksForCurrentPage with a web page
74// which has valid savable resource links.
75TEST_F(DomOperationsTests, GetSavableResourceLinksWithPageHasValidLinks) {
76 std::set<GURL> expected_resources_set;
77 // Set directory of test data.
[email protected]72cbd322009-04-07 10:17:1278 FilePath page_file_path = data_dir_.AppendASCII("dom_serializer");
initial.commitf5b16fe2008-07-27 00:20:5179
[email protected]a48146712008-11-06 17:51:2080 const char* expected_sub_resource_links[] = {
81 "file:///c:/yt/css/base_all-vfl36460.css",
82 "file:///c:/yt/js/base_all_with_bidi-vfl36451.js",
83 "file:///c:/yt/img/pixel-vfl73.gif"
initial.commitf5b16fe2008-07-27 00:20:5184 };
[email protected]72cbd322009-04-07 10:17:1285 const char* expected_frame_links[] = {
86 "youtube_1.htm",
87 "youtube_2.htm"
initial.commitf5b16fe2008-07-27 00:20:5188 };
89 // Add all expected links of sub-resource to expected set.
[email protected]a48146712008-11-06 17:51:2090 for (size_t i = 0; i < arraysize(expected_sub_resource_links); ++i)
initial.commitf5b16fe2008-07-27 00:20:5191 expected_resources_set.insert(GURL(expected_sub_resource_links[i]));
92 // Add all expected links of frame to expected set.
[email protected]a48146712008-11-06 17:51:2093 for (size_t i = 0; i < arraysize(expected_frame_links); ++i) {
[email protected]72cbd322009-04-07 10:17:1294 const FilePath expected_frame_url =
95 page_file_path.AppendASCII(expected_frame_links[i]);
initial.commitf5b16fe2008-07-27 00:20:5196 expected_resources_set.insert(
[email protected]8ac1a752008-07-31 19:40:3797 net::FilePathToFileURL(expected_frame_url));
initial.commitf5b16fe2008-07-27 00:20:5198 }
99
[email protected]72cbd322009-04-07 10:17:12100 page_file_path = page_file_path.AppendASCII("youtube_1.htm");
initial.commitf5b16fe2008-07-27 00:20:51101 GetSavableResourceLinksForPage(page_file_path, expected_resources_set);
102}
103
104// Test function GetAllSavableResourceLinksForCurrentPage with a web page
105// which does not have valid savable resource links.
106TEST_F(DomOperationsTests, GetSavableResourceLinksWithPageHasInvalidLinks) {
107 std::set<GURL> expected_resources_set;
108 // Set directory of test data.
[email protected]72cbd322009-04-07 10:17:12109 FilePath page_file_path = data_dir_.AppendASCII("dom_serializer");
initial.commitf5b16fe2008-07-27 00:20:51110
[email protected]72cbd322009-04-07 10:17:12111 const char* expected_frame_links[] = {
112 "youtube_2.htm"
initial.commitf5b16fe2008-07-27 00:20:51113 };
114 // Add all expected links of frame to expected set.
[email protected]a48146712008-11-06 17:51:20115 for (size_t i = 0; i < arraysize(expected_frame_links); ++i) {
[email protected]72cbd322009-04-07 10:17:12116 FilePath expected_frame_url =
117 page_file_path.AppendASCII(expected_frame_links[i]);
initial.commitf5b16fe2008-07-27 00:20:51118 expected_resources_set.insert(
[email protected]8ac1a752008-07-31 19:40:37119 net::FilePathToFileURL(expected_frame_url));
initial.commitf5b16fe2008-07-27 00:20:51120 }
121
[email protected]72cbd322009-04-07 10:17:12122 page_file_path = page_file_path.AppendASCII("youtube_2.htm");
initial.commitf5b16fe2008-07-27 00:20:51123 GetSavableResourceLinksForPage(page_file_path, expected_resources_set);
124}
125
126// Tests ParseIconSizes with various input.
127TEST_F(DomOperationsTests, ParseIconSizes) {
128 struct TestData {
129 const std::wstring input;
130 const bool expected_result;
131 const bool is_any;
[email protected]a48146712008-11-06 17:51:20132 const size_t expected_size_count;
initial.commitf5b16fe2008-07-27 00:20:51133 const int width1;
134 const int height1;
135 const int width2;
136 const int height2;
137 } data[] = {
138 // Bogus input cases.
139 { L"10", false, false, 0, 0, 0, 0, 0 },
140 { L"10 10", false, false, 0, 0, 0, 0, 0 },
141 { L"010", false, false, 0, 0, 0, 0, 0 },
142 { L" 010 ", false, false, 0, 0, 0, 0, 0 },
143 { L" 10x ", false, false, 0, 0, 0, 0, 0 },
144 { L" x10 ", false, false, 0, 0, 0, 0, 0 },
145 { L"any 10x10", false, false, 0, 0, 0, 0, 0 },
146 { L"", false, false, 0, 0, 0, 0, 0 },
147 { L"10ax11", false, false, 0, 0, 0, 0, 0 },
148
149 // Any.
150 { L"any", true, true, 0, 0, 0, 0, 0 },
151 { L" any", true, true, 0, 0, 0, 0, 0 },
152 { L" any ", true, true, 0, 0, 0, 0, 0 },
153
154 // Sizes.
155 { L"10x11", true, false, 1, 10, 11, 0, 0 },
156 { L" 10x11 ", true, false, 1, 10, 11, 0, 0 },
157 { L" 10x11 1x2", true, false, 2, 10, 11, 1, 2 },
158 };
[email protected]a48146712008-11-06 17:51:20159 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
initial.commitf5b16fe2008-07-27 00:20:51160 bool is_any;
161 std::vector<gfx::Size> sizes;
162 const bool result =
163 webkit_glue::ParseIconSizes(data[i].input, &sizes, &is_any);
164 ASSERT_EQ(result, data[i].expected_result);
165 if (result) {
166 ASSERT_EQ(data[i].is_any, is_any);
167 ASSERT_EQ(data[i].expected_size_count, sizes.size());
168 if (sizes.size() > 0) {
169 ASSERT_EQ(data[i].width1, sizes[0].width());
170 ASSERT_EQ(data[i].height1, sizes[0].height());
171 }
172 if (sizes.size() > 1) {
173 ASSERT_EQ(data[i].width2, sizes[1].width());
174 ASSERT_EQ(data[i].height2, sizes[1].height());
175 }
176 }
177 }
178}
license.botbf09a502008-08-24 00:55:55179
[email protected]a03c3ab2008-12-10 20:04:09180} // namespace