Home | History | Annotate | Download | only in api
      1 // Copyright (c) 2012 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 "sync/api/sync_change.h"
      6 
      7 #include <string>
      8 
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/time/time.h"
     11 #include "base/values.h"
     12 #include "sync/protocol/preference_specifics.pb.h"
     13 #include "sync/protocol/proto_value_conversions.h"
     14 #include "sync/protocol/sync.pb.h"
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 
     17 namespace syncer {
     18 
     19 // Ordered list of SyncChange's.
     20 typedef std::vector<SyncChange> SyncChangeList;
     21 
     22 namespace {
     23 
     24 typedef testing::Test SyncChangeTest;
     25 
     26 TEST_F(SyncChangeTest, LocalDelete) {
     27   SyncChange::SyncChangeType change_type = SyncChange::ACTION_DELETE;
     28   std::string tag = "client_tag";
     29   SyncChange e(FROM_HERE,
     30                change_type,
     31                SyncData::CreateLocalDelete(tag, PREFERENCES));
     32   EXPECT_EQ(change_type, e.change_type());
     33   EXPECT_EQ(tag, e.sync_data().GetTag());
     34   EXPECT_EQ(PREFERENCES, e.sync_data().GetDataType());
     35 }
     36 
     37 TEST_F(SyncChangeTest, LocalUpdate) {
     38   SyncChange::SyncChangeType change_type = SyncChange::ACTION_UPDATE;
     39   sync_pb::EntitySpecifics specifics;
     40   sync_pb::PreferenceSpecifics* pref_specifics = specifics.mutable_preference();
     41   pref_specifics->set_name("test");
     42   std::string tag = "client_tag";
     43   std::string title = "client_title";
     44   SyncChange e(FROM_HERE,
     45                change_type,
     46                SyncData::CreateLocalData(tag, title, specifics));
     47   EXPECT_EQ(change_type, e.change_type());
     48   EXPECT_EQ(tag, e.sync_data().GetTag());
     49   EXPECT_EQ(title, e.sync_data().GetTitle());
     50   EXPECT_EQ(PREFERENCES, e.sync_data().GetDataType());
     51   scoped_ptr<base::DictionaryValue> ref_spec(EntitySpecificsToValue(specifics));
     52   scoped_ptr<base::DictionaryValue> e_spec(EntitySpecificsToValue(
     53       e.sync_data().GetSpecifics()));
     54   EXPECT_TRUE(ref_spec->Equals(e_spec.get()));
     55 }
     56 
     57 TEST_F(SyncChangeTest, LocalAdd) {
     58   SyncChange::SyncChangeType change_type = SyncChange::ACTION_ADD;
     59   sync_pb::EntitySpecifics specifics;
     60   sync_pb::PreferenceSpecifics* pref_specifics = specifics.mutable_preference();
     61   pref_specifics->set_name("test");
     62   std::string tag = "client_tag";
     63   std::string title = "client_title";
     64   SyncChange e(FROM_HERE,
     65                change_type,
     66                SyncData::CreateLocalData(tag, title, specifics));
     67   EXPECT_EQ(change_type, e.change_type());
     68   EXPECT_EQ(tag, e.sync_data().GetTag());
     69   EXPECT_EQ(title, e.sync_data().GetTitle());
     70   EXPECT_EQ(PREFERENCES, e.sync_data().GetDataType());
     71   scoped_ptr<base::DictionaryValue> ref_spec(EntitySpecificsToValue(specifics));
     72   scoped_ptr<base::DictionaryValue> e_spec(EntitySpecificsToValue(
     73       e.sync_data().GetSpecifics()));
     74   EXPECT_TRUE(ref_spec->Equals(e_spec.get()));
     75 }
     76 
     77 TEST_F(SyncChangeTest, SyncerChanges) {
     78   SyncChangeList change_list;
     79 
     80   // Create an update.
     81   sync_pb::EntitySpecifics update_specifics;
     82   sync_pb::PreferenceSpecifics* pref_specifics =
     83       update_specifics.mutable_preference();
     84   pref_specifics->set_name("update");
     85   change_list.push_back(SyncChange(
     86       FROM_HERE,
     87       SyncChange::ACTION_UPDATE,
     88       SyncData::CreateRemoteData(1, update_specifics, base::Time())));
     89 
     90   // Create an add.
     91   sync_pb::EntitySpecifics add_specifics;
     92   pref_specifics = add_specifics.mutable_preference();
     93   pref_specifics->set_name("add");
     94   change_list.push_back(SyncChange(
     95       FROM_HERE,
     96       SyncChange::ACTION_ADD,
     97       SyncData::CreateRemoteData(2, add_specifics, base::Time())));
     98 
     99   // Create a delete.
    100   sync_pb::EntitySpecifics delete_specifics;
    101   pref_specifics = delete_specifics.mutable_preference();
    102   pref_specifics->set_name("add");
    103   change_list.push_back(SyncChange(
    104       FROM_HERE,
    105       SyncChange::ACTION_DELETE,
    106       SyncData::CreateRemoteData(3, delete_specifics, base::Time())));
    107 
    108   ASSERT_EQ(3U, change_list.size());
    109 
    110   // Verify update.
    111   SyncChange e = change_list[0];
    112   EXPECT_EQ(SyncChange::ACTION_UPDATE, e.change_type());
    113   EXPECT_EQ(PREFERENCES, e.sync_data().GetDataType());
    114   scoped_ptr<base::DictionaryValue> ref_spec(EntitySpecificsToValue(
    115       update_specifics));
    116   scoped_ptr<base::DictionaryValue> e_spec(EntitySpecificsToValue(
    117       e.sync_data().GetSpecifics()));
    118   EXPECT_TRUE(ref_spec->Equals(e_spec.get()));
    119 
    120   // Verify add.
    121   e = change_list[1];
    122   EXPECT_EQ(SyncChange::ACTION_ADD, e.change_type());
    123   EXPECT_EQ(PREFERENCES, e.sync_data().GetDataType());
    124   ref_spec.reset(EntitySpecificsToValue(add_specifics));
    125   e_spec.reset(EntitySpecificsToValue(e.sync_data().GetSpecifics()));
    126   EXPECT_TRUE(ref_spec->Equals(e_spec.get()));
    127 
    128   // Verify delete.
    129   e = change_list[2];
    130   EXPECT_EQ(SyncChange::ACTION_DELETE, e.change_type());
    131   EXPECT_EQ(PREFERENCES, e.sync_data().GetDataType());
    132   ref_spec.reset(EntitySpecificsToValue(delete_specifics));
    133   e_spec.reset(EntitySpecificsToValue(e.sync_data().GetSpecifics()));
    134   EXPECT_TRUE(ref_spec->Equals(e_spec.get()));
    135 }
    136 
    137 }  // namespace
    138 
    139 }  // namespace syncer
    140