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