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