Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / file_system_unittest.cc
index 74e26b5..bdd61ff 100644 (file)
@@ -8,8 +8,8 @@
 #include <vector>
 
 #include "base/bind.h"
-#include "base/file_util.h"
 #include "base/files/file_path.h"
+#include "base/files/file_util.h"
 #include "base/files/scoped_temp_dir.h"
 #include "base/memory/scoped_ptr.h"
 #include "base/message_loop/message_loop_proxy.h"
@@ -18,6 +18,7 @@
 #include "chrome/browser/chromeos/drive/change_list_loader.h"
 #include "chrome/browser/chromeos/drive/drive.pb.h"
 #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h"
+#include "chrome/browser/chromeos/drive/file_change.h"
 #include "chrome/browser/chromeos/drive/file_system_observer.h"
 #include "chrome/browser/chromeos/drive/file_system_util.h"
 #include "chrome/browser/chromeos/drive/job_scheduler.h"
@@ -65,12 +66,19 @@ class MockDirectoryChangeObserver : public FileSystemObserver {
     changed_directories_.push_back(directory_path);
   }
 
+  virtual void OnFileChanged(const FileChange& new_file_change) OVERRIDE {
+    changed_files_.Apply(new_file_change);
+  }
+
   const std::vector<base::FilePath>& changed_directories() const {
     return changed_directories_;
   }
 
+  const FileChange& changed_files() const { return changed_files_; }
+
  private:
   std::vector<base::FilePath> changed_directories_;
+  FileChange changed_files_;
   DISALLOW_COPY_AND_ASSIGN(MockDirectoryChangeObserver);
 };
 
@@ -143,7 +151,7 @@ class FileSystemTest : public testing::Test {
     FileError error = FILE_ERROR_FAILED;
     file_system_->change_list_loader_for_testing()->LoadIfNeeded(
         google_apis::test_util::CreateCopyResultCallback(&error));
-    test_util::RunBlockingPoolTask();
+    content::RunAllBlockingPoolTasksUntilIdle();
     return error == FILE_ERROR_OK;
   }
 
@@ -155,7 +163,7 @@ class FileSystemTest : public testing::Test {
     file_system_->GetResourceEntry(
         file_path,
         google_apis::test_util::CreateCopyResultCallback(&error, &entry));
-    test_util::RunBlockingPoolTask();
+    content::RunAllBlockingPoolTasksUntilIdle();
 
     return entry.Pass();
   }
@@ -169,7 +177,7 @@ class FileSystemTest : public testing::Test {
         file_path,
         base::Bind(&AccumulateReadDirectoryResult, entries.get()),
         google_apis::test_util::CreateCopyResultCallback(&error));
-    test_util::RunBlockingPoolTask();
+    content::RunAllBlockingPoolTasksUntilIdle();
     if (error != FILE_ERROR_OK)
       entries.reset();
     return entries.Pass();
