| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/value_store/value_store_frontend.h" | 5 #include "extensions/browser/value_store/value_store_frontend.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/run_loop.h" |
| 14 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
| 15 #include "extensions/browser/value_store/test_value_store_factory.h" | 16 #include "extensions/browser/value_store/test_value_store_factory.h" |
| 16 #include "extensions/common/extension_paths.h" | 17 #include "extensions/common/extension_paths.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 using content::BrowserThread; | 20 using content::BrowserThread; |
| 20 | 21 |
| 21 class ValueStoreFrontendTest : public testing::Test { | 22 class ValueStoreFrontendTest : public testing::Test { |
| 22 public: | 23 public: |
| 23 ValueStoreFrontendTest() | 24 ValueStoreFrontendTest() |
| 24 : ui_thread_(BrowserThread::UI, base::MessageLoop::current()), | 25 : ui_thread_(BrowserThread::UI, base::MessageLoop::current()), |
| 25 file_thread_(BrowserThread::FILE, base::MessageLoop::current()) { | 26 file_thread_(BrowserThread::FILE, base::MessageLoop::current()) { |
| 26 } | 27 } |
| 27 | 28 |
| 28 void SetUp() override { | 29 void SetUp() override { |
| 29 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 30 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 30 | 31 |
| 31 base::FilePath test_data_dir; | 32 base::FilePath test_data_dir; |
| 32 ASSERT_TRUE(PathService::Get(extensions::DIR_TEST_DATA, &test_data_dir)); | 33 ASSERT_TRUE(PathService::Get(extensions::DIR_TEST_DATA, &test_data_dir)); |
| 33 base::FilePath src_db(test_data_dir.AppendASCII("value_store_db")); | 34 base::FilePath src_db(test_data_dir.AppendASCII("value_store_db")); |
| 34 db_path_ = temp_dir_.path().AppendASCII("temp_db"); | 35 db_path_ = temp_dir_.path().AppendASCII("temp_db"); |
| 35 base::CopyDirectory(src_db, db_path_, true); | 36 base::CopyDirectory(src_db, db_path_, true); |
| 36 | 37 |
| 37 factory_ = new extensions::TestValueStoreFactory(db_path_); | 38 factory_ = new extensions::TestValueStoreFactory(db_path_); |
| 38 | 39 |
| 39 ResetStorage(); | 40 ResetStorage(); |
| 40 } | 41 } |
| 41 | 42 |
| 42 void TearDown() override { | 43 void TearDown() override { |
| 43 base::MessageLoop::current()->RunUntilIdle(); // wait for storage to delete | 44 base::RunLoop().RunUntilIdle(); // wait for storage to delete |
| 44 storage_.reset(); | 45 storage_.reset(); |
| 45 } | 46 } |
| 46 | 47 |
| 47 // Reset the value store, reloading the DB from disk. | 48 // Reset the value store, reloading the DB from disk. |
| 48 void ResetStorage() { | 49 void ResetStorage() { |
| 49 storage_.reset(new ValueStoreFrontend( | 50 storage_.reset(new ValueStoreFrontend( |
| 50 factory_, ValueStoreFrontend::BackendType::RULES)); | 51 factory_, ValueStoreFrontend::BackendType::RULES)); |
| 51 } | 52 } |
| 52 | 53 |
| 53 bool Get(const std::string& key, std::unique_ptr<base::Value>* output) { | 54 bool Get(const std::string& key, std::unique_ptr<base::Value>* output) { |
| 54 storage_->Get(key, base::Bind(&ValueStoreFrontendTest::GetAndWait, | 55 storage_->Get(key, base::Bind(&ValueStoreFrontendTest::GetAndWait, |
| 55 base::Unretained(this), output)); | 56 base::Unretained(this), output)); |
| 56 base::MessageLoop::current()->Run(); // wait for GetAndWait | 57 base::RunLoop().Run(); // wait for GetAndWait |
| 57 return !!output->get(); | 58 return !!output->get(); |
| 58 } | 59 } |
| 59 | 60 |
| 60 protected: | 61 protected: |
| 61 void GetAndWait(std::unique_ptr<base::Value>* output, | 62 void GetAndWait(std::unique_ptr<base::Value>* output, |
| 62 std::unique_ptr<base::Value> result) { | 63 std::unique_ptr<base::Value> result) { |
| 63 *output = std::move(result); | 64 *output = std::move(result); |
| 64 base::MessageLoop::current()->QuitWhenIdle(); | 65 base::MessageLoop::current()->QuitWhenIdle(); |
| 65 } | 66 } |
| 66 | 67 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 | 114 |
| 114 { | 115 { |
| 115 ASSERT_TRUE(Get("key1", &value)); | 116 ASSERT_TRUE(Get("key1", &value)); |
| 116 std::string result; | 117 std::string result; |
| 117 ASSERT_TRUE(value->GetAsString(&result)); | 118 ASSERT_TRUE(value->GetAsString(&result)); |
| 118 EXPECT_EQ("new1", result); | 119 EXPECT_EQ("new1", result); |
| 119 } | 120 } |
| 120 | 121 |
| 121 ASSERT_FALSE(Get("key2", &value)); | 122 ASSERT_FALSE(Get("key2", &value)); |
| 122 } | 123 } |
| OLD | NEW |