1// Copyright (c) 2009 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.
4
5#include "chrome/common/thumbnail_score.h"
6#include "testing/gtest/include/gtest/gtest.h"
7
8// Tests that the different types of thumbnails are compared properly.
9TEST(ThumbnailScoreTest, ShouldReplaceThumbnailWithType) {
10  base::Time now = base::Time::Now();
11
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);
16
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));
23}
24
25// Tests that we'll replace old thumbnails will crappier but newer ones.
26TEST(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);
31
32  ThumbnailScore oldie_but_goodie(0.1, true, true, last_year);
33  ThumbnailScore newie_but_crappie(0.9, true, true, now);
34
35  EXPECT_TRUE(ShouldReplaceThumbnailWith(oldie_but_goodie, newie_but_crappie));
36}
37
38// Having many redirects should age the thumbnail.
39TEST(ThumbnailScoreTest, RedirectCount) {
40  base::Time now = base::Time::Now();
41
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;
46
47  EXPECT_TRUE(ShouldReplaceThumbnailWith(some_redirects, no_redirects));
48
49  // This one has a lot of redirects but a better score. It should still be
50  // rejected.
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));
54}
55
56TEST(ThumbnailScoreTest, ShouldConsiderUpdating) {
57  ThumbnailScore score;
58  // By default, the score is 1.0, meaning very boring, thus we should
59  // generate a new thumbnail.
60  EXPECT_DOUBLE_EQ(1.0, score.boring_score);
61  EXPECT_TRUE(score.ShouldConsiderUpdating());
62
63  // Make it very interesting, but this is not enough.
64  score.boring_score = 0.0;
65  EXPECT_TRUE(score.ShouldConsiderUpdating());
66
67  // good_clipping is important, but sill not enough.
68  score.good_clipping = true;
69  EXPECT_TRUE(score.ShouldConsiderUpdating());
70
71  // at_top is important. Finally, the thumbnail is new and interesting enough.
72  score.at_top = true;
73  EXPECT_FALSE(score.ShouldConsiderUpdating());
74
75  // Make it old. Then, it's no longer new enough.
76  score.time_at_snapshot -= ThumbnailScore::kUpdateThumbnailTime;
77  EXPECT_TRUE(score.ShouldConsiderUpdating());
78}
79