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