File: fileapi_util_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (573 lines) | stat: -rw-r--r-- 24,205 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/file_manager/fileapi_util.h"

#include <memory>
#include <string>

#include "base/files/file_error_or.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/ash/file_system_provider/fake_extension_provider.h"
#include "chrome/browser/ash/file_system_provider/fake_provided_file_system.h"
#include "chrome/browser/ash/file_system_provider/service.h"
#include "chrome/browser/ash/file_system_provider/service_factory.h"
#include "chrome/browser/ash/fileapi/file_system_backend.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h"
#include "storage/browser/file_system/external_mount_points.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/browser/test/async_file_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/choosers/file_chooser.mojom.h"
#include "ui/shell_dialogs/selected_file_info.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace file_manager {
namespace util {
namespace {

// Helper class that sets up a temporary file system.
class TempFileSystem {
 public:
  TempFileSystem(Profile* profile, const GURL& appURL)
      : name_(base::UnguessableToken::Create().ToString()),
        appURL_(appURL),
        origin_(url::Origin::Create(appURL)),
        file_system_context_(
            GetFileSystemContextForSourceURL(profile, appURL)) {}

  ~TempFileSystem() {
    storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(name_);
  }

  // Finishes setting up the temporary file system. Must be called before use.
  bool SetUp() {
    if (!temp_dir_.CreateUniqueTempDir()) {
      return false;
    }
    if (!storage::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
            name_, storage::kFileSystemTypeLocal,
            storage::FileSystemMountOption(), temp_dir_.GetPath())) {
      return false;
    }

    // Grant the test extension the ability to access the just created
    // file system.
    ash::FileSystemBackend::Get(*file_system_context_)
        ->GrantFileAccessToOrigin(origin_, base::FilePath(name_));
    return true;
  }

  bool TearDown() {
    return storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
        name_);
  }

  // For the given FileSystemURL creates a file.
  base::File::Error CreateFile(const storage::FileSystemURL& url) {
    return storage::AsyncFileTestHelper::CreateFile(file_system_context_, url);
  }

  // For the given FileSystemURL creates a directory.
  base::File::Error CreateDirectory(const storage::FileSystemURL& url) {
    return storage::AsyncFileTestHelper::CreateDirectory(file_system_context_,
                                                         url);
  }

  // Creates an external file system URL for the given path.
  storage::FileSystemURL CreateFileSystemURL(const std::string& path) {
    return file_system_context_->CreateCrackedFileSystemURL(
        blink::StorageKey::CreateFirstParty(origin_),
        storage::kFileSystemTypeExternal,
        base::FilePath().Append(name_).Append(
            base::FilePath::FromUTF8Unsafe(path)));
  }

 private:
  const std::string name_;
  const GURL appURL_;
  const url::Origin origin_;
  const raw_ptr<storage::FileSystemContext> file_system_context_;
  base::ScopedTempDir temp_dir_;
};

class FileManagerFileAPIUtilTest : public ::testing::Test {
 public:
  // Carries information on how to create a FileSystemURL for a given file name.
  // For !valid orders we create a test URL. Otherwise, we use temp file system.
  struct FileSystemURLOrder {
    std::string file_name;
    bool valid;
  };

  void SetUp() override {
    testing::Test::SetUp();
    profile_manager_ = std::make_unique<TestingProfileManager>(
        TestingBrowserProcess::GetGlobal());
    ASSERT_TRUE(profile_manager_->SetUp());
    profile_ = profile_manager_->CreateTestingProfile("testing_profile");
  }

  void TearDown() override {
    profile_manager_->DeleteAllTestingProfiles();
    profile_ = nullptr;
    profile_manager_.reset();
  }

  TestingProfile* GetProfile() { return profile_; }