@@ -321,7 +329,7 @@ TEST_F(FileSystemTest, Copy) {
                      dest_file_path,
                      false,  // preserve_last_modified,
                      google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   // Entry is added on the server.
@@ -333,7 +341,7 @@ TEST_F(FileSystemTest, Copy) {
   fake_drive_service_->GetFileResource(
       entry->resource_id(),
       google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
   ASSERT_TRUE(server_entry);
   EXPECT_EQ(entry->title(), server_entry->title());
@@ -354,7 +362,7 @@ TEST_F(FileSystemTest, Move) {
   file_system_->Move(src_file_path,
                      dest_file_path,
                      google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   // Entry is moved on the server.
@@ -366,7 +374,7 @@ TEST_F(FileSystemTest, Move) {
   fake_drive_service_->GetFileResource(
       entry->resource_id(),
       google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
   ASSERT_TRUE(server_entry);
   EXPECT_EQ(entry->title(), server_entry->title());
@@ -385,7 +393,7 @@ TEST_F(FileSystemTest, Remove) {
       file_path,
       false,  // is_resursive
       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   // Entry is removed on the server.
@@ -394,7 +402,7 @@ TEST_F(FileSystemTest, Remove) {
   fake_drive_service_->GetFileResource(
       entry->resource_id(),
       google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
   ASSERT_TRUE(server_entry);
   EXPECT_TRUE(server_entry->labels().is_trashed());
@@ -410,7 +418,7 @@ TEST_F(FileSystemTest, CreateDirectory) {
       true,  // is_exclusive
       false,  // is_recursive
       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   // Directory is created on the server.
@@ -422,7 +430,7 @@ TEST_F(FileSystemTest, CreateDirectory) {
   fake_drive_service_->GetFileResource(
       entry->resource_id(),
       google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
   ASSERT_TRUE(server_entry);
   EXPECT_EQ(entry->title(), server_entry->title());
@@ -439,7 +447,7 @@ TEST_F(FileSystemTest, CreateFile) {
       true,  // is_exclusive
       "text/plain",
       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   // File is created on the server.
@@ -451,7 +459,7 @@ TEST_F(FileSystemTest, CreateFile) {
   fake_drive_service_->GetFileResource(
       entry->resource_id(),
       google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
   ASSERT_TRUE(server_entry);
   EXPECT_EQ(entry->title(), server_entry->title());
@@ -476,7 +484,7 @@ TEST_F(FileSystemTest, TouchFile) {
       last_accessed,
       last_modified,
       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   // File is touched on the server.
@@ -485,7 +493,7 @@ TEST_F(FileSystemTest, TouchFile) {
   fake_drive_service_->GetFileResource(
       entry->resource_id(),
       google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
   ASSERT_TRUE(server_entry);
   EXPECT_EQ(last_accessed, server_entry->last_viewed_by_me_date());
@@ -504,7 +512,7 @@ TEST_F(FileSystemTest, TruncateFile) {
       file_path,
       kLength,
       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   // File is touched on the server.
@@ -513,7 +521,7 @@ TEST_F(FileSystemTest, TruncateFile) {
   fake_drive_service_->GetFileResource(
       entry->resource_id(),
       google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
   ASSERT_TRUE(server_entry);
   EXPECT_EQ(kLength, server_entry->file_size());
@@ -569,7 +577,7 @@ TEST_F(FileSystemTest, GetExistingFile) {
       FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
   scoped_ptr<ResourceEntry> entry = GetResourceEntrySync(kFilePath);
   ASSERT_TRUE(entry);
-  EXPECT_EQ("file:subdirectory_file_1_id", entry->resource_id());
+  EXPECT_EQ("subdirectory_file_1_id", entry->resource_id());
 
   EXPECT_EQ(1, fake_drive_service_->about_resource_load_count());
   EXPECT_EQ(2, fake_drive_service_->directory_load_count());
@@ -581,7 +589,7 @@ TEST_F(FileSystemTest, GetExistingDocument) {
       FILE_PATH_LITERAL("drive/root/Document 1 excludeDir-test.gdoc"));
   scoped_ptr<ResourceEntry> entry = GetResourceEntrySync(kFilePath);
   ASSERT_TRUE(entry);
-  EXPECT_EQ("document:5_document_resource_id", entry->resource_id());
+  EXPECT_EQ("5_document_resource_id", entry->resource_id());
 }
 
 TEST_F(FileSystemTest, GetNonExistingFile) {
@@ -597,7 +605,7 @@ TEST_F(FileSystemTest, GetInSubSubdir) {
                         "Sub Sub Directory Folder"));
   scoped_ptr<ResourceEntry> entry = GetResourceEntrySync(kFilePath);
   ASSERT_TRUE(entry);
-  ASSERT_EQ("folder:sub_sub_directory_folder_id", entry->resource_id());
+  ASSERT_EQ("sub_sub_directory_folder_id", entry->resource_id());
 }
 
 TEST_F(FileSystemTest, GetOrphanFile) {
@@ -608,7 +616,7 @@ TEST_F(FileSystemTest, GetOrphanFile) {
       FILE_PATH_LITERAL("drive/other/Orphan File 1.txt"));
   scoped_ptr<ResourceEntry> entry = GetResourceEntrySync(kFilePath);
   ASSERT_TRUE(entry);
-  EXPECT_EQ("file:1_orphanfile_resource_id", entry->resource_id());
+  EXPECT_EQ("1_orphanfile_resource_id", entry->resource_id());
 }
 
 TEST_F(FileSystemTest, ReadDirectory_Root) {
@@ -659,7 +667,7 @@ TEST_F(FileSystemTest, LoadFileSystemFromUpToDateCache) {
   const int about_resource_load_count_before =
       fake_drive_service_->about_resource_load_count();
   file_system_->CheckForUpdates();
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_LT(about_resource_load_count_before,
             fake_drive_service_->about_resource_load_count());
 }
@@ -701,11 +709,12 @@ TEST_F(FileSystemTest, LoadFileSystemFromCacheWhileOffline) {
 
   file_system_->CheckForUpdates();
 
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(1, fake_drive_service_->about_resource_load_count());
   EXPECT_EQ(1, fake_drive_service_->change_list_load_count());
 
-  ASSERT_LE(1u, mock_directory_observer_->changed_directories().size());
+  ASSERT_LE(0u, mock_directory_observer_->changed_directories().size());
+  ASSERT_LE(1u, mock_directory_observer_->changed_files().size());
 }
 
 TEST_F(FileSystemTest, ReadDirectoryWhileRefreshing) {
@@ -744,7 +753,7 @@ TEST_F(FileSystemTest, CreateDirectoryByImplicitLoad) {
       true,  // is_exclusive
       false,  // is_recursive
       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
 
   // It should fail because is_exclusive is set to true.
   EXPECT_EQ(FILE_ERROR_EXISTS, error);
@@ -762,7 +771,7 @@ TEST_F(FileSystemTest, CreateDirectoryRecursively) {
       true,  // is_exclusive
       true,  // is_recursive
       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
 
   EXPECT_EQ(FILE_ERROR_OK, error);
 
@@ -771,6 +780,57 @@ TEST_F(FileSystemTest, CreateDirectoryRecursively) {
   EXPECT_TRUE(entry->file_info().is_directory());
 }
 
+TEST_F(FileSystemTest, ReadDirectoryAfterUpdateWhileLoading) {
+  // Simulate the situation that full feed fetching takes very long time,
+  // to test the recursive "fast fetch" feature is properly working.
+  fake_drive_service_->set_never_return_all_file_list(true);
+
+  // On the fake server, create the test directory.
+  scoped_ptr<google_apis::FileResource> parent;
+  {
+    google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
+    fake_drive_service_->AddNewDirectory(
+        fake_drive_service_->GetRootResourceId(),
+        "UpdateWhileLoadingTestDir",
+        DriveServiceInterface::AddNewDirectoryOptions(),
+        google_apis::test_util::CreateCopyResultCallback(&error, &parent));
+    base::RunLoop().RunUntilIdle();
+    ASSERT_EQ(google_apis::HTTP_CREATED, error);
+  }
+
+  // Fetch the directory. Currently it is empty.
+  scoped_ptr<ResourceEntryVector> before = ReadDirectorySync(base::FilePath(
+      FILE_PATH_LITERAL("drive/root/UpdateWhileLoadingTestDir")));
+  ASSERT_TRUE(before);
+  EXPECT_EQ(0u, before->size());
+
+  // Create a file in the test directory.
+  scoped_ptr<google_apis::FileResource> entry;
+  {
+    google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
+    fake_drive_service_->AddNewFile(
+        "text/plain",
+        "(dummy data)",
+        parent->file_id(),
+        "TestFile",
+        false,  // shared_with_me
+        google_apis::test_util::CreateCopyResultCallback(&error, &entry));
+    base::RunLoop().RunUntilIdle();
+    ASSERT_EQ(google_apis::HTTP_CREATED, error);
+  }
+
+  // Notify the update to the file system.
+  file_system_->CheckForUpdates();
+
+  // Read the directory once again. Although the full feed fetching is not yet
+  // finished, the "fast fetch" of the directory works and the refreshed content
+  // is returned.
+  scoped_ptr<ResourceEntryVector> after = ReadDirectorySync(base::FilePath(
+      FILE_PATH_LITERAL("drive/root/UpdateWhileLoadingTestDir")));
+  ASSERT_TRUE(after);
+  EXPECT_EQ(1u, after->size());
+}
+
 TEST_F(FileSystemTest, PinAndUnpin) {
   ASSERT_TRUE(LoadFullResourceList());
 
@@ -784,7 +844,7 @@ TEST_F(FileSystemTest, PinAndUnpin) {
   FileError error = FILE_ERROR_FAILED;
   file_system_->Pin(file_path,
                     google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   entry = GetResourceEntrySync(file_path);
@@ -796,7 +856,7 @@ TEST_F(FileSystemTest, PinAndUnpin) {
   error = FILE_ERROR_FAILED;
   file_system_->Unpin(file_path,
                       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   entry = GetResourceEntrySync(file_path);
@@ -804,9 +864,11 @@ TEST_F(FileSystemTest, PinAndUnpin) {
   EXPECT_FALSE(entry->file_specific_info().cache_state().is_pinned());
 
   // Pinned file gets synced and it results in entry state changes.
-  ASSERT_EQ(1u, mock_directory_observer_->changed_directories().size());
-  EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("drive/root")),
-            mock_directory_observer_->changed_directories()[0]);
+  ASSERT_EQ(0u, mock_directory_observer_->changed_directories().size());
+  ASSERT_EQ(1u, mock_directory_observer_->changed_files().size());
+  EXPECT_EQ(1u,
+            mock_directory_observer_->changed_files().CountDirectory(
+                base::FilePath(FILE_PATH_LITERAL("drive/root"))));
 }
 
 TEST_F(FileSystemTest, PinAndUnpin_NotSynced) {
@@ -829,7 +891,7 @@ TEST_F(FileSystemTest, PinAndUnpin_NotSynced) {
       file_path,
       google_apis::test_util::CreateCopyResultCallback(&error_unpin));
 
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error_pin);
   EXPECT_EQ(FILE_ERROR_OK, error_unpin);
 
@@ -846,7 +908,7 @@ TEST_F(FileSystemTest, GetAvailableSpace) {
   file_system_->GetAvailableSpace(
       google_apis::test_util::CreateCopyResultCallback(
           &error, &bytes_total, &bytes_used));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(6789012345LL, bytes_used);
   EXPECT_EQ(9876543210LL, bytes_total);
 }
@@ -864,7 +926,7 @@ TEST_F(FileSystemTest, MarkCacheFileAsMountedAndUnmounted) {
       file_in_root,
       google_apis::test_util::CreateCopyResultCallback(
           &error, &file_path, &entry));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   // Test for mounting.
@@ -873,7 +935,7 @@ TEST_F(FileSystemTest, MarkCacheFileAsMountedAndUnmounted) {
   file_system_->MarkCacheFileAsMounted(
       file_in_root,
       google_apis::test_util::CreateCopyResultCallback(&error, &file_path));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   // Cannot remove a cache entry while it's being mounted.
@@ -884,7 +946,7 @@ TEST_F(FileSystemTest, MarkCacheFileAsMountedAndUnmounted) {
   file_system_->MarkCacheFileAsUnmounted(
       file_path,
       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   // Now able to remove the cache entry.
@@ -904,7 +966,7 @@ TEST_F(FileSystemTest, GetShareUrl) {
       kFileInRoot,
       kEmbedOrigin,
       google_apis::test_util::CreateCopyResultCallback(&error, &share_url));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
 
   // Verify the share url to the sharing dialog.
   EXPECT_EQ(FILE_ERROR_OK, error);