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.
4
5#include "components/history/core/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 low, thus we should generate a new thumbnail.
59  EXPECT_TRUE(score.ShouldConsiderUpdating());
60
61  // Make it very interesting, but this is not enough.
62  score.boring_score = 0.0;
63  EXPECT_TRUE(score.ShouldConsiderUpdating());
64
65  // good_clipping is important, but sill not enough.
66  score.good_clipping = true;
67  EXPECT_TRUE(score.ShouldConsiderUpdating());
68
69  // at_top is important, but still not enough.
70  score.at_top = true;
71  EXPECT_TRUE(score.ShouldConsiderUpdating());
72
73  // load_completed is important. Finally, the thumbnail is new and
74  // interesting enough.
75  score.load_completed = true;
76  EXPECT_FALSE(score.ShouldConsiderUpdating());
77
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());
83
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());
88}
89