[go: nahoru, domu]

blob: 597031a57974259b80c01ff9797dd622791e6bb0 [file] [log] [blame]
satorux@chromium.org9f632b82013-08-14 04:37:111// 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
satorux@chromium.org473c9a4f2013-09-05 03:09:465#include "chrome/browser/chromeos/file_manager/url_util.h"
satorux@chromium.org9f632b82013-08-14 04:37:116
dcheng24002d02016-04-08 02:42:407#include <memory>
8
satorux@chromium.org9f632b82013-08-14 04:37:119#include "base/files/file_path.h"
10#include "base/json/json_reader.h"
11#include "base/json/json_writer.h"
Satoshi Niwa481d94ec2019-04-30 07:46:0212#include "base/strings/stringprintf.h"
satorux@chromium.org9f632b82013-08-14 04:37:1113#include "base/strings/utf_string_conversions.h"
satorux@chromium.org0bce53f2013-08-14 14:40:1514#include "base/values.h"
Catherine Mullings3b967fb2017-06-27 02:13:0615#include "extensions/common/constants.h"
satorux@chromium.org9f632b82013-08-14 04:37:1116#include "net/base/escape.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19namespace file_manager {
20namespace util {
21namespace {
22
Sam McNallyae118e42018-10-16 09:33:5223// Parse a JSON query string into a base::Value.
24base::Value ParseJsonQueryString(const std::string& query) {
Matt Menke781e04d002019-05-05 23:56:1225 const std::string json = net::UnescapeBinaryURLComponent(query);
Lei Zhang582ecd12019-02-13 20:28:5426 std::unique_ptr<base::Value> value = base::JSONReader::ReadDeprecated(json);
Sam McNallyae118e42018-10-16 09:33:5227 return value ? std::move(*value) : base::Value();
28}
29
30// Pretty print the JSON escaped in the query string.
31std::string PrettyPrintEscapedJson(const std::string& query) {
satorux@chromium.org9f632b82013-08-14 04:37:1132 std::string pretty_json;
Sam McNallyae118e42018-10-16 09:33:5233 base::JSONWriter::WriteWithOptions(ParseJsonQueryString(query),
34 base::JSONWriter::OPTIONS_PRETTY_PRINT,
35 &pretty_json);
satorux@chromium.org9f632b82013-08-14 04:37:1136 return pretty_json;
37}
38
satorux@chromium.org9f632b82013-08-14 04:37:1139TEST(FileManagerUrlUtilTest, GetFileManagerMainPageUrl) {
Luciano Pacheco58cbde102021-05-31 06:03:0940 EXPECT_EQ("chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/main.html",
41 GetFileManagerMainPageUrl().spec());
satorux@chromium.org9f632b82013-08-14 04:37:1142}
43
44TEST(FileManagerUrlUtilTest, GetFileManagerMainPageUrlWithParams_NoFileTypes) {
45 const GURL url = GetFileManagerMainPageUrlWithParams(
Jan Wilken Dörrie2c470ea2021-03-22 22:26:2446 ui::SelectFileDialog::SELECT_OPEN_FILE, u"some title",
mtomasz@chromium.org304db10e2014-01-30 06:41:1247 GURL("filesystem:chrome-extension://abc/Downloads/"),
Stuart Langley0cd0da3452018-05-10 09:58:5848 GURL("filesystem:chrome-extension://abc/Downloads/foo.txt"), "foo.txt",
49 nullptr, // No file types
50 0, // Hence no file type index.
Satoshi Niwa5a5334532020-09-16 11:01:3051 "", // search_query
52 false // show_android_picker_apps
Satoshi Niwa481d94ec2019-04-30 07:46:0253 );
Catherine Mullings3b967fb2017-06-27 02:13:0654 EXPECT_EQ(extensions::kExtensionScheme, url.scheme());
satorux@chromium.org9f632b82013-08-14 04:37:1155 EXPECT_EQ("hhaomjibdihmijegdhdafkllkbggdgoj", url.host());
Luciano Pacheco58cbde102021-05-31 06:03:0956 EXPECT_EQ("/main.html", url.path());
satorux@chromium.org9f632b82013-08-14 04:37:1157 // Confirm that "%20" is used instead of "+" in the query.
58 EXPECT_TRUE(url.query().find("+") == std::string::npos);
59 EXPECT_TRUE(url.query().find("%20") != std::string::npos);
Sam McNallyae118e42018-10-16 09:33:5260 // With DriveFS, Drive is always allowed where native paths are.
Satoshi Niwa481d94ec2019-04-30 07:46:0261 EXPECT_EQ(base::StringPrintf(
62 "{\n"
Austin Tankiang786c9a152019-11-26 04:54:2963 " \"allowedPaths\": \"nativePath\",\n"
Satoshi Niwa481d94ec2019-04-30 07:46:0264 " \"currentDirectoryURL\": "
65 "\"filesystem:chrome-extension://abc/Downloads/\",\n"
Satoshi Niwa5a5334532020-09-16 11:01:3066 " \"searchQuery\": \"\",\n"
Satoshi Niwa481d94ec2019-04-30 07:46:0267 " \"selectionURL\": "
68 "\"filesystem:chrome-extension://abc/Downloads/foo.txt\",\n"
69 " \"showAndroidPickerApps\": false,\n"
70 " \"targetName\": \"foo.txt\",\n"
71 " \"title\": \"some title\",\n"
72 " \"type\": \"open-file\"\n"
Austin Tankiang53f411e52019-09-17 04:09:3373 "}\n"),
Satoshi Niwa481d94ec2019-04-30 07:46:0274 PrettyPrintEscapedJson(url.query()));
satorux@chromium.org9f632b82013-08-14 04:37:1175}
76
77TEST(FileManagerUrlUtilTest,
78 GetFileManagerMainPageUrlWithParams_WithFileTypes) {
79 // Create a FileTypeInfo which looks like:
80 // extensions: [["htm", "html"], ["txt"]]
81 // descriptions: ["HTML", "TEXT"]
82 ui::SelectFileDialog::FileTypeInfo file_types;
Stuart Langley0cd0da3452018-05-10 09:58:5883 file_types.extensions.emplace_back();
satorux@chromium.org9f632b82013-08-14 04:37:1184 file_types.extensions[0].push_back(FILE_PATH_LITERAL("htm"));
85 file_types.extensions[0].push_back(FILE_PATH_LITERAL("html"));
Stuart Langley0cd0da3452018-05-10 09:58:5886 file_types.extensions.emplace_back();
satorux@chromium.org9f632b82013-08-14 04:37:1187 file_types.extensions[1].push_back(FILE_PATH_LITERAL("txt"));
Jan Wilken Dörrie2c470ea2021-03-22 22:26:2488 file_types.extension_description_overrides.push_back(u"HTML");
89 file_types.extension_description_overrides.push_back(u"TEXT");
satorux@chromium.org9f632b82013-08-14 04:37:1190 // "shouldReturnLocalPath" will be false if drive is supported.
hirono4d4d3392016-02-04 05:03:5091 file_types.allowed_paths = ui::SelectFileDialog::FileTypeInfo::ANY_PATH;
satorux@chromium.org9f632b82013-08-14 04:37:1192
93 const GURL url = GetFileManagerMainPageUrlWithParams(
Jan Wilken Dörrie2c470ea2021-03-22 22:26:2494 ui::SelectFileDialog::SELECT_OPEN_FILE, u"some title",
mtomasz@chromium.org304db10e2014-01-30 06:41:1295 GURL("filesystem:chrome-extension://abc/Downloads/"),
Satoshi Niwa481d94ec2019-04-30 07:46:0296 GURL("filesystem:chrome-extension://abc/Downloads/foo.txt"), "foo.txt",
satorux@chromium.org9f632b82013-08-14 04:37:1197 &file_types,
98 1, // The file type index is 1-based.
Satoshi Niwa5a5334532020-09-16 11:01:3099 "search query",
Satoshi Niwa481d94ec2019-04-30 07:46:02100 true // show_android_picker_apps
101 );
Catherine Mullings3b967fb2017-06-27 02:13:06102 EXPECT_EQ(extensions::kExtensionScheme, url.scheme());
satorux@chromium.org9f632b82013-08-14 04:37:11103 EXPECT_EQ("hhaomjibdihmijegdhdafkllkbggdgoj", url.host());
Luciano Pacheco58cbde102021-05-31 06:03:09104 EXPECT_EQ("/main.html", url.path());
satorux@chromium.org9f632b82013-08-14 04:37:11105 // Confirm that "%20" is used instead of "+" in the query.
106 EXPECT_TRUE(url.query().find("+") == std::string::npos);
107 EXPECT_TRUE(url.query().find("%20") != std::string::npos);
108 // The escaped query is hard to read. Pretty print the escaped JSON.
hironoe820f8582016-02-18 02:39:52109 EXPECT_EQ(
110 "{\n"
111 " \"allowedPaths\": \"anyPath\",\n"
112 " \"currentDirectoryURL\": "
113 "\"filesystem:chrome-extension://abc/Downloads/\",\n"
hironoe820f8582016-02-18 02:39:52114 " \"includeAllFiles\": false,\n"
Satoshi Niwa5a5334532020-09-16 11:01:30115 " \"searchQuery\": \"search query\",\n"
hironoe820f8582016-02-18 02:39:52116 " \"selectionURL\": "
117 "\"filesystem:chrome-extension://abc/Downloads/foo.txt\",\n"
Satoshi Niwa481d94ec2019-04-30 07:46:02118 " \"showAndroidPickerApps\": true,\n"
hironoe820f8582016-02-18 02:39:52119 " \"targetName\": \"foo.txt\",\n"
120 " \"title\": \"some title\",\n"
121 " \"type\": \"open-file\",\n"
122 " \"typeList\": [ {\n"
123 " \"description\": \"HTML\",\n"
124 " \"extensions\": [ \"htm\", \"html\" ],\n"
125 " \"selected\": true\n"
126 " }, {\n"
127 " \"description\": \"TEXT\",\n"
128 " \"extensions\": [ \"txt\" ],\n"
129 " \"selected\": false\n"
130 " } ]\n"
131 "}\n",
132 PrettyPrintEscapedJson(url.query()));
satorux@chromium.org9f632b82013-08-14 04:37:11133}
134
satorux@chromium.org9f632b82013-08-14 04:37:11135} // namespace
136} // namespace util
137} // namespace file_manager