Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/policy/cloud/remote_commands_invalidator_base.h" | |
| 6 | |
| 7 #include "base/memory/scoped_ptr.h" | |
|
bartfab (slow)
2015/05/15 15:37:12
Nit: Not used.
binjin
2015/05/15 15:46:46
Done.
| |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "base/run_loop.h" | |
| 10 #include "base/sequenced_task_runner.h" | |
| 11 #include "chrome/browser/invalidation/fake_invalidation_service.h" | |
| 12 #include "components/invalidation/invalidation.h" | |
| 13 #include "components/invalidation/invalidation_util.h" | |
| 14 #include "components/invalidation/invalidator_registrar.h" | |
| 15 #include "components/invalidation/invalidator_state.h" | |
| 16 #include "components/invalidation/mock_ack_handler.h" | |
| 17 #include "testing/gmock/include/gmock/gmock.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 namespace em = enterprise_management; | |
| 21 | |
| 22 using ::testing::InSequence; | |
|
bartfab (slow)
2015/05/15 15:37:13
All your tests that use |InSequence| do not actual
binjin
2015/05/15 15:46:46
Done.
| |
| 23 using ::testing::Mock; | |
| 24 using ::testing::StrictMock; | |
| 25 | |
| 26 namespace policy { | |
| 27 | |
| 28 class MockRemoteCommandInvalidator : public RemoteCommandsInvalidatorBase { | |
| 29 public: | |
| 30 MockRemoteCommandInvalidator( | |
| 31 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | |
| 32 : RemoteCommandsInvalidatorBase(task_runner) {} | |
| 33 | |
| 34 MOCK_METHOD0(OnInitialize, void()); | |
| 35 MOCK_METHOD0(OnShutdown, void()); | |
| 36 MOCK_METHOD0(OnStart, void()); | |
| 37 MOCK_METHOD0(OnStop, void()); | |
| 38 MOCK_METHOD0(DoRemoteCommandsFetch, void()); | |
| 39 | |
| 40 void SetInvalidationObjectID(const invalidation::ObjectId& object_id) { | |
| 41 em::PolicyData policy_data; | |
| 42 policy_data.set_command_invalidation_source(object_id.source()); | |
| 43 policy_data.set_command_invalidation_name(object_id.name()); | |
| 44 ReloadPolicyData(&policy_data); | |
| 45 } | |
| 46 | |
| 47 void ClearInvalidationObjectID() { | |
| 48 em::PolicyData policy_data; | |
|
bartfab (slow)
2015/05/15 15:37:12
Nit: const.
binjin
2015/05/15 15:46:46
Done.
| |
| 49 ReloadPolicyData(&policy_data); | |
| 50 } | |
| 51 | |
| 52 private: | |
| 53 DISALLOW_COPY_AND_ASSIGN(MockRemoteCommandInvalidator); | |
| 54 }; | |
| 55 | |
| 56 class RemoteCommandsInvalidatorBaseTest : public testing::Test { | |
| 57 public: | |
| 58 RemoteCommandsInvalidatorBaseTest() | |
| 59 : kTestingObjectId1(123456, "abcdef"), | |
| 60 kTestingObjectId2(654321, "defabc"), | |
| 61 invalidator_(loop_.message_loop_proxy()) { | |
| 62 } | |
| 63 | |
| 64 void EnableInvalidationService() { | |
| 65 invalidation_service_.SetInvalidatorState(syncer::INVALIDATIONS_ENABLED); | |
| 66 } | |
| 67 | |
| 68 void DisableInvalidationService() { | |
| 69 invalidation_service_.SetInvalidatorState( | |
| 70 syncer::TRANSIENT_INVALIDATION_ERROR); | |
| 71 } | |
| 72 | |
| 73 syncer::Invalidation FireInvalidation( | |
| 74 const invalidation::ObjectId& object_id) { | |
| 75 const syncer::Invalidation invalidation = | |
| 76 syncer::Invalidation::InitUnknownVersion(object_id); | |
| 77 invalidation_service_.EmitInvalidationForTest(invalidation); | |
| 78 return invalidation; | |
| 79 } | |
| 80 | |
| 81 bool IsInvalidationSent(const syncer::Invalidation& invalidation) { | |
| 82 return !invalidation_service_.GetMockAckHandler()->IsUnsent(invalidation); | |
| 83 } | |
| 84 | |
| 85 bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation) { | |
| 86 return invalidation_service_.GetMockAckHandler()->IsAcknowledged( | |
| 87 invalidation); | |
| 88 } | |
| 89 | |
| 90 bool IsInvalidatorRegistered() { | |
| 91 return !invalidation_service_.invalidator_registrar() | |
| 92 .GetRegisteredIds(&invalidator_) | |
| 93 .empty(); | |
| 94 } | |
| 95 | |
| 96 void VerifyExpectations() { Mock::VerifyAndClearExpectations(&invalidator_); } | |
|
bartfab (slow)
2015/05/15 15:37:12
Nit: Per style guide, always put a line break afte
binjin
2015/05/15 15:46:46
Done.
| |
| 97 | |
| 98 protected: | |
| 99 // Initialize and start the invalidator. | |
| 100 void InitializeAndStart() { | |
| 101 EXPECT_CALL(invalidator_, OnInitialize()).Times(1); | |
| 102 invalidator_.Initialize(&invalidation_service_); | |
| 103 VerifyExpectations(); | |
| 104 | |
| 105 EXPECT_CALL(invalidator_, OnStart()).Times(1); | |
| 106 invalidator_.Start(); | |
| 107 | |
| 108 VerifyExpectations(); | |
| 109 } | |
| 110 | |
| 111 // Stop and shutdown the invalidator. | |
| 112 void StopAndShutdown() { | |
| 113 EXPECT_CALL(invalidator_, OnStop()).Times(1); | |
| 114 EXPECT_CALL(invalidator_, OnShutdown()).Times(1); | |
| 115 invalidator_.Shutdown(); | |
| 116 | |
| 117 VerifyExpectations(); | |
| 118 } | |
| 119 | |
| 120 // Fire an invalidation to verify that invalidation is not working. | |
| 121 void VerifyInvalidationDisabled(const invalidation::ObjectId& object_id) { | |
| 122 const syncer::Invalidation invalidation = FireInvalidation(object_id); | |
| 123 | |
| 124 base::RunLoop().RunUntilIdle(); | |
| 125 EXPECT_FALSE(IsInvalidationSent(invalidation)); | |
| 126 } | |
| 127 | |
| 128 // Fire an invalidation to verify that invalidation works. | |
| 129 void VerifyInvalidationEnabled(const invalidation::ObjectId& object_id) { | |
| 130 EXPECT_TRUE(invalidator_.invalidations_enabled()); | |
| 131 | |
| 132 EXPECT_CALL(invalidator_, DoRemoteCommandsFetch()).Times(1); | |
| 133 const syncer::Invalidation invalidation = FireInvalidation(object_id); | |
| 134 | |
| 135 base::RunLoop().RunUntilIdle(); | |
| 136 EXPECT_TRUE(IsInvalidationSent(invalidation)); | |
| 137 EXPECT_TRUE(IsInvalidationAcknowledged(invalidation)); | |
| 138 VerifyExpectations(); | |
| 139 } | |
| 140 | |
| 141 const invalidation::ObjectId kTestingObjectId1; | |
| 142 const invalidation::ObjectId kTestingObjectId2; | |
| 143 | |
| 144 base::MessageLoop loop_; | |
| 145 | |
| 146 invalidation::FakeInvalidationService invalidation_service_; | |
| 147 StrictMock<MockRemoteCommandInvalidator> invalidator_; | |
| 148 | |
| 149 private: | |
| 150 DISALLOW_COPY_AND_ASSIGN(RemoteCommandsInvalidatorBaseTest); | |
| 151 }; | |
| 152 | |
| 153 // Verifies that only the fired invalidations will be received. | |
| 154 TEST_F(RemoteCommandsInvalidatorBaseTest, FiredInvalidation) { | |
| 155 InitializeAndStart(); | |
| 156 | |
| 157 // Invalidator won't work at this point. | |
| 158 EXPECT_FALSE(invalidator_.invalidations_enabled()); | |
| 159 | |
| 160 // Load the policy data, it should work now. | |
| 161 invalidator_.SetInvalidationObjectID(kTestingObjectId1); | |
| 162 EXPECT_TRUE(invalidator_.invalidations_enabled()); | |
| 163 | |
| 164 base::RunLoop().RunUntilIdle(); | |
| 165 // No invalidation will be received if no invalidation is fired. | |
| 166 VerifyExpectations(); | |
| 167 | |
| 168 // Fire an invalidation with different object id, no invalidation will be | |
| 169 // received. | |
| 170 const syncer::Invalidation invalidation1 = | |
| 171 FireInvalidation(kTestingObjectId2); | |
| 172 | |
| 173 base::RunLoop().RunUntilIdle(); | |
| 174 EXPECT_FALSE(IsInvalidationSent(invalidation1)); | |
| 175 VerifyExpectations(); | |
| 176 | |
| 177 // Fire the invalidation, it should be acknowledged and trigger a remote | |
| 178 // commands fetch. | |
| 179 EXPECT_CALL(invalidator_, DoRemoteCommandsFetch()).Times(1); | |
| 180 const syncer::Invalidation invalidation2 = | |
| 181 FireInvalidation(kTestingObjectId1); | |
| 182 | |
| 183 base::RunLoop().RunUntilIdle(); | |
| 184 EXPECT_TRUE(IsInvalidationSent(invalidation2)); | |
| 185 EXPECT_TRUE(IsInvalidationAcknowledged(invalidation2)); | |
| 186 VerifyExpectations(); | |
| 187 | |
| 188 StopAndShutdown(); | |
| 189 } | |
| 190 | |
| 191 // Verifies that no invalidation will be received when invalidator is shutdown. | |
| 192 TEST_F(RemoteCommandsInvalidatorBaseTest, ShutDown) { | |
| 193 EXPECT_FALSE(invalidator_.invalidations_enabled()); | |
| 194 FireInvalidation(kTestingObjectId1); | |
| 195 | |
| 196 base::RunLoop().RunUntilIdle(); | |
| 197 EXPECT_FALSE(invalidator_.invalidations_enabled()); | |
| 198 } | |
| 199 | |
| 200 // Verifies that no invalidation will be received when invalidator is stopped. | |
| 201 TEST_F(RemoteCommandsInvalidatorBaseTest, Stopped) { | |
| 202 EXPECT_CALL(invalidator_, OnInitialize()).Times(1); | |
| 203 invalidator_.Initialize(&invalidation_service_); | |
| 204 VerifyExpectations(); | |
| 205 | |
| 206 EXPECT_FALSE(invalidator_.invalidations_enabled()); | |
| 207 FireInvalidation(kTestingObjectId2); | |
| 208 | |
| 209 base::RunLoop().RunUntilIdle(); | |
| 210 EXPECT_FALSE(invalidator_.invalidations_enabled()); | |
| 211 | |
| 212 EXPECT_CALL(invalidator_, OnShutdown()).Times(1); | |
| 213 invalidator_.Shutdown(); | |
| 214 } | |
| 215 | |
| 216 // Verifies that stated/stopped state changes work as expected. | |
| 217 TEST_F(RemoteCommandsInvalidatorBaseTest, StartedStateChange) { | |
| 218 InSequence dummy; | |
| 219 | |
| 220 InitializeAndStart(); | |
| 221 | |
| 222 // Invalidator requires object id to work. | |
| 223 VerifyInvalidationDisabled(kTestingObjectId1); | |
| 224 EXPECT_FALSE(invalidator_.invalidations_enabled()); | |
| 225 invalidator_.SetInvalidationObjectID(kTestingObjectId1); | |
| 226 VerifyInvalidationEnabled(kTestingObjectId1); | |
| 227 | |
| 228 // Stop and restart invalidator. | |
| 229 EXPECT_CALL(invalidator_, OnStop()).Times(1); | |
| 230 invalidator_.Stop(); | |
| 231 VerifyExpectations(); | |
| 232 | |
| 233 VerifyInvalidationDisabled(kTestingObjectId1); | |
| 234 EXPECT_FALSE(invalidator_.invalidations_enabled()); | |
| 235 | |
| 236 EXPECT_CALL(invalidator_, OnStart()).Times(1); | |
| 237 invalidator_.Start(); | |
| 238 VerifyExpectations(); | |
| 239 | |
| 240 // Invalidator requires object id to work. | |
| 241 invalidator_.SetInvalidationObjectID(kTestingObjectId1); | |
| 242 VerifyInvalidationEnabled(kTestingObjectId1); | |
| 243 | |
| 244 StopAndShutdown(); | |
| 245 } | |
| 246 | |
| 247 // Verifies that registered state changes work as expected. | |
| 248 TEST_F(RemoteCommandsInvalidatorBaseTest, RegistedStateChange) { | |
| 249 InSequence dummy; | |
| 250 | |
| 251 InitializeAndStart(); | |
| 252 | |
| 253 invalidator_.SetInvalidationObjectID(kTestingObjectId1); | |
| 254 VerifyInvalidationEnabled(kTestingObjectId1); | |
| 255 | |
| 256 invalidator_.SetInvalidationObjectID(kTestingObjectId2); | |
| 257 VerifyInvalidationEnabled(kTestingObjectId2); | |
| 258 VerifyInvalidationDisabled(kTestingObjectId1); | |
| 259 | |
| 260 invalidator_.SetInvalidationObjectID(kTestingObjectId1); | |
| 261 VerifyInvalidationEnabled(kTestingObjectId1); | |
| 262 VerifyInvalidationDisabled(kTestingObjectId2); | |
| 263 | |
| 264 invalidator_.ClearInvalidationObjectID(); | |
| 265 VerifyInvalidationDisabled(kTestingObjectId1); | |
| 266 VerifyInvalidationDisabled(kTestingObjectId2); | |
| 267 EXPECT_FALSE(invalidator_.invalidations_enabled()); | |
| 268 | |
| 269 invalidator_.SetInvalidationObjectID(kTestingObjectId2); | |
| 270 VerifyInvalidationEnabled(kTestingObjectId2); | |
| 271 VerifyInvalidationDisabled(kTestingObjectId1); | |
| 272 | |
| 273 StopAndShutdown(); | |
| 274 } | |
| 275 | |
| 276 // Verifies that invalidation service enabled state changea work as expected. | |
|
bartfab (slow)
2015/05/15 15:37:12
Nit: s/changea/changes/
binjin
2015/05/15 15:46:46
Done.
| |
| 277 TEST_F(RemoteCommandsInvalidatorBaseTest, | |
| 278 InvalidationServiceEnabledStateChanged) { | |
| 279 InSequence dummy; | |
| 280 | |
| 281 InitializeAndStart(); | |
| 282 | |
| 283 invalidator_.SetInvalidationObjectID(kTestingObjectId1); | |
| 284 VerifyInvalidationEnabled(kTestingObjectId1); | |
| 285 | |
| 286 DisableInvalidationService(); | |
| 287 EXPECT_FALSE(invalidator_.invalidations_enabled()); | |
| 288 | |
| 289 EnableInvalidationService(); | |
| 290 VerifyInvalidationEnabled(kTestingObjectId1); | |
| 291 | |
| 292 EnableInvalidationService(); | |
| 293 VerifyInvalidationEnabled(kTestingObjectId1); | |
| 294 | |
| 295 DisableInvalidationService(); | |
| 296 EXPECT_FALSE(invalidator_.invalidations_enabled()); | |
| 297 | |
| 298 DisableInvalidationService(); | |
| 299 EXPECT_FALSE(invalidator_.invalidations_enabled()); | |
| 300 | |
| 301 StopAndShutdown(); | |
| 302 } | |
| 303 | |
| 304 } // namespace policy | |
| OLD | NEW |