/external/chromium/chrome/browser/sync/sessions/ordered_commit_set_unittest.cc

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk · C++ · 120 lines · 92 code · 18 blank · 10 comment · 13 complexity · 412958adc69081ce9cb3e70fe9eed5c4 MD5 · raw file

  1. // Copyright (c) 2010 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. #include "testing/gtest/include/gtest/gtest.h"
  5. #include "chrome/browser/sync/sessions/ordered_commit_set.h"
  6. #include "chrome/test/sync/engine/test_id_factory.h"
  7. using std::vector;
  8. class OrderedCommitSetTest : public testing::Test {
  9. public:
  10. OrderedCommitSetTest() {
  11. routes_[syncable::BOOKMARKS] = browser_sync::GROUP_UI;
  12. routes_[syncable::PREFERENCES] = browser_sync::GROUP_UI;
  13. routes_[syncable::AUTOFILL] = browser_sync::GROUP_DB;
  14. routes_[syncable::TOP_LEVEL_FOLDER] = browser_sync::GROUP_PASSIVE;
  15. }
  16. protected:
  17. browser_sync::TestIdFactory ids_;
  18. browser_sync::ModelSafeRoutingInfo routes_;
  19. };
  20. namespace browser_sync {
  21. namespace sessions {
  22. TEST_F(OrderedCommitSetTest, Projections) {
  23. vector<syncable::Id> expected;
  24. for (int i = 0; i < 8; i++)
  25. expected.push_back(ids_.NewLocalId());
  26. OrderedCommitSet commit_set1(routes_), commit_set2(routes_);
  27. commit_set1.AddCommitItem(0, expected[0], syncable::BOOKMARKS);
  28. commit_set1.AddCommitItem(1, expected[1], syncable::BOOKMARKS);
  29. commit_set1.AddCommitItem(2, expected[2], syncable::PREFERENCES);
  30. // Duplicates should be dropped.
  31. commit_set1.AddCommitItem(2, expected[2], syncable::PREFERENCES);
  32. commit_set1.AddCommitItem(3, expected[3], syncable::TOP_LEVEL_FOLDER);
  33. commit_set1.AddCommitItem(4, expected[4], syncable::TOP_LEVEL_FOLDER);
  34. commit_set2.AddCommitItem(7, expected[7], syncable::AUTOFILL);
  35. commit_set2.AddCommitItem(6, expected[6], syncable::AUTOFILL);
  36. commit_set2.AddCommitItem(5, expected[5], syncable::AUTOFILL);
  37. // Add something in set1 to set2, which should get dropped by AppendReverse.
  38. commit_set2.AddCommitItem(0, expected[0], syncable::BOOKMARKS);
  39. commit_set1.AppendReverse(commit_set2);
  40. // First, we should verify the projections are correct. Second, we want to
  41. // do the same verification after truncating by 1. Next, try truncating
  42. // the set to a size of 4, so that the DB projection is wiped out and
  43. // PASSIVE has one element removed. Finally, truncate to 1 so only UI is
  44. // remaining.
  45. int j = 0;
  46. do {
  47. SCOPED_TRACE(::testing::Message("Iteration j = ") << j);
  48. vector<syncable::Id> all_ids = commit_set1.GetAllCommitIds();
  49. EXPECT_EQ(expected.size(), all_ids.size());
  50. for (size_t i = 0; i < expected.size(); i++) {
  51. SCOPED_TRACE(::testing::Message("CommitSet mismatch at iteration i = ")
  52. << i);
  53. EXPECT_TRUE(expected[i] == all_ids[i]);
  54. EXPECT_TRUE(expected[i] == commit_set1.GetCommitIdAt(i));
  55. }
  56. OrderedCommitSet::Projection p1, p2, p3;
  57. p1 = commit_set1.GetCommitIdProjection(GROUP_UI);
  58. p2 = commit_set1.GetCommitIdProjection(GROUP_PASSIVE);
  59. p3 = commit_set1.GetCommitIdProjection(GROUP_DB);
  60. EXPECT_TRUE(p1.size() + p2.size() + p3.size() == expected.size()) << "Sum"
  61. << "of sizes of projections should equal full expected size!";
  62. for (size_t i = 0; i < p1.size(); i++) {
  63. SCOPED_TRACE(::testing::Message("UI projection mismatch at i = ") << i);
  64. EXPECT_TRUE(expected[p1[i]] == commit_set1.GetCommitIdAt(p1[i]))
  65. << "expected[p1[i]] = " << expected[p1[i]]
  66. << ", commit_set1[p1[i]] = " << commit_set1.GetCommitIdAt(p1[i]);
  67. }
  68. for (size_t i = 0; i < p2.size(); i++) {
  69. SCOPED_TRACE(::testing::Message("PASSIVE projection mismatch at i = ")
  70. << i);
  71. EXPECT_TRUE(expected[p2[i]] == commit_set1.GetCommitIdAt(p2[i]))
  72. << "expected[p2[i]] = " << expected[p2[i]]
  73. << ", commit_set1[p2[i]] = " << commit_set1.GetCommitIdAt(p2[i]);
  74. }
  75. for (size_t i = 0; i < p3.size(); i++) {
  76. SCOPED_TRACE(::testing::Message("DB projection mismatch at i = ") << i);
  77. EXPECT_TRUE(expected[p3[i]] == commit_set1.GetCommitIdAt(p3[i]))
  78. << "expected[p3[i]] = " << expected[p3[i]]
  79. << ", commit_set1[p3[i]] = " << commit_set1.GetCommitIdAt(p3[i]);
  80. }
  81. int cut_to_size = 7 - 3 * j++;
  82. if (cut_to_size < 0)
  83. break;
  84. expected.resize(cut_to_size);
  85. commit_set1.Truncate(cut_to_size);
  86. } while (true);
  87. }
  88. TEST_F(OrderedCommitSetTest, HasBookmarkCommitId) {
  89. OrderedCommitSet commit_set(routes_);
  90. commit_set.AddCommitItem(0, ids_.NewLocalId(), syncable::AUTOFILL);
  91. commit_set.AddCommitItem(1, ids_.NewLocalId(), syncable::TOP_LEVEL_FOLDER);
  92. EXPECT_FALSE(commit_set.HasBookmarkCommitId());
  93. commit_set.AddCommitItem(2, ids_.NewLocalId(), syncable::PREFERENCES);
  94. commit_set.AddCommitItem(3, ids_.NewLocalId(), syncable::PREFERENCES);
  95. EXPECT_FALSE(commit_set.HasBookmarkCommitId());
  96. commit_set.AddCommitItem(4, ids_.NewLocalId(), syncable::BOOKMARKS);
  97. EXPECT_TRUE(commit_set.HasBookmarkCommitId());
  98. commit_set.Truncate(4);
  99. EXPECT_FALSE(commit_set.HasBookmarkCommitId());
  100. }
  101. } // namespace sessions
  102. } // namespace browser_sync