summaryrefslogtreecommitdiffstats
path: root/chrome/test/live_sync/bookmark_model_verifier.cc
blob: 7face7b01a8ad1137d5ab0fde98f932add6179bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#if defined(BROWSER_SYNC)

#include "chrome/test/live_sync/bookmark_model_verifier.h"

#include <vector>
#include <stack>

#include "app/tree_node_iterator.h"
#include "base/rand_util.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_utils.h"
#include "chrome/test/live_sync/live_bookmarks_sync_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"

// static
void BookmarkModelVerifier::ExpectBookmarkInfoMatch(
    const BookmarkNode* expected, const BookmarkNode* actual) {
  EXPECT_EQ(expected->GetTitle(), actual->GetTitle());
  EXPECT_EQ(expected->is_folder(), actual->is_folder());
  EXPECT_EQ(expected->GetURL(), actual->GetURL());
  EXPECT_EQ(expected->GetParent()->IndexOfChild(expected),
            actual->GetParent()->IndexOfChild(actual));
}

BookmarkModelVerifier::BookmarkModelVerifier() {
  verifier_profile_.reset(LiveBookmarksSyncTest::MakeProfile(L"verifier"));
  verifier_ = verifier_profile_->GetBookmarkModel();
}

BookmarkModelVerifier* BookmarkModelVerifier::Create() {
  BookmarkModelVerifier* v = new BookmarkModelVerifier();
  LiveBookmarksSyncTest::BlockUntilLoaded(v->verifier_);
  return v;
}

void BookmarkModelVerifier::ExpectMatch(BookmarkModel* actual) {
  ExpectModelsMatch(verifier_, actual);
}

// static
void BookmarkModelVerifier::ExpectModelsMatchIncludingFavicon(
    BookmarkModel* expected, BookmarkModel* actual, bool compare_favicon) {
  TreeNodeIterator<const BookmarkNode> e_iterator(expected->root_node());
  TreeNodeIterator<const BookmarkNode> a_iterator(actual->root_node());

  // Pre-order traversal of each model, comparing at each step.
  while (e_iterator.has_next()) {
    const BookmarkNode* e_node = e_iterator.Next();
    ASSERT_TRUE(a_iterator.has_next());
    const BookmarkNode* a_node = a_iterator.Next();
    ExpectBookmarkInfoMatch(e_node, a_node);
    // Get Favicon and compare if compare_favicon flag is true.
    if (compare_favicon) {
      const SkBitmap& e_node_favicon = expected->GetFavIcon(e_node);
      const SkBitmap& a_node_favicon = actual->GetFavIcon(a_node);
      EXPECT_GT(e_node_favicon.getSize(), (size_t) 0);
      EXPECT_EQ(e_node_favicon.getSize(), a_node_favicon.getSize());
      EXPECT_EQ(e_node_favicon.width(), a_node_favicon.width());
      EXPECT_EQ(e_node_favicon.height(), a_node_favicon.height());
      SkAutoLockPixels bitmap_lock_e(e_node_favicon);
      SkAutoLockPixels bitmap_lock_a(a_node_favicon);
      void* e_node_pixel_addr = e_node_favicon.getPixels();
      ASSERT_TRUE(e_node_pixel_addr);
      void* a_node_pixel_addr = a_node_favicon.getPixels();
      ASSERT_TRUE(a_node_pixel_addr);
      EXPECT_EQ(memcmp(e_node_pixel_addr, a_node_pixel_addr,
          e_node_favicon.getSize()), 0);
    }
  }
  ASSERT_FALSE(a_iterator.has_next());
}

void BookmarkModelVerifier::VerifyNoDuplicates(BookmarkModel* model) {
  TreeNodeIterator<const BookmarkNode> iterator(model->root_node());
  // Pre-order traversal of model tree, looking for duplicate node at
  // each step.
  while (iterator.has_next()) {
    const BookmarkNode* node = iterator.Next();
    std::vector<const BookmarkNode*> nodes;
    if (node->type() != BookmarkNode::URL)
      continue;
    // Get nodes with same URL.
    model->GetNodesByURL(node->GetURL(), &nodes);
    EXPECT_TRUE(nodes.size() >= 1);
    for (std::vector<const BookmarkNode*>::const_iterator i = nodes.begin(),
         e = nodes.end(); i != e; i++) {
      // Skip if it's same node.
      int64 id = node->id();
      if (id == (*i)->id()) {
        continue;
      } else {
        // Make sure title are not same.
        EXPECT_NE(node->GetTitle(), (*i)->GetTitle());
      }
    }
  }  // end of while
}

void BookmarkModelVerifier::FindNodeInVerifier(BookmarkModel* foreign_model,
                                               const BookmarkNode* foreign_node,
                                               const BookmarkNode** result) {
  // Climb the tree.
  std::stack<int> path;
  const BookmarkNode* walker = foreign_node;
  while (walker != foreign_model->root_node()) {
    path.push(walker->GetParent()->IndexOfChild(walker));
    walker = walker->GetParent();
  }

  // Swing over to the other tree.
  walker = verifier_->root_node();

  // Climb down.
  while (!path.empty()) {
    ASSERT_TRUE(walker->is_folder());
    ASSERT_LT(path.top(), walker->GetChildCount());
    walker = walker->GetChild(path.top());
    path.pop();
  }

  ExpectBookmarkInfoMatch(foreign_node, walker);
  *result = walker;
}

