Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_system_provider / mount_path_util_unittest.cc
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 <string>
6
7 #include "base/files/file.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system.h"
10 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h"
11 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
12 #include "chrome/browser/chromeos/file_system_provider/service.h"
13 #include "chrome/browser/chromeos/file_system_provider/service_factory.h"
14 #include "chrome/browser/chromeos/login/fake_user_manager.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/test/base/testing_browser_process.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "chrome/test/base/testing_profile_manager.h"
19 #include "components/keyed_service/core/keyed_service.h"
20 #include "content/public/browser/browser_context.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "extensions/browser/extension_registry.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "webkit/browser/fileapi/external_mount_points.h"
25
26 namespace chromeos {
27 namespace file_system_provider {
28 namespace util {
29
30 namespace {
31
32 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
33 const char kFileSystemName[] = "Camera Pictures";
34
35 // Creates a FileSystemURL for tests.
36 fileapi::FileSystemURL CreateFileSystemURL(Profile* profile,
37                                            const std::string& extension_id,
38                                            int file_system_id,
39                                            const base::FilePath& file_path) {
40   const std::string origin = std::string("chrome-extension://") + kExtensionId;
41   const base::FilePath mount_path =
42       util::GetMountPath(profile, extension_id, file_system_id);
43   const fileapi::ExternalMountPoints* const mount_points =
44       fileapi::ExternalMountPoints::GetSystemInstance();
45   DCHECK(mount_points);
46   DCHECK(file_path.IsAbsolute());
47   base::FilePath relative_path(file_path.value().substr(1));
48   return mount_points->CreateCrackedFileSystemURL(
49       GURL(origin),
50       fileapi::kFileSystemTypeExternal,
51       base::FilePath(mount_path.BaseName().Append(relative_path)));
52 }
53
54 // Creates a Service instance. Used to be able to destroy the service in
55 // TearDown().
56 KeyedService* CreateService(content::BrowserContext* context) {
57   return new Service(Profile::FromBrowserContext(context),
58                      extensions::ExtensionRegistry::Get(context));
59 }
60
61 }  // namespace
62
63 class FileSystemProviderMountPathUtilTest : public testing::Test {
64  protected:
65   FileSystemProviderMountPathUtilTest() {}
66   virtual ~FileSystemProviderMountPathUtilTest() {}
67
68   virtual void SetUp() OVERRIDE {
69     profile_manager_.reset(
70         new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
71     ASSERT_TRUE(profile_manager_->SetUp());
72     profile_ = profile_manager_->CreateTestingProfile("testing-profile");
73     user_manager_ = new FakeUserManager();
74     user_manager_enabler_.reset(new ScopedUserManagerEnabler(user_manager_));
75     user_manager_->AddUser(profile_->GetProfileName());
76     ServiceFactory::GetInstance()->SetTestingFactory(profile_, &CreateService);
77     file_system_provider_service_ = Service::Get(profile_);
78     file_system_provider_service_->SetFileSystemFactoryForTests(
79         base::Bind(&FakeProvidedFileSystem::Create));
80   }
81
82   virtual void TearDown() OVERRIDE {
83     // Setting the testing factory to NULL will destroy the created service
84     // associated with the testing profile.
85     ServiceFactory::GetInstance()->SetTestingFactory(profile_, NULL);
86   }
87
88   content::TestBrowserThreadBundle thread_bundle_;
89   scoped_ptr<TestingProfileManager> profile_manager_;
90   TestingProfile* profile_;  // Owned by TestingProfileManager.
91   scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_;
92   FakeUserManager* user_manager_;
93   Service* file_system_provider_service_;  // Owned by its factory.
94 };
95
96 TEST_F(FileSystemProviderMountPathUtilTest, GetMountPath) {
97   const std::string kExtensionId = "mbflcebpggnecokmikipoihdbecnjfoj";
98   const int kFileSystemId = 1;
99
100   base::FilePath result = GetMountPath(profile_, kExtensionId, kFileSystemId);
101   EXPECT_EQ("/provided/mbflcebpggnecokmikipoihdbecnjfoj-1-testing-profile-hash",
102             result.AsUTF8Unsafe());
103 }
104
105 TEST_F(FileSystemProviderMountPathUtilTest, Parser) {
106   const int file_system_id = file_system_provider_service_->MountFileSystem(
107       kExtensionId, kFileSystemName);
108   EXPECT_LT(0, file_system_id);
109
110   const base::FilePath kFilePath =
111       base::FilePath::FromUTF8Unsafe("/hello/world.txt");
112   const fileapi::FileSystemURL url =
113       CreateFileSystemURL(profile_, kExtensionId, file_system_id, kFilePath);
114   EXPECT_TRUE(url.is_valid());
115
116   FileSystemURLParser parser(url);
117   EXPECT_TRUE(parser.Parse());
118
119   ProvidedFileSystemInterface* file_system = parser.file_system();
120   ASSERT_TRUE(file_system);
121   EXPECT_EQ(file_system_id, file_system->GetFileSystemInfo().file_system_id());
122   EXPECT_EQ(kFilePath.AsUTF8Unsafe(), parser.file_path().AsUTF8Unsafe());
123 }
124
125 TEST_F(FileSystemProviderMountPathUtilTest, Parser_RootPath) {
126   const int file_system_id = file_system_provider_service_->MountFileSystem(
127       kExtensionId, kFileSystemName);
128   EXPECT_LT(0, file_system_id);
129
130   const base::FilePath kFilePath = base::FilePath::FromUTF8Unsafe("/");
131   const fileapi::FileSystemURL url =
132       CreateFileSystemURL(profile_, kExtensionId, file_system_id, kFilePath);
133   EXPECT_TRUE(url.is_valid());
134
135   FileSystemURLParser parser(url);
136   EXPECT_TRUE(parser.Parse());
137
138   ProvidedFileSystemInterface* file_system = parser.file_system();
139   ASSERT_TRUE(file_system);
140   EXPECT_EQ(file_system_id, file_system->GetFileSystemInfo().file_system_id());
141   EXPECT_EQ(kFilePath.AsUTF8Unsafe(), parser.file_path().AsUTF8Unsafe());
142 }
143
144 TEST_F(FileSystemProviderMountPathUtilTest, Parser_WrongUrl) {
145   const int file_system_id = file_system_provider_service_->MountFileSystem(
146       kExtensionId, kFileSystemName);
147   EXPECT_LT(0, file_system_id);
148
149   const base::FilePath kFilePath = base::FilePath::FromUTF8Unsafe("/hello");
150   const fileapi::FileSystemURL url = CreateFileSystemURL(
151       profile_, kExtensionId, file_system_id + 1, kFilePath);
152   // It is impossible to create a cracked URL for a mount point which doesn't
153   // exist, therefore is will always be invalid, and empty.
154   EXPECT_FALSE(url.is_valid());
155
156   FileSystemURLParser parser(url);
157   EXPECT_FALSE(parser.Parse());
158 }
159
160 }  // namespace util
161 }  // namespace file_system_provider
162 }  // namespace chromeos