 protected:
  // Checks if the conversion of FileDefinition to EntryDefinition works
  // correctly for the given |appURLStr| and a set of |orders|. If the
  // order indicates that the file should not be created, we expect the
  // conversion to return base::File::FILE_ERROR_NOT_FOUND error. Otherwise,
  // we expect base::File::FILE_OK status.
  void CheckConvertFileDefinitionListToEntryDefinitionList(
      const std::string& appURLStr,
      const std::vector<FileSystemURLOrder>& orders) {
    GURL appURL(appURLStr);
    ASSERT_TRUE(appURL.is_valid());
    auto temp_file_system = std::make_unique<TempFileSystem>(profile_, appURL);
    ASSERT_TRUE(temp_file_system->SetUp());

    std::vector<base::File::Error> errors;
    std::vector<FileDefinition> file_definitions;
    for (const FileSystemURLOrder& order : orders) {
      storage::FileSystemURL fs_url;
      if (order.valid) {
        fs_url = temp_file_system->CreateFileSystemURL(order.file_name);
        errors.push_back(base::File::FILE_OK);
      } else {
        fs_url = storage::FileSystemURL::CreateForTest(
            blink::StorageKey::CreateFirstParty(url::Origin::Create(appURL)),
            storage::kFileSystemTypeExternal, base::FilePath(order.file_name));
        errors.push_back(base::File::FILE_ERROR_NOT_FOUND);
      }
      file_definitions.push_back({.virtual_path = fs_url.virtual_path()});
    }

    base::RunLoop run_loop;
    EntryDefinitionListCallback callback = base::BindOnce(
        [](std::unique_ptr<TempFileSystem> temp_file_system,
           std::vector<base::File::Error> errors,
           base::OnceClosure quit_closure,
           std::unique_ptr<EntryDefinitionList> entries) {
          ASSERT_EQ(errors.size(), entries->size());
          for (size_t i = 0; i < errors.size(); ++i) {
            const EntryDefinition& entry_def = (*entries)[i];
            EXPECT_EQ(errors[i], entry_def.error)
                << "for " << entry_def.full_path << " at " << i;
          }

          EXPECT_TRUE(temp_file_system->TearDown());
          std::move(quit_closure).Run();
        },
        std::move(temp_file_system), std::move(errors), run_loop.QuitClosure());
    ConvertFileDefinitionListToEntryDefinitionList(
        GetFileSystemContextForSourceURL(profile_, appURL),
        url::Origin::Create(appURL), file_definitions, std::move(callback));
    run_loop.Run();
  }

  void TestGenerateUnusedFilename(std::vector<std::string> existing_files,
                                  std::string target_filename,
                                  base::FileErrorOr<std::string> expected);

  const std::string file_system_id_ = "test-filesystem";

 private:
  base::test::ScopedFeatureList feature_list_;
  content::BrowserTaskEnvironment task_environment_;
  std::unique_ptr<TestingProfileManager> profile_manager_;
  raw_ptr<TestingProfile, DanglingUntriaged> profile_;
};

// Passes the |result| to the |output| pointer.
void PassFileChooserFileInfoList(FileChooserFileInfoList* output,
                                 FileChooserFileInfoList result) {
  for (const auto& file : result) {
    output->push_back(file->Clone());
  }
}

