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

Side by Side Diff: sync/notifier/chrome_invalidation_client_unittest.cc

Issue 10916131: [Invalidations] Add GetInvalidatorState() to Invalidator{,Frontend} (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix compile error Created 8 years, 3 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <cstddef> 5 #include <cstddef>
6 #include <set> 6 #include <set>
7 #include <string> 7 #include <string>
8 8
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 private: 124 private:
125 bool started_; 125 bool started_;
126 ObjectIdSet registered_ids_; 126 ObjectIdSet registered_ids_;
127 AckHandleSet acked_handles_; 127 AckHandleSet acked_handles_;
128 }; 128 };
129 129
130 // Fake listener tkat keeps track of invalidation counts, payloads, 130 // Fake listener tkat keeps track of invalidation counts, payloads,
131 // and state. 131 // and state.
132 class FakeListener : public ChromeInvalidationClient::Listener { 132 class FakeListener : public ChromeInvalidationClient::Listener {
133 public: 133 public:
134 FakeListener() : reason_(TRANSIENT_NOTIFICATION_ERROR) {} 134 FakeListener() : state_(TRANSIENT_INVALIDATION_ERROR) {}
135 virtual ~FakeListener() {} 135 virtual ~FakeListener() {}
136 136
137 int GetInvalidationCount(const ObjectId& id) const { 137 int GetInvalidationCount(const ObjectId& id) const {
138 ObjectIdCountMap::const_iterator it = invalidation_counts_.find(id); 138 ObjectIdCountMap::const_iterator it = invalidation_counts_.find(id);
139 return (it == invalidation_counts_.end()) ? 0 : it->second; 139 return (it == invalidation_counts_.end()) ? 0 : it->second;
140 } 140 }
141 141
142 std::string GetPayload(const ObjectId& id) const { 142 std::string GetPayload(const ObjectId& id) const {
143 ObjectIdStateMap::const_iterator it = states_.find(id); 143 ObjectIdStateMap::const_iterator it = states_.find(id);
144 return (it == states_.end()) ? "" : it->second.payload; 144 return (it == states_.end()) ? "" : it->second.payload;
145 } 145 }
146 146
147 // NO_NOTIFICATION_ERROR is the enabled state. 147 InvalidatorState GetInvalidatorState() const {
148 NotificationsDisabledReason GetNotificationsDisabledReason() const { 148 return state_;
149 return reason_;
150 } 149 }
151 150
152 // ChromeInvalidationClient::Listener implementation. 151 // ChromeInvalidationClient::Listener implementation.
153 152
154 virtual void OnInvalidate(const ObjectIdStateMap& id_state_map) OVERRIDE { 153 virtual void OnInvalidate(const ObjectIdStateMap& id_state_map) OVERRIDE {
155 for (ObjectIdStateMap::const_iterator it = id_state_map.begin(); 154 for (ObjectIdStateMap::const_iterator it = id_state_map.begin();
156 it != id_state_map.end(); ++it) { 155 it != id_state_map.end(); ++it) {
157 ++invalidation_counts_[it->first]; 156 ++invalidation_counts_[it->first];
158 states_[it->first] = it->second; 157 states_[it->first] = it->second;
159 } 158 }
160 } 159 }
161 160
162 virtual void OnNotificationsEnabled() { 161 virtual void OnInvalidatorStateChange(InvalidatorState state) {
163 reason_ = NO_NOTIFICATION_ERROR; 162 state_ = state;
164 }
165
166 virtual void OnNotificationsDisabled(NotificationsDisabledReason reason) {
167 reason_ = reason;
168 } 163 }
169 164
170 private: 165 private:
171 typedef std::map<ObjectId, int, ObjectIdLessThan> ObjectIdCountMap; 166 typedef std::map<ObjectId, int, ObjectIdLessThan> ObjectIdCountMap;
172 ObjectIdCountMap invalidation_counts_; 167 ObjectIdCountMap invalidation_counts_;
173 ObjectIdStateMap states_; 168 ObjectIdStateMap states_;
174 NotificationsDisabledReason reason_; 169 InvalidatorState state_;
175 }; 170 };
176 171
177 invalidation::InvalidationClient* CreateFakeInvalidationClient( 172 invalidation::InvalidationClient* CreateFakeInvalidationClient(
178 FakeInvalidationClient** fake_invalidation_client, 173 FakeInvalidationClient** fake_invalidation_client,
179 invalidation::SystemResources* resources, 174 invalidation::SystemResources* resources,
180 int client_type, 175 int client_type,
181 const invalidation::string& client_name, 176 const invalidation::string& client_name,
182 const invalidation::string& application_name, 177 const invalidation::string& application_name,
183 invalidation::InvalidationListener* listener) { 178 invalidation::InvalidationListener* listener) {
184 *fake_invalidation_client = new FakeInvalidationClient(); 179 *fake_invalidation_client = new FakeInvalidationClient();
(...skipping 30 matching lines...) Expand all
215 } 210 }
216 211
217 int GetInvalidationCount(const ObjectId& id) const { 212 int GetInvalidationCount(const ObjectId& id) const {
218 return fake_listener_.GetInvalidationCount(id); 213 return fake_listener_.GetInvalidationCount(id);
219 } 214 }
220 215
221 std::string GetPayload(const ObjectId& id) const { 216 std::string GetPayload(const ObjectId& id) const {
222 return fake_listener_.GetPayload(id); 217 return fake_listener_.GetPayload(id);
223 } 218 }
224 219
225 NotificationsDisabledReason GetNotificationsDisabledReason() const { 220 InvalidatorState GetInvalidatorState() const {
226 return fake_listener_.GetNotificationsDisabledReason(); 221 return fake_listener_.GetInvalidatorState();
227 } 222 }
228 223
229 int64 GetMaxVersion(const ObjectId& id) const { 224 int64 GetMaxVersion(const ObjectId& id) const {
230 return fake_tracker_.GetMaxVersion(id); 225 return fake_tracker_.GetMaxVersion(id);
231 } 226 }
232 227
233 std::string GetInvalidationState() const { 228 std::string GetInvalidationState() const {
234 return fake_tracker_.GetInvalidationState(); 229 return fake_tracker_.GetInvalidationState();
235 } 230 }
236 231
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after
640 EXPECT_TRUE(GetRegisteredIds().empty()); 635 EXPECT_TRUE(GetRegisteredIds().empty());
641 636
642 client_.Ready(fake_invalidation_client_); 637 client_.Ready(fake_invalidation_client_);
643 638
644 EXPECT_EQ(registered_ids_, GetRegisteredIds()); 639 EXPECT_EQ(registered_ids_, GetRegisteredIds());
645 } 640 }
646 641
647 // Without readying the client, disable notifications, then enable 642 // Without readying the client, disable notifications, then enable
648 // them. The listener should still think notifications are disabled. 643 // them. The listener should still think notifications are disabled.
649 TEST_F(ChromeInvalidationClientTest, EnableNotificationsNotReady) { 644 TEST_F(ChromeInvalidationClientTest, EnableNotificationsNotReady) {
650 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, 645 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR,
651 GetNotificationsDisabledReason()); 646 GetInvalidatorState());
652 647
653 DisableNotifications( 648 DisableNotifications(
654 notifier::TRANSIENT_NOTIFICATION_ERROR); 649 notifier::TRANSIENT_NOTIFICATION_ERROR);
655 650
656 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, 651 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, GetInvalidatorState());
657 GetNotificationsDisabledReason());
658 652
659 DisableNotifications( 653 DisableNotifications(notifier::NOTIFICATION_CREDENTIALS_REJECTED);
660 notifier::NOTIFICATION_CREDENTIALS_REJECTED);
661 654
662 EXPECT_EQ(NOTIFICATION_CREDENTIALS_REJECTED, 655 EXPECT_EQ(INVALIDATION_CREDENTIALS_REJECTED, GetInvalidatorState());
663 GetNotificationsDisabledReason());
664 656
665 EnableNotifications(); 657 EnableNotifications();
666 658
667 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, 659 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, GetInvalidatorState());
668 GetNotificationsDisabledReason());
669 } 660 }
670 661
671 // Enable notifications then Ready the invalidation client. The 662 // Enable notifications then Ready the invalidation client. The
672 // listener should then be ready. 663 // listener should then be ready.
673 TEST_F(ChromeInvalidationClientTest, EnableNotificationsThenReady) { 664 TEST_F(ChromeInvalidationClientTest, EnableNotificationsThenReady) {
674 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, GetNotificationsDisabledReason()); 665 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, GetInvalidatorState());
675 666
676 EnableNotifications(); 667 EnableNotifications();
677 668
678 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, GetNotificationsDisabledReason()); 669 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, GetInvalidatorState());
679 670
680 client_.Ready(fake_invalidation_client_); 671 client_.Ready(fake_invalidation_client_);
681 672
682 EXPECT_EQ(NO_NOTIFICATION_ERROR, GetNotificationsDisabledReason()); 673 EXPECT_EQ(INVALIDATIONS_ENABLED, GetInvalidatorState());
683 } 674 }
684 675
685 // Ready the invalidation client then enable notifications. The 676 // Ready the invalidation client then enable notifications. The
686 // listener should then be ready. 677 // listener should then be ready.
687 TEST_F(ChromeInvalidationClientTest, ReadyThenEnableNotifications) { 678 TEST_F(ChromeInvalidationClientTest, ReadyThenEnableNotifications) {
688 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, GetNotificationsDisabledReason()); 679 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, GetInvalidatorState());
689 680
690 client_.Ready(fake_invalidation_client_); 681 client_.Ready(fake_invalidation_client_);
691 682
692 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, GetNotificationsDisabledReason()); 683 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, GetInvalidatorState());
693 684
694 EnableNotifications(); 685 EnableNotifications();
695 686
696 EXPECT_EQ(NO_NOTIFICATION_ERROR, GetNotificationsDisabledReason()); 687 EXPECT_EQ(INVALIDATIONS_ENABLED, GetInvalidatorState());
697 } 688 }
698 689
699 // Enable notifications and ready the client. Then disable 690 // Enable notifications and ready the client. Then disable
700 // notifications with an auth error and re-enable notifications. The 691 // notifications with an auth error and re-enable notifications. The
701 // listener should go into an auth error mode and then back out. 692 // listener should go into an auth error mode and then back out.
702 TEST_F(ChromeInvalidationClientTest, PushClientAuthError) { 693 TEST_F(ChromeInvalidationClientTest, PushClientAuthError) {
703 EnableNotifications(); 694 EnableNotifications();
704 client_.Ready(fake_invalidation_client_); 695 client_.Ready(fake_invalidation_client_);
705 696
706 EXPECT_EQ(NO_NOTIFICATION_ERROR, GetNotificationsDisabledReason()); 697 EXPECT_EQ(INVALIDATIONS_ENABLED, GetInvalidatorState());
707 698
708 DisableNotifications( 699 DisableNotifications(
709 notifier::NOTIFICATION_CREDENTIALS_REJECTED); 700 notifier::NOTIFICATION_CREDENTIALS_REJECTED);
710 701
711 EXPECT_EQ(NOTIFICATION_CREDENTIALS_REJECTED, 702 EXPECT_EQ(INVALIDATION_CREDENTIALS_REJECTED, GetInvalidatorState());
712 GetNotificationsDisabledReason());
713 703
714 EnableNotifications(); 704 EnableNotifications();
715 705
716 EXPECT_EQ(NO_NOTIFICATION_ERROR, GetNotificationsDisabledReason()); 706 EXPECT_EQ(INVALIDATIONS_ENABLED, GetInvalidatorState());
717 } 707 }
718 708
719 // Enable notifications and ready the client. Then simulate an auth 709 // Enable notifications and ready the client. Then simulate an auth
720 // error from the invalidation client. Simulate some notification 710 // error from the invalidation client. Simulate some notification
721 // events, then re-ready the client. The listener should go into an 711 // events, then re-ready the client. The listener should go into an
722 // auth error mode and come out of it only after the client is ready. 712 // auth error mode and come out of it only after the client is ready.
723 TEST_F(ChromeInvalidationClientTest, InvalidationClientAuthError) { 713 TEST_F(ChromeInvalidationClientTest, InvalidationClientAuthError) {
724 EnableNotifications(); 714 EnableNotifications();
725 client_.Ready(fake_invalidation_client_); 715 client_.Ready(fake_invalidation_client_);
726 716
727 EXPECT_EQ(NO_NOTIFICATION_ERROR, GetNotificationsDisabledReason()); 717 EXPECT_EQ(INVALIDATIONS_ENABLED, GetInvalidatorState());
728 718
729 client_.InformError( 719 client_.InformError(
730 fake_invalidation_client_, 720 fake_invalidation_client_,
731 invalidation::ErrorInfo( 721 invalidation::ErrorInfo(
732 invalidation::ErrorReason::AUTH_FAILURE, 722 invalidation::ErrorReason::AUTH_FAILURE,
733 false /* is_transient */, 723 false /* is_transient */,
734 "auth error", 724 "auth error",
735 invalidation::ErrorContext())); 725 invalidation::ErrorContext()));
736 726
737 EXPECT_EQ(NOTIFICATION_CREDENTIALS_REJECTED, 727 EXPECT_EQ(INVALIDATION_CREDENTIALS_REJECTED, GetInvalidatorState());
738 GetNotificationsDisabledReason());
739 728
740 DisableNotifications( 729 DisableNotifications(notifier::TRANSIENT_NOTIFICATION_ERROR);
741 notifier::TRANSIENT_NOTIFICATION_ERROR);
742 730
743 EXPECT_EQ(NOTIFICATION_CREDENTIALS_REJECTED, 731 EXPECT_EQ(INVALIDATION_CREDENTIALS_REJECTED, GetInvalidatorState());
744 GetNotificationsDisabledReason());
745 732
746 DisableNotifications( 733 DisableNotifications(notifier::TRANSIENT_NOTIFICATION_ERROR);
747 notifier::TRANSIENT_NOTIFICATION_ERROR);
748 734
749 EXPECT_EQ(NOTIFICATION_CREDENTIALS_REJECTED, 735 EXPECT_EQ(INVALIDATION_CREDENTIALS_REJECTED, GetInvalidatorState());
750 GetNotificationsDisabledReason());
751 736
752 EnableNotifications(); 737 EnableNotifications();
753 738
754 EXPECT_EQ(NOTIFICATION_CREDENTIALS_REJECTED, 739 EXPECT_EQ(INVALIDATION_CREDENTIALS_REJECTED, GetInvalidatorState());
755 GetNotificationsDisabledReason());
756 740
757 client_.Ready(fake_invalidation_client_); 741 client_.Ready(fake_invalidation_client_);
758 742
759 EXPECT_EQ(NO_NOTIFICATION_ERROR, GetNotificationsDisabledReason()); 743 EXPECT_EQ(INVALIDATIONS_ENABLED, GetInvalidatorState());
760 } 744 }
761 745
762 } // namespace 746 } // namespace
763 747
764 } // namespace syncer 748 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698