| 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 #include "base/scoped_ptr.h" | 5 #include "base/scoped_ptr.h" |
| 6 #include "base/string_util.h" | 6 #include "base/string_util.h" |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/bookmarks/bookmark_codec.h" | 8 #include "chrome/browser/bookmarks/bookmark_codec.h" |
| 9 #include "chrome/browser/bookmarks/bookmark_model.h" | 9 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 10 #include "chrome/browser/bookmarks/bookmark_model_test_utils.h" | 10 #include "chrome/browser/bookmarks/bookmark_model_test_utils.h" |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 138 |
| 139 // The two checksums should be equal if expected_changes is true; otherwise | 139 // The two checksums should be equal if expected_changes is true; otherwise |
| 140 // they should be different. | 140 // they should be different. |
| 141 if (expected_changes) | 141 if (expected_changes) |
| 142 EXPECT_NE(*computed_checksum, stored_checksum); | 142 EXPECT_NE(*computed_checksum, stored_checksum); |
| 143 else | 143 else |
| 144 EXPECT_EQ(*computed_checksum, stored_checksum); | 144 EXPECT_EQ(*computed_checksum, stored_checksum); |
| 145 | 145 |
| 146 return model.release(); | 146 return model.release(); |
| 147 } | 147 } |
| 148 |
| 149 void CheckIDs(const BookmarkNode* node, std::set<int64>* assigned_ids) { |
| 150 DCHECK(node); |
| 151 int64 node_id = node->id(); |
| 152 EXPECT_TRUE(assigned_ids->find(node_id) == assigned_ids->end()); |
| 153 assigned_ids->insert(node_id); |
| 154 for (int i = 0; i < node->GetChildCount(); ++i) |
| 155 CheckIDs(node->GetChild(i), assigned_ids); |
| 156 } |
| 157 |
| 158 void ExpectIDsUnique(BookmarkModel* model) { |
| 159 std::set<int64> assigned_ids; |
| 160 CheckIDs(model->GetBookmarkBarNode(), &assigned_ids); |
| 161 CheckIDs(model->other_node(), &assigned_ids); |
| 162 } |
| 148 }; | 163 }; |
| 149 | 164 |
| 150 TEST_F(BookmarkCodecTest, ChecksumEncodeDecodeTest) { | 165 TEST_F(BookmarkCodecTest, ChecksumEncodeDecodeTest) { |
| 151 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1()); | 166 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel1()); |
| 152 std::string enc_checksum; | 167 std::string enc_checksum; |
| 153 scoped_ptr<Value> value(EncodeHelper(model_to_encode.get(), &enc_checksum)); | 168 scoped_ptr<Value> value(EncodeHelper(model_to_encode.get(), &enc_checksum)); |
| 154 | 169 |
| 155 EXPECT_TRUE(value.get() != NULL); | 170 EXPECT_TRUE(value.get() != NULL); |
| 156 | 171 |
| 157 std::string dec_checksum; | 172 std::string dec_checksum; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 std::string dec_checksum; | 207 std::string dec_checksum; |
| 193 scoped_ptr<BookmarkModel> decoded_model1(DecodeHelper( | 208 scoped_ptr<BookmarkModel> decoded_model1(DecodeHelper( |
| 194 *value.get(), enc_checksum, &dec_checksum, true)); | 209 *value.get(), enc_checksum, &dec_checksum, true)); |
| 195 | 210 |
| 196 // Undo the change and make sure the checksum is same as original. | 211 // Undo the change and make sure the checksum is same as original. |
| 197 ASSERT_TRUE(child1_value->SetString(BookmarkCodec::kNameKey, title)); | 212 ASSERT_TRUE(child1_value->SetString(BookmarkCodec::kNameKey, title)); |
| 198 scoped_ptr<BookmarkModel> decoded_model2(DecodeHelper( | 213 scoped_ptr<BookmarkModel> decoded_model2(DecodeHelper( |
| 199 *value.get(), enc_checksum, &dec_checksum, false)); | 214 *value.get(), enc_checksum, &dec_checksum, false)); |
| 200 } | 215 } |
| 201 | 216 |
| 217 TEST_F(BookmarkCodecTest, ChecksumManualEditIDsTest) { |
| 218 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel3()); |
| 219 |
| 220 // The test depends on existence of multiple children under bookmark bar, so |
| 221 // make sure that's the case. |
| 222 int bb_child_count = model_to_encode->GetBookmarkBarNode()->GetChildCount(); |
| 223 ASSERT_GT(bb_child_count, 1); |
| 224 |
| 225 std::string enc_checksum; |
| 226 scoped_ptr<Value> value(EncodeHelper(model_to_encode.get(), &enc_checksum)); |
| 227 |
| 228 EXPECT_TRUE(value.get() != NULL); |
| 229 |
| 230 // Change IDs for all children of bookmark bar to be 1. |
| 231 DictionaryValue* child_value; |
| 232 for (int i = 0; i < bb_child_count; ++i) { |
| 233 GetBookmarksBarChildValue(value.get(), i, &child_value); |
| 234 std::string id; |
| 235 ASSERT_TRUE(child_value->GetString(BookmarkCodec::kIdKey, &id)); |
| 236 ASSERT_TRUE(child_value->SetString(BookmarkCodec::kIdKey, "1")); |
| 237 } |
| 238 |
| 239 std::string dec_checksum; |
| 240 scoped_ptr<BookmarkModel> decoded_model(DecodeHelper( |
| 241 *value.get(), enc_checksum, &dec_checksum, true)); |
| 242 |
| 243 ExpectIDsUnique(decoded_model.get()); |
| 244 |
| 245 // add a few extra nodes to bookmark model and make sure IDs are still uniuqe. |
| 246 const BookmarkNode* bb_node = decoded_model->GetBookmarkBarNode(); |
| 247 decoded_model->AddURL(bb_node, 0, L"new url1", GURL(L"http://newurl1.com")); |
| 248 decoded_model->AddURL(bb_node, 0, L"new url2", GURL(L"http://newurl2.com")); |
| 249 |
| 250 ExpectIDsUnique(decoded_model.get()); |
| 251 } |
| 252 |
| 202 TEST_F(BookmarkCodecTest, PersistIDsTest) { | 253 TEST_F(BookmarkCodecTest, PersistIDsTest) { |
| 203 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel3()); | 254 scoped_ptr<BookmarkModel> model_to_encode(CreateTestModel3()); |
| 204 BookmarkCodec encoder; | 255 BookmarkCodec encoder; |
| 205 scoped_ptr<Value> model_value(encoder.Encode(model_to_encode.get())); | 256 scoped_ptr<Value> model_value(encoder.Encode(model_to_encode.get())); |
| 206 | 257 |
| 207 BookmarkModel decoded_model(NULL); | 258 BookmarkModel decoded_model(NULL); |
| 208 BookmarkCodec decoder; | 259 BookmarkCodec decoder; |
| 209 ASSERT_TRUE(Decode(&decoder, &decoded_model, *model_value.get())); | 260 ASSERT_TRUE(Decode(&decoder, &decoded_model, *model_value.get())); |
| 210 BookmarkModelTestUtils::AssertModelsEqual(model_to_encode.get(), | 261 BookmarkModelTestUtils::AssertModelsEqual(model_to_encode.get(), |
| 211 &decoded_model, | 262 &decoded_model, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 223 BookmarkCodec encoder2; | 274 BookmarkCodec encoder2; |
| 224 scoped_ptr<Value> model_value2(encoder2.Encode(&decoded_model)); | 275 scoped_ptr<Value> model_value2(encoder2.Encode(&decoded_model)); |
| 225 | 276 |
| 226 BookmarkModel decoded_model2(NULL); | 277 BookmarkModel decoded_model2(NULL); |
| 227 BookmarkCodec decoder2; | 278 BookmarkCodec decoder2; |
| 228 ASSERT_TRUE(Decode(&decoder2, &decoded_model2, *model_value2.get())); | 279 ASSERT_TRUE(Decode(&decoder2, &decoded_model2, *model_value2.get())); |
| 229 BookmarkModelTestUtils::AssertModelsEqual(&decoded_model, | 280 BookmarkModelTestUtils::AssertModelsEqual(&decoded_model, |
| 230 &decoded_model2, | 281 &decoded_model2, |
| 231 true); | 282 true); |
| 232 } | 283 } |
| 233 | |
| 234 class UniqueIDGeneratorTest : public testing::Test { | |
| 235 protected: | |
| 236 void TestMixed(UniqueIDGenerator* gen) { | |
| 237 // Few unique numbers. | |
| 238 for (int64 i = 1; i <= 5; ++i) { | |
| 239 EXPECT_EQ(i, gen->GetUniqueID(i)); | |
| 240 } | |
| 241 | |
| 242 // All numbers from 1 to 5 should produce numbers 6 to 10. | |
| 243 for (int64 i = 1; i <= 5; ++i) { | |
| 244 EXPECT_EQ(5 + i, gen->GetUniqueID(i)); | |
| 245 } | |
| 246 | |
| 247 // 10 should produce 11, then 11 should produce 12, and so on. | |
| 248 for (int64 i = 1; i <= 5; ++i) { | |
| 249 EXPECT_EQ(10 + i, gen->GetUniqueID(9 + i)); | |
| 250 } | |
| 251 | |
| 252 // Any numbers between 1 and 15 should produce a new numbers in sequence. | |
| 253 EXPECT_EQ(16, gen->GetUniqueID(10)); | |
| 254 EXPECT_EQ(17, gen->GetUniqueID(2)); | |
| 255 EXPECT_EQ(18, gen->GetUniqueID(14)); | |
| 256 EXPECT_EQ(19, gen->GetUniqueID(7)); | |
| 257 EXPECT_EQ(20, gen->GetUniqueID(4)); | |
| 258 | |
| 259 // Numbers not yet generated should work. | |
| 260 EXPECT_EQ(100, gen->GetUniqueID(100)); | |
| 261 EXPECT_EQ(21, gen->GetUniqueID(21)); | |
| 262 EXPECT_EQ(200, gen->GetUniqueID(200)); | |
| 263 | |
| 264 // Now any existing number should produce numbers starting from 201. | |
| 265 EXPECT_EQ(201, gen->GetUniqueID(1)); | |
| 266 EXPECT_EQ(202, gen->GetUniqueID(20)); | |
| 267 EXPECT_EQ(203, gen->GetUniqueID(21)); | |
| 268 EXPECT_EQ(204, gen->GetUniqueID(100)); | |
| 269 EXPECT_EQ(205, gen->GetUniqueID(200)); | |
| 270 } | |
| 271 }; | |
| 272 | |
| 273 TEST_F(UniqueIDGeneratorTest, SerialNumbersTest) { | |
| 274 UniqueIDGenerator gen; | |
| 275 for (int64 i = 1; i <= 10; ++i) { | |
| 276 EXPECT_EQ(i, gen.GetUniqueID(i)); | |
| 277 } | |
| 278 } | |
| 279 | |
| 280 TEST_F(UniqueIDGeneratorTest, UniqueSortedNumbersTest) { | |
| 281 UniqueIDGenerator gen; | |
| 282 for (int64 i = 1; i <= 10; i += 2) { | |
| 283 EXPECT_EQ(i, gen.GetUniqueID(i)); | |
| 284 } | |
| 285 } | |
| 286 | |
| 287 TEST_F(UniqueIDGeneratorTest, UniqueUnsortedConsecutiveNumbersTest) { | |
| 288 UniqueIDGenerator gen; | |
| 289 int numbers[] = {2, 10, 6, 3, 8, 5, 1, 7, 4, 9}; | |
| 290 for (int64 i = 0; i < ARRAYSIZE(numbers); ++i) { | |
| 291 EXPECT_EQ(numbers[i], gen.GetUniqueID(numbers[i])); | |
| 292 } | |
| 293 } | |
| 294 | |
| 295 TEST_F(UniqueIDGeneratorTest, UniqueUnsortedNumbersTest) { | |
| 296 UniqueIDGenerator gen; | |
| 297 int numbers[] = {20, 100, 60, 30, 80, 50, 10, 70, 40, 90}; | |
| 298 for (int64 i = 0; i < ARRAYSIZE(numbers); ++i) { | |
| 299 EXPECT_EQ(numbers[i], gen.GetUniqueID(numbers[i])); | |
| 300 } | |
| 301 } | |
| 302 | |
| 303 TEST_F(UniqueIDGeneratorTest, AllDuplicatesTest) { | |
| 304 UniqueIDGenerator gen; | |
| 305 for (int64 i = 1; i <= 10; ++i) { | |
| 306 EXPECT_EQ(i, gen.GetUniqueID(1)); | |
| 307 } | |
| 308 } | |
| 309 | |
| 310 TEST_F(UniqueIDGeneratorTest, MixedTest) { | |
| 311 UniqueIDGenerator gen; | |
| 312 TestMixed(&gen); | |
| 313 } | |
| 314 | |
| 315 TEST_F(UniqueIDGeneratorTest, ResetTest) { | |
| 316 UniqueIDGenerator gen; | |
| 317 for (int64 i = 0; i < 5; ++i) { | |
| 318 TestMixed(&gen); | |
| 319 gen.Reset(); | |
| 320 } | |
| 321 } | |
| OLD | NEW |