File: icon_cacher_impl_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (199 lines) | stat: -rw-r--r-- 6,908 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/ntp_tiles/icon_cacher_impl.h"

#include <utility>

#include "base/files/scoped_temp_dir.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/favicon/core/favicon_client.h"
#include "components/favicon/core/favicon_service.h"
#include "components/favicon/core/favicon_util.h"
#include "components/history/core/browser/history_database_params.h"
#include "components/history/core/browser/history_service.h"
#include "components/image_fetcher/image_fetcher.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/image/image_unittest_util.h"

using ::testing::_;
using ::testing::InSequence;
using ::testing::MockFunction;
using ::testing::Return;

namespace ntp_tiles {
namespace {

class MockImageFetcher : public image_fetcher::ImageFetcher {
 public:
  MOCK_METHOD1(SetImageFetcherDelegate,
               void(image_fetcher::ImageFetcherDelegate* delegate));
  MOCK_METHOD1(SetDataUseServiceName,
               void(image_fetcher::ImageFetcher::DataUseServiceName name));
  MOCK_METHOD3(StartOrQueueNetworkRequest,
               void(const std::string& id,
                    const GURL& image_url,
                    base::Callback<void(const std::string& id,
                                        const gfx::Image& image)> callback));
};

class IconCacherTest : public ::testing::Test {
 protected:
  IconCacherTest()
      : site_(base::string16(),  // title, unused
              GURL("http://url.google/"),
              GURL("http://url.google/icon.png"),
              GURL("http://url.google/favicon.ico"),
              GURL()),  // thumbnail, unused
        image_fetcher_(new ::testing::StrictMock<MockImageFetcher>),
        favicon_service_(/*favicon_client=*/nullptr, &history_service_) {
    CHECK(history_dir_.CreateUniqueTempDir());
    CHECK(history_service_.Init(
        history::HistoryDatabaseParams(history_dir_.GetPath(), 0, 0)));
  }

  void PreloadIcon(const GURL& url,
                   const GURL& icon_url,
                   favicon_base::IconType icon_type,
                   int width,
                   int height) {
    favicon_service_.SetFavicons(url, icon_url, icon_type,
                                 gfx::test::CreateImage(width, height));
  }

  bool IconIsCachedFor(const GURL& url, favicon_base::IconType icon_type) {
    base::CancelableTaskTracker tracker;
    bool is_cached;
    base::RunLoop loop;
    favicon::GetFaviconImageForPageURL(
        &favicon_service_, url, icon_type,
        base::Bind(
            [](bool* is_cached, base::RunLoop* loop,
               const favicon_base::FaviconImageResult& result) {
              *is_cached = !result.image.IsEmpty();
              loop->Quit();
            },
            &is_cached, &loop),
        &tracker);
    loop.Run();
    return is_cached;
  }

  base::MessageLoop message_loop_;
  PopularSites::Site site_;
  std::unique_ptr<MockImageFetcher> image_fetcher_;
  base::ScopedTempDir history_dir_;
  history::HistoryService history_service_;
  favicon::FaviconService favicon_service_;
};

template <typename Fn>
base::Callback<Fn> BindMockFunction(MockFunction<Fn>* function) {
  return base::Bind(&MockFunction<Fn>::Call, base::Unretained(function));
}

ACTION(FailFetch) {
  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE, base::Bind(arg2, arg0, gfx::Image()));
}

ACTION_P2(PassFetch, width, height) {
  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE, base::Bind(arg2, arg0, gfx::test::CreateImage(width, height)));
}

ACTION_P(Quit, run_loop) {
  run_loop->Quit();
}

TEST_F(IconCacherTest, LargeCached) {
  MockFunction<void(bool)> done;
  base::RunLoop loop;
  {
    InSequence s;
    EXPECT_CALL(*image_fetcher_,
                SetDataUseServiceName(
                    data_use_measurement::DataUseUserData::NTP_TILES));
    EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop));
  }
  PreloadIcon(site_.url, site_.large_icon_url, favicon_base::TOUCH_ICON, 128,
              128);

  IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
  cacher.StartFetch(site_, BindMockFunction(&done));
  loop.Run();
  EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
  EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
}

TEST_F(IconCacherTest, LargeNotCachedAndFetchSucceeded) {
  MockFunction<void(bool)> done;
  base::RunLoop loop;
  {
    InSequence s;
    EXPECT_CALL(*image_fetcher_,
                SetDataUseServiceName(
                    data_use_measurement::DataUseUserData::NTP_TILES));
    EXPECT_CALL(*image_fetcher_,
                StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
        .WillOnce(PassFetch(128, 128));
    EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop));
  }

  IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
  cacher.StartFetch(site_, BindMockFunction(&done));
  loop.Run();
  EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
  EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
}

TEST_F(IconCacherTest, SmallNotCachedAndFetchSucceeded) {
  site_.large_icon_url = GURL();

  MockFunction<void(bool)> done;
  base::RunLoop loop;
  {
    InSequence s;
    EXPECT_CALL(*image_fetcher_,
                SetDataUseServiceName(
                    data_use_measurement::DataUseUserData::NTP_TILES));
    EXPECT_CALL(*image_fetcher_,
                StartOrQueueNetworkRequest(_, site_.favicon_url, _))
        .WillOnce(PassFetch(128, 128));
    EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop));
  }

  IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
  cacher.StartFetch(site_, BindMockFunction(&done));
  loop.Run();
  EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
  EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
}

TEST_F(IconCacherTest, LargeNotCachedAndFetchFailed) {
  MockFunction<void(bool)> done;
  base::RunLoop loop;
  {
    InSequence s;
    EXPECT_CALL(*image_fetcher_,
                SetDataUseServiceName(
                    data_use_measurement::DataUseUserData::NTP_TILES));
    EXPECT_CALL(*image_fetcher_,
                StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
        .WillOnce(FailFetch());
    EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop));
  }

  IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
  cacher.StartFetch(site_, BindMockFunction(&done));
  loop.Run();
  EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
  EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
}

}  // namespace
}  // namespace ntp_tiles