a391d125371fdad05ab369162c682e20954d6f62
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / fake_file_system_unittest.cc
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.
4
5 #include "chrome/browser/chromeos/drive/fake_file_system.h"
6
7 #include "base/file_util.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/chromeos/drive/file_system_util.h"
10 #include "chrome/browser/drive/fake_drive_service.h"
11 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "google_apis/drive/test_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace drive {
16 namespace test_util {
17
18 class FakeFileSystemTest : public ::testing::Test {
19  protected:
20   virtual void SetUp() OVERRIDE {
21     // Initialize FakeDriveService.
22     fake_drive_service_.reset(new FakeDriveService);
23     ASSERT_TRUE(fake_drive_service_->LoadResourceListForWapi(
24         "gdata/root_feed.json"));
25     ASSERT_TRUE(fake_drive_service_->LoadAccountMetadataForWapi(
26         "gdata/account_metadata.json"));
27
28     // Create a testee instance.
29     fake_file_system_.reset(new FakeFileSystem(fake_drive_service_.get()));
30   }
31
32   content::TestBrowserThreadBundle thread_bundle_;
33   scoped_ptr<FakeDriveService> fake_drive_service_;
34   scoped_ptr<FakeFileSystem> fake_file_system_;
35 };
36
37 TEST_F(FakeFileSystemTest, GetFileContent) {
38   FileError initialize_error = FILE_ERROR_FAILED;
39   scoped_ptr<ResourceEntry> entry;
40   base::FilePath cache_file_path;
41   google_apis::test_util::TestGetContentCallback get_content_callback;
42   FileError completion_error = FILE_ERROR_FAILED;
43
44   const base::FilePath kDriveFile =
45       util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
46
47   // For the first time, the file should be downloaded from the service.
48   base::Closure cancel_download = fake_file_system_->GetFileContent(
49       kDriveFile,
50       google_apis::test_util::CreateCopyResultCallback(
51           &initialize_error, &cache_file_path, &entry),
52       get_content_callback.callback(),
53       google_apis::test_util::CreateCopyResultCallback(&completion_error));
54   base::RunLoop().RunUntilIdle();
55
56   EXPECT_EQ(FILE_ERROR_OK, initialize_error);
57   EXPECT_TRUE(entry);
58
59   // No cache file is available yet.
60   EXPECT_TRUE(cache_file_path.empty());
61
62   // The download should be happened so the |get_content_callback|
63   // should have the actual data.
64   std::string content = get_content_callback.GetConcatenatedData();
65   EXPECT_EQ(26U, content.size());
66   EXPECT_EQ(FILE_ERROR_OK, completion_error);
67
68   initialize_error = FILE_ERROR_FAILED;
69   entry.reset();
70   get_content_callback.mutable_data()->clear();
71   completion_error = FILE_ERROR_FAILED;
72
73   // For the second time, the cache file should be found.
74   cancel_download = fake_file_system_->GetFileContent(
75       kDriveFile,
76       google_apis::test_util::CreateCopyResultCallback(
77           &initialize_error, &cache_file_path, &entry),
78       get_content_callback.callback(),
79       google_apis::test_util::CreateCopyResultCallback(&completion_error));
80   base::RunLoop().RunUntilIdle();
81
82   EXPECT_EQ(FILE_ERROR_OK, initialize_error);
83   EXPECT_TRUE(entry);
84
85   // Cache file should be available.
86   ASSERT_FALSE(cache_file_path.empty());
87
88   // There should be a cache file so no data should be downloaded.
89   EXPECT_TRUE(get_content_callback.data().empty());
90   EXPECT_EQ(FILE_ERROR_OK, completion_error);
91
92   // Make sure the cached file's content.
93   std::string cache_file_content;
94   ASSERT_TRUE(
95       base::ReadFileToString(cache_file_path, &cache_file_content));
96   EXPECT_EQ(content, cache_file_content);
97 }
98
99 TEST_F(FakeFileSystemTest, GetFileContent_Directory) {
100   FileError initialize_error = FILE_ERROR_FAILED;
101   scoped_ptr<ResourceEntry> entry;
102   base::FilePath cache_file_path;
103   google_apis::test_util::TestGetContentCallback get_content_callback;
104   FileError completion_error = FILE_ERROR_FAILED;
105   base::Closure cancel_download = fake_file_system_->GetFileContent(
106       util::GetDriveMyDriveRootPath(),
107       google_apis::test_util::CreateCopyResultCallback(
108           &initialize_error, &cache_file_path, &entry),
109       get_content_callback.callback(),
110       google_apis::test_util::CreateCopyResultCallback(&completion_error));
111   base::RunLoop().RunUntilIdle();
112
113   EXPECT_EQ(FILE_ERROR_NOT_A_FILE, completion_error);
114 }
115
116 TEST_F(FakeFileSystemTest, GetResourceEntry) {
117   FileError error = FILE_ERROR_FAILED;
118   scoped_ptr<ResourceEntry> entry;
119   fake_file_system_->GetResourceEntry(
120       util::GetDriveMyDriveRootPath().AppendASCII(
121           "Directory 1/Sub Directory Folder"),
122       google_apis::test_util::CreateCopyResultCallback(&error, &entry));
123   base::RunLoop().RunUntilIdle();
124
125   ASSERT_EQ(FILE_ERROR_OK, error);
126   ASSERT_TRUE(entry);
127   EXPECT_EQ("folder:sub_dir_folder_resource_id", entry->resource_id());
128 }
129
130 TEST_F(FakeFileSystemTest, GetResourceEntry_Root) {
131   FileError error = FILE_ERROR_FAILED;
132   scoped_ptr<ResourceEntry> entry;
133   fake_file_system_->GetResourceEntry(
134       util::GetDriveMyDriveRootPath(),
135       google_apis::test_util::CreateCopyResultCallback(&error, &entry));
136   base::RunLoop().RunUntilIdle();
137
138   ASSERT_EQ(FILE_ERROR_OK, error);
139   ASSERT_TRUE(entry);
140   EXPECT_TRUE(entry->file_info().is_directory());
141   EXPECT_EQ(fake_drive_service_->GetRootResourceId(), entry->resource_id());
142   EXPECT_EQ(util::kDriveMyDriveRootDirName, entry->title());
143 }
144
145 TEST_F(FakeFileSystemTest, GetResourceEntry_Invalid) {
146   FileError error = FILE_ERROR_FAILED;
147   scoped_ptr<ResourceEntry> entry;
148   fake_file_system_->GetResourceEntry(
149       util::GetDriveMyDriveRootPath().AppendASCII("Invalid File Name"),
150       google_apis::test_util::CreateCopyResultCallback(&error, &entry));
151   base::RunLoop().RunUntilIdle();
152
153   ASSERT_EQ(FILE_ERROR_NOT_FOUND, error);
154   ASSERT_FALSE(entry);
155 }
156
157 }  // namespace test_util
158 }  // namespace drive