4d6a07ea49de7f41f57f07613f25eff3fca081ae
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / directory_loader_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 "chrome/browser/chromeos/drive/directory_loader.h"
6
7 #include "base/callback_helpers.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "base/run_loop.h"
12 #include "chrome/browser/chromeos/drive/change_list_loader.h"
13 #include "chrome/browser/chromeos/drive/change_list_loader_observer.h"
14 #include "chrome/browser/chromeos/drive/file_cache.h"
15 #include "chrome/browser/chromeos/drive/file_system_util.h"
16 #include "chrome/browser/chromeos/drive/job_scheduler.h"
17 #include "chrome/browser/chromeos/drive/resource_metadata.h"
18 #include "chrome/browser/chromeos/drive/test_util.h"
19 #include "chrome/browser/drive/event_logger.h"
20 #include "chrome/browser/drive/fake_drive_service.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "google_apis/drive/drive_api_parser.h"
23 #include "google_apis/drive/test_util.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25
26 namespace drive {
27 namespace internal {
28
29 namespace {
30
31 class TestDirectoryLoaderObserver : public ChangeListLoaderObserver {
32  public:
33   explicit TestDirectoryLoaderObserver(DirectoryLoader* loader)
34       : loader_(loader) {
35     loader_->AddObserver(this);
36   }
37
38   virtual ~TestDirectoryLoaderObserver() {
39     loader_->RemoveObserver(this);
40   }
41
42   const std::set<base::FilePath>& changed_directories() const {
43     return changed_directories_;
44   }
45   void clear_changed_directories() { changed_directories_.clear(); }
46
47   // ChageListObserver overrides:
48   virtual void OnDirectoryChanged(
49       const base::FilePath& directory_path) OVERRIDE {
50     changed_directories_.insert(directory_path);
51   }
52
53  private:
54   DirectoryLoader* loader_;
55   std::set<base::FilePath> changed_directories_;
56
57   DISALLOW_COPY_AND_ASSIGN(TestDirectoryLoaderObserver);
58 };
59
60 void AccumulateReadDirectoryResult(ResourceEntryVector* out_entries,
61                                    scoped_ptr<ResourceEntryVector> entries) {
62   ASSERT_TRUE(entries);
63   out_entries->insert(out_entries->end(), entries->begin(), entries->end());
64 }
65
66 }  // namespace
67
68 class DirectoryLoaderTest : public testing::Test {
69  protected:
70   virtual void SetUp() OVERRIDE {
71     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
72     pref_service_.reset(new TestingPrefServiceSimple);
73     test_util::RegisterDrivePrefs(pref_service_->registry());
74
75     logger_.reset(new EventLogger);
76
77     drive_service_.reset(new FakeDriveService);
78     ASSERT_TRUE(drive_service_->LoadResourceListForWapi(
79         "gdata/root_feed.json"));
80     ASSERT_TRUE(drive_service_->LoadAccountMetadataForWapi(
81         "gdata/account_metadata.json"));
82
83     scheduler_.reset(new JobScheduler(pref_service_.get(),
84                                       logger_.get(),
85                                       drive_service_.get(),
86                                       base::MessageLoopProxy::current().get()));
87     metadata_storage_.reset(new ResourceMetadataStorage(
88         temp_dir_.path(), base::MessageLoopProxy::current().get()));
89     ASSERT_TRUE(metadata_storage_->Initialize());
90
91     metadata_.reset(new ResourceMetadata(
92         metadata_storage_.get(), base::MessageLoopProxy::current().get()));
93     ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize());
94
95     cache_.reset(new FileCache(metadata_storage_.get(),
96                                temp_dir_.path(),
97                                base::MessageLoopProxy::current().get(),
98                                NULL /* free_disk_space_getter */));
99     ASSERT_TRUE(cache_->Initialize());
100
101     about_resource_loader_.reset(new AboutResourceLoader(scheduler_.get()));
102     loader_controller_.reset(new LoaderController);
103     directory_loader_.reset(
104         new DirectoryLoader(logger_.get(),
105                              base::MessageLoopProxy::current().get(),
106                              metadata_.get(),
107                              scheduler_.get(),
108                              drive_service_.get(),
109                              about_resource_loader_.get(),
110                              loader_controller_.get()));
111   }
112
113   // Adds a new file to the root directory of the service.
114   scoped_ptr<google_apis::ResourceEntry> AddNewFile(const std::string& title) {
115     google_apis::GDataErrorCode error = google_apis::GDATA_FILE_ERROR;
116     scoped_ptr<google_apis::ResourceEntry> entry;
117     drive_service_->AddNewFile(
118         "text/plain",
119         "content text",
120         drive_service_->GetRootResourceId(),
121         title,
122         false,  // shared_with_me
123         google_apis::test_util::CreateCopyResultCallback(&error, &entry));
124     base::RunLoop().RunUntilIdle();
125     EXPECT_EQ(google_apis::HTTP_CREATED, error);
126     return entry.Pass();
127   }
128
129   content::TestBrowserThreadBundle thread_bundle_;
130   base::ScopedTempDir temp_dir_;
131   scoped_ptr<TestingPrefServiceSimple> pref_service_;
132   scoped_ptr<EventLogger> logger_;
133   scoped_ptr<FakeDriveService> drive_service_;
134   scoped_ptr<JobScheduler> scheduler_;
135   scoped_ptr<ResourceMetadataStorage,
136              test_util::DestroyHelperForTests> metadata_storage_;
137   scoped_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_;
138   scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
139   scoped_ptr<AboutResourceLoader> about_resource_loader_;
140   scoped_ptr<LoaderController> loader_controller_;
141   scoped_ptr<DirectoryLoader> directory_loader_;
142 };
143
144 TEST_F(DirectoryLoaderTest, ReadDirectory_GrandRoot) {
145   TestDirectoryLoaderObserver observer(directory_loader_.get());
146
147   // Load grand root.
148   FileError error = FILE_ERROR_FAILED;
149   ResourceEntryVector entries;
150   directory_loader_->ReadDirectory(
151       util::GetDriveGrandRootPath(),
152       base::Bind(&AccumulateReadDirectoryResult, &entries),
153       google_apis::test_util::CreateCopyResultCallback(&error));
154   base::RunLoop().RunUntilIdle();
155   EXPECT_EQ(FILE_ERROR_OK, error);
156   EXPECT_EQ(0U, observer.changed_directories().size());
157   observer.clear_changed_directories();
158
159   // My Drive has resource ID.
160   ResourceEntry entry;
161   EXPECT_EQ(FILE_ERROR_OK,
162             metadata_->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(),
163                                               &entry));
164   EXPECT_EQ(drive_service_->GetRootResourceId(), entry.resource_id());
165 }
166
167 TEST_F(DirectoryLoaderTest, ReadDirectory_MyDrive) {
168   TestDirectoryLoaderObserver observer(directory_loader_.get());
169
170   // My Drive does not have resource ID yet.
171   ResourceEntry entry;
172   EXPECT_EQ(FILE_ERROR_OK,
173             metadata_->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(),
174                                               &entry));
175   EXPECT_TRUE(entry.resource_id().empty());
176
177   // Load My Drive.
178   FileError error = FILE_ERROR_FAILED;
179   ResourceEntryVector entries;
180   directory_loader_->ReadDirectory(
181       util::GetDriveMyDriveRootPath(),
182       base::Bind(&AccumulateReadDirectoryResult, &entries),
183       google_apis::test_util::CreateCopyResultCallback(&error));
184   base::RunLoop().RunUntilIdle();
185   EXPECT_EQ(FILE_ERROR_OK, error);
186   EXPECT_EQ(1U, observer.changed_directories().count(
187       util::GetDriveMyDriveRootPath()));
188
189   // My Drive has resource ID.
190   EXPECT_EQ(FILE_ERROR_OK,
191             metadata_->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(),
192                                               &entry));
193   EXPECT_EQ(drive_service_->GetRootResourceId(), entry.resource_id());
194   EXPECT_EQ(drive_service_->about_resource().largest_change_id(),
195             entry.directory_specific_info().changestamp());
196
197   // My Drive's child is present.
198   base::FilePath file_path =
199       util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
200   EXPECT_EQ(FILE_ERROR_OK,
201             metadata_->GetResourceEntryByPath(file_path, &entry));
202 }
203
204 TEST_F(DirectoryLoaderTest, ReadDirectory_MultipleCalls) {
205   TestDirectoryLoaderObserver observer(directory_loader_.get());
206
207   // Load grand root.
208   FileError error = FILE_ERROR_FAILED;
209   ResourceEntryVector entries;
210   directory_loader_->ReadDirectory(
211       util::GetDriveGrandRootPath(),
212       base::Bind(&AccumulateReadDirectoryResult, &entries),
213       google_apis::test_util::CreateCopyResultCallback(&error));
214
215   // Load grand root again without waiting for the result.
216   FileError error2 = FILE_ERROR_FAILED;
217   ResourceEntryVector entries2;
218   directory_loader_->ReadDirectory(
219       util::GetDriveGrandRootPath(),
220       base::Bind(&AccumulateReadDirectoryResult, &entries2),
221       google_apis::test_util::CreateCopyResultCallback(&error2));
222   base::RunLoop().RunUntilIdle();
223
224   // Callback is called for each method call.
225   EXPECT_EQ(FILE_ERROR_OK, error);
226   EXPECT_EQ(FILE_ERROR_OK, error2);
227 }
228
229 TEST_F(DirectoryLoaderTest, Lock) {
230   // Lock the loader.
231   scoped_ptr<base::ScopedClosureRunner> lock = loader_controller_->GetLock();
232
233   // Start loading.
234   TestDirectoryLoaderObserver observer(directory_loader_.get());
235   FileError error = FILE_ERROR_FAILED;
236   ResourceEntryVector entries;
237   directory_loader_->ReadDirectory(
238       util::GetDriveMyDriveRootPath(),
239       base::Bind(&AccumulateReadDirectoryResult, &entries),
240       google_apis::test_util::CreateCopyResultCallback(&error));
241   base::RunLoop().RunUntilIdle();
242
243   // Update is pending due to the lock.
244   EXPECT_TRUE(observer.changed_directories().empty());
245
246   // Unlock the loader, this should resume the pending udpate.
247   lock.reset();
248   base::RunLoop().RunUntilIdle();
249   EXPECT_EQ(1U, observer.changed_directories().count(
250       util::GetDriveMyDriveRootPath()));
251 }
252
253 }  // namespace internal
254 }  // namespace drive