skia_gold_pixel_diff_unittest.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/base/test/skia_gold_pixel_diff.h"
  5. #include "base/command_line.h"
  6. #include "base/scoped_environment_variable_override.h"
  7. #include "base/test/test_switches.h"
  8. #include "testing/gmock/include/gmock/gmock.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "third_party/skia/include/core/SkBitmap.h"
  11. #include "third_party/skia/include/core/SkImageInfo.h"
  12. #include "ui/base/test/skia_gold_matching_algorithm.h"
  13. #include "ui/gfx/image/image.h"
  14. #include "ui/gfx/native_widget_types.h"
  15. using ::testing::_;
  16. using ::testing::AllOf;
  17. using ::testing::AnyNumber;
  18. using ::testing::HasSubstr;
  19. using ::testing::Property;
  20. namespace ui {
  21. namespace test {
  22. class MockSkiaGoldPixelDiff : public SkiaGoldPixelDiff {
  23. public:
  24. MockSkiaGoldPixelDiff() = default;
  25. MOCK_CONST_METHOD1(LaunchProcess, int(const base::CommandLine&));
  26. };
  27. class SkiaGoldPixelDiffTest : public ::testing::Test {
  28. public:
  29. SkiaGoldPixelDiffTest() {
  30. auto* cmd_line = base::CommandLine::ForCurrentProcess();
  31. cmd_line->AppendSwitchASCII("git-revision", "test");
  32. CreateTestBitmap();
  33. }
  34. SkiaGoldPixelDiffTest(const SkiaGoldPixelDiffTest&) = delete;
  35. SkiaGoldPixelDiffTest& operator=(const SkiaGoldPixelDiffTest&) = delete;
  36. ~SkiaGoldPixelDiffTest() override {}
  37. SkBitmap GetTestBitmap() { return test_bitmap_; }
  38. void CreateTestBitmap() {
  39. SkImageInfo info =
  40. SkImageInfo::Make(10, 10, SkColorType::kBGRA_8888_SkColorType,
  41. SkAlphaType::kPremul_SkAlphaType);
  42. test_bitmap_.allocPixels(info, 10 * 4);
  43. }
  44. protected:
  45. private:
  46. SkBitmap test_bitmap_;
  47. };
  48. TEST_F(SkiaGoldPixelDiffTest, CompareScreenshotBySkBitmap) {
  49. MockSkiaGoldPixelDiff mock_pixel;
  50. EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(3);
  51. mock_pixel.Init("Prefix");
  52. bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
  53. EXPECT_TRUE(ret);
  54. }
  55. TEST_F(SkiaGoldPixelDiffTest, BypassSkiaGoldFunctionality) {
  56. base::CommandLine::ForCurrentProcess()->AppendSwitch(
  57. "bypass-skia-gold-functionality");
  58. MockSkiaGoldPixelDiff mock_pixel;
  59. EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(0);
  60. mock_pixel.Init("Prefix");
  61. bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
  62. EXPECT_TRUE(ret);
  63. }
  64. TEST_F(SkiaGoldPixelDiffTest, LuciAuthSwitch) {
  65. MockSkiaGoldPixelDiff mock_pixel;
  66. auto* cmd_line = base::CommandLine::ForCurrentProcess();
  67. cmd_line->AppendSwitch(switches::kTestLauncherBotMode);
  68. EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
  69. EXPECT_CALL(
  70. mock_pixel,
  71. LaunchProcess(AllOf(Property(&base::CommandLine::GetCommandLineString,
  72. HasSubstr(FILE_PATH_LITERAL("--luci"))))))
  73. .Times(1);
  74. mock_pixel.Init("Prefix");
  75. bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
  76. EXPECT_TRUE(ret);
  77. }
  78. TEST_F(SkiaGoldPixelDiffTest, NoLuciAuthSwitch) {
  79. MockSkiaGoldPixelDiff mock_pixel;
  80. auto* cmd_line = base::CommandLine::ForCurrentProcess();
  81. cmd_line->AppendSwitch("no-luci-auth");
  82. EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
  83. EXPECT_CALL(mock_pixel, LaunchProcess(AllOf(Property(
  84. &base::CommandLine::GetCommandLineString,
  85. Not(HasSubstr(FILE_PATH_LITERAL("--luci")))))))
  86. .Times(3);
  87. mock_pixel.Init("Prefix");
  88. bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
  89. EXPECT_TRUE(ret);
  90. }
  91. TEST_F(SkiaGoldPixelDiffTest, LocalNoLuciAuth) {
  92. MockSkiaGoldPixelDiff mock_pixel;
  93. auto* cmd_line = base::CommandLine::ForCurrentProcess();
  94. cmd_line->RemoveSwitch(switches::kTestLauncherBotMode);
  95. base::ScopedEnvironmentVariableOverride env_override(
  96. "CHROMIUM_TEST_LAUNCHER_BOT_MODE");
  97. EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
  98. EXPECT_CALL(mock_pixel, LaunchProcess(AllOf(Property(
  99. &base::CommandLine::GetCommandLineString,
  100. Not(HasSubstr(FILE_PATH_LITERAL("--luci")))))))
  101. .Times(3);
  102. mock_pixel.Init("Prefix");
  103. bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
  104. EXPECT_TRUE(ret);
  105. }
  106. TEST_F(SkiaGoldPixelDiffTest, FuzzyMatching) {
  107. MockSkiaGoldPixelDiff mock_pixel;
  108. EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
  109. EXPECT_CALL(
  110. mock_pixel,
  111. LaunchProcess(AllOf(
  112. Property(
  113. &base::CommandLine::GetCommandLineString,
  114. HasSubstr(FILE_PATH_LITERAL(
  115. "--add-test-optional-key=image_matching_algorithm:fuzzy"))),
  116. Property(
  117. &base::CommandLine::GetCommandLineString,
  118. HasSubstr(FILE_PATH_LITERAL(
  119. "--add-test-optional-key=fuzzy_max_different_pixels:1"))),
  120. Property(
  121. &base::CommandLine::GetCommandLineString,
  122. HasSubstr(FILE_PATH_LITERAL(
  123. "--add-test-optional-key=fuzzy_pixel_delta_threshold:2"))))))
  124. .Times(1);
  125. mock_pixel.Init("Prefix");
  126. FuzzySkiaGoldMatchingAlgorithm algorithm(1, 2);
  127. bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap(), &algorithm);
  128. EXPECT_TRUE(ret);
  129. }
  130. TEST_F(SkiaGoldPixelDiffTest, FuzzyMatchingWithIgnoredBorder) {
  131. MockSkiaGoldPixelDiff mock_pixel;
  132. EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
  133. EXPECT_CALL(
  134. mock_pixel,
  135. LaunchProcess(AllOf(
  136. Property(
  137. &base::CommandLine::GetCommandLineString,
  138. HasSubstr(FILE_PATH_LITERAL(
  139. "--add-test-optional-key=image_matching_algorithm:fuzzy"))),
  140. Property(
  141. &base::CommandLine::GetCommandLineString,
  142. HasSubstr(FILE_PATH_LITERAL(
  143. "--add-test-optional-key=fuzzy_max_different_pixels:1"))),
  144. Property(
  145. &base::CommandLine::GetCommandLineString,
  146. HasSubstr(FILE_PATH_LITERAL(
  147. "--add-test-optional-key=fuzzy_pixel_delta_threshold:2"))),
  148. Property(
  149. &base::CommandLine::GetCommandLineString,
  150. HasSubstr(FILE_PATH_LITERAL("--add-test-optional-key=fuzzy_"
  151. "ignored_border_thickness:3"))))))
  152. .Times(1);
  153. mock_pixel.Init("Prefix");
  154. FuzzySkiaGoldMatchingAlgorithm algorithm(1, 2, 3);
  155. bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap(), &algorithm);
  156. EXPECT_TRUE(ret);
  157. }
  158. TEST_F(SkiaGoldPixelDiffTest, SobelMatching) {
  159. MockSkiaGoldPixelDiff mock_pixel;
  160. EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
  161. EXPECT_CALL(
  162. mock_pixel,
  163. LaunchProcess(AllOf(
  164. Property(
  165. &base::CommandLine::GetCommandLineString,
  166. HasSubstr(FILE_PATH_LITERAL(
  167. "--add-test-optional-key=image_matching_algorithm:sobel"))),
  168. Property(
  169. &base::CommandLine::GetCommandLineString,
  170. HasSubstr(FILE_PATH_LITERAL(
  171. "--add-test-optional-key=fuzzy_max_different_pixels:1"))),
  172. Property(
  173. &base::CommandLine::GetCommandLineString,
  174. HasSubstr(FILE_PATH_LITERAL(
  175. "--add-test-optional-key=fuzzy_pixel_delta_threshold:2"))),
  176. Property(&base::CommandLine::GetCommandLineString,
  177. HasSubstr(FILE_PATH_LITERAL(
  178. "--add-test-optional-key=sobel_edge_threshold:3"))),
  179. Property(
  180. &base::CommandLine::GetCommandLineString,
  181. HasSubstr(FILE_PATH_LITERAL("--add-test-optional-key=fuzzy_"
  182. "ignored_border_thickness:4"))))))
  183. .Times(1);
  184. mock_pixel.Init("Prefix");
  185. SobelSkiaGoldMatchingAlgorithm algorithm(1, 2, 3, 4);
  186. bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap(), &algorithm);
  187. EXPECT_TRUE(ret);
  188. }
  189. TEST_F(SkiaGoldPixelDiffTest, DefaultCorpus) {
  190. MockSkiaGoldPixelDiff mock_pixel;
  191. EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
  192. EXPECT_CALL(mock_pixel,
  193. LaunchProcess(AllOf(Property(
  194. &base::CommandLine::GetCommandLineString,
  195. HasSubstr(FILE_PATH_LITERAL("--corpus=gtest-pixeltests"))))))
  196. .Times(1);
  197. mock_pixel.Init("Prefix");
  198. bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
  199. EXPECT_TRUE(ret);
  200. }
  201. TEST_F(SkiaGoldPixelDiffTest, ExplicitCorpus) {
  202. MockSkiaGoldPixelDiff mock_pixel;
  203. EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
  204. EXPECT_CALL(mock_pixel,
  205. LaunchProcess(AllOf(
  206. Property(&base::CommandLine::GetCommandLineString,
  207. HasSubstr(FILE_PATH_LITERAL("--corpus=corpus"))))))
  208. .Times(1);
  209. mock_pixel.Init("Prefix", "corpus");
  210. bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
  211. EXPECT_TRUE(ret);
  212. }
  213. TEST_F(SkiaGoldPixelDiffTest, DefaultCodeReviewSystem) {
  214. auto* cmd_line = base::CommandLine::ForCurrentProcess();
  215. cmd_line->AppendSwitchASCII("gerrit-issue", "1");
  216. cmd_line->AppendSwitchASCII("gerrit-patchset", "2");
  217. cmd_line->AppendSwitchASCII("buildbucket-id", "3");
  218. MockSkiaGoldPixelDiff mock_pixel;
  219. EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
  220. EXPECT_CALL(mock_pixel, LaunchProcess(AllOf(Property(
  221. &base::CommandLine::GetCommandLineString,
  222. HasSubstr(FILE_PATH_LITERAL("--crs=gerrit"))))))
  223. .Times(1);
  224. mock_pixel.Init("Prefix");
  225. bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
  226. EXPECT_TRUE(ret);
  227. }
  228. TEST_F(SkiaGoldPixelDiffTest, ExplicitCodeReviewSystem) {
  229. auto* cmd_line = base::CommandLine::ForCurrentProcess();
  230. cmd_line->AppendSwitchASCII("gerrit-issue", "1");
  231. cmd_line->AppendSwitchASCII("gerrit-patchset", "2");
  232. cmd_line->AppendSwitchASCII("buildbucket-id", "3");
  233. cmd_line->AppendSwitchASCII("code-review-system", "new-crs");
  234. MockSkiaGoldPixelDiff mock_pixel;
  235. EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
  236. EXPECT_CALL(mock_pixel,
  237. LaunchProcess(
  238. AllOf(Property(&base::CommandLine::GetCommandLineString,
  239. HasSubstr(FILE_PATH_LITERAL("--crs=new-crs"))),
  240. Property(&base::CommandLine::GetCommandLineString,
  241. Not(HasSubstr(FILE_PATH_LITERAL("gerrit")))))))
  242. .Times(1);
  243. mock_pixel.Init("Prefix");
  244. bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
  245. EXPECT_TRUE(ret);
  246. }
  247. TEST_F(SkiaGoldPixelDiffTest, DryRunLocally) {
  248. auto* cmd_line = base::CommandLine::ForCurrentProcess();
  249. cmd_line->RemoveSwitch(switches::kTestLauncherBotMode);
  250. base::ScopedEnvironmentVariableOverride env_override(
  251. "CHROMIUM_TEST_LAUNCHER_BOT_MODE");
  252. MockSkiaGoldPixelDiff mock_pixel;
  253. EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
  254. EXPECT_CALL(
  255. mock_pixel,
  256. LaunchProcess(AllOf(Property(&base::CommandLine::GetCommandLineString,
  257. HasSubstr(FILE_PATH_LITERAL("--dryrun"))))))
  258. .Times(1);
  259. mock_pixel.Init("Prefix");
  260. bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
  261. EXPECT_TRUE(ret);
  262. }
  263. TEST_F(SkiaGoldPixelDiffTest, NotDryRunOnBots) {
  264. auto* cmd_line = base::CommandLine::ForCurrentProcess();
  265. cmd_line->AppendSwitch(switches::kTestLauncherBotMode);
  266. MockSkiaGoldPixelDiff mock_pixel;
  267. EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
  268. EXPECT_CALL(mock_pixel, LaunchProcess(AllOf(Property(
  269. &base::CommandLine::GetCommandLineString,
  270. Not(HasSubstr(FILE_PATH_LITERAL("--dryrun")))))))
  271. .Times(3);
  272. mock_pixel.Init("Prefix");
  273. bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
  274. EXPECT_TRUE(ret);
  275. }
  276. TEST_F(SkiaGoldPixelDiffTest, DryRunForTryjobWithoutPatch) {
  277. auto* cmd_line = base::CommandLine::ForCurrentProcess();
  278. cmd_line->AppendSwitch(switches::kTestLauncherBotMode);
  279. cmd_line->AppendSwitchASCII(switches::kTestLauncherBatchLimit, "1");
  280. MockSkiaGoldPixelDiff mock_pixel;
  281. EXPECT_CALL(mock_pixel, LaunchProcess(_)).Times(AnyNumber());
  282. EXPECT_CALL(
  283. mock_pixel,
  284. LaunchProcess(AllOf(Property(&base::CommandLine::GetCommandLineString,
  285. HasSubstr(FILE_PATH_LITERAL("--dryrun"))))))
  286. .Times(1);
  287. mock_pixel.Init("Prefix");
  288. bool ret = mock_pixel.CompareScreenshot("test", GetTestBitmap());
  289. EXPECT_TRUE(ret);
  290. }
  291. } // namespace test
  292. } // namespace ui