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
|
// 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/ui/views/sharing_hub/preview_view.h"
#include "chrome/browser/share/share_attempt.h"
#include "chrome/test/views/chrome_views_test_base.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/view_utils.h"
namespace {
using PreviewViewTest = ChromeViewsTestBase;
views::Label* FindLabelWithText(views::View* root, std::u16string text) {
return static_cast<views::Label*>(views::test::AnyViewMatchingPredicate(
root, [=](const views::View* candidate) -> bool {
return (IsViewClass<views::Label>(candidate)) &&
static_cast<const views::Label*>(candidate)->GetText() == text;
}));
}
views::ImageView* FindImage(views::View* root) {
return static_cast<views::ImageView*>(views::test::AnyViewMatchingPredicate(
root, [](const views::View* candidate) -> bool {
return IsViewClass<views::ImageView>(candidate);
}));
}
ui::ImageModel BuildTestImage(SkColor color) {
return ui::ImageModel::FromImageSkia(gfx::ImageSkia::CreateFromBitmap(
gfx::test::CreateBitmap(/*size=*/32, color), 1.0));
}
SkColor ImageTopLeftColor(ui::ImageModel model) {
gfx::ImageSkia skia = model.Rasterize(nullptr);
return skia.bitmap()->getColor(0, 0);
}
std::unique_ptr<sharing_hub::PreviewView> BuildTestPreview() {
auto view = std::make_unique<sharing_hub::PreviewView>(
share::ShareAttempt(nullptr, u"Title", GURL("https://www.chromium.org/"),
BuildTestImage(SK_ColorRED)));
return view;
}
TEST_F(PreviewViewTest, IncludesTitle) {
auto view = BuildTestPreview();
ASSERT_TRUE(FindLabelWithText(view.get(), u"Title"));
}
TEST_F(PreviewViewTest, IncludesURL) {
auto view = BuildTestPreview();
ASSERT_TRUE(FindLabelWithText(view.get(), u"https://www.chromium.org/"));
}
TEST_F(PreviewViewTest, IncludesImage) {
auto view = BuildTestPreview();
auto* image = FindImage(view.get());
ASSERT_TRUE(image);
EXPECT_EQ(ImageTopLeftColor(image->GetImageModel()), SK_ColorRED);
}
} // namespace
|