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