tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestCairo.cpp (2738B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/
      3 */
      4 
      5 #include "cairo.h"
      6 
      7 #include "gtest/gtest.h"
      8 
      9 namespace mozilla {
     10 namespace layers {
     11 
     12 static void TryCircle(double centerX, double centerY, double radius) {
     13  printf("TestCairo:TryArcs centerY %f, radius %f\n", centerY, radius);
     14 
     15  cairo_surface_t* surf =
     16      cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 8, 21);
     17  ASSERT_TRUE(surf != nullptr);
     18 
     19  cairo_t* cairo = cairo_create(surf);
     20  ASSERT_TRUE(cairo != nullptr);
     21 
     22  cairo_set_antialias(cairo, CAIRO_ANTIALIAS_NONE);
     23  cairo_arc(cairo, 0.0, centerY, radius, 0.0, 6.2831853071795862);
     24  cairo_fill_preserve(cairo);
     25 
     26  cairo_surface_destroy(surf);
     27  cairo_destroy(cairo);
     28 }
     29 
     30 TEST(Cairo, Simple)
     31 {
     32  TryCircle(0.0, 0.0, 14.0);
     33  TryCircle(0.0, 1.0, 22.4);
     34  TryCircle(1.0, 0.0, 1422.4);
     35  TryCircle(1.0, 1.0, 3422.4);
     36  TryCircle(-10.0, 1.0, -2);
     37 }
     38 
     39 TEST(Cairo, Bug825721)
     40 {
     41  // OK:
     42  TryCircle(0.0, 0.0, 8761126469220696064.0);
     43  TryCircle(0.0, 1.0, 8761126469220696064.0);
     44 
     45  // OK:
     46  TryCircle(1.0, 0.0, 5761126469220696064.0);
     47 
     48  // This was the crash in 825721.  Note that centerY has to be non-zero,
     49  // and radius has to be not only large, but in particular range.
     50  // 825721 has a band-aid fix, where the crash is inevitable, but does
     51  // not fix the cause.  The same code crashes in cairo standalone.
     52  TryCircle(0.0, 1.0, 5761126469220696064.0);
     53 }
     54 
     55 TEST(Cairo, Bug1063486)
     56 {
     57  double x1, y1, x2, y2;
     58  const double epsilon = .01;
     59 
     60  cairo_surface_t* surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
     61  ASSERT_TRUE(surf != nullptr);
     62 
     63  cairo_t* cairo = cairo_create(surf);
     64  ASSERT_TRUE(cairo != nullptr);
     65 
     66  printf("Path 1\n");
     67  cairo_move_to(cairo, -20, -10);
     68  cairo_line_to(cairo, 20, -10);
     69  cairo_line_to(cairo, 20, 10);
     70  cairo_curve_to(cairo, 10, 10, -10, 10, -20, 10);
     71  cairo_curve_to(cairo, -30, 10, -30, -10, -20, -10);
     72 
     73  cairo_path_extents(cairo, &x1, &y1, &x2, &y2);
     74 
     75  ASSERT_LT(std::abs(-27.5 - x1), epsilon);  // the failing coordinate
     76  ASSERT_LT(std::abs(-10 - y1), epsilon);
     77  ASSERT_LT(std::abs(20 - x2), epsilon);
     78  ASSERT_LT(std::abs(10 - y2), epsilon);
     79 
     80  printf("Path 2\n");
     81  cairo_new_path(cairo);
     82  cairo_move_to(cairo, 10, 30);
     83  cairo_line_to(cairo, 90, 30);
     84  cairo_curve_to(cairo, 30, 30, 30, 30, 10, 30);
     85  cairo_curve_to(cairo, 0, 30, 0, 0, 30, 5);
     86 
     87  cairo_path_extents(cairo, &x1, &y1, &x2, &y2);
     88 
     89  ASSERT_LT(std::abs(4.019531 - x1), epsilon);  // the failing coordinate
     90  ASSERT_LT(std::abs(4.437500 - y1), epsilon);
     91  ASSERT_LT(std::abs(90. - x2), epsilon);
     92  ASSERT_LT(std::abs(30. - y2), epsilon);
     93 
     94  cairo_surface_destroy(surf);
     95  cairo_destroy(cairo);
     96 }
     97 
     98 }  // namespace layers
     99 }  // namespace mozilla