Chromium Code Reviews| 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/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 ProfileSyncServiceTestHelper::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_.GetLocalDeviceInfo(&read_device_info)); | |
| 78 | |
| 79 ResetObservedChangesCounter(); | |
| 80 | |
| 81 DeviceInfo write_device_info; | |
| 82 write_device_info.chrome_version = "v1"; | |
| 83 write_device_info.client_name = "ASDF"; | |
| 84 write_device_info.device_type = sync_pb::SyncEnums_DeviceType_TYPE_LINUX; | |
| 85 WriteLocalDeviceInfo(write_device_info); | |
| 86 | |
| 87 ASSERT_TRUE(synced_device_tracker_.GetLocalDeviceInfo(&read_device_info)); | |
| 88 | |
| 89 EXPECT_EQ(write_device_info.chrome_version, read_device_info.chrome_version); | |
|
Nicolas Zea
2012/09/14 19:15:08
Maybe add a DeviceInfoEquals method, at least for
rlarocque
2012/09/15 01:36:37
Done.
| |
| 90 EXPECT_EQ(write_device_info.client_name, read_device_info.client_name); | |
| 91 EXPECT_EQ(write_device_info.device_type, read_device_info.device_type); | |
| 92 | |
| 93 EXPECT_EQ(1, GetObservedChangesCounter()); | |
| 94 } | |
| 95 | |
| 96 // Restart scenario: update existing device info with identical data. | |
| 97 TEST_F(SyncedDeviceTrackerTest, DontModifyExistingDeviceInfo) { | |
| 98 DeviceInfo device_info; // For writing. | |
| 99 | |
| 100 device_info.chrome_version = "v1"; | |
| 101 device_info.client_name = "HJKL"; | |
| 102 device_info.device_type = sync_pb::SyncEnums_DeviceType_TYPE_LINUX; | |
| 103 WriteLocalDeviceInfo(device_info); | |
| 104 | |
| 105 DeviceInfo old_device_info; // First read. | |
| 106 ASSERT_TRUE(synced_device_tracker_.GetLocalDeviceInfo(&old_device_info)); | |
| 107 | |
| 108 ResetObservedChangesCounter(); | |
| 109 | |
| 110 // Overwrite the device info with the same data as before. | |
| 111 WriteLocalDeviceInfo(device_info); | |
| 112 | |
| 113 // Ensure that this didn't count as a change worth syncing. | |
| 114 EXPECT_EQ(0, GetObservedChangesCounter()); | |
| 115 | |
| 116 DeviceInfo new_device_info; // Second read. | |
| 117 ASSERT_TRUE(synced_device_tracker_.GetLocalDeviceInfo(&new_device_info)); | |
| 118 | |
| 119 EXPECT_EQ(old_device_info.chrome_version, new_device_info.chrome_version); | |
| 120 EXPECT_EQ(old_device_info.client_name, new_device_info.client_name); | |
| 121 EXPECT_EQ(old_device_info.device_type, new_device_info.device_type); | |
| 122 } | |
| 123 | |
| 124 // Upgrade scenario: update existing device info with new version. | |
| 125 TEST_F(SyncedDeviceTrackerTest, UpdateExistingDeviceInfo) { | |
| 126 // Write v1 device info. | |
| 127 DeviceInfo device_info_v1; | |
| 128 device_info_v1.chrome_version = "v1"; | |
| 129 device_info_v1.client_name = "name"; | |
| 130 device_info_v1.device_type = sync_pb::SyncEnums_DeviceType_TYPE_LINUX; | |
| 131 WriteLocalDeviceInfo(device_info_v1); | |
| 132 | |
| 133 ResetObservedChangesCounter(); | |
| 134 | |
| 135 // Write upgraded device info. | |
| 136 DeviceInfo device_info_v2; | |
| 137 device_info_v2.chrome_version = "v2"; | |
| 138 device_info_v2.client_name = "name"; | |
| 139 device_info_v2.device_type = sync_pb::SyncEnums_DeviceType_TYPE_LINUX; | |
| 140 WriteLocalDeviceInfo(device_info_v2); | |
| 141 | |
| 142 // Verify result. | |
| 143 DeviceInfo result_device_info; | |
| 144 ASSERT_TRUE(synced_device_tracker_.GetLocalDeviceInfo(&result_device_info)); | |
| 145 | |
| 146 EXPECT_EQ(device_info_v2.chrome_version, result_device_info.chrome_version); | |
| 147 | |
| 148 // The update write should have sent a nudge. | |
| 149 EXPECT_EQ(1, GetObservedChangesCounter()); | |
| 150 } | |
| 151 | |
| 152 } // namespace | |
| 153 | |
| 154 } // namespace browser_sync | |
| OLD | NEW |