| OLD | NEW |
| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "google/cacheinvalidation/include/invalidation-client.h" | 8 #include "google/cacheinvalidation/include/invalidation-client.h" |
| 9 #include "google/cacheinvalidation/include/types.h" | 9 #include "google/cacheinvalidation/include/types.h" |
| 10 #include "google/cacheinvalidation/v2/types.pb.h" | 10 #include "google/cacheinvalidation/v2/types.pb.h" |
| 11 #include "jingle/notifier/listener/fake_push_client.h" | 11 #include "jingle/notifier/listener/fake_push_client.h" |
| 12 #include "sync/notifier/chrome_invalidation_client.h" | 12 #include "sync/notifier/chrome_invalidation_client.h" |
| 13 #include "sync/notifier/state_writer.h" | 13 #include "sync/notifier/mock_invalidation_state_tracker.h" |
| 14 #include "sync/syncable/model_type.h" | 14 #include "sync/syncable/model_type.h" |
| 15 #include "sync/syncable/model_type_payload_map.h" | 15 #include "sync/syncable/model_type_payload_map.h" |
| 16 #include "sync/util/weak_handle.h" | 16 #include "sync/util/weak_handle.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 namespace sync_notifier { | 20 namespace sync_notifier { |
| 21 | 21 |
| 22 using ::testing::_; | 22 using ::testing::_; |
| 23 using ::testing::Return; | 23 using ::testing::Return; |
| 24 using ::testing::StrictMock; | 24 using ::testing::StrictMock; |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 const char kClientId[] = "client_id"; | 28 const char kClientId[] = "client_id"; |
| 29 const char kClientInfo[] = "client_info"; | 29 const char kClientInfo[] = "client_info"; |
| 30 const char kState[] = "state"; | 30 const char kState[] = "state"; |
| 31 const char kNewState[] = "new_state"; |
| 31 | 32 |
| 32 class MockInvalidationClient : public invalidation::InvalidationClient { | 33 class MockInvalidationClient : public invalidation::InvalidationClient { |
| 33 public: | 34 public: |
| 34 MOCK_METHOD0(Start, void()); | 35 MOCK_METHOD0(Start, void()); |
| 35 MOCK_METHOD0(Stop, void()); | 36 MOCK_METHOD0(Stop, void()); |
| 36 MOCK_METHOD1(Register, void(const invalidation::ObjectId&)); | 37 MOCK_METHOD1(Register, void(const invalidation::ObjectId&)); |
| 37 MOCK_METHOD1(Register, void(const std::vector<invalidation::ObjectId>&)); | 38 MOCK_METHOD1(Register, void(const std::vector<invalidation::ObjectId>&)); |
| 38 MOCK_METHOD1(Unregister, void(const invalidation::ObjectId&)); | 39 MOCK_METHOD1(Unregister, void(const invalidation::ObjectId&)); |
| 39 MOCK_METHOD1(Unregister, void(const std::vector<invalidation::ObjectId>&)); | 40 MOCK_METHOD1(Unregister, void(const std::vector<invalidation::ObjectId>&)); |
| 40 MOCK_METHOD1(Acknowledge, void(const invalidation::AckHandle&)); | 41 MOCK_METHOD1(Acknowledge, void(const invalidation::AckHandle&)); |
| 41 }; | 42 }; |
| 42 | 43 |
| 43 class MockListener : public ChromeInvalidationClient::Listener { | 44 class MockListener : public ChromeInvalidationClient::Listener { |
| 44 public: | 45 public: |
| 45 MOCK_METHOD1(OnInvalidate, void(const syncable::ModelTypePayloadMap&)); | 46 MOCK_METHOD1(OnInvalidate, void(const syncable::ModelTypePayloadMap&)); |
| 46 MOCK_METHOD1(OnSessionStatusChanged, void(bool)); | 47 MOCK_METHOD1(OnSessionStatusChanged, void(bool)); |
| 47 }; | 48 }; |
| 48 | 49 |
| 49 class MockInvalidationStateTracker | |
| 50 : public InvalidationStateTracker, | |
| 51 public base::SupportsWeakPtr<MockInvalidationStateTracker> { | |
| 52 public: | |
| 53 MOCK_CONST_METHOD0(GetAllMaxVersions, InvalidationVersionMap()); | |
| 54 MOCK_METHOD2(SetMaxVersion, void(syncable::ModelType, int64)); | |
| 55 MOCK_CONST_METHOD0(GetInvalidationState, std::string()); | |
| 56 MOCK_METHOD1(SetInvalidationState, void(const std::string&)); | |
| 57 }; | |
| 58 | |
| 59 class MockStateWriter : public StateWriter { | |
| 60 public: | |
| 61 MOCK_METHOD1(WriteState, void(const std::string&)); | |
| 62 }; | |
| 63 | |
| 64 } // namespace | 50 } // namespace |
| 65 | 51 |
| 66 class ChromeInvalidationClientTest : public testing::Test { | 52 class ChromeInvalidationClientTest : public testing::Test { |
| 67 protected: | 53 protected: |
| 68 ChromeInvalidationClientTest() | 54 ChromeInvalidationClientTest() |
| 69 : client_( | 55 : client_( |
| 70 scoped_ptr<notifier::PushClient>( | 56 scoped_ptr<notifier::PushClient>( |
| 71 new notifier::FakePushClient())) {} | 57 new notifier::FakePushClient())) {} |
| 72 | 58 |
| 73 virtual void SetUp() { | 59 virtual void SetUp() { |
| 74 client_.Start(kClientId, kClientInfo, kState, | 60 client_.Start(kClientId, kClientInfo, kState, |
| 75 InvalidationVersionMap(), | 61 InvalidationVersionMap(), |
| 76 browser_sync::MakeWeakHandle( | 62 browser_sync::MakeWeakHandle( |
| 77 mock_invalidation_state_tracker_.AsWeakPtr()), | 63 mock_invalidation_state_tracker_.AsWeakPtr()), |
| 78 &mock_listener_, &mock_state_writer_); | 64 &mock_listener_); |
| 79 } | 65 } |
| 80 | 66 |
| 81 virtual void TearDown() { | 67 virtual void TearDown() { |
| 82 // client_.Stop() stops the invalidation scheduler, which deletes any | 68 // client_.Stop() stops the invalidation scheduler, which deletes any |
| 83 // pending tasks without running them. Some tasks "run and delete" another | 69 // pending tasks without running them. Some tasks "run and delete" another |
| 84 // task, so they must be run in order to avoid leaking the inner task. | 70 // task, so they must be run in order to avoid leaking the inner task. |
| 85 // client_.Stop() does not schedule any tasks, so it's both necessary and | 71 // client_.Stop() does not schedule any tasks, so it's both necessary and |
| 86 // sufficient to drain the task queue before calling it. | 72 // sufficient to drain the task queue before calling it. |
| 87 message_loop_.RunAllPending(); | 73 message_loop_.RunAllPending(); |
| 88 client_.Stop(); | 74 client_.Stop(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 void FireInvalidateAll() { | 108 void FireInvalidateAll() { |
| 123 invalidation::AckHandle ack_handle("fakedata"); | 109 invalidation::AckHandle ack_handle("fakedata"); |
| 124 EXPECT_CALL(mock_invalidation_client_, Acknowledge(ack_handle)); | 110 EXPECT_CALL(mock_invalidation_client_, Acknowledge(ack_handle)); |
| 125 client_.InvalidateAll(&mock_invalidation_client_, ack_handle); | 111 client_.InvalidateAll(&mock_invalidation_client_, ack_handle); |
| 126 } | 112 } |
| 127 | 113 |
| 128 MessageLoop message_loop_; | 114 MessageLoop message_loop_; |
| 129 StrictMock<MockListener> mock_listener_; | 115 StrictMock<MockListener> mock_listener_; |
| 130 StrictMock<MockInvalidationStateTracker> | 116 StrictMock<MockInvalidationStateTracker> |
| 131 mock_invalidation_state_tracker_; | 117 mock_invalidation_state_tracker_; |
| 132 StrictMock<MockStateWriter> mock_state_writer_; | |
| 133 StrictMock<MockInvalidationClient> mock_invalidation_client_; | 118 StrictMock<MockInvalidationClient> mock_invalidation_client_; |
| 134 ChromeInvalidationClient client_; | 119 ChromeInvalidationClient client_; |
| 135 }; | 120 }; |
| 136 | 121 |
| 137 namespace { | 122 namespace { |
| 138 | 123 |
| 139 syncable::ModelTypePayloadMap MakeMap(syncable::ModelType model_type, | 124 syncable::ModelTypePayloadMap MakeMap(syncable::ModelType model_type, |
| 140 const std::string& payload) { | 125 const std::string& payload) { |
| 141 syncable::ModelTypePayloadMap type_payloads; | 126 syncable::ModelTypePayloadMap type_payloads; |
| 142 type_payloads[model_type] = payload; | 127 type_payloads[model_type] = payload; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 167 } | 152 } |
| 168 | 153 |
| 169 TEST_F(ChromeInvalidationClientTest, InvalidateWithPayload) { | 154 TEST_F(ChromeInvalidationClientTest, InvalidateWithPayload) { |
| 170 EXPECT_CALL(mock_listener_, | 155 EXPECT_CALL(mock_listener_, |
| 171 OnInvalidate(MakeMap(syncable::PREFERENCES, "payload"))); | 156 OnInvalidate(MakeMap(syncable::PREFERENCES, "payload"))); |
| 172 EXPECT_CALL(mock_invalidation_state_tracker_, | 157 EXPECT_CALL(mock_invalidation_state_tracker_, |
| 173 SetMaxVersion(syncable::PREFERENCES, 1)); | 158 SetMaxVersion(syncable::PREFERENCES, 1)); |
| 174 FireInvalidate("PREFERENCE", 1, "payload"); | 159 FireInvalidate("PREFERENCE", 1, "payload"); |
| 175 } | 160 } |
| 176 | 161 |
| 162 TEST_F(ChromeInvalidationClientTest, WriteState) { |
| 163 EXPECT_CALL(mock_invalidation_state_tracker_, |
| 164 SetInvalidationState(kNewState)); |
| 165 client_.WriteState(kNewState); |
| 166 } |
| 167 |
| 177 TEST_F(ChromeInvalidationClientTest, InvalidateVersion) { | 168 TEST_F(ChromeInvalidationClientTest, InvalidateVersion) { |
| 178 using ::testing::Mock; | 169 using ::testing::Mock; |
| 179 | 170 |
| 180 EXPECT_CALL(mock_listener_, | 171 EXPECT_CALL(mock_listener_, |
| 181 OnInvalidate(MakeMap(syncable::APPS, ""))); | 172 OnInvalidate(MakeMap(syncable::APPS, ""))); |
| 182 EXPECT_CALL(mock_invalidation_state_tracker_, | 173 EXPECT_CALL(mock_invalidation_state_tracker_, |
| 183 SetMaxVersion(syncable::APPS, 1)); | 174 SetMaxVersion(syncable::APPS, 1)); |
| 184 | 175 |
| 185 // Should trigger. | 176 // Should trigger. |
| 186 FireInvalidate("APP", 1, NULL); | 177 FireInvalidate("APP", 1, NULL); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 // Registered types should be preserved across Stop/Start. | 262 // Registered types should be preserved across Stop/Start. |
| 272 TearDown(); | 263 TearDown(); |
| 273 SetUp(); | 264 SetUp(); |
| 274 EXPECT_CALL(mock_listener_, OnInvalidate(MakeMapFromSet(types, ""))); | 265 EXPECT_CALL(mock_listener_, OnInvalidate(MakeMapFromSet(types, ""))); |
| 275 FireInvalidateAll(); | 266 FireInvalidateAll(); |
| 276 } | 267 } |
| 277 | 268 |
| 278 // TODO(akalin): Flesh out unit tests. | 269 // TODO(akalin): Flesh out unit tests. |
| 279 | 270 |
| 280 } // namespace sync_notifier | 271 } // namespace sync_notifier |
| OLD | NEW |