TEST_F(FileManagerFileAPIUtilTest,
       ConvertSelectedFileInfoListToFileChooserFileInfoList) {
  Profile* const profile = GetProfile();
  const std::string extension_id = "abc";
  auto fake_provider =
      ash::file_system_provider::FakeExtensionProvider::Create(extension_id);
  const auto kProviderId = fake_provider->GetId();
  auto* service = ash::file_system_provider::Service::Get(profile);
  service->RegisterProvider(std::move(fake_provider));
  service->MountFileSystem(
      kProviderId, ash::file_system_provider::MountOptions(file_system_id_,
                                                           "Test FileSystem"));

  // Obtain the file system context.
  content::StoragePartition* const partition =
      profile->GetStoragePartitionForUrl(GURL("http://example.com"));
  ASSERT_TRUE(partition);
  storage::FileSystemContext* const context = partition->GetFileSystemContext();
  ASSERT_TRUE(context);

  // Prepare the test input.
  SelectedFileInfoList selected_info_list;

  // Native file.
  {
    ui::SelectedFileInfo info;
    info.file_path = base::FilePath(FILE_PATH_LITERAL("/native/File 1.txt"));
    info.local_path = base::FilePath(FILE_PATH_LITERAL("/native/File 1.txt"));
    info.display_name = "display_name";
    selected_info_list.push_back(info);
  }

  const std::string path = FILE_PATH_LITERAL(base::StrCat(
      {"/provided/", extension_id, ":", file_system_id_, ":/hello.txt"}));
  // Non-native file with cache.
  {
    ui::SelectedFileInfo info;
    info.file_path = base::FilePath(path);
    info.local_path = base::FilePath(FILE_PATH_LITERAL("/native/cache/xxx"));
    info.display_name = "display_name";
    selected_info_list.push_back(info);
  }

  // Non-native file without.
  {
    ui::SelectedFileInfo info;
    info.file_path = base::FilePath(path);
    selected_info_list.push_back(info);
  }

  // Run the test target.
  FileChooserFileInfoList result;
  ConvertSelectedFileInfoListToFileChooserFileInfoList(
      context, url::Origin::Create(GURL("http://example.com")),
      selected_info_list,
      base::BindOnce(&PassFileChooserFileInfoList, &result));
  content::RunAllTasksUntilIdle();

  // Check the result.
  ASSERT_EQ(3u, result.size());

  EXPECT_TRUE(result[0]->is_native_file());
  EXPECT_EQ(FILE_PATH_LITERAL("/native/File 1.txt"),
            result[0]->get_native_file()->file_path.value());
  EXPECT_EQ(u"display_name", result[0]->get_native_file()->display_name);

  EXPECT_TRUE(result[1]->is_native_file());
  EXPECT_EQ(FILE_PATH_LITERAL("/native/cache/xxx"),
            result[1]->get_native_file()->file_path.value());
  EXPECT_EQ(u"display_name", result[1]->get_native_file()->display_name);

  EXPECT_TRUE(result[2]->is_file_system());
  EXPECT_TRUE(result[2]->get_file_system()->url.is_valid());
  const storage::FileSystemURL url =
      context->CrackURLInFirstPartyContext(result[2]->get_file_system()->url);
  EXPECT_EQ(GURL("http://example.com"), url.origin().GetURL());
  EXPECT_EQ(storage::kFileSystemTypeIsolated, url.mount_type());
  EXPECT_EQ(storage::kFileSystemTypeProvided, url.type());
  EXPECT_EQ(55u, result[2]->get_file_system()->length);
}

TEST_F(FileManagerFileAPIUtilTest,
       ConvertFileDefinitionListToEntryDefinitionListExtension) {
  std::vector<FileSystemURLOrder> orders = {
      {.file_name = "x.txt", .valid = true},
      {.file_name = "no-such-file.txt", .valid = false},
      {.file_name = "z.txt", .valid = true},
  };
  CheckConvertFileDefinitionListToEntryDefinitionList("chrome-extension://abc",
                                                      orders);
  CheckConvertFileDefinitionListToEntryDefinitionList("chrome-extension://abc/",
                                                      orders);
  CheckConvertFileDefinitionListToEntryDefinitionList(
      "chrome-extension://abc/efg", orders);
}

TEST_F(FileManagerFileAPIUtilTest,
       ConvertFileDefinitionListToEntryDefinitionListApp) {
  std::vector<FileSystemURLOrder> orders = {
      {.file_name = "a.txt", .valid = false},
      {.file_name = "b.txt", .valid = false},
      {.file_name = "i-am-a-file.txt", .valid = true},
  };
  CheckConvertFileDefinitionListToEntryDefinitionList("chrome://file-manager",
                                                      orders);
  CheckConvertFileDefinitionListToEntryDefinitionList("chrome://file-manager/",
                                                      orders);
  CheckConvertFileDefinitionListToEntryDefinitionList(
      "chrome://file-manager/abc", orders);
}

TEST_F(FileManagerFileAPIUtilTest,
       ConvertFileDefinitionListToEntryDefinitionNullContext) {
  Profile* const profile = GetProfile();
  const GURL appURL("chrome-extension://abc/");
  auto temp_file_system = std::make_unique<TempFileSystem>(profile, appURL);
  ASSERT_TRUE(temp_file_system->SetUp());
  storage::FileSystemURL x_file_url =
      temp_file_system->CreateFileSystemURL(".");
  FileDefinition x_fd = {.virtual_path = x_file_url.virtual_path()};

  // Check a simple case where the context is already null before we have
  // a chance to call the conversion function.
  base::RunLoop run_loop;
  EntryDefinitionListCallback callback = base::BindOnce(
      [](std::unique_ptr<TempFileSystem> temp_file_system,
         base::OnceClosure quit_closure,
         std::unique_ptr<EntryDefinitionList> entries) {
        ASSERT_EQ(1u, entries->size());
        EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
                  entries->at(0).error);
        EXPECT_TRUE(temp_file_system->TearDown());
        std::move(quit_closure).Run();
      },
      std::move(temp_file_system), run_loop.QuitClosure());

  ConvertFileDefinitionListToEntryDefinitionList(
      nullptr, url::Origin::Create(appURL), {x_fd}, std::move(callback));
  run_loop.Run();
}

