Home | History | Annotate | Download | only in file_system
      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/file_system/search_operation.h"
      6 
      7 #include "base/callback_helpers.h"
      8 #include "chrome/browser/chromeos/drive/change_list_loader.h"
      9 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
     10 #include "chrome/browser/drive/fake_drive_service.h"
     11 #include "content/public/test/test_utils.h"
     12 #include "google_apis/drive/drive_api_parser.h"
     13 #include "google_apis/drive/test_util.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 
     16 namespace drive {
     17 namespace file_system {
     18 
     19 typedef OperationTestBase SearchOperationTest;
     20 
     21 TEST_F(SearchOperationTest, ContentSearch) {
     22   SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
     23                             loader_controller());
     24 
     25   std::set<std::string> expected_results;
     26   expected_results.insert(
     27       "drive/root/Directory 1/Sub Directory Folder/Sub Sub Directory Folder");
     28   expected_results.insert("drive/root/Directory 1/Sub Directory Folder");
     29   expected_results.insert("drive/root/Directory 1/SubDirectory File 1.txt");
     30   expected_results.insert("drive/root/Directory 1");
     31   expected_results.insert("drive/root/Directory 2 excludeDir-test");
     32 
     33   FileError error = FILE_ERROR_FAILED;
     34   GURL next_link;
     35   scoped_ptr<std::vector<SearchResultInfo> > results;
     36 
     37   operation.Search("Directory", GURL(),
     38                    google_apis::test_util::CreateCopyResultCallback(
     39                        &error, &next_link, &results));
     40   content::RunAllBlockingPoolTasksUntilIdle();
     41 
     42   EXPECT_EQ(FILE_ERROR_OK, error);
     43   EXPECT_TRUE(next_link.is_empty());
     44   EXPECT_EQ(expected_results.size(), results->size());
     45   for (size_t i = 0; i < results->size(); i++) {
     46     EXPECT_TRUE(expected_results.count(results->at(i).path.AsUTF8Unsafe()))
     47         << results->at(i).path.AsUTF8Unsafe();
     48   }
     49 }
     50 
     51 TEST_F(SearchOperationTest, ContentSearchWithNewEntry) {
     52   SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
     53                             loader_controller());
     54 
     55   // Create a new directory in the drive service.
     56   google_apis::GDataErrorCode status = google_apis::GDATA_OTHER_ERROR;
     57   scoped_ptr<google_apis::FileResource> server_entry;
     58   fake_service()->AddNewDirectory(
     59       fake_service()->GetRootResourceId(),
     60       "New Directory 1!",
     61       DriveServiceInterface::AddNewDirectoryOptions(),
     62       google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
     63   content::RunAllBlockingPoolTasksUntilIdle();
     64   ASSERT_EQ(google_apis::HTTP_CREATED, status);
     65 
     66   // As the result of the first Search(), only entries in the current file
     67   // system snapshot are expected to be returned in the "right" path. New
     68   // entries like "New Directory 1!" is temporarily added to "drive/other".
     69   std::set<std::string> expected_results;
     70   expected_results.insert("drive/root/Directory 1");
     71   expected_results.insert("drive/other/New Directory 1!");
     72 
     73   FileError error = FILE_ERROR_FAILED;
     74   GURL next_link;
     75   scoped_ptr<std::vector<SearchResultInfo> > results;
     76 
     77   operation.Search("\"Directory 1\"", GURL(),
     78                    google_apis::test_util::CreateCopyResultCallback(
     79                        &error, &next_link, &results));
     80   content::RunAllBlockingPoolTasksUntilIdle();
     81 
     82   EXPECT_EQ(FILE_ERROR_OK, error);
     83   EXPECT_TRUE(next_link.is_empty());
     84   ASSERT_EQ(expected_results.size(), results->size());
     85   for (size_t i = 0; i < results->size(); i++) {
     86     EXPECT_TRUE(expected_results.count(results->at(i).path.AsUTF8Unsafe()))
     87         << results->at(i).path.AsUTF8Unsafe();
     88   }
     89 
     90   // Load the change from FakeDriveService.
     91   ASSERT_EQ(FILE_ERROR_OK, CheckForUpdates());
     92 
     93   // Now the new entry must be reported to be in the right directory.
     94   expected_results.clear();
     95   expected_results.insert("drive/root/Directory 1");
     96   expected_results.insert("drive/root/New Directory 1!");
     97   error = FILE_ERROR_FAILED;
     98   operation.Search("\"Directory 1\"", GURL(),
     99                    google_apis::test_util::CreateCopyResultCallback(
    100                        &error, &next_link, &results));
    101   content::RunAllBlockingPoolTasksUntilIdle();
    102 
    103   EXPECT_EQ(FILE_ERROR_OK, error);
    104   EXPECT_TRUE(next_link.is_empty());
    105   ASSERT_EQ(expected_results.size(), results->size());
    106   for (size_t i = 0; i < results->size(); i++) {
    107     EXPECT_TRUE(expected_results.count(results->at(i).path.AsUTF8Unsafe()))
    108         << results->at(i).path.AsUTF8Unsafe();
    109   }
    110 }
    111 
    112 TEST_F(SearchOperationTest, ContentSearchEmptyResult) {
    113   SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
    114                             loader_controller());
    115 
    116   FileError error = FILE_ERROR_FAILED;
    117   GURL next_link;
    118   scoped_ptr<std::vector<SearchResultInfo> > results;
    119 
    120   operation.Search("\"no-match query\"", GURL(),
    121                    google_apis::test_util::CreateCopyResultCallback(
    122                        &error, &next_link, &results));
    123   content::RunAllBlockingPoolTasksUntilIdle();
    124 
    125   EXPECT_EQ(FILE_ERROR_OK, error);
    126   EXPECT_TRUE(next_link.is_empty());
    127   EXPECT_EQ(0U, results->size());
    128 }
    129 
    130 TEST_F(SearchOperationTest, Lock) {
    131   SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
    132                             loader_controller());
    133 
    134   // Lock.
    135   scoped_ptr<base::ScopedClosureRunner> lock = loader_controller()->GetLock();
    136 
    137   // Search does not return the result as long as lock is alive.
    138   FileError error = FILE_ERROR_FAILED;
    139   GURL next_link;
    140   scoped_ptr<std::vector<SearchResultInfo> > results;
    141 
    142   operation.Search("\"Directory 1\"", GURL(),
    143                    google_apis::test_util::CreateCopyResultCallback(
    144                        &error, &next_link, &results));
    145   content::RunAllBlockingPoolTasksUntilIdle();
    146   EXPECT_EQ(FILE_ERROR_FAILED, error);
    147   EXPECT_FALSE(results);
    148 
    149   // Unlock, this should resume the pending search.
    150   lock.reset();
    151   content::RunAllBlockingPoolTasksUntilIdle();
    152   EXPECT_EQ(FILE_ERROR_OK, error);
    153   ASSERT_TRUE(results);
    154   EXPECT_EQ(1u, results->size());
    155 }
    156 
    157 }  // namespace file_system
    158 }  // namespace drive
    159