123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- // Copyright 2019 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 "ui/base/test/skia_gold_pixel_diff.h"
- #include "base/command_line.h"
- #include "base/scoped_environment_variable_override.h"
- #include "base/test/test_switches.h"
- #include "testing/gmock/include/gmock/gmock.h"
- #include "testing/gtest/include/gtest/gtest.h"
- #include "third_party/skia/include/core/SkBitmap.h"
- #include "third_party/skia/include/core/SkImageInfo.h"
- #include "ui/base/test/skia_gold_matching_algorithm.h"
- #include "ui/gfx/image/image.h"
- #include "ui/gfx/native_widget_types.h"
- using ::testing::_;
- using ::testing::AllOf;
- using ::testing::AnyNumber;
- using ::testing::HasSubstr;
- using ::testing::Property;
- namespace ui {
- namespace test {
- class MockSkiaGoldPixelDiff : public SkiaGoldPixelDiff {
- public:
- MockSkiaGoldPixelDiff() = default;
- MOCK_CONST_METHOD1(LaunchProcess, int(const base::CommandLine&));
- };
- class SkiaGoldPixelDiffTest : public ::testing::Test {
- public:
- SkiaGoldPixelDiffTest() {
- auto* cmd_line = base::CommandLine::ForCurrentProcess();
- cmd_line->AppendSwitchASCII("git-revision", "test");
- CreateTestBitmap();
- }
- SkiaGoldPixelDiffTest(const SkiaGoldPixelDiffTest&) = delete;
- SkiaGoldPixelDiffTest& operator=(const SkiaGoldPixelDiffTest&) = delete;
- ~SkiaGoldPixelDiffTest() override {}
- SkBitmap GetTestBitmap() { return test_bitmap_; }
- void CreateTestBitmap() {
- SkImageInfo info =
- SkImageInfo::Make(10, 10, SkColorType::kBGRA_8888_SkColorType,
- SkAlphaType::kPremul_SkAlphaType);
- test_bitmap_.allocPixels(info, 10 * 4);
- }
- protected:
- private:
- SkBitmap test_bitmap_;
- };
- TEST_F(SkiaGoldPixelDiffTest, CompareScreenshotBySkBitmap) {
- MockSkiaGoldPixelDiff mock_pixel;
- EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(3);
- mock_pixel.Init("Prefix");
- bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
- EXPECT_TRUE(ret);
- }
- TEST_F(SkiaGoldPixelDiffTest, BypassSkiaGoldFunctionality) {
- base::CommandLine::ForCurrentProcess()->AppendSwitch(
- "bypass-skia-gold-functionality");
- MockSkiaGoldPixelDiff mock_pixel;
- EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(0);
- mock_pixel.Init("Prefix");
- bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
- EXPECT_TRUE(ret);
- }
- TEST_F(SkiaGoldPixelDiffTest, LuciAuthSwitch) {
- MockSkiaGoldPixelDiff mock_pixel;
- auto* cmd_line = base::CommandLine::ForCurrentProcess();
- cmd_line->AppendSwitch(switches::kTestLauncherBotMode);
- EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
- EXPECT_CALL(
- mock_pixel,
- LaunchProcess(AllOf(Property(&base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL("--luci"))))))
- .Times(1);
- mock_pixel.Init("Prefix");
- bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
- EXPECT_TRUE(ret);
- }
- TEST_F(SkiaGoldPixelDiffTest, NoLuciAuthSwitch) {
- MockSkiaGoldPixelDiff mock_pixel;
- auto* cmd_line = base::CommandLine::ForCurrentProcess();
- cmd_line->AppendSwitch("no-luci-auth");
- EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
- EXPECT_CALL(mock_pixel, LaunchProcess(AllOf(Property(
- &base::CommandLine::GetCommandLineString,
- Not(HasSubstr(FILE_PATH_LITERAL("--luci")))))))
- .Times(3);
- mock_pixel.Init("Prefix");
- bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
- EXPECT_TRUE(ret);
- }
- TEST_F(SkiaGoldPixelDiffTest, LocalNoLuciAuth) {
- MockSkiaGoldPixelDiff mock_pixel;
- auto* cmd_line = base::CommandLine::ForCurrentProcess();
- cmd_line->RemoveSwitch(switches::kTestLauncherBotMode);
- base::ScopedEnvironmentVariableOverride env_override(
- "CHROMIUM_TEST_LAUNCHER_BOT_MODE");
- EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
- EXPECT_CALL(mock_pixel, LaunchProcess(AllOf(Property(
- &base::CommandLine::GetCommandLineString,
- Not(HasSubstr(FILE_PATH_LITERAL("--luci")))))))
- .Times(3);
- mock_pixel.Init("Prefix");
- bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
- EXPECT_TRUE(ret);
- }
- TEST_F(SkiaGoldPixelDiffTest, FuzzyMatching) {
- MockSkiaGoldPixelDiff mock_pixel;
- EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
- EXPECT_CALL(
- mock_pixel,
- LaunchProcess(AllOf(
- Property(
- &base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL(
- "--add-test-optional-key=image_matching_algorithm:fuzzy"))),
- Property(
- &base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL(
- "--add-test-optional-key=fuzzy_max_different_pixels:1"))),
- Property(
- &base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL(
- "--add-test-optional-key=fuzzy_pixel_delta_threshold:2"))))))
- .Times(1);
- mock_pixel.Init("Prefix");
- FuzzySkiaGoldMatchingAlgorithm algorithm(1, 2);
- bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap(), &algorithm);
- EXPECT_TRUE(ret);
- }
- TEST_F(SkiaGoldPixelDiffTest, FuzzyMatchingWithIgnoredBorder) {
- MockSkiaGoldPixelDiff mock_pixel;
- EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
- EXPECT_CALL(
- mock_pixel,
- LaunchProcess(AllOf(
- Property(
- &base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL(
- "--add-test-optional-key=image_matching_algorithm:fuzzy"))),
- Property(
- &base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL(
- "--add-test-optional-key=fuzzy_max_different_pixels:1"))),
- Property(
- &base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL(
- "--add-test-optional-key=fuzzy_pixel_delta_threshold:2"))),
- Property(
- &base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL("--add-test-optional-key=fuzzy_"
- "ignored_border_thickness:3"))))))
- .Times(1);
- mock_pixel.Init("Prefix");
- FuzzySkiaGoldMatchingAlgorithm algorithm(1, 2, 3);
- bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap(), &algorithm);
- EXPECT_TRUE(ret);
- }
- TEST_F(SkiaGoldPixelDiffTest, SobelMatching) {
- MockSkiaGoldPixelDiff mock_pixel;
- EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
- EXPECT_CALL(
- mock_pixel,
- LaunchProcess(AllOf(
- Property(
- &base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL(
- "--add-test-optional-key=image_matching_algorithm:sobel"))),
- Property(
- &base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL(
- "--add-test-optional-key=fuzzy_max_different_pixels:1"))),
- Property(
- &base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL(
- "--add-test-optional-key=fuzzy_pixel_delta_threshold:2"))),
- Property(&base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL(
- "--add-test-optional-key=sobel_edge_threshold:3"))),
- Property(
- &base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL("--add-test-optional-key=fuzzy_"
- "ignored_border_thickness:4"))))))
- .Times(1);
- mock_pixel.Init("Prefix");
- SobelSkiaGoldMatchingAlgorithm algorithm(1, 2, 3, 4);
- bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap(), &algorithm);
- EXPECT_TRUE(ret);
- }
- TEST_F(SkiaGoldPixelDiffTest, DefaultCorpus) {
- MockSkiaGoldPixelDiff mock_pixel;
- EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
- EXPECT_CALL(mock_pixel,
- LaunchProcess(AllOf(Property(
- &base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL("--corpus=gtest-pixeltests"))))))
- .Times(1);
- mock_pixel.Init("Prefix");
- bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
- EXPECT_TRUE(ret);
- }
- TEST_F(SkiaGoldPixelDiffTest, ExplicitCorpus) {
- MockSkiaGoldPixelDiff mock_pixel;
- EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
- EXPECT_CALL(mock_pixel,
- LaunchProcess(AllOf(
- Property(&base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL("--corpus=corpus"))))))
- .Times(1);
- mock_pixel.Init("Prefix", "corpus");
- bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
- EXPECT_TRUE(ret);
- }
- TEST_F(SkiaGoldPixelDiffTest, DefaultCodeReviewSystem) {
- auto* cmd_line = base::CommandLine::ForCurrentProcess();
- cmd_line->AppendSwitchASCII("gerrit-issue", "1");
- cmd_line->AppendSwitchASCII("gerrit-patchset", "2");
- cmd_line->AppendSwitchASCII("buildbucket-id", "3");
- MockSkiaGoldPixelDiff mock_pixel;
- EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
- EXPECT_CALL(mock_pixel, LaunchProcess(AllOf(Property(
- &base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL("--crs=gerrit"))))))
- .Times(1);
- mock_pixel.Init("Prefix");
- bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
- EXPECT_TRUE(ret);
- }
- TEST_F(SkiaGoldPixelDiffTest, ExplicitCodeReviewSystem) {
- auto* cmd_line = base::CommandLine::ForCurrentProcess();
- cmd_line->AppendSwitchASCII("gerrit-issue", "1");
- cmd_line->AppendSwitchASCII("gerrit-patchset", "2");
- cmd_line->AppendSwitchASCII("buildbucket-id", "3");
- cmd_line->AppendSwitchASCII("code-review-system", "new-crs");
- MockSkiaGoldPixelDiff mock_pixel;
- EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
- EXPECT_CALL(mock_pixel,
- LaunchProcess(
- AllOf(Property(&base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL("--crs=new-crs"))),
- Property(&base::CommandLine::GetCommandLineString,
- Not(HasSubstr(FILE_PATH_LITERAL("gerrit")))))))
- .Times(1);
- mock_pixel.Init("Prefix");
- bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
- EXPECT_TRUE(ret);
- }
- TEST_F(SkiaGoldPixelDiffTest, DryRunLocally) {
- auto* cmd_line = base::CommandLine::ForCurrentProcess();
- cmd_line->RemoveSwitch(switches::kTestLauncherBotMode);
- base::ScopedEnvironmentVariableOverride env_override(
- "CHROMIUM_TEST_LAUNCHER_BOT_MODE");
- MockSkiaGoldPixelDiff mock_pixel;
- EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
- EXPECT_CALL(
- mock_pixel,
- LaunchProcess(AllOf(Property(&base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL("--dryrun"))))))
- .Times(1);
- mock_pixel.Init("Prefix");
- bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
- EXPECT_TRUE(ret);
- }
- TEST_F(SkiaGoldPixelDiffTest, NotDryRunOnBots) {
- auto* cmd_line = base::CommandLine::ForCurrentProcess();
- cmd_line->AppendSwitch(switches::kTestLauncherBotMode);
- MockSkiaGoldPixelDiff mock_pixel;
- EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
- EXPECT_CALL(mock_pixel, LaunchProcess(AllOf(Property(
- &base::CommandLine::GetCommandLineString,
- Not(HasSubstr(FILE_PATH_LITERAL("--dryrun")))))))
- .Times(3);
- mock_pixel.Init("Prefix");
- bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
- EXPECT_TRUE(ret);
- }
- TEST_F(SkiaGoldPixelDiffTest, DryRunForTryjobWithoutPatch) {
- auto* cmd_line = base::CommandLine::ForCurrentProcess();
- cmd_line->AppendSwitch(switches::kTestLauncherBotMode);
- cmd_line->AppendSwitchASCII(switches::kTestLauncherBatchLimit, "1");
- MockSkiaGoldPixelDiff mock_pixel;
- EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
- EXPECT_CALL(
- mock_pixel,
- LaunchProcess(AllOf(Property(&base::CommandLine::GetCommandLineString,
- HasSubstr(FILE_PATH_LITERAL("--dryrun"))))))
- .Times(1);
- mock_pixel.Init("Prefix");
- bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
- EXPECT_TRUE(ret);
- }
- } // namespace test
- } // namespace ui
|