TEST_F(FileManagerFileAPIUtilTest,
       ConvertFileDefinitionListToEntryDefinitionContextReset) {
  Profile* const profile = GetProfile();
  const GURL appURL("chrome-extension://abc/");
  auto temp_file_system = std::make_unique<TempFileSystem>(profile, appURL);
  ASSERT_TRUE(temp_file_system->SetUp());
  storage::FileSystemURL x_file_url =
      temp_file_system->CreateFileSystemURL(".");
  FileDefinition x_fd = {.virtual_path = x_file_url.virtual_path()};
  scoped_refptr<storage::FileSystemContext> file_system_context =
      GetFileSystemContextForSourceURL(profile, appURL);

  base::RunLoop run_loop;
  EntryDefinitionListCallback callback = base::BindOnce(
      [](std::unique_ptr<TempFileSystem> temp_file_system,
         base::OnceClosure quit_closure,
         std::unique_ptr<EntryDefinitionList> entries) {
        ASSERT_EQ(1u, entries->size());
        EXPECT_EQ(base::File::FILE_OK, entries->at(0).error);
        EXPECT_TRUE(temp_file_system->TearDown());
        std::move(quit_closure).Run();
      },
      std::move(temp_file_system), run_loop.QuitClosure());

  // Check the case where the context is not null, but is reset to null as
  // soon as function call is completed. Conversion takes place on a
  // different thread, after the function call returns. However, since
  // it holds to a copy of a scoped pointer we expect it to succeed.
  ConvertFileDefinitionListToEntryDefinitionList(file_system_context,
                                                 url::Origin::Create(appURL),
                                                 {x_fd}, std::move(callback));
  file_system_context.reset();

  run_loop.Run();
}

TEST_F(FileManagerFileAPIUtilTest, IsFileManagerURL) {
  EXPECT_TRUE(IsFileManagerURL(GetFileManagerURL()));
  EXPECT_TRUE(IsFileManagerURL(GetFileManagerURL().Resolve("/some/path")));
  EXPECT_TRUE(IsFileManagerURL(
      GetFileManagerURL().Resolve("/some/path").Resolve("#anchor")));
  EXPECT_TRUE(IsFileManagerURL(GetFileManagerURL()
                                   .Resolve("/some/path")
                                   .Resolve("#anchor")
                                   .Resolve("?a=b")));
  EXPECT_FALSE(IsFileManagerURL(GURL("chrome://not-file-manager")));
  EXPECT_FALSE(IsFileManagerURL(GURL("chrome://not-file-manager/")));
  EXPECT_FALSE(IsFileManagerURL(
      GURL("chrome-extension://iamnotafilemanagerextensionid")));
  EXPECT_FALSE(IsFileManagerURL(
      GURL("chrome-extension://iamnotafilemanagerextensionid/")));
}

void FileManagerFileAPIUtilTest::TestGenerateUnusedFilename(
    std::vector<std::string> existing_files,
    std::string target_filename,
    base::FileErrorOr<std::string> expected) {
  const GURL appURL("chrome-extension://abc/");
  auto temp_file_system =
      std::make_unique<TempFileSystem>(GetProfile(), appURL);
  ASSERT_TRUE(temp_file_system->SetUp());
  storage::FileSystemURL root_url = temp_file_system->CreateFileSystemURL("");
  scoped_refptr<storage::FileSystemContext> file_system_context =
      GetFileSystemContextForSourceURL(GetProfile(), appURL);

  for (const std::string& file : existing_files) {
    if (file.back() == '/') {
      temp_file_system->CreateDirectory(
          temp_file_system->CreateFileSystemURL(file));
    } else {
      temp_file_system->CreateFile(temp_file_system->CreateFileSystemURL(file));
    }
  }

  base::RunLoop run_loop;
  GenerateUnusedFilename(
      root_url, base::FilePath(target_filename), file_system_context,
      base::BindLambdaForTesting(
          [&](base::FileErrorOr<storage::FileSystemURL> result) {
            if (!expected.has_value()) {
              EXPECT_FALSE(result.has_value())
                  << "Unexpected result " << result->ToGURL();
              EXPECT_EQ(expected.error(), result.error());
            } else {
              EXPECT_TRUE(result.has_value())
                  << "Unexpected error " << result.error();
              EXPECT_EQ(temp_file_system->CreateFileSystemURL(expected.value())
                            .ToGURL(),
                        result->ToGURL());
            }
            run_loop.Quit();
          }));
  run_loop.Run();
}

