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
|
// Copyright 2017 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_snippets/remote/cached_image_fetcher.h"
#include <map>
#include <memory>
#include <utility>
#include "base/bind.h"
#include "base/callback.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "components/image_fetcher/core/fake_image_decoder.h"
#include "components/image_fetcher/core/image_fetcher.h"
#include "components/image_fetcher/core/image_fetcher_impl.h"
#include "components/leveldb_proto/testing/fake_db.h"
#include "components/ntp_snippets/remote/proto/ntp_snippets.pb.h"
#include "components/ntp_snippets/remote/remote_suggestions_database.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_unittest_util.h"
using leveldb_proto::test::FakeDB;
using testing::_;
using testing::Eq;
using testing::Property;
namespace ntp_snippets {
namespace {
const char kImageData[] = "data";
const char kImageURL[] = "http://image.test/test.png";
const char kSnippetID[] = "http://localhost";
const ContentSuggestion::ID kSuggestionID(
Category::FromKnownCategory(KnownCategories::ARTICLES),
kSnippetID);
enum class TestType {
kImageCallback,
kImageDataCallback,
kBothCallbacks,
};
} // namespace
// This test is parameterized to run all tests in the three configurations:
// both callbacks used, only image_callback used, only image_data_callback used.
class NtpSnippetsCachedImageFetcherTest
: public testing::TestWithParam<TestType> {
public:
NtpSnippetsCachedImageFetcherTest() {
RequestThrottler::RegisterProfilePrefs(pref_service_.registry());
// Setup RemoteSuggestionsDatabase with fake ProtoDBs.
auto suggestion_db =
std::make_unique<FakeDB<SnippetProto>>(&suggestion_db_storage_);
auto image_db =
std::make_unique<FakeDB<SnippetImageProto>>(&image_db_storage_);
suggestion_db_ = suggestion_db.get();
image_db_ = image_db.get();
database_ = std::make_unique<RemoteSuggestionsDatabase>(
std::move(suggestion_db), std::move(image_db));
suggestion_db_->InitStatusCallback(leveldb_proto::Enums::InitStatus::kOK);
image_db_->InitStatusCallback(leveldb_proto::Enums::InitStatus::kOK);
shared_factory_ =
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_);
auto decoder = std::make_unique<image_fetcher::FakeImageDecoder>();
fake_image_decoder_ = decoder.get();
auto image_fetcher = std::make_unique<image_fetcher::ImageFetcherImpl>(
std::move(decoder), shared_factory_);
cached_image_fetcher_ = std::make_unique<ntp_snippets::CachedImageFetcher>(
std::move(image_fetcher), &pref_service_, database_.get());
EXPECT_TRUE(database_->IsInitialized());
}
~NtpSnippetsCachedImageFetcherTest() override {
cached_image_fetcher_.reset();
database_.reset();
}
void Fetch(std::string expected_image_data, bool expect_image) {
fake_image_decoder()->SetEnabled(GetParam() !=
TestType::kImageDataCallback);
base::MockCallback<ImageFetchedCallback> image_callback;
base::MockCallback<ImageDataFetchedCallback> image_data_callback;
switch (GetParam()) {
case TestType::kImageCallback: {
EXPECT_CALL(image_callback,
Run(Property(&gfx::Image::IsEmpty, Eq(!expect_image))));
cached_image_fetcher()->FetchSuggestionImage(
kSuggestionID, GURL(kImageURL), ImageDataFetchedCallback(),
image_callback.Get());
image_db_->GetCallback(true);
} break;
case TestType::kImageDataCallback: {
EXPECT_CALL(image_data_callback, Run(expected_image_data));
cached_image_fetcher()->FetchSuggestionImage(
kSuggestionID, GURL(kImageURL), image_data_callback.Get(),
ImageFetchedCallback());
image_db_->GetCallback(true);
} break;
case TestType::kBothCallbacks: {
EXPECT_CALL(image_data_callback, Run(expected_image_data));
EXPECT_CALL(image_callback,
Run(Property(&gfx::Image::IsEmpty, Eq(!expect_image))));
cached_image_fetcher()->FetchSuggestionImage(
kSuggestionID, GURL(kImageURL), image_data_callback.Get(),
image_callback.Get());
image_db_->GetCallback(true);
} break;
}
task_environment_.RunUntilIdle();
}
RemoteSuggestionsDatabase* database() { return database_.get(); }
image_fetcher::FakeImageDecoder* fake_image_decoder() {
return fake_image_decoder_;
}
network::TestURLLoaderFactory* test_url_loader_factory() {
return &test_url_loader_factory_;
}
CachedImageFetcher* cached_image_fetcher() {
return cached_image_fetcher_.get();
}
FakeDB<SnippetProto>* suggestion_db() { return suggestion_db_; }
FakeDB<SnippetImageProto>* image_db() { return image_db_; }
private:
network::TestURLLoaderFactory test_url_loader_factory_;
scoped_refptr<network::SharedURLLoaderFactory> shared_factory_;
std::unique_ptr<CachedImageFetcher> cached_image_fetcher_;
image_fetcher::FakeImageDecoder* fake_image_decoder_;
std::unique_ptr<RemoteSuggestionsDatabase> database_;
std::map<std::string, SnippetProto> suggestion_db_storage_;
std::map<std::string, SnippetImageProto> image_db_storage_;
// Owned by |database_|.
FakeDB<SnippetProto>* suggestion_db_;
FakeDB<SnippetImageProto>* image_db_;
TestingPrefServiceSimple pref_service_;
base::test::TaskEnvironment task_environment_;
DISALLOW_COPY_AND_ASSIGN(NtpSnippetsCachedImageFetcherTest);
};
TEST_P(NtpSnippetsCachedImageFetcherTest, FetchImageFromCache) {
// Save the image in the database.
database()->SaveImage(kSnippetID, kImageData);
image_db()->UpdateCallback(true);
// Do not provide any URL responses and expect that the image is fetched (from
// cache).
Fetch(kImageData, true);
}
TEST_P(NtpSnippetsCachedImageFetcherTest, FetchImagePopulatesCache) {
// Expect the image to be fetched by URL.
{
test_url_loader_factory()->AddResponse(kImageURL, kImageData);
Fetch(kImageData, true);
}
// Fetch again. The cache should be populated, no network request is needed.
{
test_url_loader_factory()->ClearResponses();
Fetch(kImageData, true);
}
}
TEST_P(NtpSnippetsCachedImageFetcherTest, FetchNonExistingImage) {
const std::string kErrorResponse = "error-response";
test_url_loader_factory()->AddResponse(kImageURL, kErrorResponse,
net::HTTP_NOT_FOUND);
// Expect an empty image is fetched if the URL cannot be requested.
Fetch("", false);
}
INSTANTIATE_TEST_SUITE_P(NTP,
NtpSnippetsCachedImageFetcherTest,
testing::Values(TestType::kImageCallback,
TestType::kImageDataCallback,
TestType::kBothCallbacks));
} // namespace ntp_snippets
|