Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/dom_distiller/core/dom_distiller_database.h" | 5 #include "components/dom_distiller/core/dom_distiller_database.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/threading/thread.h" | |
| 13 #include "components/dom_distiller/core/article_entry.h" | 14 #include "components/dom_distiller/core/article_entry.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 using base::MessageLoop; | 18 using base::MessageLoop; |
| 18 using base::ScopedTempDir; | 19 using base::ScopedTempDir; |
| 19 using testing::Invoke; | 20 using testing::Invoke; |
| 20 using testing::Return; | 21 using testing::Return; |
| 21 using testing::_; | 22 using testing::_; |
| 22 | 23 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 std::string serialized_actual = actual[i].SerializeAsString(); | 81 std::string serialized_actual = actual[i].SerializeAsString(); |
| 81 EXPECT_EQ(serialized_expected, serialized_actual); | 82 EXPECT_EQ(serialized_expected, serialized_actual); |
| 82 expected.erase(expected_it); | 83 expected.erase(expected_it); |
| 83 } | 84 } |
| 84 } | 85 } |
| 85 | 86 |
| 86 class DomDistillerDatabaseTest : public testing::Test { | 87 class DomDistillerDatabaseTest : public testing::Test { |
| 87 public: | 88 public: |
| 88 virtual void SetUp() { | 89 virtual void SetUp() { |
| 89 main_loop_.reset(new MessageLoop()); | 90 main_loop_.reset(new MessageLoop()); |
| 90 db_ = new DomDistillerDatabase(main_loop_->message_loop_proxy()); | 91 db_.reset(new DomDistillerDatabase(main_loop_->message_loop_proxy())); |
| 91 } | 92 } |
| 92 | 93 |
| 93 virtual void TearDown() { | 94 virtual void TearDown() { |
| 94 DestroyDB(); | 95 db_.reset(); |
| 95 main_loop_.reset(NULL); | 96 base::RunLoop().RunUntilIdle(); |
| 97 main_loop_.reset(); | |
| 96 } | 98 } |
| 97 | 99 |
| 98 void DestroyDB() { | 100 scoped_ptr<DomDistillerDatabase> db_; |
| 99 if (db_) { | |
| 100 db_->Destroy(); | |
| 101 base::RunLoop().RunUntilIdle(); | |
| 102 db_ = NULL; | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 DomDistillerDatabase* db_; | |
| 107 scoped_ptr<MessageLoop> main_loop_; | 101 scoped_ptr<MessageLoop> main_loop_; |
| 108 }; | 102 }; |
| 109 | 103 |
| 110 // Test that DomDistillerDatabase calls Init on the underlying database and that | 104 // Test that DomDistillerDatabase calls Init on the underlying database and that |
| 111 // the caller's InitCallback is called with the correct value. | 105 // the caller's InitCallback is called with the correct value. |
| 112 TEST_F(DomDistillerDatabaseTest, TestDBInitSuccess) { | 106 TEST_F(DomDistillerDatabaseTest, TestDBInitSuccess) { |
| 113 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); | 107 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); |
| 114 | 108 |
| 115 MockDB* mock_db = new MockDB(); | 109 MockDB* mock_db = new MockDB(); |
| 116 EXPECT_CALL(*mock_db, Init(path)).WillOnce(Return(true)); | 110 EXPECT_CALL(*mock_db, Init(path)).WillOnce(Return(true)); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 | 251 |
| 258 EXPECT_CALL(*mock_db, Save(_)).WillOnce(Return(false)); | 252 EXPECT_CALL(*mock_db, Save(_)).WillOnce(Return(false)); |
| 259 EXPECT_CALL(caller, SaveCallback(false)); | 253 EXPECT_CALL(caller, SaveCallback(false)); |
| 260 db_->SaveEntries( | 254 db_->SaveEntries( |
| 261 entries.Pass(), | 255 entries.Pass(), |
| 262 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller))); | 256 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller))); |
| 263 | 257 |
| 264 base::RunLoop().RunUntilIdle(); | 258 base::RunLoop().RunUntilIdle(); |
| 265 } | 259 } |
| 266 | 260 |
| 261 // This tests that normal usage of the real database does not cause any | |
| 262 // threading violations. | |
| 263 TEST(DomDistillerDatabaseThreadingTest, TestDBDestruction) { | |
| 264 base::MessageLoop main_loop; | |
| 265 | |
| 266 ScopedTempDir temp_dir; | |
| 267 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 268 | |
| 269 scoped_ptr<DomDistillerDatabase::LevelDB> level_db( | |
| 270 new DomDistillerDatabase::LevelDB()); | |
|
Nico
2013/11/03 23:29:52
Can this be stack-allocated?
cjhopman
2013/11/04 18:40:59
This isn't even needed :/
| |
| 271 | |
| 272 base::Thread db_thread("dbthread"); | |
| 273 ASSERT_TRUE(db_thread.Start()); | |
| 274 | |
| 275 scoped_ptr<DomDistillerDatabase> db( | |
| 276 new DomDistillerDatabase(db_thread.message_loop_proxy())); | |
| 277 | |
| 278 MockDatabaseCaller caller; | |
| 279 EXPECT_CALL(caller, InitCallback(_)); | |
| 280 db->Init( | |
| 281 temp_dir.path(), | |
| 282 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); | |
| 283 | |
| 284 db.reset(); | |
| 285 | |
| 286 base::RunLoop run_loop; | |
| 287 db_thread.message_loop_proxy()->PostTaskAndReply( | |
| 288 FROM_HERE, base::Bind(base::DoNothing), run_loop.QuitClosure()); | |
| 289 run_loop.Run(); | |
| 290 } | |
| 291 | |
| 267 // Test that the LevelDB properly saves entries and that load returns the saved | 292 // Test that the LevelDB properly saves entries and that load returns the saved |
| 268 // entries. If |close_after_save| is true, the database will be closed after | 293 // entries. If |close_after_save| is true, the database will be closed after |
| 269 // saving and then re-opened to ensure that the data is properly persisted. | 294 // saving and then re-opened to ensure that the data is properly persisted. |
| 270 void TestLevelDBSaveAndLoad(bool close_after_save) { | 295 void TestLevelDBSaveAndLoad(bool close_after_save) { |
| 271 ScopedTempDir temp_dir; | 296 ScopedTempDir temp_dir; |
| 272 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 297 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 273 | 298 |
| 274 EntryMap model = GetSmallModel(); | 299 EntryMap model = GetSmallModel(); |
| 275 EntryVector save_entries; | 300 EntryVector save_entries; |
| 276 EntryVector load_entries; | 301 EntryVector load_entries; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 296 | 321 |
| 297 TEST(DomDistillerDatabaseLevelDBTest, TestDBSaveAndLoad) { | 322 TEST(DomDistillerDatabaseLevelDBTest, TestDBSaveAndLoad) { |
| 298 TestLevelDBSaveAndLoad(false); | 323 TestLevelDBSaveAndLoad(false); |
| 299 } | 324 } |
| 300 | 325 |
| 301 TEST(DomDistillerDatabaseLevelDBTest, TestDBCloseAndReopen) { | 326 TEST(DomDistillerDatabaseLevelDBTest, TestDBCloseAndReopen) { |
| 302 TestLevelDBSaveAndLoad(true); | 327 TestLevelDBSaveAndLoad(true); |
| 303 } | 328 } |
| 304 | 329 |
| 305 } // namespace dom_distiller | 330 } // namespace dom_distiller |
| OLD | NEW |