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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/supervised_user/android/favicon_fetcher.h"
#include <cstddef>
#include <string>
#include <utility>
#include "base/android/scoped_java_ref.h"
#include "base/memory/raw_ptr.h"
#include "base/no_destructor.h"
#include "base/task/cancelable_task_tracker.h"
#include "base/test/gmock_callback_support.h"
#include "components/favicon/core/large_icon_service.h"
#include "components/favicon_base/favicon_callback.h"
#include "components/favicon_base/favicon_types.h"
#include "favicon_fetcher.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "url/gurl.h"
namespace {
const GURL& StringToGURL(std::string uri) {
static const base::NoDestructor<GURL> url(uri);
return *url;
}
const base::CancelableTaskTracker::TaskId kTaskId = 1;
favicon_base::LargeIconImageResult CreateEmptyTestImageResult() {
SkBitmap bitmap_result = SkBitmap();
return favicon_base::LargeIconImageResult(
gfx::Image::CreateFrom1xBitmap(bitmap_result), StringToGURL("icon.com"));
}
favicon_base::LargeIconImageResult CreateTestImageResult() {
return favicon_base::LargeIconImageResult(gfx::test::CreateImage(/*size=*/10),
StringToGURL("icon.com"));
}
class MockLargeIconService : public favicon::LargeIconService {
public:
MockLargeIconService() = default;
MockLargeIconService(const MockLargeIconService&) = delete;
MockLargeIconService& operator=(const MockLargeIconService&) = delete;
~MockLargeIconService() override = default;
// LargeIconService overrides.
MOCK_METHOD(void,
GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache,
(const GURL& page_url,
bool should_trim_page_url_path,
const net::NetworkTrafficAnnotationTag& traffic_annotation,
favicon_base::GoogleFaviconServerCallback callback),
(override));
MOCK_METHOD(base::CancelableTaskTracker::TaskId,
GetLargeIconRawBitmapOrFallbackStyleForPageUrl,
(const GURL& page_url,
int min_source_size_in_pixel,
int desired_size_in_pixel,
favicon_base::LargeIconCallback callback,
base::CancelableTaskTracker* tracker),
(override));
MOCK_METHOD(base::CancelableTaskTracker::TaskId,
GetLargeIconImageOrFallbackStyleForPageUrl,
(const GURL& page_url,
int min_source_size_in_pixel,
int desired_size_in_pixel,
favicon_base::LargeIconImageCallback callback,
base::CancelableTaskTracker* tracker),
(override));
MOCK_METHOD(
base::CancelableTaskTracker::TaskId,
GetLargeIconRawBitmapForPageUrl,
(const GURL& page_url,
int min_source_size_in_pixel,
std::optional<int> size_in_pixel_to_resize_to,
LargeIconService::NoBigEnoughIconBehavior no_big_enough_icon_behavior,
favicon_base::LargeIconCallback callback,
base::CancelableTaskTracker* tracker),
(override));
MOCK_METHOD(base::CancelableTaskTracker::TaskId,
GetLargeIconRawBitmapOrFallbackStyleForIconUrl,
(const GURL& icon_url,
int min_source_size_in_pixel,
int desired_size_in_pixel,
favicon_base::LargeIconCallback callback,
base::CancelableTaskTracker* tracker),
(override));
MOCK_METHOD(base::CancelableTaskTracker::TaskId,
GetIconRawBitmapOrFallbackStyleForPageUrl,
(const GURL& page_url,
int desired_size_in_pixel,
favicon_base::LargeIconCallback callback,
base::CancelableTaskTracker* tracker),
(override));
MOCK_METHOD(void,
GetLargeIconFromCacheFallbackToGoogleServer,
(const GURL& page_url,
StandardIconSize min_source_size_in_pixel,
std::optional<StandardIconSize> size_in_pixel_to_resize_to,
NoBigEnoughIconBehavior no_big_enough_icon_behavior,
bool should_trim_page_url_path,
const net::NetworkTrafficAnnotationTag& traffic_annotation,
favicon_base::LargeIconCallback callback,
base::CancelableTaskTracker* tracker),
(override));
MOCK_METHOD(void,
TouchIconFromGoogleServer,
(const GURL& icon_url),
(override));
};
// FaviconFetcher is the class under test, however we need to mock some of its
// methods.
class MockFaviconFetcher : public ::FaviconFetcher {
public:
explicit MockFaviconFetcher(
raw_ptr<favicon::LargeIconService> large_icon_service)
: FaviconFetcher(large_icon_service) {}
MOCK_METHOD(void,
ExecuteFaviconCallback,
(const base::android::ScopedJavaGlobalRef<jobject>& callback,
SkBitmap bitmap),
(override));
};
class FaviconFetcherTest : public ::testing::Test {
public:
FaviconFetcherTest() = default;
FaviconFetcherTest(const FaviconFetcherTest&) = delete;
FaviconFetcherTest& operator=(const FaviconFetcherTest&) = delete;
protected:
MockLargeIconService mock_large_icon_service_;
raw_ptr<MockFaviconFetcher> favicon_fetcher_ =
new MockFaviconFetcher(&mock_large_icon_service_);
};
// Checks that an image was successfully returned from the server
// after an image fetch request. Checks that the image is returned to the
// caller and the fetcher object is successfully destroyed.
TEST_F(FaviconFetcherTest, SucessfullImageRequestSuccessfulImageFetch) {
// Successful image request from server.
EXPECT_CALL(mock_large_icon_service_,
GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
testing::_, testing::_, testing::_, testing::_))
.Times(1)
.WillOnce(base::test::RunOnceCallback<3>(
favicon_base::GoogleFaviconServerRequestStatus::SUCCESS));
// Returns an empty image at first, then a valid image (after requesting it
// from server).
EXPECT_CALL(mock_large_icon_service_,
GetLargeIconImageOrFallbackStyleForPageUrl(
testing::_, testing::_, testing::_, testing::_, testing::_))
.Times(2)
.WillOnce([=](auto, auto, auto,
favicon_base::LargeIconImageCallback image_callback, auto) {
std::move(image_callback).Run(CreateEmptyTestImageResult());
return kTaskId;
})
.WillOnce([=](auto, auto, auto,
favicon_base::LargeIconImageCallback image_callback, auto) {
std::move(image_callback).Run(CreateTestImageResult());
return kTaskId;
});
// Tests that favicon is returned to the caller (Android callback execution).
EXPECT_CALL(*favicon_fetcher_, ExecuteFaviconCallback(testing::_, testing::_))
.Times(1);
base::WeakPtr<FaviconFetcher> faviconFetcherWeakPtr =
favicon_fetcher_->GetWeakPtr();
favicon_fetcher_->FetchFavicon(StringToGURL("page.com"), true, 16, 32,
base::android::ScopedJavaGlobalRef<jobject>());
// Tests that the favicon_fetcher_ instance test is destroyed.
EXPECT_TRUE(faviconFetcherWeakPtr.WasInvalidated());
}
// Checks that an image that does not meet our specs is returned from the server
// after an image fetch request. Checks that no image is returned to the
// caller and the fetcher object is successfully destroyed.
TEST_F(FaviconFetcherTest, SucessfullImageRequestUnsuccessfulImageFetch) {
// Successful image request from server.
EXPECT_CALL(mock_large_icon_service_,
GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
testing::_, testing::_, testing::_, testing::_))
.Times(1)
.WillOnce(base::test::RunOnceCallback<3>(
favicon_base::GoogleFaviconServerRequestStatus::SUCCESS));
// Returns an empty image twice (when image does not comply to our specs).
EXPECT_CALL(mock_large_icon_service_,
GetLargeIconImageOrFallbackStyleForPageUrl(
testing::_, testing::_, testing::_, testing::_, testing::_))
.Times(2)
.WillRepeatedly([=](auto, auto, auto,
favicon_base::LargeIconImageCallback image_callback,
auto) {
std::move(image_callback).Run(CreateEmptyTestImageResult());
return kTaskId;
});
// No favicon is returned to the caller.
EXPECT_CALL(*favicon_fetcher_, ExecuteFaviconCallback(testing::_, testing::_))
.Times(0);
base::WeakPtr<FaviconFetcher> faviconFetcherWeakPtr =
favicon_fetcher_->GetWeakPtr();
favicon_fetcher_->FetchFavicon(StringToGURL("page.com"), true, 16, 32,
base::android::ScopedJavaGlobalRef<jobject>());
// Tests that the favicon_fetcher_ instance test is destroyed.
EXPECT_TRUE(faviconFetcherWeakPtr.WasInvalidated());
}
// Checks that an image is successfully returned from the cache without
// making a request to retrieve it from the server. Checks that the image is
// returned to the caller and the fetcher object is successfully destroyed.
TEST_F(FaviconFetcherTest, SuccessfullImageFetchFromCache) {
// Returns a valid image (already cached).
EXPECT_CALL(mock_large_icon_service_,
GetLargeIconImageOrFallbackStyleForPageUrl(
testing::_, testing::_, testing::_, testing::_, testing::_))
.Times(1)
.WillOnce([=](auto, auto, auto,
favicon_base::LargeIconImageCallback image_callback, auto) {
std::move(image_callback).Run(CreateTestImageResult());
return kTaskId;
});
// Tests that favicon is returned to the caller (Android callback execution).
EXPECT_CALL(*favicon_fetcher_, ExecuteFaviconCallback(testing::_, testing::_))
.Times(1);
// No server request is done when image exists in cache.
EXPECT_CALL(mock_large_icon_service_,
GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
testing::_, testing::_, testing::_, testing::_))
.Times(0);
base::WeakPtr<FaviconFetcher> faviconFetcherWeakPtr =
favicon_fetcher_->GetWeakPtr();
favicon_fetcher_->FetchFavicon(StringToGURL("page.com"), true, 16, 32,
base::android::ScopedJavaGlobalRef<jobject>());
// Tests that the favicon_fetcher_ instance test is destroyed.
EXPECT_TRUE(faviconFetcherWeakPtr.WasInvalidated());
}
// Checks that no image is successfuly returned from the server after requesting
// it. Checks that no image is returned to the caller and the fetcher object is
// successfully destroyed.
TEST_F(FaviconFetcherTest, UnuccessfullImageRequest) {
// Unsuccessful image request from server.
EXPECT_CALL(mock_large_icon_service_,
GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
testing::_, testing::_, testing::_, testing::_))
.Times(1)
.WillOnce(base::test::RunOnceCallback<3>(
favicon_base::GoogleFaviconServerRequestStatus::FAILURE_HTTP_ERROR));
// Returns an empty image.
EXPECT_CALL(mock_large_icon_service_,
GetLargeIconImageOrFallbackStyleForPageUrl(
testing::_, testing::_, testing::_, testing::_, testing::_))
.Times(1)
.WillOnce([=](auto, auto, auto,
favicon_base::LargeIconImageCallback image_callback, auto) {
std::move(image_callback).Run(CreateEmptyTestImageResult());
return kTaskId;
});
// Nothing is returned to the caller.
EXPECT_CALL(*favicon_fetcher_, ExecuteFaviconCallback(testing::_, testing::_))
.Times(0);
base::WeakPtr<FaviconFetcher> faviconFetcherWeakPtr =
favicon_fetcher_->GetWeakPtr();
favicon_fetcher_->FetchFavicon(StringToGURL("page.com"), true, 16, 32,
base::android::ScopedJavaGlobalRef<jobject>());
// Tests that the favicon_fetcher_ instance test is destroyed.
EXPECT_TRUE(faviconFetcherWeakPtr.WasInvalidated());
}
} // namespace
|