| 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 "sync/sessions/nudge_tracker.h" | 5 #include "sync/sessions/nudge_tracker.h" |
| 6 | 6 |
| 7 #include "sync/internal_api/public/base/model_type_invalidation_map.h" | 7 #include "sync/internal_api/public/base/model_type_invalidation_map.h" |
| 8 #include "sync/internal_api/public/sessions/sync_source_info.h" | 8 #include "sync/internal_api/public/sessions/sync_source_info.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 return gu_trigger.datatype_refresh_nudges(); | 54 return gu_trigger.datatype_refresh_nudges(); |
| 55 } | 55 } |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 // Exercise an empty NudgeTracker. | 58 // Exercise an empty NudgeTracker. |
| 59 // Use with valgrind to detect uninitialized members. | 59 // Use with valgrind to detect uninitialized members. |
| 60 TEST_F(NudgeTrackerTest, EmptyNudgeTracker) { | 60 TEST_F(NudgeTrackerTest, EmptyNudgeTracker) { |
| 61 NudgeTracker nudge_tracker; | 61 NudgeTracker nudge_tracker; |
| 62 | 62 |
| 63 EXPECT_FALSE(nudge_tracker.IsSyncRequired()); | 63 EXPECT_FALSE(nudge_tracker.IsSyncRequired()); |
| 64 EXPECT_FALSE(nudge_tracker.IsGetUpdatesRequired()); |
| 64 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::UNKNOWN, | 65 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::UNKNOWN, |
| 65 nudge_tracker.updates_source()); | 66 nudge_tracker.updates_source()); |
| 66 | 67 |
| 67 sync_pb::GetUpdateTriggers gu_trigger; | 68 sync_pb::GetUpdateTriggers gu_trigger; |
| 68 nudge_tracker.FillProtoMessage(BOOKMARKS, &gu_trigger); | 69 nudge_tracker.FillProtoMessage(BOOKMARKS, &gu_trigger); |
| 69 | 70 |
| 70 SyncSourceInfo source_info = nudge_tracker.GetSourceInfo(); | 71 SyncSourceInfo source_info = nudge_tracker.GetSourceInfo(); |
| 71 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::UNKNOWN, | 72 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::UNKNOWN, |
| 72 source_info.updates_source); | 73 source_info.updates_source); |
| 73 } | 74 } |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 // Invalidations. | 294 // Invalidations. |
| 294 ModelTypeInvalidationMap invalidation_map = | 295 ModelTypeInvalidationMap invalidation_map = |
| 295 ModelTypeSetToInvalidationMap(ModelTypeSet(PREFERENCES), | 296 ModelTypeSetToInvalidationMap(ModelTypeSet(PREFERENCES), |
| 296 std::string("hint")); | 297 std::string("hint")); |
| 297 nudge_tracker.RecordRemoteInvalidation(invalidation_map); | 298 nudge_tracker.RecordRemoteInvalidation(invalidation_map); |
| 298 EXPECT_TRUE(nudge_tracker.IsSyncRequired()); | 299 EXPECT_TRUE(nudge_tracker.IsSyncRequired()); |
| 299 nudge_tracker.RecordSuccessfulSyncCycle(); | 300 nudge_tracker.RecordSuccessfulSyncCycle(); |
| 300 EXPECT_FALSE(nudge_tracker.IsSyncRequired()); | 301 EXPECT_FALSE(nudge_tracker.IsSyncRequired()); |
| 301 } | 302 } |
| 302 | 303 |
| 304 // Basic tests for the IsGetUpdatesRequired() flag. |
| 305 TEST_F(NudgeTrackerTest, IsGetUpdatesRequired) { |
| 306 NudgeTracker nudge_tracker; |
| 307 EXPECT_FALSE(nudge_tracker.IsGetUpdatesRequired()); |
| 308 |
| 309 // Local changes. |
| 310 nudge_tracker.RecordLocalChange(ModelTypeSet(SESSIONS)); |
| 311 EXPECT_FALSE(nudge_tracker.IsGetUpdatesRequired()); |
| 312 nudge_tracker.RecordSuccessfulSyncCycle(); |
| 313 EXPECT_FALSE(nudge_tracker.IsGetUpdatesRequired()); |
| 314 |
| 315 // Refresh requests. |
| 316 nudge_tracker.RecordLocalRefreshRequest(ModelTypeSet(SESSIONS)); |
| 317 EXPECT_TRUE(nudge_tracker.IsGetUpdatesRequired()); |
| 318 nudge_tracker.RecordSuccessfulSyncCycle(); |
| 319 EXPECT_FALSE(nudge_tracker.IsGetUpdatesRequired()); |
| 320 |
| 321 // Invalidations. |
| 322 ModelTypeInvalidationMap invalidation_map = |
| 323 ModelTypeSetToInvalidationMap(ModelTypeSet(PREFERENCES), |
| 324 std::string("hint")); |
| 325 nudge_tracker.RecordRemoteInvalidation(invalidation_map); |
| 326 EXPECT_TRUE(nudge_tracker.IsGetUpdatesRequired()); |
| 327 nudge_tracker.RecordSuccessfulSyncCycle(); |
| 328 EXPECT_FALSE(nudge_tracker.IsGetUpdatesRequired()); |
| 329 } |
| 330 |
| 303 // Test IsSyncRequired() responds correctly to data type throttling. | 331 // Test IsSyncRequired() responds correctly to data type throttling. |
| 304 TEST_F(NudgeTrackerTest, IsSyncRequired_Throttling) { | 332 TEST_F(NudgeTrackerTest, IsSyncRequired_Throttling) { |
| 305 NudgeTracker nudge_tracker; | 333 NudgeTracker nudge_tracker; |
| 306 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234); | 334 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234); |
| 307 const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10); | 335 const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10); |
| 308 const base::TimeTicks t1 = t0 + throttle_length; | 336 const base::TimeTicks t1 = t0 + throttle_length; |
| 309 | 337 |
| 310 EXPECT_FALSE(nudge_tracker.IsSyncRequired()); | 338 EXPECT_FALSE(nudge_tracker.IsSyncRequired()); |
| 311 | 339 |
| 312 // A local change to sessions enables the flag. | 340 // A local change to sessions enables the flag. |
| 313 nudge_tracker.RecordLocalChange(ModelTypeSet(SESSIONS)); | 341 nudge_tracker.RecordLocalChange(ModelTypeSet(SESSIONS)); |
| 314 EXPECT_TRUE(nudge_tracker.IsSyncRequired()); | 342 EXPECT_TRUE(nudge_tracker.IsSyncRequired()); |
| 315 | 343 |
| 316 // But the throttling of sessions unsets it. | 344 // But the throttling of sessions unsets it. |
| 317 nudge_tracker.SetTypesThrottledUntil(ModelTypeSet(SESSIONS), | 345 nudge_tracker.SetTypesThrottledUntil(ModelTypeSet(SESSIONS), |
| 318 throttle_length, | 346 throttle_length, |
| 319 t0); | 347 t0); |
| 320 EXPECT_FALSE(nudge_tracker.IsSyncRequired()); | 348 EXPECT_FALSE(nudge_tracker.IsSyncRequired()); |
| 321 | 349 |
| 322 // A refresh request for bookmarks means we have reason to sync again. | 350 // A refresh request for bookmarks means we have reason to sync again. |
| 323 nudge_tracker.RecordLocalChange(ModelTypeSet(BOOKMARKS)); | 351 nudge_tracker.RecordLocalRefreshRequest(ModelTypeSet(BOOKMARKS)); |
| 324 EXPECT_TRUE(nudge_tracker.IsSyncRequired()); | 352 EXPECT_TRUE(nudge_tracker.IsSyncRequired()); |
| 325 | 353 |
| 326 // A successful sync cycle means we took care of bookmarks. | 354 // A successful sync cycle means we took care of bookmarks. |
| 327 nudge_tracker.RecordSuccessfulSyncCycle(); | 355 nudge_tracker.RecordSuccessfulSyncCycle(); |
| 328 EXPECT_FALSE(nudge_tracker.IsSyncRequired()); | 356 EXPECT_FALSE(nudge_tracker.IsSyncRequired()); |
| 329 | 357 |
| 330 // But we still haven't dealt with sessions. We'll need to remember | 358 // But we still haven't dealt with sessions. We'll need to remember |
| 331 // that sessions are out of sync and re-enable the flag when their | 359 // that sessions are out of sync and re-enable the flag when their |
| 332 // throttling interval expires. | 360 // throttling interval expires. |
| 333 nudge_tracker.UpdateTypeThrottlingState(t1); | 361 nudge_tracker.UpdateTypeThrottlingState(t1); |
| 334 EXPECT_FALSE(nudge_tracker.IsTypeThrottled(SESSIONS)); | 362 EXPECT_FALSE(nudge_tracker.IsTypeThrottled(SESSIONS)); |
| 335 EXPECT_TRUE(nudge_tracker.IsSyncRequired()); | 363 EXPECT_TRUE(nudge_tracker.IsSyncRequired()); |
| 336 } | 364 } |
| 337 | 365 |
| 366 // Test IsGetUpdatesRequired() responds correctly to data type throttling. |
| 367 TEST_F(NudgeTrackerTest, IsGetUpdatesRequired_Throttling) { |
| 368 NudgeTracker nudge_tracker; |
| 369 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234); |
| 370 const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10); |
| 371 const base::TimeTicks t1 = t0 + throttle_length; |
| 372 |
| 373 EXPECT_FALSE(nudge_tracker.IsGetUpdatesRequired()); |
| 374 |
| 375 // A refresh request to sessions enables the flag. |
| 376 nudge_tracker.RecordLocalRefreshRequest(ModelTypeSet(SESSIONS)); |
| 377 EXPECT_TRUE(nudge_tracker.IsGetUpdatesRequired()); |
| 378 |
| 379 // But the throttling of sessions unsets it. |
| 380 nudge_tracker.SetTypesThrottledUntil(ModelTypeSet(SESSIONS), |
| 381 throttle_length, |
| 382 t0); |
| 383 EXPECT_FALSE(nudge_tracker.IsGetUpdatesRequired()); |
| 384 |
| 385 // A refresh request for bookmarks means we have reason to sync again. |
| 386 nudge_tracker.RecordLocalRefreshRequest(ModelTypeSet(BOOKMARKS)); |
| 387 EXPECT_TRUE(nudge_tracker.IsGetUpdatesRequired()); |
| 388 |
| 389 // A successful sync cycle means we took care of bookmarks. |
| 390 nudge_tracker.RecordSuccessfulSyncCycle(); |
| 391 EXPECT_FALSE(nudge_tracker.IsGetUpdatesRequired()); |
| 392 |
| 393 // But we still haven't dealt with sessions. We'll need to remember |
| 394 // that sessions are out of sync and re-enable the flag when their |
| 395 // throttling interval expires. |
| 396 nudge_tracker.UpdateTypeThrottlingState(t1); |
| 397 EXPECT_FALSE(nudge_tracker.IsTypeThrottled(SESSIONS)); |
| 398 EXPECT_TRUE(nudge_tracker.IsGetUpdatesRequired()); |
| 399 } |
| 400 |
| 338 // Tests throttling-related getter functions when no types are throttled. | 401 // Tests throttling-related getter functions when no types are throttled. |
| 339 TEST_F(NudgeTrackerTest, NoTypesThrottled) { | 402 TEST_F(NudgeTrackerTest, NoTypesThrottled) { |
| 340 NudgeTracker nudge_tracker; | 403 NudgeTracker nudge_tracker; |
| 341 | 404 |
| 342 EXPECT_FALSE(nudge_tracker.IsAnyTypeThrottled()); | 405 EXPECT_FALSE(nudge_tracker.IsAnyTypeThrottled()); |
| 343 EXPECT_FALSE(nudge_tracker.IsTypeThrottled(SESSIONS)); | 406 EXPECT_FALSE(nudge_tracker.IsTypeThrottled(SESSIONS)); |
| 344 EXPECT_TRUE(nudge_tracker.GetThrottledTypes().Empty()); | 407 EXPECT_TRUE(nudge_tracker.GetThrottledTypes().Empty()); |
| 345 } | 408 } |
| 346 | 409 |
| 347 // Tests throttling-related getter functions when some types are throttled. | 410 // Tests throttling-related getter functions when some types are throttled. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 EXPECT_EQ(throttle2_length - throttle1_length, | 470 EXPECT_EQ(throttle2_length - throttle1_length, |
| 408 nudge_tracker.GetTimeUntilNextUnthrottle(t1)); | 471 nudge_tracker.GetTimeUntilNextUnthrottle(t1)); |
| 409 | 472 |
| 410 // Expire the second interval. | 473 // Expire the second interval. |
| 411 nudge_tracker.UpdateTypeThrottlingState(t2); | 474 nudge_tracker.UpdateTypeThrottlingState(t2); |
| 412 EXPECT_TRUE(nudge_tracker.GetThrottledTypes().Empty()); | 475 EXPECT_TRUE(nudge_tracker.GetThrottledTypes().Empty()); |
| 413 } | 476 } |
| 414 | 477 |
| 415 } // namespace sessions | 478 } // namespace sessions |
| 416 } // namespace syncer | 479 } // namespace syncer |
| OLD | NEW |