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 <utility> | 8 #include <utility> |
8 | 9 |
9 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
11 #include "base/memory/scoped_ptr.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 "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread.h" |
15 #include "extensions/browser/value_store/test_value_store_factory.h" | 15 #include "extensions/browser/value_store/test_value_store_factory.h" |
16 #include "extensions/common/extension_paths.h" | 16 #include "extensions/common/extension_paths.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 using content::BrowserThread; | 19 using content::BrowserThread; |
20 | 20 |
21 class ValueStoreFrontendTest : public testing::Test { | 21 class ValueStoreFrontendTest : public testing::Test { |
(...skipping 21 matching lines...) Expand all Loading... |
43 base::MessageLoop::current()->RunUntilIdle(); // wait for storage to delete | 43 base::MessageLoop::current()->RunUntilIdle(); // wait for storage to delete |
44 storage_.reset(); | 44 storage_.reset(); |
45 } | 45 } |
46 | 46 |
47 // Reset the value store, reloading the DB from disk. | 47 // Reset the value store, reloading the DB from disk. |
48 void ResetStorage() { | 48 void ResetStorage() { |
49 storage_.reset(new ValueStoreFrontend( | 49 storage_.reset(new ValueStoreFrontend( |
50 factory_, ValueStoreFrontend::BackendType::RULES)); | 50 factory_, ValueStoreFrontend::BackendType::RULES)); |
51 } | 51 } |
52 | 52 |
53 bool Get(const std::string& key, scoped_ptr<base::Value>* output) { | 53 bool Get(const std::string& key, std::unique_ptr<base::Value>* output) { |
54 storage_->Get(key, base::Bind(&ValueStoreFrontendTest::GetAndWait, | 54 storage_->Get(key, base::Bind(&ValueStoreFrontendTest::GetAndWait, |
55 base::Unretained(this), output)); | 55 base::Unretained(this), output)); |
56 base::MessageLoop::current()->Run(); // wait for GetAndWait | 56 base::MessageLoop::current()->Run(); // wait for GetAndWait |
57 return !!output->get(); | 57 return !!output->get(); |
58 } | 58 } |
59 | 59 |
60 protected: | 60 protected: |
61 void GetAndWait(scoped_ptr<base::Value>* output, | 61 void GetAndWait(std::unique_ptr<base::Value>* output, |
62 scoped_ptr<base::Value> result) { | 62 std::unique_ptr<base::Value> result) { |
63 *output = std::move(result); | 63 *output = std::move(result); |
64 base::MessageLoop::current()->QuitWhenIdle(); | 64 base::MessageLoop::current()->QuitWhenIdle(); |
65 } | 65 } |
66 | 66 |
67 scoped_refptr<extensions::TestValueStoreFactory> factory_; | 67 scoped_refptr<extensions::TestValueStoreFactory> factory_; |
68 scoped_ptr<ValueStoreFrontend> storage_; | 68 std::unique_ptr<ValueStoreFrontend> storage_; |
69 base::ScopedTempDir temp_dir_; | 69 base::ScopedTempDir temp_dir_; |
70 base::FilePath db_path_; | 70 base::FilePath db_path_; |
71 base::MessageLoop message_loop_; | 71 base::MessageLoop message_loop_; |
72 content::TestBrowserThread ui_thread_; | 72 content::TestBrowserThread ui_thread_; |
73 content::TestBrowserThread file_thread_; | 73 content::TestBrowserThread file_thread_; |
74 }; | 74 }; |
75 | 75 |
76 TEST_F(ValueStoreFrontendTest, GetExistingData) { | 76 TEST_F(ValueStoreFrontendTest, GetExistingData) { |
77 scoped_ptr<base::Value> value; | 77 std::unique_ptr<base::Value> value; |
78 ASSERT_FALSE(Get("key0", &value)); | 78 ASSERT_FALSE(Get("key0", &value)); |
79 | 79 |
80 // Test existing keys in the DB. | 80 // Test existing keys in the DB. |
81 { | 81 { |
82 ASSERT_TRUE(Get("key1", &value)); | 82 ASSERT_TRUE(Get("key1", &value)); |
83 std::string result; | 83 std::string result; |
84 ASSERT_TRUE(value->GetAsString(&result)); | 84 ASSERT_TRUE(value->GetAsString(&result)); |
85 EXPECT_EQ("value1", result); | 85 EXPECT_EQ("value1", result); |
86 } | 86 } |
87 | 87 |
88 { | 88 { |
89 ASSERT_TRUE(Get("key2", &value)); | 89 ASSERT_TRUE(Get("key2", &value)); |
90 int result; | 90 int result; |
91 ASSERT_TRUE(value->GetAsInteger(&result)); | 91 ASSERT_TRUE(value->GetAsInteger(&result)); |
92 EXPECT_EQ(2, result); | 92 EXPECT_EQ(2, result); |
93 } | 93 } |
94 } | 94 } |
95 | 95 |
96 TEST_F(ValueStoreFrontendTest, ChangesPersistAfterReload) { | 96 TEST_F(ValueStoreFrontendTest, ChangesPersistAfterReload) { |
97 storage_->Set("key0", scoped_ptr<base::Value>(new base::FundamentalValue(0))); | 97 storage_->Set("key0", |
98 storage_->Set("key1", scoped_ptr<base::Value>(new base::StringValue("new1"))); | 98 std::unique_ptr<base::Value>(new base::FundamentalValue(0))); |
| 99 storage_->Set("key1", |
| 100 std::unique_ptr<base::Value>(new base::StringValue("new1"))); |
99 storage_->Remove("key2"); | 101 storage_->Remove("key2"); |
100 | 102 |
101 // Reload the DB and test our changes. | 103 // Reload the DB and test our changes. |
102 ResetStorage(); | 104 ResetStorage(); |
103 | 105 |
104 scoped_ptr<base::Value> value; | 106 std::unique_ptr<base::Value> value; |
105 { | 107 { |
106 ASSERT_TRUE(Get("key0", &value)); | 108 ASSERT_TRUE(Get("key0", &value)); |
107 int result; | 109 int result; |
108 ASSERT_TRUE(value->GetAsInteger(&result)); | 110 ASSERT_TRUE(value->GetAsInteger(&result)); |
109 EXPECT_EQ(0, result); | 111 EXPECT_EQ(0, result); |
110 } | 112 } |
111 | 113 |
112 { | 114 { |
113 ASSERT_TRUE(Get("key1", &value)); | 115 ASSERT_TRUE(Get("key1", &value)); |
114 std::string result; | 116 std::string result; |
115 ASSERT_TRUE(value->GetAsString(&result)); | 117 ASSERT_TRUE(value->GetAsString(&result)); |
116 EXPECT_EQ("new1", result); | 118 EXPECT_EQ("new1", result); |
117 } | 119 } |
118 | 120 |
119 ASSERT_FALSE(Get("key2", &value)); | 121 ASSERT_FALSE(Get("key2", &value)); |
120 } | 122 } |
OLD | NEW |