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