| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 #if defined(BROWSER_SYNC) | 5 #if defined(BROWSER_SYNC) |
| 6 | 6 |
| 7 #include "chrome/test/live_sync/bookmark_model_verifier.h" | 7 #include "chrome/test/live_sync/bookmark_model_verifier.h" |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 #include <stack> | 10 #include <stack> |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 ASSERT_FALSE(a_iterator.has_next()); | 75 ASSERT_FALSE(a_iterator.has_next()); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void BookmarkModelVerifier::VerifyNoDuplicates(BookmarkModel* model) { | 78 void BookmarkModelVerifier::VerifyNoDuplicates(BookmarkModel* model) { |
| 79 TreeNodeIterator<const BookmarkNode> iterator(model->root_node()); | 79 TreeNodeIterator<const BookmarkNode> iterator(model->root_node()); |
| 80 // Pre-order traversal of model tree, looking for duplicate node at | 80 // Pre-order traversal of model tree, looking for duplicate node at |
| 81 // each step. | 81 // each step. |
| 82 while (iterator.has_next()) { | 82 while (iterator.has_next()) { |
| 83 const BookmarkNode* node = iterator.Next(); | 83 const BookmarkNode* node = iterator.Next(); |
| 84 std::vector<const BookmarkNode*> nodes; | 84 std::vector<const BookmarkNode*> nodes; |
| 85 if (node->type() != BookmarkNode::URL) { continue; } | 85 if (node->type() != BookmarkNode::URL) |
| 86 continue; |
| 86 // Get nodes with same URL. | 87 // Get nodes with same URL. |
| 87 model->GetNodesByURL(node->GetURL(),&nodes); | 88 model->GetNodesByURL(node->GetURL(), &nodes); |
| 88 EXPECT_TRUE(nodes.size()>=1); | 89 EXPECT_TRUE(nodes.size() >= 1); |
| 89 for(std::vector<const BookmarkNode*>::const_iterator i=nodes.begin(), e=node
s.end(); i!=e; i++) { | 90 for (std::vector<const BookmarkNode*>::const_iterator i = nodes.begin(), |
| 91 e = nodes.end(); i != e; i++) { |
| 90 // Skip if it's same node. | 92 // Skip if it's same node. |
| 91 int64 id = node->id(); | 93 int64 id = node->id(); |
| 92 if ( id == (*i)->id()) { continue; } | 94 if (id == (*i)->id()) { |
| 93 else { | 95 continue; |
| 96 } else { |
| 94 // Make sure title are not same. | 97 // Make sure title are not same. |
| 95 EXPECT_NE(node->GetTitle(),(*i)->GetTitle()); | 98 EXPECT_NE(node->GetTitle(), (*i)->GetTitle()); |
| 96 } | 99 } |
| 97 } | 100 } |
| 98 } // end of while | 101 } // end of while |
| 99 } | 102 } |
| 100 | 103 |
| 101 void BookmarkModelVerifier::FindNodeInVerifier(BookmarkModel* foreign_model, | 104 void BookmarkModelVerifier::FindNodeInVerifier(BookmarkModel* foreign_model, |
| 102 const BookmarkNode* foreign_node, | 105 const BookmarkNode* foreign_node, |
| 103 const BookmarkNode** result) { | 106 const BookmarkNode** result) { |
| 104 // Climb the tree. | 107 // Climb the tree. |
| 105 std::stack<int> path; | 108 std::stack<int> path; |
| 106 const BookmarkNode* walker = foreign_node; | 109 const BookmarkNode* walker = foreign_node; |
| 107 while (walker != foreign_model->root_node()) { | 110 while (walker != foreign_model->root_node()) { |
| 108 path.push(walker->GetParent()->IndexOfChild(walker)); | 111 path.push(walker->GetParent()->IndexOfChild(walker)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 123 ExpectBookmarkInfoMatch(foreign_node, walker); | 126 ExpectBookmarkInfoMatch(foreign_node, walker); |
| 124 *result = walker; | 127 *result = walker; |
| 125 } | 128 } |
| 126 | 129 |
| 127 const BookmarkNode* BookmarkModelVerifier::AddGroup(BookmarkModel* model, | 130 const BookmarkNode* BookmarkModelVerifier::AddGroup(BookmarkModel* model, |
| 128 const BookmarkNode* parent, int index, const string16& title) { | 131 const BookmarkNode* parent, int index, const string16& title) { |
| 129 const BookmarkNode* v_parent = NULL; | 132 const BookmarkNode* v_parent = NULL; |
| 130 FindNodeInVerifier(model, parent, &v_parent); | 133 FindNodeInVerifier(model, parent, &v_parent); |
| 131 const BookmarkNode* result = model->AddGroup(parent, index, title); | 134 const BookmarkNode* result = model->AddGroup(parent, index, title); |
| 132 EXPECT_TRUE(result); | 135 EXPECT_TRUE(result); |
| 133 if (!result) return NULL; | 136 if (!result) |
| 137 return NULL; |
| 134 const BookmarkNode* v_node = verifier_->AddGroup(v_parent, index, title); | 138 const BookmarkNode* v_node = verifier_->AddGroup(v_parent, index, title); |
| 135 EXPECT_TRUE(v_node); | 139 EXPECT_TRUE(v_node); |
| 136 if (!v_node) return NULL; | 140 if (!v_node) |
| 141 return NULL; |
| 137 ExpectBookmarkInfoMatch(v_node, result); | 142 ExpectBookmarkInfoMatch(v_node, result); |
| 138 return result; | 143 return result; |
| 139 } | 144 } |
| 140 | 145 |
| 141 const BookmarkNode* BookmarkModelVerifier::AddNonEmptyGroup( | 146 const BookmarkNode* BookmarkModelVerifier::AddNonEmptyGroup( |
| 142 BookmarkModel* model, const BookmarkNode* parent, int index, | 147 BookmarkModel* model, const BookmarkNode* parent, int index, |
| 143 const string16& title, int children_count) { | 148 const string16& title, int children_count) { |
| 144 const BookmarkNode* bm_folder = AddGroup(model, parent, index, title); | 149 const BookmarkNode* bm_folder = AddGroup(model, parent, index, title); |
| 145 EXPECT_TRUE(bm_folder); | 150 EXPECT_TRUE(bm_folder); |
| 146 if (!bm_folder) return NULL; | 151 if (!bm_folder) |
| 152 return NULL; |
| 147 for (int child_index = 0; child_index < children_count; child_index++) { | 153 for (int child_index = 0; child_index < children_count; child_index++) { |
| 148 int random_int = base::RandInt(1, 100); | 154 int random_int = base::RandInt(1, 100); |
| 149 // To create randomness in order, 60% of time add bookmarks | 155 // To create randomness in order, 60% of time add bookmarks |
| 150 if (random_int > 40) { | 156 if (random_int > 40) { |
| 151 string16 child_bm_title(bm_folder->GetTitle()); | 157 string16 child_bm_title(bm_folder->GetTitle()); |
| 152 child_bm_title.append(L"-ChildBM"); | 158 child_bm_title.append(L"-ChildBM"); |
| 153 string16 url(L"http://www.nofaviconurl"); | 159 string16 url(L"http://www.nofaviconurl"); |
| 154 string16 index_str = IntToString16(child_index); | 160 string16 index_str = IntToString16(child_index); |
| 155 child_bm_title.append(index_str); | 161 child_bm_title.append(index_str); |
| 156 url.append(index_str); | 162 url.append(index_str); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 170 return bm_folder; | 176 return bm_folder; |
| 171 } | 177 } |
| 172 | 178 |
| 173 const BookmarkNode* BookmarkModelVerifier::AddURL(BookmarkModel* model, | 179 const BookmarkNode* BookmarkModelVerifier::AddURL(BookmarkModel* model, |
| 174 const BookmarkNode* parent, int index, const string16& title, | 180 const BookmarkNode* parent, int index, const string16& title, |
| 175 const GURL& url) { | 181 const GURL& url) { |
| 176 const BookmarkNode* v_parent = NULL; | 182 const BookmarkNode* v_parent = NULL; |
| 177 FindNodeInVerifier(model, parent, &v_parent); | 183 FindNodeInVerifier(model, parent, &v_parent); |
| 178 const BookmarkNode* result = model->AddURL(parent, index, title, url); | 184 const BookmarkNode* result = model->AddURL(parent, index, title, url); |
| 179 EXPECT_TRUE(result); | 185 EXPECT_TRUE(result); |
| 180 if (!result) return NULL; | 186 if (!result) |
| 187 return NULL; |
| 181 const BookmarkNode* v_node = verifier_->AddURL(v_parent, index, title, url); | 188 const BookmarkNode* v_node = verifier_->AddURL(v_parent, index, title, url); |
| 182 EXPECT_TRUE(v_node); | 189 EXPECT_TRUE(v_node); |
| 183 if (!v_node) return NULL; | 190 if (!v_node) |
| 191 return NULL; |
| 184 ExpectBookmarkInfoMatch(v_node, result); | 192 ExpectBookmarkInfoMatch(v_node, result); |
| 185 return result; | 193 return result; |
| 186 } | 194 } |
| 187 | 195 |
| 188 void BookmarkModelVerifier::SetTitle(BookmarkModel* model, | 196 void BookmarkModelVerifier::SetTitle(BookmarkModel* model, |
| 189 const BookmarkNode* node, | 197 const BookmarkNode* node, |
| 190 const string16& title) { | 198 const string16& title) { |
| 191 const BookmarkNode* v_node = NULL; | 199 const BookmarkNode* v_node = NULL; |
| 192 FindNodeInVerifier(model, node, &v_node); | 200 FindNodeInVerifier(model, node, &v_node); |
| 193 model->SetTitle(node, title); | 201 model->SetTitle(node, title); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 218 const BookmarkNode* parent) { | 226 const BookmarkNode* parent) { |
| 219 const BookmarkNode* v_parent = NULL; | 227 const BookmarkNode* v_parent = NULL; |
| 220 FindNodeInVerifier(model, parent, &v_parent); | 228 FindNodeInVerifier(model, parent, &v_parent); |
| 221 model->SortChildren(parent); | 229 model->SortChildren(parent); |
| 222 verifier_->SortChildren(v_parent); | 230 verifier_->SortChildren(v_parent); |
| 223 } | 231 } |
| 224 | 232 |
| 225 void BookmarkModelVerifier::ReverseChildOrder(BookmarkModel* model, | 233 void BookmarkModelVerifier::ReverseChildOrder(BookmarkModel* model, |
| 226 const BookmarkNode* parent) { | 234 const BookmarkNode* parent) { |
| 227 int child_count = parent->GetChildCount(); | 235 int child_count = parent->GetChildCount(); |
| 228 if (child_count <= 0) return; | 236 if (child_count <= 0) |
| 229 for (int index = 0; index < child_count; index++) { | 237 return; |
| 238 for (int index = 0; index < child_count; index++) |
| 230 Move(model, parent->GetChild(index), parent, child_count-index); | 239 Move(model, parent->GetChild(index), parent, child_count-index); |
| 231 } | |
| 232 } | 240 } |
| 233 | 241 |
| 234 const BookmarkNode* BookmarkModelVerifier::SetURL(BookmarkModel* model, | 242 const BookmarkNode* BookmarkModelVerifier::SetURL(BookmarkModel* model, |
| 235 const BookmarkNode* node, | 243 const BookmarkNode* node, |
| 236 const GURL& new_url) { | 244 const GURL& new_url) { |
| 237 const BookmarkNode* v_node = NULL; | 245 const BookmarkNode* v_node = NULL; |
| 238 FindNodeInVerifier(model, node, &v_node); | 246 FindNodeInVerifier(model, node, &v_node); |
| 239 const BookmarkNode* result = bookmark_utils::ApplyEditsWithNoGroupChange( | 247 const BookmarkNode* result = bookmark_utils::ApplyEditsWithNoGroupChange( |
| 240 model, node->GetParent(), BookmarkEditor::EditDetails(node), | 248 model, node->GetParent(), BookmarkEditor::EditDetails(node), |
| 241 node->GetTitle(), new_url, NULL); | 249 node->GetTitle(), new_url, NULL); |
| 242 bookmark_utils::ApplyEditsWithNoGroupChange(verifier_, v_node->GetParent(), | 250 bookmark_utils::ApplyEditsWithNoGroupChange(verifier_, v_node->GetParent(), |
| 243 BookmarkEditor::EditDetails(v_node), v_node->GetTitle(), new_url, NULL); | 251 BookmarkEditor::EditDetails(v_node), v_node->GetTitle(), new_url, NULL); |
| 244 return result; | 252 return result; |
| 245 } | 253 } |
| 246 | 254 |
| 247 #endif // defined(BROWSER_SYNC) | 255 #endif // defined(BROWSER_SYNC) |
| OLD | NEW |