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

Side by Side Diff: components/history/core/browser/thumbnail_database_unittest.cc

Issue 2856873002: [Thumbnails DB] Allow setting last_requested time when accessing favicons. (Closed)
Patch Set: Minor touches Created 3 years, 7 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/files/file_enumerator.h" 10 #include "base/files/file_enumerator.h"
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 EXPECT_NE(0, id); 196 EXPECT_NE(0, id);
197 197
198 EXPECT_NE(0, db.AddIconMapping(url, id)); 198 EXPECT_NE(0, db.AddIconMapping(url, id));
199 std::vector<IconMapping> icon_mappings; 199 std::vector<IconMapping> icon_mappings;
200 EXPECT_TRUE(db.GetIconMappingsForPageURL(url, &icon_mappings)); 200 EXPECT_TRUE(db.GetIconMappingsForPageURL(url, &icon_mappings));
201 EXPECT_EQ(1u, icon_mappings.size()); 201 EXPECT_EQ(1u, icon_mappings.size());
202 EXPECT_EQ(url, icon_mappings.front().page_url); 202 EXPECT_EQ(url, icon_mappings.front().page_url);
203 EXPECT_EQ(id, icon_mappings.front().icon_id); 203 EXPECT_EQ(id, icon_mappings.front().icon_id);
204 } 204 }
205 205
206 TEST_F(ThumbnailDatabaseTest, LastRequestedTime) { 206 TEST_F(ThumbnailDatabaseTest, CleanUnusedFavicons) {
207 ThumbnailDatabase db(NULL); 207 ThumbnailDatabase db(NULL);
208 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); 208 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
209 db.BeginTransaction(); 209 db.BeginTransaction();
210 210
211 base::Time start;
212 ASSERT_TRUE(base::Time::FromUTCExploded({2017, 5, 0, 1, 0, 0, 0, 0}, &start));
213 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
214 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
215
216 GURL url1("http://google.com");
217 favicon_base::FaviconID icon1 = db.AddFavicon(url1, favicon_base::FAVICON);
218 EXPECT_NE(0, icon1);
219 FaviconBitmapID bitmap1 =
220 db.AddFaviconBitmap(icon1, favicon, start, gfx::Size());
221 EXPECT_NE(0, bitmap1);
222 EXPECT_NE(0, db.AddIconMapping(url1, icon1));
223
224 GURL url2("http://youtube.com");
225 favicon_base::FaviconID icon2 = db.AddFavicon(url2, favicon_base::FAVICON);
226 EXPECT_NE(0, icon2);
227 EXPECT_NE(0, db.AddFaviconBitmap(icon2, favicon, start, gfx::Size()));
228 EXPECT_NE(0, db.AddIconMapping(url2, icon2));
229
230 // Update the last_requested info for the first icon 3 weeks later.
231 scoped_refptr<base::RefCountedMemory> bitmap_data_out;
232 gfx::Size pixel_size_out;
233 EXPECT_TRUE(db.GetFaviconBitmap(bitmap1,
234 start + base::TimeDelta::FromDays(21),
235 nullptr, &bitmap_data_out, &pixel_size_out));
236
237 db.CleanUnusedFavicons(start + base::TimeDelta::FromDays(14));
238
239 // The first icon is not deleted.
240 std::vector<IconMapping> icon_mapping;
241 EXPECT_TRUE(db.GetFaviconHeader(icon1, nullptr, nullptr));
242 std::vector<FaviconBitmap> favicon_bitmaps;
243 EXPECT_TRUE(db.GetFaviconBitmaps(icon1, &favicon_bitmaps));
244 ASSERT_EQ(1u, favicon_bitmaps.size());
245 EXPECT_TRUE(db.GetIconMappingsForPageURL(url1, &icon_mapping));
246 ASSERT_EQ(1u, icon_mapping.size());
247 EXPECT_EQ(icon1, icon_mapping[0].icon_id);
248
249 // The second icon gets deleted with all details.
250 EXPECT_FALSE(db.GetIconMappingsForPageURL(url2, nullptr));
251 EXPECT_FALSE(db.GetFaviconHeader(icon2, nullptr, nullptr));
252 EXPECT_FALSE(db.GetFaviconBitmaps(icon2, nullptr));
253 }
254
255 TEST_F(ThumbnailDatabaseTest, DoNotUpdateLastRequestedTimeIfZero) {
256 ThumbnailDatabase db(NULL);
257 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
258 db.BeginTransaction();
259
260 base::Time start;
261 ASSERT_TRUE(base::Time::FromUTCExploded({2017, 5, 0, 1, 0, 0, 0, 0}, &start));
211 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1)); 262 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
212 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 263 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
213 264
214 GURL url("http://google.com"); 265 GURL url("http://google.com");
215 base::Time now = base::Time::Now(); 266 favicon_base::FaviconID icon = db.AddFavicon(url, favicon_base::FAVICON);
216 favicon_base::FaviconID id = 267 EXPECT_NE(0, icon);
217 db.AddFavicon(url, favicon_base::TOUCH_ICON, favicon, now, gfx::Size()); 268 FaviconBitmapID bitmap =
218 ASSERT_NE(0, id); 269 db.AddFaviconBitmap(icon, favicon, start, gfx::Size());
270 EXPECT_NE(0, bitmap);
271 EXPECT_NE(0, db.AddIconMapping(url, icon));
219 272
220 // Fetching the last requested time of a non-existent bitmap should fail. 273 // Access the icon 3 weeks later with zero time - this happens e.g. from an
221 base::Time last_requested = base::Time::UnixEpoch(); 274 // incognito profile.
222 EXPECT_FALSE(db.GetFaviconBitmap(id + 1, NULL, &last_requested, NULL, NULL)); 275 scoped_refptr<base::RefCountedMemory> bitmap_data_out;
223 EXPECT_EQ(last_requested, base::Time::UnixEpoch()); // Remains unchanged. 276 gfx::Size pixel_size_out;
277 EXPECT_TRUE(db.GetFaviconBitmap(bitmap, base::Time(), nullptr,
278 &bitmap_data_out, &pixel_size_out));
224 279
225 // Fetching the last requested time of a bitmap that has no last request 280 db.CleanUnusedFavicons(start + base::TimeDelta::FromDays(14));
226 // should return a null timestamp.
227 last_requested = base::Time::UnixEpoch();
228 EXPECT_TRUE(db.GetFaviconBitmap(id, NULL, &last_requested, NULL, NULL));
229 EXPECT_TRUE(last_requested.is_null());
230 281
231 // Setting the last requested time of an existing bitmap should succeed, and 282 // The icon gets deleted with all details because the (incognito) visit with
232 // the set time should be returned by the corresponding "Get". 283 // zero time did not count.
233 last_requested = base::Time::UnixEpoch(); 284 EXPECT_FALSE(db.GetIconMappingsForPageURL(url, nullptr));
234 EXPECT_TRUE(db.SetFaviconBitmapLastRequestedTime(id, now)); 285 EXPECT_FALSE(db.GetFaviconHeader(icon, nullptr, nullptr));
235 EXPECT_TRUE(db.GetFaviconBitmap(id, NULL, &last_requested, NULL, NULL)); 286 EXPECT_FALSE(db.GetFaviconBitmaps(icon, nullptr));
236 EXPECT_EQ(last_requested, now);
237 } 287 }
238 288
239 TEST_F(ThumbnailDatabaseTest, DeleteIconMappings) { 289 TEST_F(ThumbnailDatabaseTest, DeleteIconMappings) {
240 ThumbnailDatabase db(NULL); 290 ThumbnailDatabase db(NULL);
241 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); 291 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
242 db.BeginTransaction(); 292 db.BeginTransaction();
243 293
244 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1)); 294 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
245 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 295 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
246 296
(...skipping 833 matching lines...) Expand 10 before | Expand all | Expand 10 after
1080 ThumbnailDatabase db(NULL); 1130 ThumbnailDatabase db(NULL);
1081 ASSERT_EQ(sql::INIT_OK, db.Init(db_path)); 1131 ASSERT_EQ(sql::INIT_OK, db.Init(db_path));
1082 1132
1083 // Verify that the resulting schema is correct, whether it 1133 // Verify that the resulting schema is correct, whether it
1084 // involved razing the file or fixing things in place. 1134 // involved razing the file or fixing things in place.
1085 VerifyTablesAndColumns(&db.db_); 1135 VerifyTablesAndColumns(&db.db_);
1086 } 1136 }
1087 } 1137 }
1088 1138
1089 } // namespace history 1139 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698