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