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
3 // found in the LICENSE file.
5 #include "components/enhanced_bookmarks/test_image_store.h"
7 #include "third_party/skia/include/core/SkBitmap.h"
8 #include "ui/gfx/geometry/size.h"
11 TestImageStore::TestImageStore() {
14 bool TestImageStore::HasKey(const GURL
& page_url
) {
15 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
17 return store_
.find(page_url
) != store_
.end();
20 void TestImageStore::Insert(
22 scoped_refptr
<enhanced_bookmarks::ImageRecord
> image_record
) {
23 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
26 store_
.insert(std::make_pair(page_url
, image_record
));
29 void TestImageStore::Erase(const GURL
& page_url
) {
30 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
32 store_
.erase(page_url
);
35 scoped_refptr
<enhanced_bookmarks::ImageRecord
> TestImageStore::Get(
36 const GURL
& page_url
) {
37 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
39 if (store_
.find(page_url
) == store_
.end()) {
40 return scoped_refptr
<enhanced_bookmarks::ImageRecord
>(
41 new enhanced_bookmarks::ImageRecord());
44 return store_
[page_url
];
47 gfx::Size
TestImageStore::GetSize(const GURL
& page_url
) {
48 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
50 ImageMap::const_iterator pair_enumerator
= store_
.find(page_url
);
51 if (pair_enumerator
== store_
.end())
54 return store_
[page_url
]->image
->Size();
57 void TestImageStore::GetAllPageUrls(std::set
<GURL
>* urls
) {
58 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
59 DCHECK(urls
->empty());
61 for (ImageMap::const_iterator it
= store_
.begin(); it
!= store_
.end(); ++it
)
62 urls
->insert(it
->first
);
65 void TestImageStore::ClearAll() {
66 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
71 int64
TestImageStore::GetStoreSizeInBytes() {
72 // Not 100% accurate, but it's for testing so the actual value is not
74 int64 size
= sizeof(store_
);
75 for (ImageMap::const_iterator it
= store_
.begin(); it
!= store_
.end(); ++it
) {
76 size
+= sizeof(it
->first
);
77 size
+= it
->first
.spec().length();
78 size
+= sizeof(it
->second
);
79 SkBitmap bitmap
= it
->second
->image
->AsBitmap();
80 size
+= bitmap
.getSize();
81 size
+= it
->second
->url
.spec().length();
82 size
+= sizeof(it
->second
->dominant_color
);
87 TestImageStore::~TestImageStore() {
88 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());