Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(798)

Unified Diff: sync/notifier/sync_notifier_helper_unittest.cc

Issue 10702074: Refactor sync-specific parts out of SyncNotifier/SyncNotifierObserver (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Now with tests Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sync/notifier/sync_notifier_helper_unittest.cc
diff --git a/sync/notifier/sync_notifier_helper_unittest.cc b/sync/notifier/sync_notifier_helper_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..63da8fb921088c7b593c56062da540e3251045b2
--- /dev/null
+++ b/sync/notifier/sync_notifier_helper_unittest.cc
@@ -0,0 +1,167 @@
+// 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 "sync/notifier/sync_notifier_helper.h"
+#include "sync/notifier/mock_sync_notifier_observer.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using testing::StrictMock;
+
+namespace syncer {
+
+namespace {
+
+const int kTestSourceId = 4; // Specified by Tango for testing.
+
+} // namespace
+
+class SyncNotifierHelperTest : public testing::Test {
+ protected:
+ SyncNotifierHelperTest() : kObjectId1(kTestSourceId, "a"),
+ kObjectId2(kTestSourceId, "b"),
+ kObjectId3(kTestSourceId, "c") {
+ }
+
+ invalidation::ObjectId kObjectId1;
+ invalidation::ObjectId kObjectId2;
+ invalidation::ObjectId kObjectId3;
+};
+
+TEST_F(SyncNotifierHelperTest, Basic) {
+ SyncNotifierHelper helper;
+ StrictMock<MockSyncNotifierObserver> observer;
+ ObjectIdSet ids;
+ ids.insert(kObjectId1);
+ ids.insert(kObjectId2);
+ helper.UpdateRegisteredIds(&observer, ids);
+
+ ObjectIdPayloadMap dispatched_payloads;
+ dispatched_payloads[kObjectId1] = "1";
+ dispatched_payloads[kObjectId2] = "2";
+ dispatched_payloads[kObjectId3] = "3";
+
+ // A object ID with no registration should be ignored.
+ ObjectIdPayloadMap expected_payload1;
+ expected_payload1[kObjectId1] = "1";
+ expected_payload1[kObjectId2] = "2";
+ EXPECT_CALL(observer, OnIncomingNotification(expected_payload1,
+ syncer::REMOTE_NOTIFICATION));
+ helper.DispatchInvalidationsToHandlers(dispatched_payloads,
+ syncer::REMOTE_NOTIFICATION);
+
+ // Removed object IDs should not be notified, newly-added ones should.
+ ids.erase(kObjectId1);
+ ids.insert(kObjectId3);
+ helper.UpdateRegisteredIds(&observer, ids);
+
+ ObjectIdPayloadMap expected_payload2;
+ expected_payload2[kObjectId2] = "2";
+ expected_payload2[kObjectId3] = "3";
+ EXPECT_CALL(observer, OnIncomingNotification(expected_payload2,
+ syncer::REMOTE_NOTIFICATION));
+ helper.DispatchInvalidationsToHandlers(dispatched_payloads,
+ syncer::REMOTE_NOTIFICATION);
+}
+
+// Tests that we correctly bucket and dispatch invalidations on multiple objects
+// to the corresponding handlers.
+TEST_F(SyncNotifierHelperTest, MultipleHandlers) {
+ SyncNotifierHelper helper;
+ StrictMock<MockSyncNotifierObserver> observer;
+ ObjectIdSet ids;
+ ids.insert(kObjectId1);
+ ids.insert(kObjectId2);
+ helper.UpdateRegisteredIds(&observer, ids);
+ StrictMock<MockSyncNotifierObserver> observer2;
+ ObjectIdSet ids2;
+ ids2.insert(kObjectId3);
+ helper.UpdateRegisteredIds(&observer2, ids2);
+
+ ObjectIdPayloadMap expected_payload1;
+ expected_payload1[kObjectId1] = "1";
+ expected_payload1[kObjectId2] = "2";
+ EXPECT_CALL(observer, OnIncomingNotification(expected_payload1,
+ syncer::REMOTE_NOTIFICATION));
+ ObjectIdPayloadMap expected_payload2;
+ expected_payload2[kObjectId3] = "3";
+ EXPECT_CALL(observer2, OnIncomingNotification(expected_payload2,
+ syncer::REMOTE_NOTIFICATION));
+
+ ObjectIdPayloadMap dispatched_payloads;
+ dispatched_payloads[kObjectId1] = "1";
+ dispatched_payloads[kObjectId2] = "2";
+ dispatched_payloads[kObjectId3] = "3";
+ helper.DispatchInvalidationsToHandlers(dispatched_payloads,
+ syncer::REMOTE_NOTIFICATION);
+
+ // Also verify that the callbacks for OnNotificationsEnabled/Disabled work.
+ EXPECT_CALL(observer, OnNotificationsEnabled());
+ EXPECT_CALL(observer2, OnNotificationsEnabled());
+ EXPECT_CALL(observer,
+ OnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR));
+ EXPECT_CALL(observer2,
+ OnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR));
+ helper.OnNotificationsEnabled();
+ helper.OnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR);
+}
+
+TEST_F(SyncNotifierHelperTest, MultipleRegistration) {
+ SyncNotifierHelper helper;
+ StrictMock<MockSyncNotifierObserver> observer;
+ ObjectIdSet ids;
+ ids.insert(kObjectId1);
+ ids.insert(kObjectId2);
+ helper.UpdateRegisteredIds(&observer, ids);
+
+ StrictMock<MockSyncNotifierObserver> observer2;
+ helper.UpdateRegisteredIds(&observer2, ids);
+
+ // Only the first observer should receive notifications.
+ ObjectIdPayloadMap expected_payload;
+ expected_payload[kObjectId1] = "1";
+ expected_payload[kObjectId2] = "2";
+ EXPECT_CALL(observer, OnIncomingNotification(expected_payload,
+ syncer::REMOTE_NOTIFICATION));
+
+ ObjectIdPayloadMap dispatched_payloads;
+ dispatched_payloads[kObjectId1] = "1";
+ dispatched_payloads[kObjectId2] = "2";
+ helper.DispatchInvalidationsToHandlers(dispatched_payloads,
+ syncer::REMOTE_NOTIFICATION);
+}
+
+TEST_F(SyncNotifierHelperTest, EmptySetUnregisters) {
+ SyncNotifierHelper helper;
+ StrictMock<MockSyncNotifierObserver> observer;
+ ObjectIdSet ids;
+ ids.insert(kObjectId1);
+ ids.insert(kObjectId2);
+ helper.UpdateRegisteredIds(&observer, ids);
+ // Control observer.
+ StrictMock<MockSyncNotifierObserver> observer2;
+ ObjectIdSet ids2;
+ ids2.insert(kObjectId3);
+ helper.UpdateRegisteredIds(&observer2, ids2);
+ // Unregister the first observer. It should not receive any further callbacks.
+ helper.UpdateRegisteredIds(&observer, ObjectIdSet());
+
+ ObjectIdPayloadMap expected_payload2;
+ expected_payload2[kObjectId3] = "3";
+ EXPECT_CALL(observer2, OnIncomingNotification(expected_payload2,
+ syncer::REMOTE_NOTIFICATION));
+ EXPECT_CALL(observer2, OnNotificationsEnabled());
+ EXPECT_CALL(observer2,
+ OnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR));
+
+ ObjectIdPayloadMap dispatched_payloads;
+ dispatched_payloads[kObjectId1] = "1";
+ dispatched_payloads[kObjectId2] = "2";
+ dispatched_payloads[kObjectId3] = "3";
+ helper.DispatchInvalidationsToHandlers(dispatched_payloads,
+ syncer::REMOTE_NOTIFICATION);
+ helper.OnNotificationsEnabled();
+ helper.OnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR);
+}
+
+} // namespace syncer

Powered by Google App Engine
This is Rietveld 408576698