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