Index: chrome/browser/sync/glue/synced_device_tracker_unittest.cc |
diff --git a/chrome/browser/sync/glue/synced_device_tracker_unittest.cc b/chrome/browser/sync/glue/synced_device_tracker_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..12f0ffc451649de6537460a34c0eb95aa7c7aac0 |
--- /dev/null |
+++ b/chrome/browser/sync/glue/synced_device_tracker_unittest.cc |
@@ -0,0 +1,154 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <string> |
+ |
+#include "base/message_loop.h" |
+#include "base/run_loop.h" |
+#include "chrome/browser/sync/abstract_profile_sync_service_test.h" |
+#include "chrome/browser/sync/glue/synced_device_tracker.h" |
+#include "sync/internal_api/public/base/model_type.h" |
+#include "sync/internal_api/public/test/test_user_share.h" |
+#include "sync/protocol/sync.pb.h" |
+#include "sync/test/mock_transaction_observer.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace browser_sync { |
+ |
+class SyncedDeviceTrackerTest : public ::testing::Test { |
+ protected: |
+ SyncedDeviceTrackerTest() : transaction_count_baseline_(0) { } |
+ ~SyncedDeviceTrackerTest() { } |
+ |
+ void SetUp() { |
+ test_user_share_.SetUp(); |
+ ProfileSyncServiceTestHelper::CreateRoot(syncer::DEVICE_INFO, user_share()); |
+ |
+ // We don't actually touch the Profile, so we can get away with passing in a |
+ // NULL here. Constructing a TestingProfile can take over a 100ms, so this |
+ // optimization can be the difference between 'tests run with a noticeable |
+ // delay' and 'tests run instantaneously'. |
+ synced_device_tracker_.Start(NULL, user_share()); |
+ } |
+ |
+ void TearDown() { |
+ synced_device_tracker_.Stop(); |
+ test_user_share_.TearDown(); |
+ } |
+ |
+ syncer::UserShare* user_share() { |
+ return test_user_share_.user_share(); |
+ } |
+ |
+ // Expose the private method to our tests. |
+ void WriteLocalDeviceInfo(const DeviceInfo& info) { |
+ synced_device_tracker_.WriteLocalDeviceInfo(info); |
+ } |
+ |
+ void ResetObservedChangesCounter() { |
+ transaction_count_baseline_ = GetTotalTransactionsCount(); |
+ } |
+ |
+ int GetObservedChangesCounter() { |
+ return GetTotalTransactionsCount() - transaction_count_baseline_; |
+ } |
+ |
+ SyncedDeviceTracker synced_device_tracker_; |
+ |
+ private: |
+ // Count of how many closed WriteTransactions notified of meaningful changes. |
+ int GetTotalTransactionsCount() { |
+ base::RunLoop run_loop; |
rlarocque
2012/09/08 01:20:44
In case you were wondering, this is the reason why
|
+ run_loop.RunUntilIdle(); |
+ return test_user_share_.transaction_observer()->transactions_observed(); |
+ } |
+ |
+ MessageLoop message_loop_; |
+ syncer::TestUserShare test_user_share_; |
+ int transaction_count_baseline_; |
+}; |
+ |
+namespace { |
+ |
+// New client scenario: set device info when no previous info existed. |
+TEST_F(SyncedDeviceTrackerTest, CreateNewDeviceInfo) { |
+ DeviceInfo read_device_info; |
+ ASSERT_FALSE(synced_device_tracker_.GetLocalDeviceInfo(&read_device_info)); |
+ |
+ ResetObservedChangesCounter(); |
+ |
+ DeviceInfo write_device_info; |
+ write_device_info.chrome_version = "v1"; |
+ write_device_info.session_name = "session"; |
+ write_device_info.platform = "Linux"; |
+ WriteLocalDeviceInfo(write_device_info); |
+ |
+ ASSERT_TRUE(synced_device_tracker_.GetLocalDeviceInfo(&read_device_info)); |
+ |
+ EXPECT_EQ(write_device_info.chrome_version, read_device_info.chrome_version); |
+ EXPECT_EQ(write_device_info.session_name, read_device_info.session_name); |
+ EXPECT_EQ(write_device_info.platform, read_device_info.platform); |
+ |
+ EXPECT_EQ(1, GetObservedChangesCounter()); |
Nicolas Zea
2012/09/13 00:45:55
this seems kindda "white box", does the number of
rlarocque
2012/09/14 01:03:07
Actually, I believe the observer will only see wri
|
+} |
+ |
+// Restart scenario: update existing device info with identical data. |
+TEST_F(SyncedDeviceTrackerTest, DontModifyExistingDeviceInfo) { |
+ DeviceInfo device_info; // For writing. |
+ |
+ device_info.chrome_version = "v1"; |
+ device_info.session_name = "name"; |
+ device_info.platform = "CrOS"; |
+ WriteLocalDeviceInfo(device_info); |
+ |
+ DeviceInfo old_device_info; // First read. |
+ ASSERT_TRUE(synced_device_tracker_.GetLocalDeviceInfo(&old_device_info)); |
+ |
+ ResetObservedChangesCounter(); |
+ |
+ // Overwrite the device info with the same data as before. |
+ WriteLocalDeviceInfo(device_info); |
+ |
+ // Ensure that this didn't count as a change worth syncing. |
+ EXPECT_EQ(0, GetObservedChangesCounter()); |
+ |
+ DeviceInfo new_device_info; // Second read. |
+ ASSERT_TRUE(synced_device_tracker_.GetLocalDeviceInfo(&new_device_info)); |
+ |
+ EXPECT_EQ(old_device_info.chrome_version, new_device_info.chrome_version); |
+ EXPECT_EQ(old_device_info.session_name, new_device_info.session_name); |
+ EXPECT_EQ(old_device_info.platform, new_device_info.platform); |
+} |
+ |
+// Upgrade scenario: update existing device info with new version. |
+TEST_F(SyncedDeviceTrackerTest, UpdateExistingDeviceInfo) { |
+ // Write v1 device info. |
+ DeviceInfo device_info_v1; |
+ device_info_v1.chrome_version = "v1"; |
+ device_info_v1.session_name = "name"; |
+ device_info_v1.platform = "CrOS"; |
+ WriteLocalDeviceInfo(device_info_v1); |
+ |
+ ResetObservedChangesCounter(); |
+ |
+ // Write upgraded device info. |
+ DeviceInfo device_info_v2; |
+ device_info_v2.chrome_version = "v2"; |
+ device_info_v2.session_name = "name"; |
+ device_info_v2.platform = "CrOS"; |
+ WriteLocalDeviceInfo(device_info_v2); |
+ |
+ // Verify result. |
+ DeviceInfo result_device_info; |
+ ASSERT_TRUE(synced_device_tracker_.GetLocalDeviceInfo(&result_device_info)); |
+ |
+ EXPECT_EQ(device_info_v2.chrome_version, result_device_info.chrome_version); |
+ |
+ // The update write should have sent a nudge. |
+ EXPECT_EQ(1, GetObservedChangesCounter()); |
+} |
+ |
+} // namespace |
+ |
+} // namespace browser_sync |