1// Copyright 2014 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/fileapi/external_file_url_util.h"
6
7#include "base/strings/utf_string_conversions.h"
8#include "chrome/browser/chromeos/drive/file_system_util.h"
9#include "chrome/test/base/testing_browser_process.h"
10#include "chrome/test/base/testing_profile_manager.h"
11#include "content/public/test/test_browser_thread_bundle.h"
12#include "storage/browser/fileapi/file_system_url.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15namespace chromeos {
16
17namespace {
18
19// Sets up ProfileManager for testing and marks the current thread as UI by
20// TestBrowserThreadBundle. We need the thread since Profile objects must be
21// touched from UI and hence has CHECK/DCHECKs for it.
22class ExternalFileURLUtilTest : public testing::Test {
23 protected:
24  ExternalFileURLUtilTest()
25      : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {}
26
27  virtual void SetUp() OVERRIDE {
28    ASSERT_TRUE(testing_profile_manager_.SetUp());
29  }
30
31  TestingProfileManager& testing_profile_manager() {
32    return testing_profile_manager_;
33  }
34
35  storage::FileSystemURL CreateExpectedURL(const base::FilePath& path) {
36    return storage::FileSystemURL::CreateForTest(
37        GURL("chrome-extension://xxx"),
38        storage::kFileSystemTypeExternal,
39        base::FilePath("drive-test-user-hash").Append(path),
40        "",
41        storage::kFileSystemTypeDrive,
42        base::FilePath(),
43        "",
44        storage::FileSystemMountOption());
45  }
46
47 private:
48  content::TestBrowserThreadBundle thread_bundle_;
49  TestingProfileManager testing_profile_manager_;
50};
51
52}  // namespace
53
54TEST_F(ExternalFileURLUtilTest, FilePathToExternalFileURL) {
55  storage::FileSystemURL url;
56
57  // Path with alphabets and numbers.
58  url = CreateExpectedURL(base::FilePath("foo/bar012.txt"));
59  EXPECT_EQ(url.virtual_path(),
60            ExternalFileURLToVirtualPath(FileSystemURLToExternalFileURL(url)));
61
62  // Path with symbols.
63  url = CreateExpectedURL(base::FilePath(" !\"#$%&'()*+,-.:;<=>?@[\\]^_`{|}~"));
64  EXPECT_EQ(url.virtual_path(),
65            ExternalFileURLToVirtualPath(FileSystemURLToExternalFileURL(url)));
66
67  // Path with '%'.
68  url = CreateExpectedURL(base::FilePath("%19%20%21.txt"));
69  EXPECT_EQ(url.virtual_path(),
70            ExternalFileURLToVirtualPath(FileSystemURLToExternalFileURL(url)));
71
72  // Path with multi byte characters.
73  base::string16 utf16_string;
74  utf16_string.push_back(0x307b);  // HIRAGANA_LETTER_HO
75  utf16_string.push_back(0x3052);  // HIRAGANA_LETTER_GE
76  url = CreateExpectedURL(
77      base::FilePath::FromUTF8Unsafe(base::UTF16ToUTF8(utf16_string) + ".txt"));
78  EXPECT_EQ(url.virtual_path().AsUTF8Unsafe(),
79            ExternalFileURLToVirtualPath(FileSystemURLToExternalFileURL(url))
80                .AsUTF8Unsafe());
81}
82
83TEST_F(ExternalFileURLUtilTest, ParseFileURLWithExternalFileOrigin) {
84  // filesystem:externalfile:/xxx is used only internally. It should not parsed
85  // directly.
86  ASSERT_FALSE(storage::FileSystemURL::CreateForTest(
87                   GURL("filesystem:externalfile:/")).is_valid());
88  ASSERT_FALSE(storage::FileSystemURL::CreateForTest(
89                   GURL(
90                       "filesystem:externalfile:/external/drive-test-user-hash/"
91                       "file.txt")).is_valid());
92}
93
94}  // namespace chromeos
95