Home | History | Annotate | Download | only in sync_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/sync_file_system/file_change.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/logging.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 namespace sync_file_system {
     12 
     13 namespace {
     14 
     15 FileChange AddOrUpdateFile() {
     16   return FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, SYNC_FILE_TYPE_FILE);
     17 }
     18 
     19 FileChange DeleteFile() {
     20   return FileChange(FileChange::FILE_CHANGE_DELETE, SYNC_FILE_TYPE_FILE);
     21 }
     22 
     23 FileChange AddDirectory() {
     24   return FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
     25                     SYNC_FILE_TYPE_DIRECTORY);
     26 }
     27 
     28 FileChange DeleteDirectory() {
     29   return FileChange(FileChange::FILE_CHANGE_DELETE, SYNC_FILE_TYPE_DIRECTORY);
     30 }
     31 
     32 template <size_t INPUT_SIZE>
     33 void CreateList(FileChangeList* list, const FileChange (&inputs)[INPUT_SIZE]) {
     34   list->clear();
     35   for (size_t i = 0; i < INPUT_SIZE; ++i)
     36     list->Update(inputs[i]);
     37 }
     38 
     39 template <size_t EXPECTED_SIZE>
     40 void VerifyList(const FileChangeList& list,
     41                 const FileChange (&expected)[EXPECTED_SIZE]) {
     42   SCOPED_TRACE(testing::Message() << "actual:" << list.DebugString());
     43   ASSERT_EQ(EXPECTED_SIZE, list.size());
     44   for (size_t i = 0; i < list.size(); ++i) {
     45     SCOPED_TRACE(testing::Message() << i << ": "
     46                  << " expected:" << expected[i].DebugString()
     47                  << " actual:" << list.list().at(i).DebugString());
     48     EXPECT_EQ(expected[i], list.list().at(i));
     49   }
     50 }
     51 
     52 }  // namespace
     53 
     54 TEST(FileChangeListTest, UpdateSimple) {
     55   FileChangeList list;
     56   const FileChange kInput1[] = { AddOrUpdateFile() };
     57   const FileChange kExpected1[] = { AddOrUpdateFile() };
     58   CreateList(&list, kInput1);
     59   VerifyList(list, kExpected1);
     60 
     61   // AddOrUpdate + Delete -> Delete.
     62   const FileChange kInput2[] = { AddOrUpdateFile(), DeleteFile() };
     63   const FileChange kExpected2[] = { DeleteFile() };
     64   CreateList(&list, kInput2);
     65   VerifyList(list, kExpected2);
     66 
     67   // Add + Delete -> empty (directory).
     68   const FileChange kInput3[] = { AddDirectory(), DeleteDirectory() };
     69   CreateList(&list, kInput3);
     70   ASSERT_TRUE(list.empty());
     71 
     72   // Delete + AddOrUpdate -> AddOrUpdate.
     73   const FileChange kInput4[] = { DeleteFile(), AddOrUpdateFile() };
     74   const FileChange kExpected4[] = { AddOrUpdateFile() };
     75   CreateList(&list, kInput4);
     76   VerifyList(list, kExpected4);
     77 
     78   // Delete + Add -> Add (directory).
     79   const FileChange kInput5[] = { DeleteDirectory(), AddDirectory() };
     80   const FileChange kExpected5[] = { AddDirectory() };
     81   CreateList(&list, kInput5);
     82   VerifyList(list, kExpected5);
     83 }
     84 
     85 TEST(FileChangeListTest, UpdateCombined) {
     86   FileChangeList list;
     87 
     88   // Longer ones.
     89   const FileChange kInput1[] = {
     90     AddOrUpdateFile(),
     91     AddOrUpdateFile(),
     92     AddOrUpdateFile(),
     93     AddOrUpdateFile(),
     94     DeleteFile(),
     95     AddDirectory(),
     96   };
     97   const FileChange kExpected1[] = { DeleteFile(), AddDirectory() };
     98   CreateList(&list, kInput1);
     99   VerifyList(list, kExpected1);
    100 
    101   const FileChange kInput2[] = {
    102     AddOrUpdateFile(),
    103     DeleteFile(),
    104     AddOrUpdateFile(),
    105     AddOrUpdateFile(),
    106     AddOrUpdateFile(),
    107   };
    108   const FileChange kExpected2[] = { AddOrUpdateFile() };
    109   CreateList(&list, kInput2);
    110   VerifyList(list, kExpected2);
    111 
    112   const FileChange kInput3[] = {
    113     AddDirectory(),
    114     DeleteDirectory(),
    115     AddOrUpdateFile(),
    116     AddOrUpdateFile(),
    117     AddOrUpdateFile(),
    118   };
    119   const FileChange kExpected3[] = { AddOrUpdateFile() };
    120   CreateList(&list, kInput3);
    121   VerifyList(list, kExpected3);
    122 
    123   const FileChange kInput4[] = {
    124     AddDirectory(),
    125     DeleteDirectory(),
    126     AddOrUpdateFile(),
    127     DeleteFile(),
    128     AddOrUpdateFile(),
    129     DeleteFile(),
    130   };
    131   const FileChange kExpected4[] = { DeleteFile() };
    132   CreateList(&list, kInput4);
    133   VerifyList(list, kExpected4);
    134 }
    135 
    136 }  // namespace sync_file_system
    137