OLD | NEW |
(Empty) | |
| 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/message_loop.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" |
| 10 #include "chrome/browser/sync/glue/device_info.h" |
| 11 #include "chrome/browser/sync/glue/synced_device_tracker.h" |
| 12 #include "sync/internal_api/public/base/model_type.h" |
| 13 #include "sync/internal_api/public/test/test_user_share.h" |
| 14 #include "sync/protocol/sync.pb.h" |
| 15 #include "sync/test/mock_transaction_observer.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace browser_sync { |
| 19 |
| 20 class SyncedDeviceTrackerTest : public ::testing::Test { |
| 21 protected: |
| 22 SyncedDeviceTrackerTest() : transaction_count_baseline_(0) { } |
| 23 ~SyncedDeviceTrackerTest() { } |
| 24 |
| 25 void SetUp() { |
| 26 test_user_share_.SetUp(); |
| 27 ProfileSyncServiceTestHelper::CreateRoot(syncer::DEVICE_INFO, user_share()); |
| 28 |
| 29 // We don't actually touch the Profile, so we can get away with passing in a |
| 30 // NULL here. Constructing a TestingProfile can take over a 100ms, so this |
| 31 // optimization can be the difference between 'tests run with a noticeable |
| 32 // delay' and 'tests run instantaneously'. |
| 33 synced_device_tracker_.Start(NULL, user_share()); |
| 34 } |
| 35 |
| 36 void TearDown() { |
| 37 synced_device_tracker_.Stop(); |
| 38 test_user_share_.TearDown(); |
| 39 } |
| 40 |
| 41 syncer::UserShare* user_share() { |
| 42 return test_user_share_.user_share(); |
| 43 } |
| 44 |
| 45 // Expose the private method to our tests. |
| 46 void WriteLocalDeviceInfo(const DeviceInfo& info) { |
| 47 synced_device_tracker_.WriteLocalDeviceInfo(info); |
| 48 } |
| 49 |
| 50 void ResetObservedChangesCounter() { |
| 51 transaction_count_baseline_ = GetTotalTransactionsCount(); |
| 52 } |
| 53 |
| 54 int GetObservedChangesCounter() { |
| 55 return GetTotalTransactionsCount() - transaction_count_baseline_; |
| 56 } |
| 57 |
| 58 SyncedDeviceTracker synced_device_tracker_; |
| 59 |
| 60 private: |
| 61 // Count of how many closed WriteTransactions notified of meaningful changes. |
| 62 int GetTotalTransactionsCount() { |
| 63 base::RunLoop run_loop; |
| 64 run_loop.RunUntilIdle(); |
| 65 return test_user_share_.transaction_observer()->transactions_observed(); |
| 66 } |
| 67 |
| 68 MessageLoop message_loop_; |
| 69 syncer::TestUserShare test_user_share_; |
| 70 int transaction_count_baseline_; |
| 71 }; |
| 72 |
| 73 namespace { |
| 74 |
| 75 // New client scenario: set device info when no previous info existed. |
| 76 TEST_F(SyncedDeviceTrackerTest, CreateNewDeviceInfo) { |
| 77 DeviceInfo read_device_info; |
| 78 ASSERT_FALSE(synced_device_tracker_.ReadLocalDeviceInfo(&read_device_info)); |
| 79 |
| 80 ResetObservedChangesCounter(); |
| 81 |
| 82 DeviceInfo write_device_info( |
| 83 "Name", "v1", sync_pb::SyncEnums_DeviceType_TYPE_LINUX); |
| 84 WriteLocalDeviceInfo(write_device_info); |
| 85 |
| 86 ASSERT_TRUE(synced_device_tracker_.ReadLocalDeviceInfo(&read_device_info)); |
| 87 EXPECT_TRUE(write_device_info.Equals(read_device_info)); |
| 88 |
| 89 EXPECT_EQ(1, GetObservedChangesCounter()); |
| 90 } |
| 91 |
| 92 // Restart scenario: update existing device info with identical data. |
| 93 TEST_F(SyncedDeviceTrackerTest, DontModifyExistingDeviceInfo) { |
| 94 // For writing. |
| 95 DeviceInfo device_info( |
| 96 "Name", "v1", sync_pb::SyncEnums_DeviceType_TYPE_LINUX); |
| 97 WriteLocalDeviceInfo(device_info); |
| 98 |
| 99 DeviceInfo old_device_info; // First read. |
| 100 ASSERT_TRUE(synced_device_tracker_.ReadLocalDeviceInfo(&old_device_info)); |
| 101 |
| 102 ResetObservedChangesCounter(); |
| 103 |
| 104 // Overwrite the device info with the same data as before. |
| 105 WriteLocalDeviceInfo(device_info); |
| 106 |
| 107 // Ensure that this didn't count as a change worth syncing. |
| 108 EXPECT_EQ(0, GetObservedChangesCounter()); |
| 109 |
| 110 DeviceInfo new_device_info; // Second read. |
| 111 ASSERT_TRUE(synced_device_tracker_.ReadLocalDeviceInfo(&new_device_info)); |
| 112 EXPECT_TRUE(old_device_info.Equals(new_device_info)); |
| 113 } |
| 114 |
| 115 // Upgrade scenario: update existing device info with new version. |
| 116 TEST_F(SyncedDeviceTrackerTest, UpdateExistingDeviceInfo) { |
| 117 // Write v1 device info. |
| 118 DeviceInfo device_info_v1( |
| 119 "Name", "v1", sync_pb::SyncEnums_DeviceType_TYPE_LINUX); |
| 120 WriteLocalDeviceInfo(device_info_v1); |
| 121 |
| 122 ResetObservedChangesCounter(); |
| 123 |
| 124 // Write upgraded device info. |
| 125 DeviceInfo device_info_v2( |
| 126 "Name", "v2", sync_pb::SyncEnums_DeviceType_TYPE_LINUX); |
| 127 WriteLocalDeviceInfo(device_info_v2); |
| 128 |
| 129 // Verify result. |
| 130 DeviceInfo result_device_info; |
| 131 ASSERT_TRUE(synced_device_tracker_.ReadLocalDeviceInfo(&result_device_info)); |
| 132 |
| 133 EXPECT_EQ(device_info_v2.chrome_version(), |
| 134 result_device_info.chrome_version()); |
| 135 |
| 136 // The update write should have sent a nudge. |
| 137 EXPECT_EQ(1, GetObservedChangesCounter()); |
| 138 } |
| 139 |
| 140 } // namespace |
| 141 |
| 142 } // namespace browser_sync |
OLD | NEW |