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