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
{
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(value
.get(),
27 base::JSONWriter::OPTIONS_PRETTY_PRINT
,
32 TEST(FileManagerUrlUtilTest
, GetFileManagerBaseUrl
) {
33 EXPECT_EQ("chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/",
34 GetFileManagerBaseUrl().spec());
37 TEST(FileManagerUrlUtilTest
, GetFileManagerMainPageUrl
) {
38 EXPECT_EQ("chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/main.html",
39 GetFileManagerMainPageUrl().spec());
42 TEST(FileManagerUrlUtilTest
, GetFileManagerMainPageUrlWithParams_NoFileTypes
) {
43 const GURL url
= GetFileManagerMainPageUrlWithParams(
44 ui::SelectFileDialog::SELECT_OPEN_FILE
,
45 base::UTF8ToUTF16("some title"),
46 GURL("filesystem:chrome-extension://abc/Downloads/"),
47 GURL("filesystem:chrome-extension://abc/Downloads/foo.txt"),
49 NULL
, // No file types
50 0, // Hence no file type index.
51 FILE_PATH_LITERAL("txt"));
52 EXPECT_EQ("chrome-extension", url
.scheme());
53 EXPECT_EQ("hhaomjibdihmijegdhdafkllkbggdgoj", url
.host());
54 EXPECT_EQ("/main.html", url
.path());
55 // Confirm that "%20" is used instead of "+" in the query.
56 EXPECT_TRUE(url
.query().find("+") == std::string::npos
);
57 EXPECT_TRUE(url
.query().find("%20") != std::string::npos
);
58 // The escaped query is hard to read. Pretty print the escaped JSON.
60 " \"currentDirectoryURL\": "
61 "\"filesystem:chrome-extension://abc/Downloads/\",\n"
62 " \"defaultExtension\": \"txt\",\n"
64 "\"filesystem:chrome-extension://abc/Downloads/foo.txt\",\n"
65 " \"shouldReturnLocalPath\": true,\n"
66 " \"targetName\": \"foo.txt\",\n"
67 " \"title\": \"some title\",\n"
68 " \"type\": \"open-file\"\n"
70 PrettyPrintEscapedJson(url
.query()));
73 TEST(FileManagerUrlUtilTest
,
74 GetFileManagerMainPageUrlWithParams_WithFileTypes
) {
75 // Create a FileTypeInfo which looks like:
76 // extensions: [["htm", "html"], ["txt"]]
77 // descriptions: ["HTML", "TEXT"]
78 ui::SelectFileDialog::FileTypeInfo file_types
;
79 file_types
.extensions
.push_back(std::vector
<base::FilePath::StringType
>());
80 file_types
.extensions
[0].push_back(FILE_PATH_LITERAL("htm"));
81 file_types
.extensions
[0].push_back(FILE_PATH_LITERAL("html"));
82 file_types
.extensions
.push_back(std::vector
<base::FilePath::StringType
>());
83 file_types
.extensions
[1].push_back(FILE_PATH_LITERAL("txt"));
84 file_types
.extension_description_overrides
.push_back(
85 base::UTF8ToUTF16("HTML"));
86 file_types
.extension_description_overrides
.push_back(
87 base::UTF8ToUTF16("TEXT"));
88 // "shouldReturnLocalPath" will be false if drive is supported.
89 file_types
.support_drive
= true;
91 const GURL url
= GetFileManagerMainPageUrlWithParams(
92 ui::SelectFileDialog::SELECT_OPEN_FILE
,
93 base::UTF8ToUTF16("some title"),
94 GURL("filesystem:chrome-extension://abc/Downloads/"),
95 GURL("filesystem:chrome-extension://abc/Downloads/foo.txt"),
98 1, // The file type index is 1-based.
99 FILE_PATH_LITERAL("txt"));
100 EXPECT_EQ("chrome-extension", url
.scheme());
101 EXPECT_EQ("hhaomjibdihmijegdhdafkllkbggdgoj", url
.host());
102 EXPECT_EQ("/main.html", url
.path());
103 // Confirm that "%20" is used instead of "+" in the query.
104 EXPECT_TRUE(url
.query().find("+") == std::string::npos
);
105 EXPECT_TRUE(url
.query().find("%20") != std::string::npos
);
106 // The escaped query is hard to read. Pretty print the escaped JSON.
108 " \"currentDirectoryURL\": "
109 "\"filesystem:chrome-extension://abc/Downloads/\",\n"
110 " \"defaultExtension\": \"txt\",\n"
111 " \"includeAllFiles\": false,\n"
112 " \"selectionURL\": "
113 "\"filesystem:chrome-extension://abc/Downloads/foo.txt\",\n"
114 " \"shouldReturnLocalPath\": false,\n"
115 " \"targetName\": \"foo.txt\",\n"
116 " \"title\": \"some title\",\n"
117 " \"type\": \"open-file\",\n"
118 " \"typeList\": [ {\n"
119 " \"description\": \"HTML\",\n"
120 " \"extensions\": [ \"htm\", \"html\" ],\n"
121 " \"selected\": true\n"
123 " \"description\": \"TEXT\",\n"
124 " \"extensions\": [ \"txt\" ],\n"
125 " \"selected\": false\n"
128 PrettyPrintEscapedJson(url
.query()));
133 } // namespace file_manager