Home | History | Annotate | Download | only in engine
      1 // Copyright (c) 2010 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/engine/verify_updates_command.h"
      6 #include "chrome/browser/sync/protocol/bookmark_specifics.pb.h"
      7 #include "chrome/browser/sync/sessions/sync_session.h"
      8 #include "chrome/browser/sync/syncable/directory_manager.h"
      9 #include "chrome/browser/sync/engine/mock_model_safe_workers.h"
     10 #include "chrome/browser/sync/syncable/syncable.h"
     11 #include "chrome/browser/sync/syncable/syncable_id.h"
     12 #include "chrome/test/sync/engine/syncer_command_test.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 
     15 namespace browser_sync {
     16 
     17 using sessions::SyncSession;
     18 using sessions::StatusController;
     19 using std::string;
     20 using syncable::Entry;
     21 using syncable::Id;
     22 using syncable::MutableEntry;
     23 using syncable::ReadTransaction;
     24 using syncable::ScopedDirLookup;
     25 using syncable::UNITTEST;
     26 using syncable::WriteTransaction;
     27 
     28 class VerifyUpdatesCommandTest : public SyncerCommandTest {
     29  public:
     30   virtual void SetUp() {
     31     workers()->clear();
     32     mutable_routing_info()->clear();
     33     workers()->push_back(make_scoped_refptr(new MockDBModelWorker()));
     34     workers()->push_back(make_scoped_refptr(new MockUIModelWorker()));
     35     (*mutable_routing_info())[syncable::PREFERENCES] = GROUP_UI;
     36     (*mutable_routing_info())[syncable::BOOKMARKS] = GROUP_UI;
     37     (*mutable_routing_info())[syncable::AUTOFILL] = GROUP_DB;
     38     SyncerCommandTest::SetUp();
     39   }
     40 
     41   void CreateLocalItem(const std::string& item_id,
     42                        const std::string& parent_id,
     43                        const syncable::ModelType& type) {
     44     ScopedDirLookup dir(syncdb()->manager(), syncdb()->name());
     45     ASSERT_TRUE(dir.good());
     46     WriteTransaction trans(dir, UNITTEST, __FILE__, __LINE__);
     47     MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM,
     48         Id::CreateFromServerId(item_id));
     49     ASSERT_TRUE(entry.good());
     50 
     51     entry.Put(syncable::BASE_VERSION, 1);
     52     entry.Put(syncable::SERVER_VERSION, 1);
     53     entry.Put(syncable::NON_UNIQUE_NAME, item_id);
     54     entry.Put(syncable::PARENT_ID, Id::CreateFromServerId(parent_id));
     55     sync_pb::EntitySpecifics default_specifics;
     56     AddDefaultExtensionValue(type, &default_specifics);
     57     entry.Put(syncable::SERVER_SPECIFICS, default_specifics);
     58   }
     59 
     60   void AddUpdate(GetUpdatesResponse* updates,
     61       const std::string& id, const std::string& parent,
     62       const syncable::ModelType& type) {
     63     sync_pb::SyncEntity* e = updates->add_entries();
     64     e->set_id_string("b1");
     65     e->set_parent_id_string(parent);
     66     e->set_non_unique_name("b1");
     67     e->set_name("b1");
     68     AddDefaultExtensionValue(type, e->mutable_specifics());
     69   }
     70 
     71   VerifyUpdatesCommand command_;
     72 
     73 };
     74 
     75 TEST_F(VerifyUpdatesCommandTest, AllVerified) {
     76   string root = syncable::kNullId.GetServerId();
     77 
     78   CreateLocalItem("b1", root, syncable::BOOKMARKS);
     79   CreateLocalItem("b2", root, syncable::BOOKMARKS);
     80   CreateLocalItem("p1", root, syncable::PREFERENCES);
     81   CreateLocalItem("a1", root, syncable::AUTOFILL);
     82 
     83   GetUpdatesResponse* updates = session()->status_controller()->
     84       mutable_updates_response()->mutable_get_updates();
     85   AddUpdate(updates, "b1", root, syncable::BOOKMARKS);
     86   AddUpdate(updates, "b2", root, syncable::BOOKMARKS);
     87   AddUpdate(updates, "p1", root, syncable::PREFERENCES);
     88   AddUpdate(updates, "a1", root, syncable::AUTOFILL);
     89 
     90   command_.ExecuteImpl(session());
     91 
     92   StatusController* status = session()->status_controller();
     93   {
     94     sessions::ScopedModelSafeGroupRestriction r(status, GROUP_UI);
     95     EXPECT_EQ(3, status->update_progress().VerifiedUpdatesSize());
     96   }
     97   {
     98     sessions::ScopedModelSafeGroupRestriction r(status, GROUP_DB);
     99     EXPECT_EQ(1, status->update_progress().VerifiedUpdatesSize());
    100   }
    101   {
    102     sessions::ScopedModelSafeGroupRestriction r(status, GROUP_PASSIVE);
    103     EXPECT_EQ(0, status->update_progress().VerifiedUpdatesSize());
    104   }
    105 }
    106 
    107 }
    108