1 // 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.
5 #include "components/drive/fake_file_system.h"
7 #include "base/files/file_util.h"
8 #include "base/run_loop.h"
9 #include "components/drive/file_system_core_util.h"
10 #include "components/drive/service/fake_drive_service.h"
11 #include "components/drive/service/test_util.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "google_apis/drive/test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
19 class FakeFileSystemTest
: public ::testing::Test
{
21 void SetUp() override
{
22 // Initialize FakeDriveService.
23 fake_drive_service_
.reset(new FakeDriveService
);
24 ASSERT_TRUE(SetUpTestEntries(fake_drive_service_
.get()));
26 // Create a testee instance.
27 fake_file_system_
.reset(new FakeFileSystem(fake_drive_service_
.get()));
30 content::TestBrowserThreadBundle thread_bundle_
;
31 scoped_ptr
<FakeDriveService
> fake_drive_service_
;
32 scoped_ptr
<FakeFileSystem
> fake_file_system_
;
35 TEST_F(FakeFileSystemTest
, GetFileContent
) {
36 FileError initialize_error
= FILE_ERROR_FAILED
;
37 scoped_ptr
<ResourceEntry
> entry
;
38 base::FilePath cache_file_path
;
39 google_apis::test_util::TestGetContentCallback get_content_callback
;
40 FileError completion_error
= FILE_ERROR_FAILED
;
42 const base::FilePath kDriveFile
=
43 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
45 // For the first time, the file should be downloaded from the service.
46 base::Closure cancel_download
= fake_file_system_
->GetFileContent(
48 google_apis::test_util::CreateCopyResultCallback(
49 &initialize_error
, &cache_file_path
, &entry
),
50 get_content_callback
.callback(),
51 google_apis::test_util::CreateCopyResultCallback(&completion_error
));
52 base::RunLoop().RunUntilIdle();
54 EXPECT_EQ(FILE_ERROR_OK
, initialize_error
);
57 // No cache file is available yet.
58 EXPECT_TRUE(cache_file_path
.empty());
60 // The download should be happened so the |get_content_callback|
61 // should have the actual data.
62 std::string content
= get_content_callback
.GetConcatenatedData();
63 EXPECT_EQ(26U, content
.size());
64 EXPECT_EQ(FILE_ERROR_OK
, completion_error
);
66 initialize_error
= FILE_ERROR_FAILED
;
68 get_content_callback
.mutable_data()->clear();
69 completion_error
= FILE_ERROR_FAILED
;
71 // For the second time, the cache file should be found.
72 cancel_download
= fake_file_system_
->GetFileContent(
74 google_apis::test_util::CreateCopyResultCallback(
75 &initialize_error
, &cache_file_path
, &entry
),
76 get_content_callback
.callback(),
77 google_apis::test_util::CreateCopyResultCallback(&completion_error
));
78 base::RunLoop().RunUntilIdle();
80 EXPECT_EQ(FILE_ERROR_OK
, initialize_error
);
83 // Cache file should be available.
84 ASSERT_FALSE(cache_file_path
.empty());
86 // There should be a cache file so no data should be downloaded.
87 EXPECT_TRUE(get_content_callback
.data().empty());
88 EXPECT_EQ(FILE_ERROR_OK
, completion_error
);
90 // Make sure the cached file's content.
91 std::string cache_file_content
;
93 base::ReadFileToString(cache_file_path
, &cache_file_content
));
94 EXPECT_EQ(content
, cache_file_content
);
97 TEST_F(FakeFileSystemTest
, GetFileContent_Directory
) {
98 FileError initialize_error
= FILE_ERROR_FAILED
;
99 scoped_ptr
<ResourceEntry
> entry
;
100 base::FilePath cache_file_path
;
101 google_apis::test_util::TestGetContentCallback get_content_callback
;
102 FileError completion_error
= FILE_ERROR_FAILED
;
103 base::Closure cancel_download
= fake_file_system_
->GetFileContent(
104 util::GetDriveMyDriveRootPath(),
105 google_apis::test_util::CreateCopyResultCallback(
106 &initialize_error
, &cache_file_path
, &entry
),
107 get_content_callback
.callback(),
108 google_apis::test_util::CreateCopyResultCallback(&completion_error
));
109 base::RunLoop().RunUntilIdle();
111 EXPECT_EQ(FILE_ERROR_NOT_A_FILE
, completion_error
);
114 TEST_F(FakeFileSystemTest
, GetResourceEntry
) {
115 FileError error
= FILE_ERROR_FAILED
;
116 scoped_ptr
<ResourceEntry
> entry
;
117 fake_file_system_
->GetResourceEntry(
118 util::GetDriveMyDriveRootPath().AppendASCII(
119 "Directory 1/Sub Directory Folder"),
120 google_apis::test_util::CreateCopyResultCallback(&error
, &entry
));
121 base::RunLoop().RunUntilIdle();
123 ASSERT_EQ(FILE_ERROR_OK
, error
);
125 EXPECT_EQ("sub_dir_folder_resource_id", entry
->resource_id());
128 TEST_F(FakeFileSystemTest
, GetResourceEntry_Root
) {
129 FileError error
= FILE_ERROR_FAILED
;
130 scoped_ptr
<ResourceEntry
> entry
;
131 fake_file_system_
->GetResourceEntry(
132 util::GetDriveMyDriveRootPath(),
133 google_apis::test_util::CreateCopyResultCallback(&error
, &entry
));
134 base::RunLoop().RunUntilIdle();
136 ASSERT_EQ(FILE_ERROR_OK
, error
);
138 EXPECT_TRUE(entry
->file_info().is_directory());
139 EXPECT_EQ(fake_drive_service_
->GetRootResourceId(), entry
->resource_id());
140 EXPECT_EQ(util::kDriveMyDriveRootDirName
, entry
->title());
143 TEST_F(FakeFileSystemTest
, GetResourceEntry_Invalid
) {
144 FileError error
= FILE_ERROR_FAILED
;
145 scoped_ptr
<ResourceEntry
> entry
;
146 fake_file_system_
->GetResourceEntry(
147 util::GetDriveMyDriveRootPath().AppendASCII("Invalid File Name"),
148 google_apis::test_util::CreateCopyResultCallback(&error
, &entry
));
149 base::RunLoop().RunUntilIdle();
151 ASSERT_EQ(FILE_ERROR_NOT_FOUND
, error
);
155 } // namespace test_util