1// 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.
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
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.
23  void GetSavableResourceLinksForPage(const 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
38void DomOperationsTests::GetSavableResourceLinksForPage(
39    const 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> referrers_list;
50  std::vector<GURL> frames_list;
51  webkit_glue::SavableResourcesResult result(&resources_list,
52                                             &referrers_list,
53                                             &frames_list);
54
55  const char* savable_schemes[] = {
56    "http",
57    "https",
58    "file",
59    NULL
60  };
61
62  ASSERT_TRUE(webkit_glue::GetAllSavableResourceLinksForCurrentPage(
63      test_shell_->webView(), file_url, &result, savable_schemes));
64  // Check all links of sub-resource
65  for (std::vector<GURL>::const_iterator cit = resources_list.begin();
66       cit != resources_list.end(); ++cit) {
67    ASSERT_TRUE(expected_resources_set.find(*cit) !=
68                expected_resources_set.end());
69  }
70  // Check all links of frame.
71  for (std::vector<GURL>::const_iterator cit = frames_list.begin();
72       cit != frames_list.end(); ++cit) {
73    ASSERT_TRUE(expected_resources_set.find(*cit) !=
74                expected_resources_set.end());
75  }
76}
77
78// Test function GetAllSavableResourceLinksForCurrentPage with a web page
79// which has valid savable resource links.
80TEST_F(DomOperationsTests, GetSavableResourceLinksWithPageHasValidLinks) {
81  std::set<GURL> expected_resources_set;
82  // Set directory of test data.
83  FilePath page_file_path = data_dir_.AppendASCII("dom_serializer");
84
85  const char* expected_sub_resource_links[] = {
86    "file:///c:/yt/css/base_all-vfl36460.css",
87    "file:///c:/yt/js/base_all_with_bidi-vfl36451.js",
88    "file:///c:/yt/img/pixel-vfl73.gif"
89  };
90  const char* expected_frame_links[] = {
91    "youtube_1.htm",
92    "youtube_2.htm"
93  };
94  // Add all expected links of sub-resource to expected set.
95  for (size_t i = 0; i < arraysize(expected_sub_resource_links); ++i)
96    expected_resources_set.insert(GURL(expected_sub_resource_links[i]));
97  // Add all expected links of frame to expected set.
98  for (size_t i = 0; i < arraysize(expected_frame_links); ++i) {
99    const FilePath expected_frame_url =
100        page_file_path.AppendASCII(expected_frame_links[i]);
101    expected_resources_set.insert(
102        net::FilePathToFileURL(expected_frame_url));
103  }
104
105  page_file_path = page_file_path.AppendASCII("youtube_1.htm");
106  GetSavableResourceLinksForPage(page_file_path, expected_resources_set);
107}
108
109// Test function GetAllSavableResourceLinksForCurrentPage with a web page
110// which does not have valid savable resource links.
111TEST_F(DomOperationsTests, GetSavableResourceLinksWithPageHasInvalidLinks) {
112  std::set<GURL> expected_resources_set;
113  // Set directory of test data.
114  FilePath page_file_path = data_dir_.AppendASCII("dom_serializer");
115
116  const char* expected_frame_links[] = {
117    "youtube_2.htm"
118  };
119  // Add all expected links of frame to expected set.
120  for (size_t i = 0; i < arraysize(expected_frame_links); ++i) {
121    FilePath expected_frame_url =
122        page_file_path.AppendASCII(expected_frame_links[i]);
123    expected_resources_set.insert(
124        net::FilePathToFileURL(expected_frame_url));
125  }
126
127  page_file_path = page_file_path.AppendASCII("youtube_2.htm");
128  GetSavableResourceLinksForPage(page_file_path, expected_resources_set);
129}
130
131}  // namespace
132