const BookmarkNode* BookmarkModelVerifier::AddGroup(BookmarkModel* model,
    const BookmarkNode* parent, int index, const string16& title) {
  const BookmarkNode* v_parent = NULL;
  FindNodeInVerifier(model, parent, &v_parent);
  const BookmarkNode* result = model->AddGroup(parent, index, title);
  EXPECT_TRUE(result);
  if (!result)
    return NULL;
  const BookmarkNode* v_node = verifier_->AddGroup(v_parent, index, title);
  EXPECT_TRUE(v_node);
  if (!v_node)
    return NULL;
  ExpectBookmarkInfoMatch(v_node, result);
  return result;
}

const BookmarkNode* BookmarkModelVerifier::AddNonEmptyGroup(
    BookmarkModel* model, const BookmarkNode* parent, int index,
    const string16& title, int children_count) {
  const BookmarkNode* bm_folder = AddGroup(model, parent, index, title);
  EXPECT_TRUE(bm_folder);
  if (!bm_folder)
    return NULL;
  for (int child_index = 0; child_index < children_count; child_index++) {
    int random_int = base::RandInt(1, 100);
    // To create randomness in order, 60% of time add bookmarks
    if (random_int > 40) {
      string16 child_bm_title(bm_folder->GetTitle());
      child_bm_title.append(L"-ChildBM");
      string16 url(L"http://www.nofaviconurl");
      string16 index_str = IntToString16(child_index);
      child_bm_title.append(index_str);
      url.append(index_str);
      url.append(L".com");
      const BookmarkNode* child_nofavicon_bm =
         AddURL(model, bm_folder, child_index, child_bm_title, GURL(url));
    } else {
      // Remaining % of time - Add Bookmark folders
      string16 child_bmfolder_title(bm_folder->GetTitle());
      child_bmfolder_title.append(L"-ChildBMFolder");
      string16 index_str = IntToString16(child_index);
      child_bmfolder_title.append(index_str);
      const BookmarkNode* child_bm_folder =
          AddGroup(model, bm_folder, child_index, child_bmfolder_title);
    }
  }
  return bm_folder;
}

const BookmarkNode* BookmarkModelVerifier::AddURL(BookmarkModel* model,
    const BookmarkNode* parent, int index, const string16& title,
    const GURL& url) {
  const BookmarkNode* v_parent = NULL;
  FindNodeInVerifier(model, parent, &v_parent);
  const BookmarkNode* result = model->AddURL(parent, index, title, url);
  EXPECT_TRUE(result);
  if (!result)
    return NULL;
  const BookmarkNode* v_node = verifier_->AddURL(v_parent, index, title, url);
  EXPECT_TRUE(v_node);
  if (!v_node)
    return NULL;
  ExpectBookmarkInfoMatch(v_node, result);
  return result;
}

void BookmarkModelVerifier::SetTitle(BookmarkModel* model,
                                     const BookmarkNode* node,
                                     const string16& title) {
  const BookmarkNode* v_node = NULL;
  FindNodeInVerifier(model, node, &v_node);
  model->SetTitle(node, title);
  verifier_->SetTitle(v_node, title);
}

void BookmarkModelVerifier::Move(BookmarkModel* model, const BookmarkNode* node,
                                 const BookmarkNode* new_parent, int index) {
  const BookmarkNode* v_new_parent = NULL;
  const BookmarkNode* v_node = NULL;
  FindNodeInVerifier(model, new_parent, &v_new_parent);
  FindNodeInVerifier(model, node, &v_node);
  model->Move(node, new_parent, index);
  verifier_->Move(v_node, v_new_parent, index);
}

void BookmarkModelVerifier::Remove(BookmarkModel* model,
                                   const BookmarkNode* parent,
                                   int index) {
  const BookmarkNode* v_parent = NULL;
  FindNodeInVerifier(model, parent, &v_parent);
  ExpectBookmarkInfoMatch(parent->GetChild(index), v_parent->GetChild(index));
  model->Remove(parent, index);
  verifier_->Remove(v_parent, index);
}

void BookmarkModelVerifier::SortChildren(BookmarkModel* model,
                                         const BookmarkNode* parent) {
  const BookmarkNode* v_parent = NULL;
  FindNodeInVerifier(model, parent, &v_parent);
  model->SortChildren(parent);
  verifier_->SortChildren(v_parent);
}

void BookmarkModelVerifier::ReverseChildOrder(BookmarkModel* model,
                                         const BookmarkNode* parent) {
  int child_count = parent->GetChildCount();
  if (child_count <= 0)
    return;
  for (int index = 0; index < child_count; index++)
    Move(model, parent->GetChild(index), parent, child_count-index);
}

const BookmarkNode* BookmarkModelVerifier::SetURL(BookmarkModel* model,
                                   const BookmarkNode* node,
                                   const GURL& new_url) {
  const BookmarkNode* v_node = NULL;
  FindNodeInVerifier(model, node, &v_node);
  const BookmarkNode* result = bookmark_utils::ApplyEditsWithNoGroupChange(
      model, node->GetParent(), BookmarkEditor::EditDetails(node),
      node->GetTitle(), new_url, NULL);
  bookmark_utils::ApplyEditsWithNoGroupChange(verifier_, v_node->GetParent(),
      BookmarkEditor::EditDetails(v_node), v_node->GetTitle(), new_url, NULL);
  return result;
}

#endif  // defined(BROWSER_SYNC)