Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: components/sync_preferences/pref_service_syncable_unittest.cc

Issue 2782553004: Move TestingPrefService to use unique_ptr<Value> (Closed)
Patch Set: comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/sync_preferences/pref_service_syncable.h" 5 #include "components/sync_preferences/pref_service_syncable.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 list.push_back(MakeRemoteChange(1, "unknown preference", expected, 374 list.push_back(MakeRemoteChange(1, "unknown preference", expected,
375 SyncChange::ACTION_UPDATE)); 375 SyncChange::ACTION_UPDATE));
376 pref_sync_service_->ProcessSyncChanges(FROM_HERE, list); 376 pref_sync_service_->ProcessSyncChanges(FROM_HERE, list);
377 // Nothing interesting happens on the client when it gets an update 377 // Nothing interesting happens on the client when it gets an update
378 // of an unknown preference. We just should not crash. 378 // of an unknown preference. We just should not crash.
379 } 379 }
380 380
381 TEST_F(PrefServiceSyncableTest, ManagedPreferences) { 381 TEST_F(PrefServiceSyncableTest, ManagedPreferences) {
382 // Make the homepage preference managed. 382 // Make the homepage preference managed.
383 base::Value managed_value("http://example.com"); 383 base::Value managed_value("http://example.com");
384 prefs_.SetManagedPref(kStringPrefName, managed_value.DeepCopy()); 384 prefs_.SetManagedPref(kStringPrefName, managed_value.CreateDeepCopy());
385 385
386 syncer::SyncChangeList out; 386 syncer::SyncChangeList out;
387 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out); 387 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out);
388 out.clear(); 388 out.clear();
389 389
390 // Changing the homepage preference should not sync anything. 390 // Changing the homepage preference should not sync anything.
391 base::Value user_value("http://chromium..com"); 391 base::Value user_value("http://chromium..com");
392 prefs_.SetUserPref(kStringPrefName, user_value.DeepCopy()); 392 prefs_.SetUserPref(kStringPrefName, user_value.CreateDeepCopy());
393 EXPECT_TRUE(out.empty()); 393 EXPECT_TRUE(out.empty());
394 394
395 // An incoming sync transaction should change the user value, not the managed 395 // An incoming sync transaction should change the user value, not the managed
396 // value. 396 // value.
397 base::Value sync_value("http://crbug.com"); 397 base::Value sync_value("http://crbug.com");
398 syncer::SyncChangeList list; 398 syncer::SyncChangeList list;
399 list.push_back(MakeRemoteChange(1, kStringPrefName, sync_value, 399 list.push_back(MakeRemoteChange(1, kStringPrefName, sync_value,
400 SyncChange::ACTION_UPDATE)); 400 SyncChange::ACTION_UPDATE));
401 pref_sync_service_->ProcessSyncChanges(FROM_HERE, list); 401 pref_sync_service_->ProcessSyncChanges(FROM_HERE, list);
402 402
403 EXPECT_TRUE(managed_value.Equals(prefs_.GetManagedPref(kStringPrefName))); 403 EXPECT_TRUE(managed_value.Equals(prefs_.GetManagedPref(kStringPrefName)));
404 EXPECT_TRUE(sync_value.Equals(prefs_.GetUserPref(kStringPrefName))); 404 EXPECT_TRUE(sync_value.Equals(prefs_.GetUserPref(kStringPrefName)));
405 } 405 }
406 406
407 // List preferences have special handling at association time due to our ability 407 // List preferences have special handling at association time due to our ability
408 // to merge the local and sync value. Make sure the merge logic doesn't merge 408 // to merge the local and sync value. Make sure the merge logic doesn't merge
409 // managed preferences. 409 // managed preferences.
410 TEST_F(PrefServiceSyncableTest, ManagedListPreferences) { 410 TEST_F(PrefServiceSyncableTest, ManagedListPreferences) {
411 // Make the list of urls to restore on startup managed. 411 // Make the list of urls to restore on startup managed.
412 base::ListValue managed_value; 412 base::ListValue managed_value;
413 managed_value.AppendString(kExampleUrl0); 413 managed_value.AppendString(kExampleUrl0);
414 managed_value.AppendString(kExampleUrl1); 414 managed_value.AppendString(kExampleUrl1);
415 prefs_.SetManagedPref(kListPrefName, managed_value.DeepCopy()); 415 prefs_.SetManagedPref(kListPrefName, managed_value.CreateDeepCopy());
416 416
417 // Set a cloud version. 417 // Set a cloud version.
418 syncer::SyncDataList in; 418 syncer::SyncDataList in;
419 syncer::SyncChangeList out; 419 syncer::SyncChangeList out;
420 base::ListValue urls_to_restore; 420 base::ListValue urls_to_restore;
421 urls_to_restore.AppendString(kExampleUrl1); 421 urls_to_restore.AppendString(kExampleUrl1);
422 urls_to_restore.AppendString(kExampleUrl2); 422 urls_to_restore.AppendString(kExampleUrl2);
423 AddToRemoteDataList(kListPrefName, urls_to_restore, &in); 423 AddToRemoteDataList(kListPrefName, urls_to_restore, &in);
424 424
425 // Start sync and verify the synced value didn't get merged. 425 // Start sync and verify the synced value didn't get merged.
426 InitWithSyncDataTakeOutput(in, &out); 426 InitWithSyncDataTakeOutput(in, &out);
427 EXPECT_FALSE(FindValue(kListPrefName, out).get()); 427 EXPECT_FALSE(FindValue(kListPrefName, out).get());
428 out.clear(); 428 out.clear();
429 429
430 // Changing the user's urls to restore on startup pref should not sync 430 // Changing the user's urls to restore on startup pref should not sync
431 // anything. 431 // anything.
432 base::ListValue user_value; 432 base::ListValue user_value;
433 user_value.AppendString("http://chromium.org"); 433 user_value.AppendString("http://chromium.org");
434 prefs_.SetUserPref(kListPrefName, user_value.DeepCopy()); 434 prefs_.SetUserPref(kListPrefName, user_value.CreateDeepCopy());
435 EXPECT_FALSE(FindValue(kListPrefName, out).get()); 435 EXPECT_FALSE(FindValue(kListPrefName, out).get());
436 436
437 // An incoming sync transaction should change the user value, not the managed 437 // An incoming sync transaction should change the user value, not the managed
438 // value. 438 // value.
439 base::ListValue sync_value; 439 base::ListValue sync_value;
440 sync_value.AppendString("http://crbug.com"); 440 sync_value.AppendString("http://crbug.com");
441 syncer::SyncChangeList list; 441 syncer::SyncChangeList list;
442 list.push_back(MakeRemoteChange(1, kListPrefName, sync_value, 442 list.push_back(MakeRemoteChange(1, kListPrefName, sync_value,
443 SyncChange::ACTION_UPDATE)); 443 SyncChange::ACTION_UPDATE));
444 pref_sync_service_->ProcessSyncChanges(FROM_HERE, list); 444 pref_sync_service_->ProcessSyncChanges(FROM_HERE, list);
445 445
446 EXPECT_TRUE(managed_value.Equals(prefs_.GetManagedPref(kListPrefName))); 446 EXPECT_TRUE(managed_value.Equals(prefs_.GetManagedPref(kListPrefName)));
447 EXPECT_TRUE(sync_value.Equals(prefs_.GetUserPref(kListPrefName))); 447 EXPECT_TRUE(sync_value.Equals(prefs_.GetUserPref(kListPrefName)));
448 } 448 }
449 449
450 TEST_F(PrefServiceSyncableTest, DynamicManagedPreferences) { 450 TEST_F(PrefServiceSyncableTest, DynamicManagedPreferences) {
451 syncer::SyncChangeList out; 451 syncer::SyncChangeList out;
452 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out); 452 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out);
453 out.clear(); 453 out.clear();
454 base::Value initial_value("http://example.com/initial"); 454 base::Value initial_value("http://example.com/initial");
455 GetPrefs()->Set(kStringPrefName, initial_value); 455 GetPrefs()->Set(kStringPrefName, initial_value);
456 std::unique_ptr<base::Value> actual(FindValue(kStringPrefName, out)); 456 std::unique_ptr<base::Value> actual(FindValue(kStringPrefName, out));
457 ASSERT_TRUE(actual.get()); 457 ASSERT_TRUE(actual.get());
458 EXPECT_TRUE(initial_value.Equals(actual.get())); 458 EXPECT_TRUE(initial_value.Equals(actual.get()));
459 459
460 // Switch kHomePage to managed and set a different value. 460 // Switch kHomePage to managed and set a different value.
461 base::Value managed_value("http://example.com/managed"); 461 base::Value managed_value("http://example.com/managed");
462 GetTestingPrefService()->SetManagedPref(kStringPrefName, 462 GetTestingPrefService()->SetManagedPref(kStringPrefName,
463 managed_value.DeepCopy()); 463 managed_value.CreateDeepCopy());
464 464
465 // The pref value should be the one dictated by policy. 465 // The pref value should be the one dictated by policy.
466 EXPECT_TRUE(managed_value.Equals(&GetPreferenceValue(kStringPrefName))); 466 EXPECT_TRUE(managed_value.Equals(&GetPreferenceValue(kStringPrefName)));
467 467
468 // Switch kHomePage back to unmanaged. 468 // Switch kHomePage back to unmanaged.
469 GetTestingPrefService()->RemoveManagedPref(kStringPrefName); 469 GetTestingPrefService()->RemoveManagedPref(kStringPrefName);
470 470
471 // The original value should be picked up. 471 // The original value should be picked up.
472 EXPECT_TRUE(initial_value.Equals(&GetPreferenceValue(kStringPrefName))); 472 EXPECT_TRUE(initial_value.Equals(&GetPreferenceValue(kStringPrefName)));
473 } 473 }
474 474
475 TEST_F(PrefServiceSyncableTest, DynamicManagedPreferencesWithSyncChange) { 475 TEST_F(PrefServiceSyncableTest, DynamicManagedPreferencesWithSyncChange) {
476 syncer::SyncChangeList out; 476 syncer::SyncChangeList out;
477 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out); 477 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out);
478 out.clear(); 478 out.clear();
479 479
480 base::Value initial_value("http://example.com/initial"); 480 base::Value initial_value("http://example.com/initial");
481 GetPrefs()->Set(kStringPrefName, initial_value); 481 GetPrefs()->Set(kStringPrefName, initial_value);
482 std::unique_ptr<base::Value> actual(FindValue(kStringPrefName, out)); 482 std::unique_ptr<base::Value> actual(FindValue(kStringPrefName, out));
483 EXPECT_TRUE(initial_value.Equals(actual.get())); 483 EXPECT_TRUE(initial_value.Equals(actual.get()));
484 484
485 // Switch kHomePage to managed and set a different value. 485 // Switch kHomePage to managed and set a different value.
486 base::Value managed_value("http://example.com/managed"); 486 base::Value managed_value("http://example.com/managed");
487 GetTestingPrefService()->SetManagedPref(kStringPrefName, 487 GetTestingPrefService()->SetManagedPref(kStringPrefName,
488 managed_value.DeepCopy()); 488 managed_value.CreateDeepCopy());
489 489
490 // Change the sync value. 490 // Change the sync value.
491 base::Value sync_value("http://example.com/sync"); 491 base::Value sync_value("http://example.com/sync");
492 syncer::SyncChangeList list; 492 syncer::SyncChangeList list;
493 list.push_back(MakeRemoteChange(1, kStringPrefName, sync_value, 493 list.push_back(MakeRemoteChange(1, kStringPrefName, sync_value,
494 SyncChange::ACTION_UPDATE)); 494 SyncChange::ACTION_UPDATE));
495 pref_sync_service_->ProcessSyncChanges(FROM_HERE, list); 495 pref_sync_service_->ProcessSyncChanges(FROM_HERE, list);
496 496
497 // The pref value should still be the one dictated by policy. 497 // The pref value should still be the one dictated by policy.
498 EXPECT_TRUE(managed_value.Equals(&GetPreferenceValue(kStringPrefName))); 498 EXPECT_TRUE(managed_value.Equals(&GetPreferenceValue(kStringPrefName)));
(...skipping 12 matching lines...) Expand all
511 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out); 511 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out);
512 512
513 EXPECT_TRUE(IsSynced(kStringPrefName)); 513 EXPECT_TRUE(IsSynced(kStringPrefName));
514 EXPECT_TRUE(pref->IsDefaultValue()); 514 EXPECT_TRUE(pref->IsDefaultValue());
515 EXPECT_FALSE(FindValue(kStringPrefName, out).get()); 515 EXPECT_FALSE(FindValue(kStringPrefName, out).get());
516 out.clear(); 516 out.clear();
517 517
518 // Switch kHomePage to managed and set a different value. 518 // Switch kHomePage to managed and set a different value.
519 base::Value managed_value("http://example.com/managed"); 519 base::Value managed_value("http://example.com/managed");
520 GetTestingPrefService()->SetManagedPref(kStringPrefName, 520 GetTestingPrefService()->SetManagedPref(kStringPrefName,
521 managed_value.DeepCopy()); 521 managed_value.CreateDeepCopy());
522 // The pref value should be the one dictated by policy. 522 // The pref value should be the one dictated by policy.
523 EXPECT_TRUE(managed_value.Equals(&GetPreferenceValue(kStringPrefName))); 523 EXPECT_TRUE(managed_value.Equals(&GetPreferenceValue(kStringPrefName)));
524 EXPECT_FALSE(pref->IsDefaultValue()); 524 EXPECT_FALSE(pref->IsDefaultValue());
525 // There should be no synced value. 525 // There should be no synced value.
526 EXPECT_FALSE(FindValue(kStringPrefName, out).get()); 526 EXPECT_FALSE(FindValue(kStringPrefName, out).get());
527 // Switch kHomePage back to unmanaged. 527 // Switch kHomePage back to unmanaged.
528 GetTestingPrefService()->RemoveManagedPref(kStringPrefName); 528 GetTestingPrefService()->RemoveManagedPref(kStringPrefName);
529 // The original value should be picked up. 529 // The original value should be picked up.
530 EXPECT_TRUE(pref->IsDefaultValue()); 530 EXPECT_TRUE(pref->IsDefaultValue());
531 // There should still be no synced value. 531 // There should still be no synced value.
(...skipping 22 matching lines...) Expand all
554 base::Bind(&Increment, &num_callbacks)); 554 base::Bind(&Increment, &num_callbacks));
555 EXPECT_EQ(0, num_callbacks); 555 EXPECT_EQ(0, num_callbacks);
556 556
557 InitWithNoSyncData(); 557 InitWithNoSyncData();
558 EXPECT_EQ(1, num_callbacks); 558 EXPECT_EQ(1, num_callbacks);
559 } 559 }
560 560
561 } // namespace 561 } // namespace
562 562
563 } // namespace sync_preferences 563 } // namespace sync_preferences
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698