#include "chrome/browser/ui/webui/sanitized_image_source.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted_memory.h"
#include "base/strings/strcat.h"
#include "base/test/mock_callback.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/testing_profile.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "content/public/test/browser_task_environment.h"
#include "net/http/http_status_code.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 "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image.h"
#include "url/url_util.h"
AnimationFramePtr;
namespace {
AnimationFramePtr MakeImageFrame(SkColor color) { … }
}
MATCHER_P(MemoryEq, other, "Eq matcher for base::RefCountedMemory contents") { … }
class MockDataDecoderDelegate
: public SanitizedImageSource::DataDecoderDelegate { … };
class SanitizedImageSourceTest : public testing::Test { … };
TEST_F(SanitizedImageSourceTest, MultiRequest) { … }
TEST_F(SanitizedImageSourceTest, FailedLoad) { … }
TEST_F(SanitizedImageSourceTest, HttpScheme) { … }
TEST_F(SanitizedImageSourceTest, WrongUrl) { … }
TEST_F(SanitizedImageSourceTest, GooglePhotosImage) { … }
TEST_F(SanitizedImageSourceTest, StaticImage) { … }
TEST_F(SanitizedImageSourceTest, StaticImageWithWebPEncode) { … }
#if BUILDFLAG(IS_CHROMEOS)
TEST_F(SanitizedImageSourceTest, AnimatedImage) {
const std::string test_body = "abc";
const std::string test_url = "https://foo.com/img.png";
base::MockCallback<content::URLDataSource::GotDataCallback> callback;
EXPECT_CALL(*mock_data_decoder_delegate_,
DecodeAnimation(test_body, testing::_))
.Times(1)
.WillOnce([](const std::string&,
SanitizedImageSource::DecodeAnimationCallback callback) {
std::vector<AnimationFramePtr> frames;
frames.push_back(MakeImageFrame(SK_ColorRED));
frames.push_back(MakeImageFrame(SK_ColorBLUE));
frames.push_back(MakeImageFrame(SK_ColorGREEN));
std::move(callback).Run(std::move(frames));
});
auto image =
gfx::Image::CreateFrom1xBitmap(MakeImageFrame(SK_ColorRED)->bitmap);
EXPECT_CALL(callback, Run(testing::_))
.Times(1)
.WillOnce([](scoped_refptr<base::RefCountedMemory> bytes) {
std::string data_string(reinterpret_cast<char const*>(bytes->data()));
EXPECT_TRUE(base::StartsWith(data_string, "RIFF"));
});
sanitized_image_source_->StartDataRequest(
GURL(base::StrCat({chrome::kChromeUIImageURL, "?", test_url})),
content::WebContents::Getter(), callback.Get());
auto* request = test_url_loader_factory_.GetPendingRequest(0);
EXPECT_EQ(network::mojom::CredentialsMode::kOmit,
request->request.credentials_mode);
EXPECT_EQ(test_url, request->request.url);
test_url_loader_factory_.SimulateResponseWithoutRemovingFromPendingList(
request, test_body);
task_environment_.RunUntilIdle();
}
TEST_F(SanitizedImageSourceTest, AnimatedImageWithStaticEncode) {
const std::string test_body = "abc";
const std::string test_url = "https://foo.com/img.png";
base::MockCallback<content::URLDataSource::GotDataCallback> callback;
EXPECT_CALL(*mock_data_decoder_delegate_, DecodeImage(test_body, testing::_))
.Times(1)
.WillOnce([](const std::string&,
SanitizedImageSource::DecodeImageCallback callback) {
std::move(callback).Run(std::move(MakeImageFrame(SK_ColorRED)->bitmap));
});
auto image =
gfx::Image::CreateFrom1xBitmap(MakeImageFrame(SK_ColorRED)->bitmap);
EXPECT_CALL(callback, Run(MemoryEq(image.As1xPNGBytes()))).Times(1);
sanitized_image_source_->StartDataRequest(
GURL(base::StrCat({chrome::kChromeUIImageURL, "?url=", test_url,
"&staticEncode=true"})),
content::WebContents::Getter(), callback.Get());
auto* request = test_url_loader_factory_.GetPendingRequest(0);
EXPECT_EQ(network::mojom::CredentialsMode::kOmit,
request->request.credentials_mode);
EXPECT_EQ(test_url, request->request.url);
test_url_loader_factory_.SimulateResponseWithoutRemovingFromPendingList(
request, test_body);
task_environment_.RunUntilIdle();
}
#endif