chromium/chrome/browser/ui/webui/sanitized_image_source_unittest.cc

// Copyright 2020 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/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) {}

}  // namespace

MATCHER_P(MemoryEq, other, "Eq matcher for base::RefCountedMemory contents") {}

class MockDataDecoderDelegate
    : public SanitizedImageSource::DataDecoderDelegate {};

class SanitizedImageSourceTest : public testing::Test {};

// Verifies that the image source can handle multiple requests in parallel.
TEST_F(SanitizedImageSourceTest, MultiRequest) {}

// Verifies that the image source sends back an empty image in case the external
// image download fails.
TEST_F(SanitizedImageSourceTest, FailedLoad) {}

// Verifies that the image source sends back an error in case the external
// image is served via an HTTP scheme.
TEST_F(SanitizedImageSourceTest, HttpScheme) {}

// Verifies that the image source ignores requests with a wrong URL.
TEST_F(SanitizedImageSourceTest, WrongUrl) {}

// Verifies that the image source sends a Google Photos auth token with its data
// request if and only if asked to by URL specification.
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";

  // Set up expectations and mock data.
  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()));
        // Make sure the image is encoded into WebP format.
        EXPECT_TRUE(base::StartsWith(data_string, "RIFF"));
      });

  // Issue requests.
  sanitized_image_source_->StartDataRequest(
      GURL(base::StrCat({chrome::kChromeUIImageURL, "?", test_url})),
      content::WebContents::Getter(), callback.Get());

  // Answer requests and check correctness.
  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";

  // Set up expectations and mock data.
  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);
  // Make sure the image is encoded into static PNG bytes.
  EXPECT_CALL(callback, Run(MemoryEq(image.As1xPNGBytes()))).Times(1);

  // Issue requests.
  sanitized_image_source_->StartDataRequest(
      GURL(base::StrCat({chrome::kChromeUIImageURL, "?url=", test_url,
                         "&staticEncode=true"})),
      content::WebContents::Getter(), callback.Get());

  // Answer requests and check correctness.
  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  // BUILDFLAG(IS_CHROMEOS)