1 // Copyright (c) 2011 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 "chrome/common/thumbnail_score.h"
6 #include "testing/gtest/include/gtest/gtest.h"
8 // Tests that the different types of thumbnails are compared properly.
9 TEST(ThumbnailScoreTest
, ShouldReplaceThumbnailWithType
) {
10 base::Time now
= base::Time::Now();
12 ThumbnailScore
nothing_good(0.5, false, false, now
);
13 ThumbnailScore
not_at_top(0.5, false, true, now
);
14 ThumbnailScore
bad_clipping(0.5, true, false, now
);
15 ThumbnailScore
life_is_awesome(0.5, true, true, now
);
17 EXPECT_TRUE(ShouldReplaceThumbnailWith(nothing_good
, not_at_top
));
18 EXPECT_TRUE(ShouldReplaceThumbnailWith(nothing_good
, bad_clipping
));
19 EXPECT_TRUE(ShouldReplaceThumbnailWith(nothing_good
, life_is_awesome
));
20 EXPECT_TRUE(ShouldReplaceThumbnailWith(not_at_top
, bad_clipping
));
21 EXPECT_TRUE(ShouldReplaceThumbnailWith(not_at_top
, life_is_awesome
));
22 EXPECT_TRUE(ShouldReplaceThumbnailWith(bad_clipping
, life_is_awesome
));
25 // Tests that we'll replace old thumbnails will crappier but newer ones.
26 TEST(ThumbnailScoreTest
, ShouldReplaceThumbnailWithTime
) {
27 // Use a really long time for the difference so we aren't sensitive to the
28 // degrading schedule.
29 base::Time now
= base::Time::Now();
30 base::Time last_year
= now
- base::TimeDelta::FromDays(365);
32 ThumbnailScore
oldie_but_goodie(0.1, true, true, last_year
);
33 ThumbnailScore
newie_but_crappie(0.9, true, true, now
);
35 EXPECT_TRUE(ShouldReplaceThumbnailWith(oldie_but_goodie
, newie_but_crappie
));
38 // Having many redirects should age the thumbnail.
39 TEST(ThumbnailScoreTest
, RedirectCount
) {
40 base::Time now
= base::Time::Now();
42 ThumbnailScore
no_redirects(0.5, true, true, now
);
43 no_redirects
.redirect_hops_from_dest
= 0;
44 ThumbnailScore
some_redirects(0.5, true, true, now
);
45 some_redirects
.redirect_hops_from_dest
= 1;
47 EXPECT_TRUE(ShouldReplaceThumbnailWith(some_redirects
, no_redirects
));
49 // This one has a lot of redirects but a better score. It should still be
51 ThumbnailScore
lotsa_redirects(0.4, true, true, now
);
52 lotsa_redirects
.redirect_hops_from_dest
= 4;
53 EXPECT_FALSE(ShouldReplaceThumbnailWith(no_redirects
, lotsa_redirects
));
56 TEST(ThumbnailScoreTest
, ShouldConsiderUpdating
) {
58 // By default, the score is low, thus we should generate a new thumbnail.
59 EXPECT_TRUE(score
.ShouldConsiderUpdating());
61 // Make it very interesting, but this is not enough.
62 score
.boring_score
= 0.0;
63 EXPECT_TRUE(score
.ShouldConsiderUpdating());
65 // good_clipping is important, but sill not enough.
66 score
.good_clipping
= true;
67 EXPECT_TRUE(score
.ShouldConsiderUpdating());
69 // at_top is important, but still not enough.
71 EXPECT_TRUE(score
.ShouldConsiderUpdating());
73 // load_completed is important. Finally, the thumbnail is new and
74 // interesting enough.
75 score
.load_completed
= true;
76 EXPECT_FALSE(score
.ShouldConsiderUpdating());
78 // Make it very boring, but it won't change the result. The boring score
79 // isn't used for judging whether we should update or not. See comments
80 // at boring_score in thumbnail_score.h for why.
81 score
.boring_score
= 1.0;
82 EXPECT_FALSE(score
.ShouldConsiderUpdating());
84 // Make it old. Then, it's no longer new enough.
85 score
.time_at_snapshot
-=
86 base::TimeDelta::FromDays(ThumbnailScore::kUpdateThumbnailTimeDays
);
87 EXPECT_TRUE(score
.ShouldConsiderUpdating());