Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / file_manager / url_util_unittest.cc
bloba23ee30ef01cbab5001395328e5eed89c5a2ceb7
1 // Copyright 2013 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.
5 #include "chrome/browser/chromeos/file_manager/url_util.h"
7 #include "base/files/file_path.h"
8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "net/base/escape.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace file_manager {
17 namespace util {
18 namespace {
20 // Pretty print the JSON escaped in the query string.
21 std::string PrettyPrintEscapedJson(const std::string& query) {
22 const std::string json = net::UnescapeURLComponent(
23 query, net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS);
24 scoped_ptr<base::Value> value = base::JSONReader::Read(json);
25 std::string pretty_json;
26 base::JSONWriter::WriteWithOptions(
27 *value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &pretty_json);
28 return pretty_json;
31 TEST(FileManagerUrlUtilTest, GetFileManagerMainPageUrl) {
32 EXPECT_EQ("chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/main.html",
33 GetFileManagerMainPageUrl().spec());
36 TEST(FileManagerUrlUtilTest, GetFileManagerMainPageUrlWithParams_NoFileTypes) {
37 const GURL url = GetFileManagerMainPageUrlWithParams(
38 ui::SelectFileDialog::SELECT_OPEN_FILE,
39 base::UTF8ToUTF16("some title"),
40 GURL("filesystem:chrome-extension://abc/Downloads/"),
41 GURL("filesystem:chrome-extension://abc/Downloads/foo.txt"),
42 "foo.txt",
43 NULL, // No file types
44 0, // Hence no file type index.
45 FILE_PATH_LITERAL("txt"));
46 EXPECT_EQ("chrome-extension", url.scheme());
47 EXPECT_EQ("hhaomjibdihmijegdhdafkllkbggdgoj", url.host());
48 EXPECT_EQ("/main.html", url.path());
49 // Confirm that "%20" is used instead of "+" in the query.
50 EXPECT_TRUE(url.query().find("+") == std::string::npos);
51 EXPECT_TRUE(url.query().find("%20") != std::string::npos);
52 // The escaped query is hard to read. Pretty print the escaped JSON.
53 EXPECT_EQ("{\n"
54 " \"currentDirectoryURL\": "
55 "\"filesystem:chrome-extension://abc/Downloads/\",\n"
56 " \"defaultExtension\": \"txt\",\n"
57 " \"selectionURL\": "
58 "\"filesystem:chrome-extension://abc/Downloads/foo.txt\",\n"
59 " \"shouldReturnLocalPath\": true,\n"
60 " \"targetName\": \"foo.txt\",\n"
61 " \"title\": \"some title\",\n"
62 " \"type\": \"open-file\"\n"
63 "}\n",
64 PrettyPrintEscapedJson(url.query()));
67 TEST(FileManagerUrlUtilTest,
68 GetFileManagerMainPageUrlWithParams_WithFileTypes) {
69 // Create a FileTypeInfo which looks like:
70 // extensions: [["htm", "html"], ["txt"]]
71 // descriptions: ["HTML", "TEXT"]
72 ui::SelectFileDialog::FileTypeInfo file_types;
73 file_types.extensions.push_back(std::vector<base::FilePath::StringType>());
74 file_types.extensions[0].push_back(FILE_PATH_LITERAL("htm"));
75 file_types.extensions[0].push_back(FILE_PATH_LITERAL("html"));
76 file_types.extensions.push_back(std::vector<base::FilePath::StringType>());
77 file_types.extensions[1].push_back(FILE_PATH_LITERAL("txt"));
78 file_types.extension_description_overrides.push_back(
79 base::UTF8ToUTF16("HTML"));
80 file_types.extension_description_overrides.push_back(
81 base::UTF8ToUTF16("TEXT"));
82 // "shouldReturnLocalPath" will be false if drive is supported.
83 file_types.support_drive = true;
85 const GURL url = GetFileManagerMainPageUrlWithParams(
86 ui::SelectFileDialog::SELECT_OPEN_FILE,
87 base::UTF8ToUTF16("some title"),
88 GURL("filesystem:chrome-extension://abc/Downloads/"),
89 GURL("filesystem:chrome-extension://abc/Downloads/foo.txt"),
90 "foo.txt",
91 &file_types,
92 1, // The file type index is 1-based.
93 FILE_PATH_LITERAL("txt"));
94 EXPECT_EQ("chrome-extension", url.scheme());
95 EXPECT_EQ("hhaomjibdihmijegdhdafkllkbggdgoj", url.host());
96 EXPECT_EQ("/main.html", url.path());
97 // Confirm that "%20" is used instead of "+" in the query.
98 EXPECT_TRUE(url.query().find("+") == std::string::npos);
99 EXPECT_TRUE(url.query().find("%20") != std::string::npos);
100 // The escaped query is hard to read. Pretty print the escaped JSON.
101 EXPECT_EQ("{\n"
102 " \"currentDirectoryURL\": "
103 "\"filesystem:chrome-extension://abc/Downloads/\",\n"
104 " \"defaultExtension\": \"txt\",\n"
105 " \"includeAllFiles\": false,\n"
106 " \"selectionURL\": "
107 "\"filesystem:chrome-extension://abc/Downloads/foo.txt\",\n"
108 " \"shouldReturnLocalPath\": false,\n"
109 " \"targetName\": \"foo.txt\",\n"
110 " \"title\": \"some title\",\n"
111 " \"type\": \"open-file\",\n"
112 " \"typeList\": [ {\n"
113 " \"description\": \"HTML\",\n"
114 " \"extensions\": [ \"htm\", \"html\" ],\n"
115 " \"selected\": true\n"
116 " }, {\n"
117 " \"description\": \"TEXT\",\n"
118 " \"extensions\": [ \"txt\" ],\n"
119 " \"selected\": false\n"
120 " } ]\n"
121 "}\n",
122 PrettyPrintEscapedJson(url.query()));
125 } // namespace
126 } // namespace util
127 } // namespace file_manager