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.
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/path_service.h"
10 #include "base/stl_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/threading/thread_restrictions.h"
13 #include "build/build_config.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_commands.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/ui_test_utils.h"
22 #include "components/prefs/pref_service.h"
23 #include "content/public/browser/download_manager.h"
24 #include "content/public/browser/navigation_controller.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/notification_source.h"
27 #include "content/public/browser/notification_types.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/test/browser_test.h"
30 #include "content/public/test/download_test_observer.h"
31 #include "content/public/test/test_navigation_observer.h"
32 #include "net/test/embedded_test_server/embedded_test_server.h"
36 struct EncodingTestData {
37 const char* file_name;
38 const char* encoding_name;
41 const EncodingTestData kEncodingTestDatas[] = {
42 { "Big5.html", "Big5" },
43 { "EUC-JP.html", "EUC-JP" },
44 { "gb18030.html", "gb18030" },
45 { "iso-8859-1.html", "windows-1252" },
46 { "ISO-8859-2.html", "ISO-8859-2" },
47 { "ISO-8859-4.html", "ISO-8859-4" },
48 { "ISO-8859-5.html", "ISO-8859-5" },
49 { "ISO-8859-6.html", "ISO-8859-6" },
50 { "ISO-8859-7.html", "ISO-8859-7" },
51 { "ISO-8859-8.html", "ISO-8859-8" },
52 { "ISO-8859-13.html", "ISO-8859-13" },
53 { "ISO-8859-15.html", "ISO-8859-15" },
54 { "KOI8-R.html", "KOI8-R" },
55 { "KOI8-U.html", "KOI8-U" },
56 { "macintosh.html", "macintosh" },
57 { "Shift-JIS.html", "Shift_JIS" },
58 { "US-ASCII.html", "windows-1252" }, // http://crbug.com/15801
59 { "UTF-8.html", "UTF-8" },
60 { "UTF-16LE.html", "UTF-16LE" },
61 { "windows-874.html", "windows-874" },
62 { "EUC-KR.html", "EUC-KR" },
63 { "windows-1250.html", "windows-1250" },
64 { "windows-1251.html", "windows-1251" },
65 { "windows-1252.html", "windows-1252" },
66 { "windows-1253.html", "windows-1253" },
67 { "windows-1254.html", "windows-1254" },
68 { "windows-1255.html", "windows-1255" },
69 { "windows-1256.html", "windows-1256" },
70 { "windows-1257.html", "windows-1257" },
71 { "windows-1258.html", "windows-1258" }
76 static const base::FilePath::CharType* kTestDir =
77 FILE_PATH_LITERAL("encoding_tests");
79 class BrowserEncodingTest
80 : public InProcessBrowserTest,
81 public testing::WithParamInterface<EncodingTestData> {
83 BrowserEncodingTest() {}
85 // Saves the current page and verifies that the output matches the expected
87 void SaveAndCompare(const char* filename_to_write,
88 const base::FilePath& expected,
90 // Dump the page, the content of dump page should be identical to the
91 // expected result file.
92 base::FilePath full_file_name = save_dir_.AppendASCII(filename_to_write);
93 // We save the page as way of complete HTML file, which requires a directory
94 // name to save sub resources in it. Although this test file does not have
95 // sub resources, but the directory name is still required.
96 scoped_refptr<content::MessageLoopRunner> loop_runner(
97 new content::MessageLoopRunner);
98 content::SavePackageFinishedObserver observer(
99 content::BrowserContext::GetDownloadManager(browser()->profile()),
100 loop_runner->QuitClosure());
101 browser()->tab_strip_model()->GetActiveWebContents()->SavePage(
102 full_file_name, temp_sub_resource_dir_,
103 content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML);
106 base::FilePath expected_file_name = ui_test_utils::GetTestFilePath(
107 base::FilePath(kTestDir), expected);
109 std::string actual_contents;
110 std::string expected_contents;
113 base::ScopedAllowBlockingForTesting allow_blocking;
114 ASSERT_TRUE(base::ReadFileToString(full_file_name, &actual_contents));
116 base::ReadFileToString(expected_file_name, &expected_contents));
119 // Add "Mark of the Web" path with source URL.
120 expected_contents = base::StringPrintf(
121 expected_contents.c_str(), url.spec().length(), url.spec().c_str());
123 EXPECT_EQ(expected_contents, actual_contents);
126 void SetUpOnMainThread() override {
127 base::FilePath test_data_dir;
128 ASSERT_TRUE(base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
129 embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
130 ASSERT_TRUE(embedded_test_server()->Start());
132 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
133 save_dir_ = temp_dir_.GetPath();
134 temp_sub_resource_dir_ = save_dir_.AppendASCII("sub_resource_files");
137 base::ScopedTempDir temp_dir_;
138 base::FilePath save_dir_;
139 base::FilePath temp_sub_resource_dir_;
142 // TODO(jnd): 1. Some encodings are missing here. It'll be added later. See
143 // http://crbug.com/13306.
144 // 2. Add more files with multiple encoding name variants for each canonical
145 // encoding name). Webkit layout tests cover some, but testing in the UI test is
147 IN_PROC_BROWSER_TEST_P(BrowserEncodingTest, TestEncodingAliasMapping) {
148 const char* const kAliasTestDir = "alias_mapping";
150 base::FilePath test_dir_path = base::FilePath(kTestDir).AppendASCII(
152 base::FilePath test_file_path(test_dir_path);
153 test_file_path = test_file_path.AppendASCII(
154 GetParam().file_name);
157 embedded_test_server()->GetURL("/" + test_file_path.MaybeAsASCII());
158 ui_test_utils::NavigateToURL(browser(), url);
159 EXPECT_EQ(GetParam().encoding_name,
160 browser()->tab_strip_model()->GetActiveWebContents()->
164 INSTANTIATE_TEST_SUITE_P(EncodingAliases,
166 testing::ValuesIn(kEncodingTestDatas));
168 // The following encodings are excluded from the auto-detection test because
169 // it's a known issue that the current encoding detector does not detect them:
180 IN_PROC_BROWSER_TEST_F(BrowserEncodingTest, TestEncodingAutoDetect) {
181 struct EncodingAutoDetectTestData {
182 const char* test_file_name; // File name of test data.
183 const char* expected_result; // File name of expected results.
184 const char* expected_encoding; // expected encoding.
186 const EncodingAutoDetectTestData kTestDatas[] = {
187 { "Big5_with_no_encoding_specified.html",
188 "expected_Big5_saved_from_no_encoding_specified.html",
190 { "GBK_with_no_encoding_specified.html",
191 "expected_GBK_saved_from_no_encoding_specified.html",
193 { "iso-8859-1_with_no_encoding_specified.html",
194 "expected_iso-8859-1_saved_from_no_encoding_specified.html",
196 { "ISO-8859-5_with_no_encoding_specified.html",
197 "expected_ISO-8859-5_saved_from_no_encoding_specified.html",
199 { "ISO-8859-6_with_no_encoding_specified.html",
200 "expected_ISO-8859-6_saved_from_no_encoding_specified.html",
202 { "ISO-8859-7_with_no_encoding_specified.html",
203 "expected_ISO-8859-7_saved_from_no_encoding_specified.html",
205 { "ISO-8859-8-I_with_no_encoding_specified.html",
206 "expected_ISO-8859-8-I_saved_from_no_encoding_specified.html",
208 { "KOI8-R_with_no_encoding_specified.html",
209 "expected_KOI8-R_saved_from_no_encoding_specified.html",
211 { "Shift-JIS_with_no_encoding_specified.html",
212 "expected_Shift-JIS_saved_from_no_encoding_specified.html",
214 { "EUC-KR_with_no_encoding_specified.html",
215 "expected_EUC-KR_saved_from_no_encoding_specified.html",
217 { "windows-1251_with_no_encoding_specified.html",
218 "expected_windows-1251_saved_from_no_encoding_specified.html",
220 { "windows-1254_with_no_encoding_specified.html",
221 "expected_windows-1254_saved_from_no_encoding_specified.html",
223 { "windows-1255_with_no_encoding_specified.html",
224 "expected_windows-1255_saved_from_no_encoding_specified.html",
226 { "windows-1256_with_no_encoding_specified.html",
227 "expected_windows-1256_saved_from_no_encoding_specified.html",
230 const char* const kAutoDetectDir = "auto_detect";
231 // Directory of the files of expected results.
232 const char* const kExpectedResultDir = "expected_results";
234 base::FilePath test_dir_path =
235 base::FilePath(kTestDir).AppendASCII(kAutoDetectDir);
237 // Set the default charset to one of encodings not supported by the current
238 // auto-detector (Please refer to the above comments) to make sure we
239 // incorrectly decode the page. Now we use ISO-8859-4.
240 browser()->profile()->GetPrefs()->SetString(prefs::kDefaultCharset,
243 content::WebContents* web_contents =
244 browser()->tab_strip_model()->GetActiveWebContents();
245 for (size_t i = 0; i < base::size(kTestDatas); ++i) {
246 base::FilePath test_file_path(test_dir_path);
247 test_file_path = test_file_path.AppendASCII(kTestDatas[i].test_file_name);
249 embedded_test_server()->GetURL("/" + test_file_path.MaybeAsASCII());
250 ui_test_utils::NavigateToURL(browser(), url);
252 // Get the encoding of page. It should return the real encoding now.
253 EXPECT_EQ(kTestDatas[i].expected_encoding, web_contents->GetEncoding());
255 // Dump the page, the content of dump page should be equal with our expect
257 base::FilePath expected_result_file_name =
258 base::FilePath().AppendASCII(kAutoDetectDir).
259 AppendASCII(kExpectedResultDir).
260 AppendASCII(kTestDatas[i].expected_result);
261 SaveAndCompare(kTestDatas[i].test_file_name, expected_result_file_name,