Home | History | Annotate | Download | only in glue
      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 <string>
      6 
      7 #include "base/guid.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/memory/scoped_vector.h"
     10 #include "base/message_loop/message_loop.h"
     11 #include "base/run_loop.h"
     12 #include "chrome/browser/sync/glue/device_info.h"
     13 #include "chrome/browser/sync/glue/synced_device_tracker.h"
     14 #include "sync/internal_api/public/base/model_type.h"
     15 #include "sync/internal_api/public/test/test_user_share.h"
     16 #include "sync/protocol/sync.pb.h"
     17 #include "sync/syncable/directory.h"
     18 #include "sync/test/test_transaction_observer.h"
     19 #include "testing/gtest/include/gtest/gtest.h"
     20 
     21 namespace browser_sync {
     22 
     23 namespace {
     24 
     25 void ConvertDeviceInfoSpecifics(
     26     const DeviceInfo& device_info,
     27     sync_pb::DeviceInfoSpecifics* specifics) {
     28   specifics->set_cache_guid(device_info.guid());
     29   specifics->set_client_name(device_info.client_name());
     30   specifics->set_chrome_version(device_info.chrome_version());
     31   specifics->set_sync_user_agent(device_info.sync_user_agent());
     32   specifics->set_device_type(device_info.device_type());
     33 }
     34 
     35 }  // namespace
     36 
     37 class SyncedDeviceTrackerTest : public ::testing::Test {
     38  protected:
     39   SyncedDeviceTrackerTest() : transaction_count_baseline_(0) { }
     40   virtual ~SyncedDeviceTrackerTest() { }
     41 
     42   virtual void SetUp() {
     43     test_user_share_.SetUp();
     44     syncer::TestUserShare::CreateRoot(syncer::DEVICE_INFO, user_share());
     45 
     46     synced_device_tracker_.reset(
     47         new SyncedDeviceTracker(user_share(),
     48                                 user_share()->directory->cache_guid()));
     49 
     50     // We don't actually touch the Profile, so we can get away with passing in a
     51     // NULL here.  Constructing a TestingProfile can take over a 100ms, so this
     52     // optimization can be the difference between 'tests run with a noticeable
     53     // delay' and 'tests run instantaneously'.
     54     synced_device_tracker_->Start(user_share());
     55   }
     56 
     57   virtual void TearDown() {
     58     synced_device_tracker_.reset();
     59     test_user_share_.TearDown();
     60   }
     61 
     62   syncer::UserShare* user_share() {
     63     return test_user_share_.user_share();
     64   }
     65 
     66   // Expose the private method to our tests.
     67   void WriteLocalDeviceInfo(const DeviceInfo& info) {
     68     synced_device_tracker_->WriteLocalDeviceInfo(info);
     69   }
     70 
     71   void WriteDeviceInfo(const DeviceInfo& device_info) {
     72     sync_pb::DeviceInfoSpecifics specifics;
     73     ConvertDeviceInfoSpecifics(device_info, &specifics);
     74     synced_device_tracker_->WriteDeviceInfo(specifics, device_info.guid());
     75   }
     76 
     77   void ResetObservedChangesCounter() {
     78     transaction_count_baseline_ = GetTotalTransactionsCount();
     79   }
     80 
     81   int GetObservedChangesCounter() {
     82     return GetTotalTransactionsCount() - transaction_count_baseline_;
     83   }
     84 
     85   scoped_ptr<SyncedDeviceTracker> synced_device_tracker_;
     86 
     87  private:
     88   // Count of how many closed WriteTransactions notified of meaningful changes.
     89   int GetTotalTransactionsCount() {
     90     base::RunLoop run_loop;
     91     run_loop.RunUntilIdle();
     92     return test_user_share_.transaction_observer()->transactions_observed();
     93   }
     94 
     95   base::MessageLoop message_loop_;
     96   syncer::TestUserShare test_user_share_;
     97   int transaction_count_baseline_;
     98 };
     99 
    100 namespace {
    101 
    102 // New client scenario: set device info when no previous info existed.
    103 TEST_F(SyncedDeviceTrackerTest, CreateNewDeviceInfo) {
    104   ASSERT_FALSE(synced_device_tracker_->ReadLocalDeviceInfo());
    105 
    106   ResetObservedChangesCounter();
    107 
    108   // Include the non-ASCII character "" (typographic apostrophe) in the client
    109   // name to ensure that SyncedDeviceTracker can properly handle non-ASCII
    110   // characters, which client names can include on some platforms (e.g., Mac
    111   // and iOS).
    112   DeviceInfo write_device_info(
    113       user_share()->directory->cache_guid(),
    114       "Johns Device", "Chromium 3000", "ChromeSyncAgent 3000",
    115       sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
    116   WriteLocalDeviceInfo(write_device_info);
    117 
    118   scoped_ptr<DeviceInfo> read_device_info(
    119       synced_device_tracker_->ReadLocalDeviceInfo());
    120   ASSERT_TRUE(read_device_info);
    121   EXPECT_TRUE(write_device_info.Equals(*read_device_info.get()));
    122 
    123   EXPECT_EQ(1, GetObservedChangesCounter());
    124 }
    125 
    126 // Restart scenario: update existing device info with identical data.
    127 TEST_F(SyncedDeviceTrackerTest, DontModifyExistingDeviceInfo) {
    128   // For writing.
    129   DeviceInfo device_info(
    130       user_share()->directory->cache_guid(),
    131       "Johns Device", "XYZ v1", "XYZ SyncAgent v1",
    132       sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
    133   WriteLocalDeviceInfo(device_info);
    134 
    135   // First read.
    136   scoped_ptr<DeviceInfo> old_device_info(
    137       synced_device_tracker_->ReadLocalDeviceInfo());
    138   ASSERT_TRUE(old_device_info);
    139 
    140   ResetObservedChangesCounter();
    141 
    142   // Overwrite the device info with the same data as before.
    143   WriteLocalDeviceInfo(device_info);
    144 
    145   // Ensure that this didn't count as a change worth syncing.
    146   EXPECT_EQ(0, GetObservedChangesCounter());
    147 
    148   // Second read.
    149   scoped_ptr<DeviceInfo> new_device_info(
    150       synced_device_tracker_->ReadLocalDeviceInfo());
    151   ASSERT_TRUE(new_device_info);
    152   EXPECT_TRUE(old_device_info->Equals(*new_device_info.get()));
    153 }
    154 
    155 // Upgrade scenario: update existing device info with new version.
    156 TEST_F(SyncedDeviceTrackerTest, UpdateExistingDeviceInfo) {
    157   // Write v1 device info.
    158   DeviceInfo device_info_v1(
    159       user_share()->directory->cache_guid(),
    160       "Johns Device", "XYZ v1", "XYZ SyncAgent v1",
    161       sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
    162   WriteLocalDeviceInfo(device_info_v1);
    163 
    164   ResetObservedChangesCounter();
    165 
    166   // Write upgraded device info.
    167   DeviceInfo device_info_v2(
    168       user_share()->directory->cache_guid(),
    169       "Johns Device", "XYZ v2", "XYZ SyncAgent v2",
    170       sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
    171   WriteLocalDeviceInfo(device_info_v2);
    172 
    173   // Verify result.
    174   scoped_ptr<DeviceInfo> result_device_info(
    175       synced_device_tracker_->ReadLocalDeviceInfo());
    176   ASSERT_TRUE(result_device_info);
    177 
    178   EXPECT_TRUE(result_device_info->Equals(device_info_v2));
    179 
    180   // The update write should have sent a nudge.
    181   EXPECT_EQ(1, GetObservedChangesCounter());
    182 }
    183 
    184 // Test retrieving DeviceInfos for all the syncing devices.
    185 TEST_F(SyncedDeviceTrackerTest, GetAllDeviceInfo) {
    186   DeviceInfo device_info1(
    187       base::GenerateGUID(),
    188       "abc Device", "XYZ v1", "XYZ SyncAgent v1",
    189       sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
    190 
    191   std::string guid1 = base::GenerateGUID();
    192 
    193   DeviceInfo device_info2(
    194       base::GenerateGUID(),
    195       "def Device", "XYZ v2", "XYZ SyncAgent v2",
    196       sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
    197 
    198   std::string guid2 = base::GenerateGUID();
    199 
    200   WriteDeviceInfo(device_info1);
    201   WriteDeviceInfo(device_info2);
    202 
    203   ScopedVector<DeviceInfo> device_info;
    204   synced_device_tracker_->GetAllSyncedDeviceInfo(&device_info);
    205 
    206   EXPECT_EQ(device_info.size(), 2U);
    207   EXPECT_TRUE(device_info[0]->Equals(device_info1));
    208   EXPECT_TRUE(device_info[1]->Equals(device_info2));
    209 }
    210 
    211 }  // namespace
    212 
    213 }  // namespace browser_sync
    214