TEST_F(FileManagerFileAPIUtilTest, GenerateUnusedFilenameBasic) {
  TestGenerateUnusedFilename({}, "foo.bar", {"foo.bar"});
  TestGenerateUnusedFilename({"foo.bar"}, "foo.bar", {"foo (1).bar"});
  TestGenerateUnusedFilename({"foo.bar/"}, "foo.bar", {"foo (1).bar"});
  TestGenerateUnusedFilename({"foo (1).bar"}, "foo.bar", {"foo.bar"});
  TestGenerateUnusedFilename({"foo.bar", "foo (1).bar"}, "foo.bar",
                             {"foo (2).bar"});
  TestGenerateUnusedFilename({"foo.bar", "foo (1).bar/"}, "foo.bar",
                             {"foo (2).bar"});
  TestGenerateUnusedFilename({"foo.bar", "foo (2).bar"}, "foo.bar",
                             {"foo (1).bar"});
  TestGenerateUnusedFilename({"foo.bar/", "foo (1).bar"}, "foo (1).bar",
                             {"foo (2).bar"});
  TestGenerateUnusedFilename({"foo (3).bar"}, "foo (3).bar", {"foo (1).bar"});
  TestGenerateUnusedFilename({"foo (2).bar"}, "foo (1).bar", {"foo (1).bar"});
  TestGenerateUnusedFilename({"foo (2) (1).bar"}, "foo (2) (1).bar",
                             {"foo (2) (2).bar"});
  TestGenerateUnusedFilename({"foo (2) (2).bar"}, "foo (2) (2).bar",
                             {"foo (2) (1).bar"});
  TestGenerateUnusedFilename({}, " foo.bar", {" foo.bar"});
  TestGenerateUnusedFilename({" foo.bar"}, " foo.bar", {" foo (1).bar"});
  TestGenerateUnusedFilename({"foo.bar"}, " foo.bar", {" foo.bar"});
}

TEST_F(FileManagerFileAPIUtilTest, GenerateUnusedFilenameNewLine) {
  TestGenerateUnusedFilename({}, "new\nline.bar", {"new\nline.bar"});
  TestGenerateUnusedFilename({"new\nline.bar"}, "new\nline.bar",
                             {"new\nline (1).bar"});
  TestGenerateUnusedFilename({"new\nline.bar", "new\nline (1).bar"},
                             "new\nline.bar", {"new\nline (2).bar"});
  TestGenerateUnusedFilename({}, "new\nline (\n)", {"new\nline (\n)"});
  TestGenerateUnusedFilename({"new\nline (\n)"}, "new\nline (\n)",
                             {"new\nline (\n) (1)"});
}

TEST_F(FileManagerFileAPIUtilTest, GenerateUnusedFilenameUnicode) {
  TestGenerateUnusedFilename({}, "é è ê ô œ.txt€", {"é è ê ô œ.txt€"});
  TestGenerateUnusedFilename({"é è ê ô œ.txt€"}, "é è ê ô œ.txt€",
                             {"é è ê ô œ (1).txt€"});
}

TEST_F(FileManagerFileAPIUtilTest, GenerateUnusedFilenameNoExtension) {
  TestGenerateUnusedFilename({}, "no-ext", {"no-ext"});
  TestGenerateUnusedFilename({"no-ext"}, "no-ext", {"no-ext (1)"});
  TestGenerateUnusedFilename({"no-ext/"}, "no-ext", {"no-ext (1)"});
  TestGenerateUnusedFilename({"no-ext (1)"}, "no-ext (1)", {"no-ext (2)"});

  TestGenerateUnusedFilename({}, "a", {"a"});
  TestGenerateUnusedFilename({"a"}, "a", {"a (1)"});
  TestGenerateUnusedFilename({"a/"}, "a", {"a (1)"});
}

