| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/ntp_snippets/ntp_snippets_database.h" | 5 #include "components/ntp_snippets/ntp_snippets_database.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 | 76 |
| 77 db_.reset(new NTPSnippetsDatabase(database_dir_.path(), | 77 db_.reset(new NTPSnippetsDatabase(database_dir_.path(), |
| 78 base::ThreadTaskRunnerHandle::Get())); | 78 base::ThreadTaskRunnerHandle::Get())); |
| 79 } | 79 } |
| 80 | 80 |
| 81 NTPSnippetsDatabase* db() { return db_.get(); } | 81 NTPSnippetsDatabase* db() { return db_.get(); } |
| 82 | 82 |
| 83 void OnSnippetsLoaded(NTPSnippet::PtrVector snippets) { | 83 void OnSnippetsLoaded(NTPSnippet::PtrVector snippets) { |
| 84 OnSnippetsLoadedImpl(snippets); | 84 OnSnippetsLoadedImpl(snippets); |
| 85 } | 85 } |
| 86 | |
| 87 MOCK_METHOD1(OnSnippetsLoadedImpl, | 86 MOCK_METHOD1(OnSnippetsLoadedImpl, |
| 88 void(const NTPSnippet::PtrVector& snippets)); | 87 void(const NTPSnippet::PtrVector& snippets)); |
| 89 | 88 |
| 89 MOCK_METHOD1(OnImageLoaded, void(std::string)); |
| 90 |
| 90 private: | 91 private: |
| 91 base::MessageLoop message_loop_; | 92 base::MessageLoop message_loop_; |
| 92 base::ScopedTempDir database_dir_; | 93 base::ScopedTempDir database_dir_; |
| 93 std::unique_ptr<NTPSnippetsDatabase> db_; | 94 std::unique_ptr<NTPSnippetsDatabase> db_; |
| 94 | 95 |
| 95 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabaseTest); | 96 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabaseTest); |
| 96 }; | 97 }; |
| 97 | 98 |
| 98 TEST_F(NTPSnippetsDatabaseTest, Init) { | 99 TEST_F(NTPSnippetsDatabaseTest, Init) { |
| 99 ASSERT_FALSE(db()); | 100 ASSERT_FALSE(db()); |
| 100 | 101 |
| 101 CreateDatabase(); | 102 CreateDatabase(); |
| 102 EXPECT_FALSE(db()->IsInitialized()); | 103 EXPECT_FALSE(db()->IsInitialized()); |
| 103 | 104 |
| 104 base::RunLoop().RunUntilIdle(); | 105 base::RunLoop().RunUntilIdle(); |
| 105 EXPECT_TRUE(db()->IsInitialized()); | 106 EXPECT_TRUE(db()->IsInitialized()); |
| 106 } | 107 } |
| 107 | 108 |
| 108 TEST_F(NTPSnippetsDatabaseTest, LoadBeforeInit) { | 109 TEST_F(NTPSnippetsDatabaseTest, LoadBeforeInit) { |
| 109 CreateDatabase(); | 110 CreateDatabase(); |
| 110 EXPECT_FALSE(db()->IsInitialized()); | 111 EXPECT_FALSE(db()->IsInitialized()); |
| 111 | 112 |
| 113 // Start a snippet and image load before the DB is initialized. |
| 112 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | 114 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 113 base::Unretained(this))); | 115 base::Unretained(this))); |
| 116 db()->LoadImage("id", base::Bind(&NTPSnippetsDatabaseTest::OnImageLoaded, |
| 117 base::Unretained(this))); |
| 114 | 118 |
| 119 // They should be serviced once initialization finishes. |
| 115 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)); | 120 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)); |
| 121 EXPECT_CALL(*this, OnImageLoaded(_)); |
| 116 base::RunLoop().RunUntilIdle(); | 122 base::RunLoop().RunUntilIdle(); |
| 117 EXPECT_TRUE(db()->IsInitialized()); | 123 EXPECT_TRUE(db()->IsInitialized()); |
| 118 } | 124 } |
| 119 | 125 |
| 120 TEST_F(NTPSnippetsDatabaseTest, LoadAfterInit) { | 126 TEST_F(NTPSnippetsDatabaseTest, LoadAfterInit) { |
| 121 CreateDatabase(); | 127 CreateDatabase(); |
| 122 EXPECT_FALSE(db()->IsInitialized()); | 128 EXPECT_FALSE(db()->IsInitialized()); |
| 123 | 129 |
| 124 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)).Times(0); | 130 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)).Times(0); |
| 125 base::RunLoop().RunUntilIdle(); | 131 base::RunLoop().RunUntilIdle(); |
| 126 EXPECT_TRUE(db()->IsInitialized()); | 132 EXPECT_TRUE(db()->IsInitialized()); |
| 127 | 133 |
| 128 Mock::VerifyAndClearExpectations(this); | 134 Mock::VerifyAndClearExpectations(this); |
| 129 | 135 |
| 136 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)); |
| 130 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | 137 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 131 base::Unretained(this))); | 138 base::Unretained(this))); |
| 132 | 139 EXPECT_CALL(*this, OnImageLoaded(_)); |
| 133 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)); | 140 db()->LoadImage("id", base::Bind(&NTPSnippetsDatabaseTest::OnImageLoaded, |
| 141 base::Unretained(this))); |
| 134 base::RunLoop().RunUntilIdle(); | 142 base::RunLoop().RunUntilIdle(); |
| 135 } | 143 } |
| 136 | 144 |
| 137 TEST_F(NTPSnippetsDatabaseTest, Save) { | 145 TEST_F(NTPSnippetsDatabaseTest, Save) { |
| 138 CreateDatabase(); | 146 CreateDatabase(); |
| 139 base::RunLoop().RunUntilIdle(); | 147 base::RunLoop().RunUntilIdle(); |
| 140 ASSERT_TRUE(db()->IsInitialized()); | 148 ASSERT_TRUE(db()->IsInitialized()); |
| 141 | 149 |
| 142 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); | 150 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); |
| 151 std::string image_data("pretty image"); |
| 143 | 152 |
| 153 // Store a snippet and an image. |
| 144 db()->SaveSnippet(*snippet); | 154 db()->SaveSnippet(*snippet); |
| 145 base::RunLoop().RunUntilIdle(); | 155 db()->SaveImage(snippet->id(), image_data); |
| 146 | 156 |
| 157 // Make sure they're there. |
| 158 EXPECT_CALL(*this, |
| 159 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); |
| 147 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | 160 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 148 base::Unretained(this))); | 161 base::Unretained(this))); |
| 149 | |
| 150 EXPECT_CALL(*this, | |
| 151 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); | |
| 152 base::RunLoop().RunUntilIdle(); | 162 base::RunLoop().RunUntilIdle(); |
| 153 | 163 |
| 154 Mock::VerifyAndClearExpectations(this); | 164 Mock::VerifyAndClearExpectations(this); |
| 155 | 165 |
| 156 // The snippet should still exist after recreating the database. | 166 EXPECT_CALL(*this, OnImageLoaded(image_data)); |
| 167 db()->LoadImage(snippet->id(), |
| 168 base::Bind(&NTPSnippetsDatabaseTest::OnImageLoaded, |
| 169 base::Unretained(this))); |
| 170 base::RunLoop().RunUntilIdle(); |
| 171 } |
| 172 |
| 173 TEST_F(NTPSnippetsDatabaseTest, SavePersist) { |
| 157 CreateDatabase(); | 174 CreateDatabase(); |
| 175 base::RunLoop().RunUntilIdle(); |
| 176 ASSERT_TRUE(db()->IsInitialized()); |
| 158 | 177 |
| 159 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | 178 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); |
| 160 base::Unretained(this))); | 179 std::string image_data("pretty image"); |
| 180 |
| 181 // Store a snippet and an image. |
| 182 db()->SaveSnippet(*snippet); |
| 183 db()->SaveImage(snippet->id(), image_data); |
| 184 base::RunLoop().RunUntilIdle(); |
| 185 |
| 186 // They should still exist after recreating the database. |
| 187 CreateDatabase(); |
| 161 | 188 |
| 162 EXPECT_CALL(*this, | 189 EXPECT_CALL(*this, |
| 163 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); | 190 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); |
| 191 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 192 base::Unretained(this))); |
| 193 EXPECT_CALL(*this, OnImageLoaded(image_data)); |
| 194 db()->LoadImage(snippet->id(), |
| 195 base::Bind(&NTPSnippetsDatabaseTest::OnImageLoaded, |
| 196 base::Unretained(this))); |
| 164 base::RunLoop().RunUntilIdle(); | 197 base::RunLoop().RunUntilIdle(); |
| 165 } | 198 } |
| 166 | 199 |
| 167 TEST_F(NTPSnippetsDatabaseTest, Update) { | 200 TEST_F(NTPSnippetsDatabaseTest, Update) { |
| 168 CreateDatabase(); | 201 CreateDatabase(); |
| 169 base::RunLoop().RunUntilIdle(); | 202 base::RunLoop().RunUntilIdle(); |
| 170 ASSERT_TRUE(db()->IsInitialized()); | 203 ASSERT_TRUE(db()->IsInitialized()); |
| 171 | 204 |
| 172 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); | 205 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); |
| 173 | 206 |
| 207 // Store a snippet. |
| 174 db()->SaveSnippet(*snippet); | 208 db()->SaveSnippet(*snippet); |
| 175 base::RunLoop().RunUntilIdle(); | |
| 176 | 209 |
| 210 // Change it. |
| 177 const std::string text("some text"); | 211 const std::string text("some text"); |
| 178 snippet->set_snippet(text); | 212 snippet->set_snippet(text); |
| 213 db()->SaveSnippet(*snippet); |
| 179 | 214 |
| 180 db()->SaveSnippet(*snippet); | 215 // Make sure we get the updated version. |
| 181 base::RunLoop().RunUntilIdle(); | 216 EXPECT_CALL(*this, |
| 182 | 217 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); |
| 183 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | 218 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 184 base::Unretained(this))); | 219 base::Unretained(this))); |
| 185 | |
| 186 EXPECT_CALL(*this, | |
| 187 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); | |
| 188 base::RunLoop().RunUntilIdle(); | 220 base::RunLoop().RunUntilIdle(); |
| 189 } | 221 } |
| 190 | 222 |
| 191 TEST_F(NTPSnippetsDatabaseTest, Delete) { | 223 TEST_F(NTPSnippetsDatabaseTest, Delete) { |
| 192 CreateDatabase(); | 224 CreateDatabase(); |
| 193 base::RunLoop().RunUntilIdle(); | 225 base::RunLoop().RunUntilIdle(); |
| 194 ASSERT_TRUE(db()->IsInitialized()); | 226 ASSERT_TRUE(db()->IsInitialized()); |
| 195 | 227 |
| 196 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); | 228 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); |
| 197 | 229 |
| 230 // Store a snippet. |
| 198 db()->SaveSnippet(*snippet); | 231 db()->SaveSnippet(*snippet); |
| 232 |
| 233 // Make sure it's there. |
| 234 EXPECT_CALL(*this, |
| 235 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); |
| 236 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 237 base::Unretained(this))); |
| 199 base::RunLoop().RunUntilIdle(); | 238 base::RunLoop().RunUntilIdle(); |
| 200 | 239 |
| 240 Mock::VerifyAndClearExpectations(this); |
| 241 |
| 242 // Delete the snippet. |
| 201 db()->DeleteSnippet(snippet->id()); | 243 db()->DeleteSnippet(snippet->id()); |
| 202 base::RunLoop().RunUntilIdle(); | |
| 203 | 244 |
| 245 // Make sure it's gone. |
| 246 EXPECT_CALL(*this, OnSnippetsLoadedImpl(IsEmpty())); |
| 204 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | 247 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 205 base::Unretained(this))); | 248 base::Unretained(this))); |
| 206 | |
| 207 EXPECT_CALL(*this, OnSnippetsLoadedImpl(IsEmpty())); | |
| 208 base::RunLoop().RunUntilIdle(); | 249 base::RunLoop().RunUntilIdle(); |
| 209 } | 250 } |
| 210 | 251 |
| 252 TEST_F(NTPSnippetsDatabaseTest, DeleteSnippetAlsoDeletesImage) { |
| 253 CreateDatabase(); |
| 254 base::RunLoop().RunUntilIdle(); |
| 255 ASSERT_TRUE(db()->IsInitialized()); |
| 256 |
| 257 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); |
| 258 std::string image_data("pretty image"); |
| 259 |
| 260 // Store a snippet and image. |
| 261 db()->SaveSnippet(*snippet); |
| 262 db()->SaveImage(snippet->id(), image_data); |
| 263 base::RunLoop().RunUntilIdle(); |
| 264 |
| 265 // Make sure they're there. |
| 266 EXPECT_CALL(*this, |
| 267 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); |
| 268 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 269 base::Unretained(this))); |
| 270 EXPECT_CALL(*this, OnImageLoaded(image_data)); |
| 271 db()->LoadImage(snippet->id(), |
| 272 base::Bind(&NTPSnippetsDatabaseTest::OnImageLoaded, |
| 273 base::Unretained(this))); |
| 274 base::RunLoop().RunUntilIdle(); |
| 275 |
| 276 Mock::VerifyAndClearExpectations(this); |
| 277 |
| 278 // Delete the snippet. |
| 279 db()->DeleteSnippet(snippet->id()); |
| 280 |
| 281 // Make sure the image is gone. |
| 282 EXPECT_CALL(*this, OnImageLoaded(std::string())); |
| 283 db()->LoadImage(snippet->id(), |
| 284 base::Bind(&NTPSnippetsDatabaseTest::OnImageLoaded, |
| 285 base::Unretained(this))); |
| 286 base::RunLoop().RunUntilIdle(); |
| 287 } |
| 288 |
| 211 } // namespace ntp_snippets | 289 } // namespace ntp_snippets |
| OLD | NEW |