| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 return OnDiskDirectoryBackingStore::SaveChanges(snapshot); | 78 return OnDiskDirectoryBackingStore::SaveChanges(snapshot); |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 | 81 |
| 82 // A directory whose Save() function can be set to always fail. | 82 // A directory whose Save() function can be set to always fail. |
| 83 class TestDirectory : public Directory { | 83 class TestDirectory : public Directory { |
| 84 public: | 84 public: |
| 85 // A factory function used to work around some initialization order issues. | 85 // A factory function used to work around some initialization order issues. |
| 86 static TestDirectory* Create( | 86 static TestDirectory* Create( |
| 87 Encryptor *encryptor, | 87 Encryptor *encryptor, |
| 88 UnrecoverableErrorHandler *handler, | 88 const WeakHandle<UnrecoverableErrorHandler>& handler, |
| 89 const std::string& dir_name, | 89 const std::string& dir_name, |
| 90 const base::FilePath& backing_filepath); | 90 const base::FilePath& backing_filepath); |
| 91 | 91 |
| 92 ~TestDirectory() override; | 92 ~TestDirectory() override; |
| 93 | 93 |
| 94 void StartFailingSaveChanges() { | 94 void StartFailingSaveChanges() { |
| 95 backing_store_->StartFailingSaveChanges(); | 95 backing_store_->StartFailingSaveChanges(); |
| 96 } | 96 } |
| 97 | 97 |
| 98 private: | 98 private: |
| 99 TestDirectory(Encryptor* encryptor, | 99 TestDirectory(Encryptor* encryptor, |
| 100 UnrecoverableErrorHandler* handler, | 100 const WeakHandle<UnrecoverableErrorHandler>& handler, |
| 101 TestBackingStore* backing_store); | 101 TestBackingStore* backing_store); |
| 102 | 102 |
| 103 TestBackingStore* backing_store_; | 103 TestBackingStore* backing_store_; |
| 104 }; | 104 }; |
| 105 | 105 |
| 106 TestDirectory* TestDirectory::Create( | 106 TestDirectory* TestDirectory::Create( |
| 107 Encryptor *encryptor, | 107 Encryptor *encryptor, |
| 108 UnrecoverableErrorHandler *handler, | 108 const WeakHandle<UnrecoverableErrorHandler>& handler, |
| 109 const std::string& dir_name, | 109 const std::string& dir_name, |
| 110 const base::FilePath& backing_filepath) { | 110 const base::FilePath& backing_filepath) { |
| 111 TestBackingStore* backing_store = | 111 TestBackingStore* backing_store = |
| 112 new TestBackingStore(dir_name, backing_filepath); | 112 new TestBackingStore(dir_name, backing_filepath); |
| 113 return new TestDirectory(encryptor, handler, backing_store); | 113 return new TestDirectory(encryptor, handler, backing_store); |
| 114 } | 114 } |
| 115 | 115 |
| 116 TestDirectory::TestDirectory(Encryptor* encryptor, | 116 TestDirectory::TestDirectory( |
| 117 UnrecoverableErrorHandler* handler, | 117 Encryptor* encryptor, |
| 118 TestBackingStore* backing_store) | 118 const WeakHandle<UnrecoverableErrorHandler>& handler, |
| 119 TestBackingStore* backing_store) |
| 119 : Directory(backing_store, handler, base::Closure(), NULL, NULL), | 120 : Directory(backing_store, handler, base::Closure(), NULL, NULL), |
| 120 backing_store_(backing_store) { | 121 backing_store_(backing_store) {} |
| 121 } | |
| 122 | 122 |
| 123 TestDirectory::~TestDirectory() { } | 123 TestDirectory::~TestDirectory() { } |
| 124 | 124 |
| 125 TEST(OnDiskSyncableDirectory, FailInitialWrite) { | 125 TEST(OnDiskSyncableDirectory, FailInitialWrite) { |
| 126 base::MessageLoop message_loop; | 126 base::MessageLoop message_loop; |
| 127 FakeEncryptor encryptor; | 127 FakeEncryptor encryptor; |
| 128 TestUnrecoverableErrorHandler handler; | 128 TestUnrecoverableErrorHandler handler; |
| 129 base::ScopedTempDir temp_dir; | 129 base::ScopedTempDir temp_dir; |
| 130 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 130 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 131 base::FilePath file_path = temp_dir.path().Append( | 131 base::FilePath file_path = temp_dir.path().Append( |
| 132 FILE_PATH_LITERAL("Test.sqlite3")); | 132 FILE_PATH_LITERAL("Test.sqlite3")); |
| 133 std::string name = "user@x.com"; | 133 std::string name = "user@x.com"; |
| 134 NullDirectoryChangeDelegate delegate; | 134 NullDirectoryChangeDelegate delegate; |
| 135 | 135 |
| 136 scoped_ptr<TestDirectory> test_dir( | 136 scoped_ptr<TestDirectory> test_dir(TestDirectory::Create( |
| 137 TestDirectory::Create(&encryptor, &handler, name, file_path)); | 137 &encryptor, MakeWeakHandle(handler.GetWeakPtr()), name, file_path)); |
| 138 | 138 |
| 139 test_dir->StartFailingSaveChanges(); | 139 test_dir->StartFailingSaveChanges(); |
| 140 ASSERT_EQ(FAILED_INITIAL_WRITE, test_dir->Open(name, &delegate, | 140 ASSERT_EQ(FAILED_INITIAL_WRITE, test_dir->Open(name, &delegate, |
| 141 NullTransactionObserver())); | 141 NullTransactionObserver())); |
| 142 } | 142 } |
| 143 | 143 |
| 144 // A variant of SyncableDirectoryTest that uses a real sqlite database. | 144 // A variant of SyncableDirectoryTest that uses a real sqlite database. |
| 145 class OnDiskSyncableDirectoryTest : public SyncableDirectoryTest { | 145 class OnDiskSyncableDirectoryTest : public SyncableDirectoryTest { |
| 146 protected: | 146 protected: |
| 147 // SetUp() is called before each test case is run. | 147 // SetUp() is called before each test case is run. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 159 // This also closes file handles. | 159 // This also closes file handles. |
| 160 dir()->SaveChanges(); | 160 dir()->SaveChanges(); |
| 161 dir().reset(); | 161 dir().reset(); |
| 162 base::DeleteFile(file_path_, true); | 162 base::DeleteFile(file_path_, true); |
| 163 SyncableDirectoryTest::TearDown(); | 163 SyncableDirectoryTest::TearDown(); |
| 164 } | 164 } |
| 165 | 165 |
| 166 // Creates a new directory. Deletes the old directory, if it exists. | 166 // Creates a new directory. Deletes the old directory, if it exists. |
| 167 void CreateDirectory() { | 167 void CreateDirectory() { |
| 168 test_directory_ = TestDirectory::Create( | 168 test_directory_ = TestDirectory::Create( |
| 169 encryptor(), unrecoverable_error_handler(), kDirectoryName, file_path_); | 169 encryptor(), |
| 170 MakeWeakHandle(unrecoverable_error_handler()->GetWeakPtr()), |
| 171 kDirectoryName, file_path_); |
| 170 dir().reset(test_directory_); | 172 dir().reset(test_directory_); |
| 171 ASSERT_TRUE(dir().get()); | 173 ASSERT_TRUE(dir().get()); |
| 172 ASSERT_EQ(OPENED, | 174 ASSERT_EQ(OPENED, |
| 173 dir()->Open(kDirectoryName, | 175 dir()->Open(kDirectoryName, |
| 174 directory_change_delegate(), | 176 directory_change_delegate(), |
| 175 NullTransactionObserver())); | 177 NullTransactionObserver())); |
| 176 ASSERT_TRUE(dir()->good()); | 178 ASSERT_TRUE(dir()->good()); |
| 177 } | 179 } |
| 178 | 180 |
| 179 void SaveAndReloadDir() { | 181 void SaveAndReloadDir() { |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 specifics.mutable_bookmark()->set_favicon("PNG"); | 339 specifics.mutable_bookmark()->set_favicon("PNG"); |
| 338 specifics.mutable_bookmark()->set_url("http://nowhere"); | 340 specifics.mutable_bookmark()->set_url("http://nowhere"); |
| 339 create.PutSpecifics(specifics); | 341 create.PutSpecifics(specifics); |
| 340 update.PutServerSpecifics(specifics); | 342 update.PutServerSpecifics(specifics); |
| 341 create_pre_save = create.GetKernelCopy(); | 343 create_pre_save = create.GetKernelCopy(); |
| 342 update_pre_save = update.GetKernelCopy(); | 344 update_pre_save = update.GetKernelCopy(); |
| 343 create_id = create.GetId(); | 345 create_id = create.GetId(); |
| 344 } | 346 } |
| 345 | 347 |
| 346 dir()->SaveChanges(); | 348 dir()->SaveChanges(); |
| 347 dir().reset(new Directory( | 349 dir().reset( |
| 348 new OnDiskDirectoryBackingStore(kDirectoryName, file_path_), | 350 new Directory(new OnDiskDirectoryBackingStore(kDirectoryName, file_path_), |
| 349 unrecoverable_error_handler(), base::Closure(), NULL, NULL)); | 351 MakeWeakHandle(unrecoverable_error_handler()->GetWeakPtr()), |
| 352 base::Closure(), NULL, NULL)); |
| 350 | 353 |
| 351 ASSERT_TRUE(dir().get()); | 354 ASSERT_TRUE(dir().get()); |
| 352 ASSERT_EQ(OPENED, | 355 ASSERT_EQ(OPENED, |
| 353 dir()->Open(kDirectoryName, | 356 dir()->Open(kDirectoryName, |
| 354 directory_change_delegate(), | 357 directory_change_delegate(), |
| 355 NullTransactionObserver())); | 358 NullTransactionObserver())); |
| 356 ASSERT_TRUE(dir()->good()); | 359 ASSERT_TRUE(dir()->good()); |
| 357 | 360 |
| 358 { | 361 { |
| 359 ReadTransaction trans(FROM_HERE, dir().get()); | 362 ReadTransaction trans(FROM_HERE, dir().get()); |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 FakeEncryptor encryptor_; | 555 FakeEncryptor encryptor_; |
| 553 TestUnrecoverableErrorHandler handler_; | 556 TestUnrecoverableErrorHandler handler_; |
| 554 NullDirectoryChangeDelegate delegate_; | 557 NullDirectoryChangeDelegate delegate_; |
| 555 }; | 558 }; |
| 556 | 559 |
| 557 TEST_F(SyncableDirectoryManagement, TestFileRelease) { | 560 TEST_F(SyncableDirectoryManagement, TestFileRelease) { |
| 558 base::FilePath path = | 561 base::FilePath path = |
| 559 temp_dir_.path().Append(Directory::kSyncDatabaseFilename); | 562 temp_dir_.path().Append(Directory::kSyncDatabaseFilename); |
| 560 | 563 |
| 561 { | 564 { |
| 562 Directory dir(new OnDiskDirectoryBackingStore("ScopeTest", path), &handler_, | 565 Directory dir(new OnDiskDirectoryBackingStore("ScopeTest", path), |
| 563 base::Closure(), NULL, NULL); | 566 MakeWeakHandle(handler_.GetWeakPtr()), base::Closure(), NULL, |
| 567 NULL); |
| 564 DirOpenResult result = | 568 DirOpenResult result = |
| 565 dir.Open("ScopeTest", &delegate_, NullTransactionObserver()); | 569 dir.Open("ScopeTest", &delegate_, NullTransactionObserver()); |
| 566 ASSERT_EQ(result, OPENED); | 570 ASSERT_EQ(result, OPENED); |
| 567 } | 571 } |
| 568 | 572 |
| 569 // Destroying the directory should have released the backing database file. | 573 // Destroying the directory should have released the backing database file. |
| 570 ASSERT_TRUE(base::DeleteFile(path, true)); | 574 ASSERT_TRUE(base::DeleteFile(path, true)); |
| 571 } | 575 } |
| 572 | 576 |
| 573 class SyncableClientTagTest : public SyncableDirectoryTest { | 577 class SyncableClientTagTest : public SyncableDirectoryTest { |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 TEST_F(SyncableClientTagTest, TestClientTagIndexDuplicateServer) { | 664 TEST_F(SyncableClientTagTest, TestClientTagIndexDuplicateServer) { |
| 661 EXPECT_TRUE(CreateWithDefaultTag(factory_.NewServerId(), true)); | 665 EXPECT_TRUE(CreateWithDefaultTag(factory_.NewServerId(), true)); |
| 662 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), true)); | 666 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), true)); |
| 663 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), false)); | 667 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), false)); |
| 664 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), false)); | 668 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), false)); |
| 665 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), true)); | 669 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), true)); |
| 666 } | 670 } |
| 667 | 671 |
| 668 } // namespace syncable | 672 } // namespace syncable |
| 669 } // namespace syncer | 673 } // namespace syncer |
| OLD | NEW |