Home | History | Annotate | Download | only in drive_backend_v1
      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/sync_file_system/drive_backend_v1/drive_metadata_store.h"
      6 
      7 #include <utility>
      8 
      9 #include "base/files/file_path.h"
     10 #include "base/files/scoped_temp_dir.h"
     11 #include "base/message_loop/message_loop.h"
     12 #include "base/single_thread_task_runner.h"
     13 #include "base/stl_util.h"
     14 #include "base/strings/string_number_conversions.h"
     15 #include "base/strings/string_util.h"
     16 #include "base/threading/thread.h"
     17 #include "base/values.h"
     18 #include "chrome/browser/sync_file_system/drive_backend/metadata_db_migration_util.h"
     19 #include "chrome/browser/sync_file_system/drive_backend_v1/drive_file_sync_service.h"
     20 #include "chrome/browser/sync_file_system/drive_backend_v1/drive_file_sync_util.h"
     21 #include "chrome/browser/sync_file_system/sync_file_system.pb.h"
     22 #include "chrome/browser/sync_file_system/syncable_file_system_util.h"
     23 #include "content/public/browser/browser_thread.h"
     24 #include "testing/gtest/include/gtest/gtest.h"
     25 #include "third_party/leveldatabase/src/include/leveldb/db.h"
     26 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
     27 #include "webkit/browser/fileapi/isolated_context.h"
     28 
     29 #define FPL FILE_PATH_LITERAL
     30 
     31 using content::BrowserThread;
     32 
     33 namespace sync_file_system {
     34 
     35 namespace {
     36 
     37 const char kOrigin[] = "chrome-extension://example";
     38 
     39 typedef DriveMetadataStore::ResourceIdByOrigin ResourceIdByOrigin;
     40 typedef DriveMetadataStore::OriginByResourceId OriginByResourceId;
     41 
     42 fileapi::FileSystemURL URL(const base::FilePath& path) {
     43   return CreateSyncableFileSystemURL(GURL(kOrigin), path);
     44 }
     45 
     46 std::string GetResourceID(const ResourceIdByOrigin& sync_origins,
     47                           const GURL& origin) {
     48   ResourceIdByOrigin::const_iterator itr = sync_origins.find(origin);
     49   if (itr == sync_origins.end())
     50     return std::string();
     51   return itr->second;
     52 }
     53 
     54 std::string CreateResourceId(const std::string& resource_id) {
     55   return IsDriveAPIDisabled() ? resource_id
     56                               : drive_backend::RemoveWapiIdPrefix(resource_id);
     57 }
     58 
     59 DriveMetadata CreateMetadata(const std::string& resource_id,
     60                              const std::string& md5_checksum,
     61                              bool conflicted,
     62                              bool to_be_fetched,
     63                              DriveMetadata_ResourceType file_type) {
     64   DriveMetadata metadata;
     65   metadata.set_resource_id(CreateResourceId(resource_id));
     66   metadata.set_md5_checksum(md5_checksum);
     67   metadata.set_conflicted(conflicted);
     68   metadata.set_to_be_fetched(to_be_fetched);
     69   metadata.set_type(file_type);
     70   return metadata;
     71 }
     72 
     73 }  // namespace
     74 
     75 class DriveMetadataStoreTest : public testing::Test {
     76  public:
     77   DriveMetadataStoreTest()
     78       : created_(false) {}
     79 
     80   virtual ~DriveMetadataStoreTest() {}
     81 
     82   virtual void SetUp() OVERRIDE {
     83     file_thread_.reset(new base::Thread("Thread_File"));
     84     file_thread_->Start();
     85 
     86     ui_task_runner_ = base::MessageLoopProxy::current();
     87     file_task_runner_ = file_thread_->message_loop_proxy();
     88 
     89     ASSERT_TRUE(base_dir_.CreateUniqueTempDir());
     90     RegisterSyncableFileSystem();
     91   }
     92 
     93   virtual void TearDown() OVERRIDE {
     94     SetDisableDriveAPI(false);
     95     RevokeSyncableFileSystem();
     96 
     97     DropDatabase();
     98     file_thread_->Stop();
     99     message_loop_.RunUntilIdle();
    100   }
    101 
    102  protected:
    103   void InitializeDatabase() {
    104     EXPECT_TRUE(ui_task_runner_->RunsTasksOnCurrentThread());
    105 
    106     bool done = false;
    107     SyncStatusCode status = SYNC_STATUS_UNKNOWN;
    108     bool created = false;
    109 
    110     drive_metadata_store_.reset(
    111         new DriveMetadataStore(base_dir_.path(), file_task_runner_.get()));
    112     drive_metadata_store_->Initialize(
    113         base::Bind(&DriveMetadataStoreTest::DidInitializeDatabase,
    114                    base::Unretained(this),
    115                    &done,
    116                    &status,
    117                    &created));
    118     message_loop_.Run();
    119 
    120     EXPECT_TRUE(done);
    121     EXPECT_EQ(SYNC_STATUS_OK, status);
    122 
    123     if (created) {
    124       EXPECT_FALSE(created_);
    125       created_ = created;
    126       return;
    127     }
    128     EXPECT_TRUE(created_);
    129   }
    130 
    131   void DropDatabase() {
    132     EXPECT_TRUE(ui_task_runner_->RunsTasksOnCurrentThread());
    133     drive_metadata_store_.reset();
    134   }
    135 
    136   SyncStatusCode EnableOrigin(const GURL& origin) {
    137     SyncStatusCode status = SYNC_STATUS_UNKNOWN;
    138     drive_metadata_store_->EnableOrigin(
    139         origin, base::Bind(&DriveMetadataStoreTest::DidFinishDBTask,
    140                            base::Unretained(this), &status));
    141     message_loop_.Run();
    142     return status;
    143   }
    144 
    145   SyncStatusCode DisableOrigin(const GURL& origin) {
    146     SyncStatusCode status = SYNC_STATUS_UNKNOWN;
    147     drive_metadata_store_->DisableOrigin(
    148         origin, base::Bind(&DriveMetadataStoreTest::DidFinishDBTask,
    149                            base::Unretained(this), &status));
    150     message_loop_.Run();
    151     return status;
    152   }
    153 
    154   SyncStatusCode RemoveOrigin(const GURL& url) {
    155     SyncStatusCode status = SYNC_STATUS_UNKNOWN;
    156     drive_metadata_store_->RemoveOrigin(
    157         url, base::Bind(&DriveMetadataStoreTest::DidFinishDBTask,
    158                         base::Unretained(this), &status));
    159     message_loop_.Run();
    160     return status;
    161   }
    162 
    163   SyncStatusCode UpdateEntry(const fileapi::FileSystemURL& url,
    164                              const DriveMetadata& metadata) {
    165     SyncStatusCode status = SYNC_STATUS_UNKNOWN;
    166     drive_metadata_store_->UpdateEntry(
    167         url, metadata,
    168         base::Bind(&DriveMetadataStoreTest::DidFinishDBTask,
    169                    base::Unretained(this), &status));
    170     message_loop_.Run();
    171     return status;
    172   }
    173 
    174   SyncStatusCode DeleteEntry(const fileapi::FileSystemURL& url) {
    175     SyncStatusCode status = SYNC_STATUS_UNKNOWN;
    176     drive_metadata_store_->DeleteEntry(
    177         url,
    178         base::Bind(&DriveMetadataStoreTest::DidFinishDBTask,
    179                    base::Unretained(this), &status));
    180     message_loop_.Run();
    181     return status;
    182   }
    183 
    184   SyncStatusCode SetLargestChangeStamp(int64 changestamp) {
    185     SyncStatusCode status = SYNC_STATUS_UNKNOWN;
    186     drive_metadata_store_->SetLargestChangeStamp(
    187         changestamp, base::Bind(&DriveMetadataStoreTest::DidFinishDBTask,
    188                                 base::Unretained(this), &status));
    189     message_loop_.Run();
    190     return status;
    191   }
    192 
    193   void DidFinishDBTask(SyncStatusCode* status_out,
    194                        SyncStatusCode status) {
    195     *status_out = status;
    196     message_loop_.Quit();
    197   }
    198 
    199   void MarkAsCreated() {
    200     created_ = true;
    201   }
    202 
    203   void VerifyUntrackedOrigin(const GURL& origin) {
    204     EXPECT_FALSE(metadata_store()->IsIncrementalSyncOrigin(origin));
    205     EXPECT_FALSE(metadata_store()->IsOriginDisabled(origin));
    206   }
    207 
    208   void VerifyIncrementalSyncOrigin(const GURL& origin,
    209                                    const std::string& resource_id) {
    210     EXPECT_TRUE(metadata_store()->IsIncrementalSyncOrigin(origin));
    211     EXPECT_FALSE(metadata_store()->IsOriginDisabled(origin));
    212     EXPECT_EQ(resource_id,
    213               GetResourceID(metadata_store()->incremental_sync_origins(),
    214                             origin));
    215   }
    216 
    217   void VerifyDisabledOrigin(const GURL& origin,
    218                             const std::string& resource_id) {
    219     EXPECT_FALSE(metadata_store()->IsIncrementalSyncOrigin(origin));
    220     EXPECT_TRUE(metadata_store()->IsOriginDisabled(origin));
    221     EXPECT_EQ(resource_id,
    222               GetResourceID(metadata_store()->disabled_origins(), origin));
    223   }
    224 
    225   DriveMetadataStore* metadata_store() {
    226     return drive_metadata_store_.get();
    227   }
    228 
    229   const DriveMetadataStore::MetadataMap& metadata_map() {
    230     return drive_metadata_store_->metadata_map_;
    231   }
    232 
    233   void VerifyReverseMap() {
    234     const ResourceIdByOrigin& incremental_sync_origins =
    235         drive_metadata_store_->incremental_sync_origins_;
    236     const ResourceIdByOrigin& disabled_origins =
    237         drive_metadata_store_->disabled_origins_;
    238     const OriginByResourceId& origin_by_resource_id =
    239         drive_metadata_store_->origin_by_resource_id_;
    240 
    241     size_t expected_size = incremental_sync_origins.size() +
    242                            disabled_origins.size();
    243     size_t actual_size = origin_by_resource_id.size();
    244     EXPECT_EQ(expected_size, actual_size);
    245     EXPECT_TRUE(VerifyReverseMapInclusion(incremental_sync_origins,
    246                                           origin_by_resource_id));
    247     EXPECT_TRUE(VerifyReverseMapInclusion(disabled_origins,
    248                                           origin_by_resource_id));
    249   }
    250 
    251   void ReadWrite_Body();
    252   void GetConflictURLs_Body();
    253   void GetToBeFetchedFiles_Body();
    254   void StoreSyncRootDirectory_Body();
    255   void StoreSyncOrigin_Body();
    256   void DisableOrigin_Body();
    257   void RemoveOrigin_Body();
    258   void GetResourceIdForOrigin_Body();
    259   void ResetOriginRootDirectory_Body();
    260   void GetFileMetadataMap_Body();
    261 
    262  private:
    263   void DidInitializeDatabase(bool* done_out,
    264                              SyncStatusCode* status_out,
    265                              bool* created_out,
    266                              SyncStatusCode status,
    267                              bool created) {
    268     *done_out = true;
    269     *status_out = status;
    270     *created_out = created;
    271     message_loop_.Quit();
    272   }
    273 
    274   bool VerifyReverseMapInclusion(const ResourceIdByOrigin& left,
    275                                  const OriginByResourceId& right) {
    276     for (ResourceIdByOrigin::const_iterator itr = left.begin();
    277          itr != left.end(); ++itr) {
    278       OriginByResourceId::const_iterator found = right.find(itr->second);
    279       if (found == right.end() || found->second != itr->first)
    280         return false;
    281     }
    282     return true;
    283   }
    284 
    285   base::ScopedTempDir base_dir_;
    286 
    287   base::MessageLoop message_loop_;
    288   scoped_ptr<base::Thread> file_thread_;
    289 
    290   scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
    291   scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
    292 
    293   scoped_ptr<DriveMetadataStore> drive_metadata_store_;
    294 
    295   bool created_;
    296 
    297   DISALLOW_COPY_AND_ASSIGN(DriveMetadataStoreTest);
    298 };
    299 
    300 void DriveMetadataStoreTest::ReadWrite_Body() {
    301   InitializeDatabase();
    302 
    303   const fileapi::FileSystemURL url = URL(base::FilePath());
    304   DriveMetadata metadata;
    305   EXPECT_EQ(SYNC_DATABASE_ERROR_NOT_FOUND,
    306             metadata_store()->ReadEntry(url, &metadata));
    307 
    308   metadata = CreateMetadata(
    309       "file:1234567890", "09876543210", true, false,
    310       DriveMetadata_ResourceType_RESOURCE_TYPE_FILE);
    311   EXPECT_EQ(SYNC_STATUS_OK, UpdateEntry(url, metadata));
    312   EXPECT_EQ(SYNC_STATUS_OK, SetLargestChangeStamp(1));
    313 
    314   DropDatabase();
    315   InitializeDatabase();
    316 
    317   EXPECT_EQ(1, metadata_store()->GetLargestChangeStamp());
    318 
    319   DriveMetadata metadata2;
    320   EXPECT_EQ(SYNC_STATUS_OK,
    321             metadata_store()->ReadEntry(url, &metadata2));
    322   EXPECT_EQ(metadata.resource_id(), metadata2.resource_id());
    323   EXPECT_EQ(metadata.md5_checksum(), metadata2.md5_checksum());
    324   EXPECT_EQ(metadata.conflicted(), metadata2.conflicted());
    325 
    326   EXPECT_EQ(SYNC_STATUS_OK, DeleteEntry(url));
    327   EXPECT_EQ(SYNC_DATABASE_ERROR_NOT_FOUND,
    328             metadata_store()->ReadEntry(url, &metadata));
    329   EXPECT_EQ(SYNC_DATABASE_ERROR_NOT_FOUND, DeleteEntry(url));
    330 
    331   VerifyReverseMap();
    332 }
    333 
    334 void DriveMetadataStoreTest::GetConflictURLs_Body() {
    335   InitializeDatabase();
    336 
    337   fileapi::FileSystemURLSet urls;
    338   EXPECT_EQ(SYNC_STATUS_OK, metadata_store()->GetConflictURLs(&urls));
    339   EXPECT_EQ(0U, urls.size());
    340 
    341   const base::FilePath path1(FPL("file1"));
    342   const base::FilePath path2(FPL("file2"));
    343   const base::FilePath path3(FPL("file3"));
    344 
    345   // Populate metadata in DriveMetadataStore. The metadata identified by "file2"
    346   // and "file3" are marked as conflicted.
    347   EXPECT_EQ(SYNC_STATUS_OK, UpdateEntry(
    348       URL(path1),
    349       CreateMetadata("file:1", "1", false, false,
    350                      DriveMetadata_ResourceType_RESOURCE_TYPE_FILE)));
    351   EXPECT_EQ(SYNC_STATUS_OK, UpdateEntry(
    352       URL(path2),
    353       CreateMetadata("file:2", "2", true, false,
    354                      DriveMetadata_ResourceType_RESOURCE_TYPE_FILE)));
    355   EXPECT_EQ(SYNC_STATUS_OK, UpdateEntry(
    356       URL(path3),
    357       CreateMetadata("file:3", "3", true, false,
    358                      DriveMetadata_ResourceType_RESOURCE_TYPE_FILE)));
    359 
    360   EXPECT_EQ(SYNC_STATUS_OK, metadata_store()->GetConflictURLs(&urls));
    361   EXPECT_EQ(2U, urls.size());
    362   EXPECT_FALSE(ContainsKey(urls, URL(path1)));
    363   EXPECT_TRUE(ContainsKey(urls, URL(path2)));
    364   EXPECT_TRUE(ContainsKey(urls, URL(path3)));
    365 
    366   VerifyReverseMap();
    367 }
    368 
    369 void DriveMetadataStoreTest::GetToBeFetchedFiles_Body() {
    370   InitializeDatabase();
    371 
    372   DriveMetadataStore::URLAndDriveMetadataList list;
    373   EXPECT_EQ(SYNC_STATUS_OK, metadata_store()->GetToBeFetchedFiles(&list));
    374   EXPECT_TRUE(list.empty());
    375 
    376   const base::FilePath path1(FPL("file1"));
    377   const base::FilePath path2(FPL("file2"));
    378   const base::FilePath path3(FPL("file3"));
    379 
    380   // Populate metadata in DriveMetadataStore. The metadata identified by "file2"
    381   // and "file3" are marked to be fetched.
    382   EXPECT_EQ(SYNC_STATUS_OK, UpdateEntry(
    383       URL(path1),
    384       CreateMetadata("file:1", "1", false, false,
    385                      DriveMetadata_ResourceType_RESOURCE_TYPE_FILE)));
    386   EXPECT_EQ(SYNC_STATUS_OK, UpdateEntry(
    387       URL(path2),
    388       CreateMetadata("file:2", "2", false, true,
    389                      DriveMetadata_ResourceType_RESOURCE_TYPE_FILE)));
    390   EXPECT_EQ(SYNC_STATUS_OK, UpdateEntry(
    391       URL(path3),
    392       CreateMetadata("file:3", "3", false, true,
    393                      DriveMetadata_ResourceType_RESOURCE_TYPE_FILE)));
    394 
    395   EXPECT_EQ(SYNC_STATUS_OK,
    396             metadata_store()->GetToBeFetchedFiles(&list));
    397   EXPECT_EQ(2U, list.size());
    398   EXPECT_EQ(list[0].first, URL(path2));
    399   EXPECT_EQ(list[1].first, URL(path3));
    400 
    401   VerifyReverseMap();
    402 }
    403 
    404 void DriveMetadataStoreTest::StoreSyncRootDirectory_Body() {
    405   const std::string kResourceId(CreateResourceId("folder:hoge"));
    406 
    407   InitializeDatabase();
    408   EXPECT_TRUE(metadata_store()->sync_root_directory().empty());
    409 
    410   metadata_store()->SetSyncRootDirectory(kResourceId);
    411   EXPECT_EQ(kResourceId, metadata_store()->sync_root_directory());
    412 
    413   DropDatabase();
    414   InitializeDatabase();
    415   EXPECT_EQ(kResourceId, metadata_store()->sync_root_directory());
    416 }
    417 
    418 void DriveMetadataStoreTest::StoreSyncOrigin_Body() {
    419   const GURL kOrigin1("chrome-extension://example1");
    420   const GURL kOrigin2("chrome-extension://example2");
    421   const std::string kResourceId1(CreateResourceId("folder:hoge"));
    422   const std::string kResourceId2(CreateResourceId("folder:fuga"));
    423 
    424   InitializeDatabase();
    425 
    426   // Make sure origins have not been marked yet.
    427   VerifyUntrackedOrigin(kOrigin1);
    428   VerifyUntrackedOrigin(kOrigin2);
    429 
    430   // Mark origins as incremental sync origins.
    431   metadata_store()->AddIncrementalSyncOrigin(kOrigin1, kResourceId1);
    432   metadata_store()->AddIncrementalSyncOrigin(kOrigin2, kResourceId2);
    433   VerifyIncrementalSyncOrigin(kOrigin1, kResourceId1);
    434   VerifyIncrementalSyncOrigin(kOrigin2, kResourceId2);
    435 
    436   // Disabled origin 2, origin 1 should still be incremental.
    437   DisableOrigin(kOrigin2);
    438   VerifyIncrementalSyncOrigin(kOrigin1, kResourceId1);
    439   VerifyDisabledOrigin(kOrigin2, kResourceId2);
    440 
    441   DropDatabase();
    442   InitializeDatabase();
    443 
    444   // Make sure origins have been restored.
    445   VerifyIncrementalSyncOrigin(kOrigin1, kResourceId1);
    446   VerifyDisabledOrigin(kOrigin2, kResourceId2);
    447 
    448   VerifyReverseMap();
    449 }
    450 
    451 void DriveMetadataStoreTest::DisableOrigin_Body() {
    452   const GURL kOrigin1("chrome-extension://example1");
    453   const std::string kResourceId1(CreateResourceId("folder:hoge"));
    454 
    455   InitializeDatabase();
    456   EXPECT_EQ(SYNC_STATUS_OK, SetLargestChangeStamp(1));
    457 
    458   metadata_store()->AddIncrementalSyncOrigin(kOrigin1, kResourceId1);
    459   VerifyIncrementalSyncOrigin(kOrigin1, kResourceId1);
    460 
    461   DisableOrigin(kOrigin1);
    462   VerifyDisabledOrigin(kOrigin1, kResourceId1);
    463 
    464   // Re-enabled origins go back to DriveFileSyncService and are not tracked
    465   // in DriveMetadataStore.
    466   EnableOrigin(kOrigin1);
    467   VerifyUntrackedOrigin(kOrigin1);
    468 }
    469 
    470 void DriveMetadataStoreTest::RemoveOrigin_Body() {
    471   const GURL kOrigin1("chrome-extension://example1");
    472   const GURL kOrigin2("chrome-extension://example2");
    473   const GURL kOrigin3("chrome-extension://example3");
    474   const std::string kResourceId1(CreateResourceId("folder:hogera"));
    475   const std::string kResourceId2(CreateResourceId("folder:fugaga"));
    476   const std::string kResourceId3(CreateResourceId("folder:piyopiyo"));
    477 
    478   InitializeDatabase();
    479   EXPECT_EQ(SYNC_STATUS_OK, SetLargestChangeStamp(1));
    480 
    481   metadata_store()->AddIncrementalSyncOrigin(kOrigin1, kResourceId1);
    482   metadata_store()->AddIncrementalSyncOrigin(kOrigin2, kResourceId2);
    483   metadata_store()->AddIncrementalSyncOrigin(kOrigin3, kResourceId3);
    484   DisableOrigin(kOrigin3);
    485   EXPECT_EQ(2u, metadata_store()->incremental_sync_origins().size());
    486   EXPECT_EQ(1u, metadata_store()->disabled_origins().size());
    487 
    488   EXPECT_EQ(SYNC_STATUS_OK, UpdateEntry(
    489       CreateSyncableFileSystemURL(kOrigin1, base::FilePath(FPL("guf"))),
    490       CreateMetadata("file:foo", "spam", false, false,
    491                      DriveMetadata_ResourceType_RESOURCE_TYPE_FILE)));
    492   EXPECT_EQ(SYNC_STATUS_OK, UpdateEntry(
    493       CreateSyncableFileSystemURL(kOrigin2, base::FilePath(FPL("mof"))),
    494       CreateMetadata("file:bar", "ham", false, false,
    495                      DriveMetadata_ResourceType_RESOURCE_TYPE_FILE)));
    496   EXPECT_EQ(SYNC_STATUS_OK, UpdateEntry(
    497       CreateSyncableFileSystemURL(kOrigin3, base::FilePath(FPL("waf"))),
    498       CreateMetadata("folder:baz", "egg", false, false,
    499                      DriveMetadata_ResourceType_RESOURCE_TYPE_FOLDER)));
    500 
    501   EXPECT_EQ(SYNC_STATUS_OK, RemoveOrigin(kOrigin2));
    502   EXPECT_EQ(SYNC_STATUS_OK, RemoveOrigin(kOrigin3));
    503 
    504   DropDatabase();
    505   InitializeDatabase();
    506 
    507   // kOrigin1 should be the only one left.
    508   EXPECT_EQ(1u, metadata_store()->incremental_sync_origins().size());
    509   EXPECT_EQ(0u, metadata_store()->disabled_origins().size());
    510   EXPECT_TRUE(metadata_store()->IsIncrementalSyncOrigin(kOrigin1));
    511   EXPECT_EQ(1u, metadata_map().size());
    512 
    513   DriveMetadataStore::MetadataMap::const_iterator found =
    514       metadata_map().find(kOrigin1);
    515   EXPECT_TRUE(found != metadata_map().end() && found->second.size() == 1u);
    516 
    517   VerifyReverseMap();
    518 }
    519 
    520 void DriveMetadataStoreTest::GetResourceIdForOrigin_Body() {
    521   const GURL kOrigin1("chrome-extension://example1");
    522   const GURL kOrigin2("chrome-extension://example2");
    523   const GURL kOrigin3("chrome-extension://example3");
    524   const std::string kResourceId1(CreateResourceId("folder:hogera"));
    525   const std::string kResourceId2(CreateResourceId("folder:fugaga"));
    526   const std::string kResourceId3(CreateResourceId("folder:piyopiyo"));
    527 
    528   InitializeDatabase();
    529   EXPECT_EQ(SYNC_STATUS_OK, SetLargestChangeStamp(1));
    530   metadata_store()->SetSyncRootDirectory(CreateResourceId("folder:root"));
    531 
    532   metadata_store()->AddIncrementalSyncOrigin(kOrigin1, kResourceId1);
    533   metadata_store()->AddIncrementalSyncOrigin(kOrigin2, kResourceId2);
    534   metadata_store()->AddIncrementalSyncOrigin(kOrigin3, kResourceId3);
    535   DisableOrigin(kOrigin3);
    536 
    537   EXPECT_EQ(kResourceId1, metadata_store()->GetResourceIdForOrigin(kOrigin1));
    538   EXPECT_EQ(kResourceId2, metadata_store()->GetResourceIdForOrigin(kOrigin2));
    539   EXPECT_EQ(kResourceId3, metadata_store()->GetResourceIdForOrigin(kOrigin3));
    540 
    541   DropDatabase();
    542   InitializeDatabase();
    543 
    544   EXPECT_EQ(kResourceId1, metadata_store()->GetResourceIdForOrigin(kOrigin1));
    545   EXPECT_EQ(kResourceId2, metadata_store()->GetResourceIdForOrigin(kOrigin2));
    546   EXPECT_EQ(kResourceId3, metadata_store()->GetResourceIdForOrigin(kOrigin3));
    547 
    548   // Resetting the root directory resource ID to empty makes any
    549   // GetResourceIdForOrigin return an empty resource ID too, regardless of
    550   // whether they are known origin or not.
    551   metadata_store()->SetSyncRootDirectory(std::string());
    552   EXPECT_TRUE(metadata_store()->GetResourceIdForOrigin(kOrigin1).empty());
    553   EXPECT_TRUE(metadata_store()->GetResourceIdForOrigin(kOrigin2).empty());
    554   EXPECT_TRUE(metadata_store()->GetResourceIdForOrigin(kOrigin3).empty());
    555 
    556   // Make sure they're still known origins.
    557   EXPECT_TRUE(metadata_store()->IsKnownOrigin(kOrigin1));
    558   EXPECT_TRUE(metadata_store()->IsKnownOrigin(kOrigin2));
    559   EXPECT_TRUE(metadata_store()->IsKnownOrigin(kOrigin3));
    560 
    561   VerifyReverseMap();
    562 }
    563 
    564 void DriveMetadataStoreTest::ResetOriginRootDirectory_Body() {
    565   const GURL kOrigin1("chrome-extension://example1");
    566   const std::string kResourceId1(CreateResourceId("folder:hoge"));
    567   const std::string kResourceId2(CreateResourceId("folder:fuga"));
    568 
    569   InitializeDatabase();
    570   EXPECT_EQ(SYNC_STATUS_OK, SetLargestChangeStamp(1));
    571 
    572   metadata_store()->AddIncrementalSyncOrigin(kOrigin1, kResourceId1);
    573   VerifyIncrementalSyncOrigin(kOrigin1, kResourceId1);
    574   VerifyReverseMap();
    575 
    576   metadata_store()->SetOriginRootDirectory(kOrigin1, kResourceId2);
    577   VerifyIncrementalSyncOrigin(kOrigin1, kResourceId2);
    578   VerifyReverseMap();
    579 }
    580 
    581 void DriveMetadataStoreTest::GetFileMetadataMap_Body() {
    582   InitializeDatabase();
    583 
    584   // Save one file and folder to the origin.
    585   const base::FilePath file_path = base::FilePath(FPL("file_0"));
    586   const base::FilePath folder_path = base::FilePath(FPL("folder_0"));
    587 
    588   const GURL origin = GURL("chrome-extension://app_a");
    589   const fileapi::FileSystemURL url_0 = CreateSyncableFileSystemURL(
    590       origin, file_path);
    591   const fileapi::FileSystemURL url_1 = CreateSyncableFileSystemURL(
    592       origin, folder_path);
    593 
    594   // Insert DrivaMetadata objects.
    595   EXPECT_EQ(SYNC_STATUS_OK, UpdateEntry(
    596       url_0,
    597       CreateMetadata("file:0", "1", false, false,
    598                      DriveMetadata_ResourceType_RESOURCE_TYPE_FILE)));
    599   EXPECT_EQ(SYNC_STATUS_OK, UpdateEntry(
    600       url_1,
    601       CreateMetadata("folder:0", "2", false, true,
    602                      DriveMetadata_ResourceType_RESOURCE_TYPE_FOLDER)));
    603 
    604   // Check that DriveMetadata objects get mapped back to generalized
    605   // FileMetadata objects.
    606   scoped_ptr<base::ListValue> files = metadata_store()->DumpFiles(origin);
    607   ASSERT_EQ(2u, files->GetSize());
    608 
    609   base::DictionaryValue* file = NULL;
    610   std::string str;
    611 
    612   ASSERT_TRUE(files->GetDictionary(0, &file));
    613   EXPECT_TRUE(file->GetString("title", &str) && str == "file_0");
    614   EXPECT_TRUE(file->GetString("type", &str) && str == "file");
    615   EXPECT_TRUE(file->HasKey("details"));
    616 
    617   ASSERT_TRUE(files->GetDictionary(1, &file));
    618   EXPECT_TRUE(file->GetString("title", &str) && str == "folder_0");
    619   EXPECT_TRUE(file->GetString("type", &str) && str == "folder");
    620   EXPECT_TRUE(file->HasKey("details"));
    621 }
    622 
    623 TEST_F(DriveMetadataStoreTest, Initialization) {
    624   ASSERT_FALSE(IsDriveAPIDisabled());
    625   InitializeDatabase();
    626 }
    627 
    628 TEST_F(DriveMetadataStoreTest, Initialization_WAPI) {
    629   SetDisableDriveAPI(true);
    630   InitializeDatabase();
    631 }
    632 
    633 TEST_F(DriveMetadataStoreTest, ReadWrite) {
    634   ASSERT_FALSE(IsDriveAPIDisabled());
    635   ReadWrite_Body();
    636 }
    637 
    638 TEST_F(DriveMetadataStoreTest, ReadWrite_WAPI) {
    639   SetDisableDriveAPI(true);
    640   ReadWrite_Body();
    641 }
    642 
    643 TEST_F(DriveMetadataStoreTest, GetConflictURLs) {
    644   ASSERT_FALSE(IsDriveAPIDisabled());
    645   GetConflictURLs_Body();
    646 }
    647 
    648 TEST_F(DriveMetadataStoreTest, GetConflictURLs_WAPI) {
    649   SetDisableDriveAPI(true);
    650   GetConflictURLs_Body();
    651 }
    652 
    653 TEST_F(DriveMetadataStoreTest, GetToBeFetchedFiles) {
    654   ASSERT_FALSE(IsDriveAPIDisabled());
    655   GetToBeFetchedFiles_Body();
    656 }
    657 
    658 TEST_F(DriveMetadataStoreTest, GetToBeFetchedFiles_WAPI) {
    659   SetDisableDriveAPI(true);
    660   GetToBeFetchedFiles_Body();
    661 }
    662 
    663 TEST_F(DriveMetadataStoreTest, StoreSyncRootDirectory) {
    664   ASSERT_FALSE(IsDriveAPIDisabled());
    665   StoreSyncRootDirectory_Body();
    666 }
    667 
    668 TEST_F(DriveMetadataStoreTest, StoreSyncRootDirectory_WAPI) {
    669   SetDisableDriveAPI(true);
    670   StoreSyncRootDirectory_Body();
    671 }
    672 
    673 TEST_F(DriveMetadataStoreTest, StoreSyncOrigin) {
    674   ASSERT_FALSE(IsDriveAPIDisabled());
    675   StoreSyncOrigin_Body();
    676 }
    677 
    678 TEST_F(DriveMetadataStoreTest, StoreSyncOrigin_WAPI) {
    679   SetDisableDriveAPI(true);
    680   StoreSyncOrigin_Body();
    681 }
    682 
    683 TEST_F(DriveMetadataStoreTest, DisableOrigin) {
    684   ASSERT_FALSE(IsDriveAPIDisabled());
    685   DisableOrigin_Body();
    686 }
    687 
    688 TEST_F(DriveMetadataStoreTest, DisableOrigin_WAPI) {
    689   SetDisableDriveAPI(true);
    690   DisableOrigin_Body();
    691 }
    692 
    693 TEST_F(DriveMetadataStoreTest, RemoveOrigin) {
    694   ASSERT_FALSE(IsDriveAPIDisabled());
    695   RemoveOrigin_Body();
    696 }
    697 
    698 TEST_F(DriveMetadataStoreTest, RemoveOrigin_WAPI) {
    699   SetDisableDriveAPI(true);
    700   RemoveOrigin_Body();
    701 }
    702 
    703 TEST_F(DriveMetadataStoreTest, GetResourceIdForOrigin) {
    704   ASSERT_FALSE(IsDriveAPIDisabled());
    705   GetResourceIdForOrigin_Body();
    706 }
    707 
    708 TEST_F(DriveMetadataStoreTest, GetResourceIdForOrigin_WAPI) {
    709   SetDisableDriveAPI(true);
    710   GetResourceIdForOrigin_Body();
    711 }
    712 
    713 TEST_F(DriveMetadataStoreTest, ResetOriginRootDirectory) {
    714   ASSERT_FALSE(IsDriveAPIDisabled());
    715   ResetOriginRootDirectory_Body();
    716 }
    717 
    718 TEST_F(DriveMetadataStoreTest, ResetOriginRootDirectory_WAPI) {
    719   SetDisableDriveAPI(true);
    720   ResetOriginRootDirectory_Body();
    721 }
    722 
    723 TEST_F(DriveMetadataStoreTest, GetFileMetadataMap) {
    724   ASSERT_FALSE(IsDriveAPIDisabled());
    725   GetFileMetadataMap_Body();
    726 }
    727 
    728 TEST_F(DriveMetadataStoreTest, GetFileMetadataMap_WAPI) {
    729   SetDisableDriveAPI(true);
    730   GetFileMetadataMap_Body();
    731 }
    732 
    733 }  // namespace sync_file_system
    734