OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/chromeos/extensions/file_manager/url_util.h" | |
6 | |
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" | |
15 | |
16 namespace file_manager { | |
17 namespace util { | |
18 namespace { | |
19 | |
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, | |
28 &pretty_json); | |
29 return pretty_json; | |
30 } | |
31 | |
32 TEST(FileManagerUrlUtilTest, GetFileManagerBaseUrl) { | |
33 EXPECT_EQ("chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/", | |
34 GetFileManagerBaseUrl().spec()); | |
35 } | |
36 | |
37 TEST(FileManagerUrlUtilTest, GetFileManagerMainPageUrl) { | |
38 EXPECT_EQ("chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/main.html", | |
39 GetFileManagerMainPageUrl().spec()); | |
40 } | |
41 | |
42 TEST(FileManagerUrlUtilTest, GetFileManagerMainPageUrlWithParams_NoFileTypes) { | |
43 const GURL url = GetFileManagerMainPageUrlWithParams( | |
44 ui::SelectFileDialog::SELECT_OPEN_FILE, | |
45 base::UTF8ToUTF16("some title"), | |
46 base::FilePath::FromUTF8Unsafe("foo.txt"), | |
47 NULL, // No file types | |
48 0, // Hence no file type index. | |
49 FILE_PATH_LITERAL("txt")); | |
50 EXPECT_EQ("chrome-extension", url.scheme()); | |
51 EXPECT_EQ("hhaomjibdihmijegdhdafkllkbggdgoj", url.host()); | |
52 EXPECT_EQ("/main.html", url.path()); | |
53 // Confirm that "%20" is used instead of "+" in the query. | |
54 EXPECT_TRUE(url.query().find("+") == std::string::npos); | |
55 EXPECT_TRUE(url.query().find("%20") != std::string::npos); | |
56 // The escaped query is hard to read. Pretty print the escaped JSON. | |
57 EXPECT_EQ("{\n" | |
58 " \"defaultExtension\": \"txt\",\n" | |
59 " \"defaultPath\": \"foo.txt\",\n" | |
60 " \"shouldReturnLocalPath\": true,\n" | |
61 " \"title\": \"some title\",\n" | |
62 " \"type\": \"open-file\"\n" | |
63 "}\n", | |
64 PrettyPrintEscapedJson(url.query())); | |
65 } | |
66 | |
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; | |
84 | |
85 const GURL url = GetFileManagerMainPageUrlWithParams( | |
86 ui::SelectFileDialog::SELECT_OPEN_FILE, | |
87 base::UTF8ToUTF16("some title"), | |
88 base::FilePath::FromUTF8Unsafe("foo.txt"), | |
89 &file_types, | |
90 1, // The file type index is 1-based. | |
91 FILE_PATH_LITERAL("txt")); | |
92 EXPECT_EQ("chrome-extension", url.scheme()); | |
93 EXPECT_EQ("hhaomjibdihmijegdhdafkllkbggdgoj", url.host()); | |
94 EXPECT_EQ("/main.html", url.path()); | |
95 // Confirm that "%20" is used instead of "+" in the query. | |
96 EXPECT_TRUE(url.query().find("+") == std::string::npos); | |
97 EXPECT_TRUE(url.query().find("%20") != std::string::npos); | |
98 // The escaped query is hard to read. Pretty print the escaped JSON. | |
99 EXPECT_EQ("{\n" | |
100 " \"defaultExtension\": \"txt\",\n" | |
101 " \"defaultPath\": \"foo.txt\",\n" | |
102 " \"includeAllFiles\": false,\n" | |
103 " \"shouldReturnLocalPath\": false,\n" | |
104 " \"title\": \"some title\",\n" | |
105 " \"type\": \"open-file\",\n" | |
106 " \"typeList\": [ {\n" | |
107 " \"description\": \"HTML\",\n" | |
108 " \"extensions\": [ \"htm\", \"html\" ],\n" | |
109 " \"selected\": true\n" | |
110 " }, {\n" | |
111 " \"description\": \"TEXT\",\n" | |
112 " \"extensions\": [ \"txt\" ],\n" | |
113 " \"selected\": false\n" | |
114 " } ]\n" | |
115 "}\n", | |
116 PrettyPrintEscapedJson(url.query())); | |
117 } | |
118 | |
119 TEST(FileManagerUrlUtilTest, GetActionChoiceUrl_RegularMode) { | |
120 EXPECT_EQ("chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/" | |
121 "action_choice.html#/foo.txt", | |
122 GetActionChoiceUrl(base::FilePath::FromUTF8Unsafe("foo.txt"), | |
123 false).spec()); | |
124 } | |
125 | |
126 TEST(FileManagerUrlUtilTest, GetActionChoiceUrl_AdvancedMode) { | |
127 EXPECT_EQ("chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/" | |
128 "action_choice.html?advanced-mode#/foo.txt", | |
129 GetActionChoiceUrl(base::FilePath::FromUTF8Unsafe("foo.txt"), | |
130 true).spec()); | |
131 } | |
132 | |
133 } // namespace | |
134 } // namespace util | |
135 } // namespace file_manager | |
OLD | NEW |