Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(426)

Side by Side Diff: components/ntp_snippets/remote/ntp_snippets_database_unittest.cc

Issue 2386103009: NTPSnippetsService: Garbage collect orphaned images at startup. (Closed)
Patch Set: final comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/remote/ntp_snippets_database.h" 5 #include "components/ntp_snippets/remote/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"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/files/scoped_temp_dir.h" 12 #include "base/files/scoped_temp_dir.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h"
14 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h" 16 #include "base/run_loop.h"
16 #include "base/threading/thread_task_runner_handle.h" 17 #include "base/threading/thread_task_runner_handle.h"
17 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 20
20 using testing::ElementsAre; 21 using testing::ElementsAre;
22 using testing::Eq;
21 using testing::IsEmpty; 23 using testing::IsEmpty;
22 using testing::Mock; 24 using testing::Mock;
23 using testing::_; 25 using testing::_;
24 26
25 namespace ntp_snippets { 27 namespace ntp_snippets {
26 28
27 bool operator==(const SnippetSource& lhs, const SnippetSource& rhs) { 29 bool operator==(const SnippetSource& lhs, const SnippetSource& rhs) {
28 return lhs.url == rhs.url && lhs.publisher_name == rhs.publisher_name && 30 return lhs.url == rhs.url && lhs.publisher_name == rhs.publisher_name &&
29 lhs.amp_url == rhs.amp_url; 31 lhs.amp_url == rhs.amp_url;
30 } 32 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 // Explicitly destroy any existing database first, so it releases the lock 75 // Explicitly destroy any existing database first, so it releases the lock
74 // on the file. 76 // on the file.
75 db_.reset(); 77 db_.reset();
76 78
77 db_.reset(new NTPSnippetsDatabase(database_dir_.GetPath(), 79 db_.reset(new NTPSnippetsDatabase(database_dir_.GetPath(),
78 base::ThreadTaskRunnerHandle::Get())); 80 base::ThreadTaskRunnerHandle::Get()));
79 } 81 }
80 82
81 NTPSnippetsDatabase* db() { return db_.get(); } 83 NTPSnippetsDatabase* db() { return db_.get(); }
82 84
85 // TODO(tschumann): MOCK_METHODS on non mock objects are an anti-pattern.
86 // Clean up.
83 void OnSnippetsLoaded(NTPSnippet::PtrVector snippets) { 87 void OnSnippetsLoaded(NTPSnippet::PtrVector snippets) {
84 OnSnippetsLoadedImpl(snippets); 88 OnSnippetsLoadedImpl(snippets);
85 } 89 }
86 MOCK_METHOD1(OnSnippetsLoadedImpl, 90 MOCK_METHOD1(OnSnippetsLoadedImpl,
87 void(const NTPSnippet::PtrVector& snippets)); 91 void(const NTPSnippet::PtrVector& snippets));
88 92
89 MOCK_METHOD1(OnImageLoaded, void(std::string)); 93 MOCK_METHOD1(OnImageLoaded, void(std::string));
90 94
91 private: 95 private:
92 base::MessageLoop message_loop_; 96 base::MessageLoop message_loop_;
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 db()->DeleteImage(snippet->id()); 315 db()->DeleteImage(snippet->id());
312 316
313 // Make sure the image is gone. 317 // Make sure the image is gone.
314 EXPECT_CALL(*this, OnImageLoaded(std::string())); 318 EXPECT_CALL(*this, OnImageLoaded(std::string()));
315 db()->LoadImage(snippet->id(), 319 db()->LoadImage(snippet->id(),
316 base::Bind(&NTPSnippetsDatabaseTest::OnImageLoaded, 320 base::Bind(&NTPSnippetsDatabaseTest::OnImageLoaded,
317 base::Unretained(this))); 321 base::Unretained(this)));
318 base::RunLoop().RunUntilIdle(); 322 base::RunLoop().RunUntilIdle();
319 } 323 }
320 324
325 namespace {
326
327 void LoadExpectedImage(NTPSnippetsDatabase* db,
328 const std::string& id,
329 const std::string& expected_data) {
330 base::RunLoop run_loop;
331 db->LoadImage(id, base::Bind(
332 [](base::Closure signal, std::string expected_data,
333 std::string actual_data) {
334 EXPECT_THAT(actual_data, Eq(expected_data));
335 signal.Run();
336 },
337 run_loop.QuitClosure(), expected_data));
338 run_loop.Run();
339 }
340
341 } // namespace
342
343 TEST_F(NTPSnippetsDatabaseTest, ShouldGarbageCollectImages) {
344 CreateDatabase();
345 base::RunLoop().RunUntilIdle();
346 ASSERT_TRUE(db()->IsInitialized());
347
348 // Store images.
349 db()->SaveImage("snippet-id-1", "pretty-image-1");
350 db()->SaveImage("snippet-id-2", "pretty-image-2");
351 db()->SaveImage("snippet-id-3", "pretty-image-3");
352 base::RunLoop().RunUntilIdle();
353
354 // Make sure the to-be-garbage collected images are there.
355 LoadExpectedImage(db(), "snippet-id-1", "pretty-image-1");
356 LoadExpectedImage(db(), "snippet-id-3", "pretty-image-3");
357
358 // Garbage collect all except the second.
359 db()->GarbageCollectImages(base::MakeUnique<std::set<std::string>>(
360 std::set<std::string>({"snippet-id-2"})));
361 base::RunLoop().RunUntilIdle();
362
363 // Make sure the images are gone.
364 LoadExpectedImage(db(), "snippet-id-1", "");
365 LoadExpectedImage(db(), "snippet-id-3", "");
366 // Make sure the second still exists.
367 LoadExpectedImage(db(), "snippet-id-2", "pretty-image-2");
368 }
369
321 } // namespace ntp_snippets 370 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/remote/ntp_snippets_database.cc ('k') | components/ntp_snippets/remote/ntp_snippets_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698