TEST_F(FileManagerFileAPIUtilTest, GenerateUnusedFilenameDoubleExtension) {
  TestGenerateUnusedFilename({}, "double.ext.10.13.txt",
                             {"double.ext.10.13.txt"});
  TestGenerateUnusedFilename({"double.ext.10.13.txt"}, "double.ext.10.13.txt",
                             {"double.ext.10.13 (1).txt"});
  TestGenerateUnusedFilename({"double.ext.10.13.txt/"}, "double.ext.10.13.txt",
                             {"double.ext.10.13 (1).txt"});

  TestGenerateUnusedFilename({}, "archive.tar.gz", {"archive.tar.gz"});
  TestGenerateUnusedFilename({"archive.tar.gz"}, "archive.tar.gz",
                             {"archive (1).tar.gz"});
}

TEST_F(FileManagerFileAPIUtilTest, GenerateUnusedFilenameInvalidFilename) {
  TestGenerateUnusedFilename(
      {}, "", base::unexpected(base::File::FILE_ERROR_INVALID_OPERATION));
  TestGenerateUnusedFilename(
      {}, "path/with/slashes",
      base::unexpected(base::File::FILE_ERROR_INVALID_OPERATION));
}

TEST_F(FileManagerFileAPIUtilTest, GenerateUnusedFilenameFileSystemProvider) {
  Profile* const profile = GetProfile();
  const std::string extension_id = "abc";

  // Create and mount the FileSystemProvider.
  auto fake_provider =
      ash::file_system_provider::FakeExtensionProvider::Create(extension_id);
  const auto kProviderId = fake_provider->GetId();
  auto* service = ash::file_system_provider::Service::Get(profile);
  service->RegisterProvider(std::move(fake_provider));
  const base::File::Error result = service->MountFileSystem(
      kProviderId, ash::file_system_provider::MountOptions(file_system_id_,
                                                           "Test FileSystem"));
  ASSERT_EQ(base::File::FILE_OK, result);

  auto* provided_file_system =
      static_cast<ash::file_system_provider::FakeProvidedFileSystem*>(
          service->GetProvidedFileSystem(kProviderId, file_system_id_));
  ASSERT_TRUE(provided_file_system);
  const base::FilePath mount_point_name =
      provided_file_system->GetFileSystemInfo().mount_path().BaseName();

  const std::string origin = "chrome-extension://abc/";
  storage::FileSystemContext* const context =
      GetFileSystemContextForSourceURL(profile, GURL(origin));
  ASSERT_TRUE(context);

  // Make sure we can access the filesystem from the above origin.
  ash::FileSystemBackend::Get(*context)->GrantFileAccessToOrigin(
      url::Origin::Create(GURL(origin)), base::FilePath(mount_point_name));

  const storage::ExternalMountPoints* const mount_points =
      storage::ExternalMountPoints::GetSystemInstance();
  auto destination_folder_url = mount_points->CreateCrackedFileSystemURL(
      blink::StorageKey::CreateFromStringForTesting(origin),
      storage::kFileSystemTypeExternal, mount_point_name);
  auto expected_url = mount_points->CreateCrackedFileSystemURL(
      blink::StorageKey::CreateFromStringForTesting(origin),
      storage::kFileSystemTypeExternal,
      mount_point_name.Append("hello (1).txt"));

  base::RunLoop run_loop;
  GenerateUnusedFilename(
      destination_folder_url,
      base::FilePath(ash::file_system_provider::kFakeFilePath).BaseName(),
      context,
      base::BindLambdaForTesting(
          [&](base::FileErrorOr<storage::FileSystemURL> result) {
            EXPECT_TRUE(result.has_value())
                << "Unexpected error " << result.error();
            EXPECT_EQ(expected_url.ToGURL(), result->ToGURL());
            run_loop.Quit();
          }));
  run_loop.Run();
}

}  // namespace
}  // namespace util
}  // namespace file_manager