Skip to content

Rounded corners are not consistent #220

Closed

Description

@schloerke

Related #136
Related #4
Related rstudio/chromote#96

Rounded corners are not being produced consistently on the same platforms. Shadows and borders are particularly difficult as they are being calculated with a bit of randomness. I believe this is due to inconsistent sub pixel rounding and/or snapping.

Related chromium issues:

This was originally addressed in #136 by not adjusting the screen size within {chromote}. While the output cards were addressed, the change in {chromote} did not fix all rounded corner issues.

Example:

See red corners of both the modal and focused text in the image below. Red color means that a difference was found using the command gm compare old.png new.png -highlight-style assign -file diff.png.

image


Proposal:

Add a compare function to AppDriver$expect_sreenshot(compare=). Add a new comparison method that computes a "difference" metric and compare it against a set threshold.

The current compare function is set to testthat::compare_file_binary(old, new).

Proposed shinytest2::image_threshold(threshold = 0, size = 100) algorithm:

  • Compare images using testthat::compare_file_binary()
    • If successful, return success
  • Read in red, green, blue changes of each file
  • Find abs differences in each channel for each file
  • If threshold == 0 || size <= 1
    • Return sum(abs_diffs) == 0
  • Run convolution over diff matrix using a kernal w/ width and height set to size
  • total = sum(convo_vals)
  • Return total < threshold

Implementation would be done using {cpp11}.

Activity

  1. added this to the v0.2.0 milestone on Jun 3, 2022
  2. bersbersbers commented on Jun 23, 2022

    @bersbersbers
    Contributor

    Sorry to hear that #136 did not fix all rounded corners.

    In addition, I have come across a potentially related case which I like to report here so it can maybe inform the improved image comparison algorithm. Namely, with sliders:

    image

    I have yet to 100% exclude the possibility that this is timing-related, but so far, I don't have much reason to believe it is. Edit: I have already added 3-s waits before taking these snapshots, and the issue still comes up from time to time. With 5-s wait, it currently looks stable, but I somehow feel I only have to wait some more time for it to come back.

  3. schloerke commented on Jun 23, 2022

    @schloerke
    CollaboratorAuthor

    @bersbersbers Could you reply with the two snapshot files? It'll help me play with different threshold values recommendations. Thank you!

  4. bersbersbers commented on Jun 23, 2022

    @bersbersbers
    Contributor

    @schloerke sure, here they are. I cropped the full snapshots to the slider area since it's a non-public Shiny app, but since I did everything based on PNG files, these operations should all be lossless.

    image
    image

  5. jhen1115 commented on Sep 12, 2022

    @jhen1115

    Hi,

    May I know if the below red points around Back button relates to this issue (rounded corners are not consistent) as well?
    Because I found that the screenshots sometimes passed and sometimes failed in Linux, so I created small example to test in Linux and Windows.
    The generated screenshots in Windows more stable than Linux, but it still has this issue when I rerun the test many times in Windows (for example, please refer to Rerun_Tests.txt, the screenshots were different when the test was reran 64 times, but the "64" will be changed everytime. Sometimes, the screenshots were all passed when rerunning for 100 times).

    rintrojs

    The generated 2 png files as below:
    002
    002 new

  6. schloerke commented on Sep 12, 2022

    @schloerke
    CollaboratorAuthor

    @jhen1115 Exactly this situation. 😞


    @jhen1115 @bersbersbers, would you be willing to try a PR for me?

    I've added a fuzzy image comparison method that sweeps across the image and looks at the differences within a square kernel. If any absolute sum of the differences is larger than the threshold, then an error is thrown letting us know the images are different.

    With the default kernel size of 5 and 4 color channels, the max value is 5 * 5 * 4 = 100. This value will change given the kernel size.

    The code below using threshold should be more stable.

    @bersbersbers You can also supply your own comparison method if you'd like. app$expect_screenshot(compare = METHOD). If you have a method you like to use, I'd be keen to add it to the package.

    Thank you!

    remotes::install_github("rstudio/shinytest2#231")
    
    library(shinytest2)
    
    # .... Your regular app setup code
    
    app$expect_screenshot(threshold = 3) # Should pass
    app$expect_screenshot() # Might fail

    Using @jhen1115 's two images and #231...

    file1 <- tempfile(fileext = ".png")
    file2 <- tempfile(fileext = ".png")
    
    download.file("https://user-images.githubusercontent.com/85337074/189584759-86f3bb60-7fa4-4c93-8c11-deba719d8dc8.png", file1)
    download.file("https://user-images.githubusercontent.com/85337074/189584742-0a3c0818-aef0-4a52-ac24-e8ce863dc1f0.png", file2)
    
    shinytest2::screenshot_max_difference(file1, file2)
    #> [1] 0.02352941

    ... therefore a threshold value of 0.1 should be safe to use for this image.

    Ex:

    app <- # load jhen1115's app
    
    # Might fail eventually
    app$expect_screenshot(threshold = NULL)
    app$expect_screenshot(threshold = 0)
    app$expect_screenshot(threshold = 0.024)
    
    # Should pass
    app$expect_screenshot(threshold = 0.1)
  7. bersbersbers commented on Sep 14, 2022

    @bersbersbers
    Contributor

    @schloerke sorry for the delayed response - I am currently working non-R mostly, and the few times I have gone back to shinytest2, I actually haven't come across this issue any more (which is not too surprising giving the tremendous amount of updates of related CRAN packages). I'll try this PR (well, main branch now, I guess) this weekend to see if it breaks anything, but there will be limited information from this test as nothing else is broken at this time :)

  8. jhen1115 commented on Sep 14, 2022

    @jhen1115

    @schloerke Hi, I will check the the development version and get back to you this week.

  9. jhen1115 commented on Sep 15, 2022

    @jhen1115

    @schloerke Hi, I tested the development version in Windows and Linux, please find the findings as below.
    And may I know how to decide the value of threshold? If we only can decide the value of threshold when we find the small issues in screenshots?

    1. The app$expect_screenshot(threshold = 0.1) is work, and the session information and Chrome version as below:
    > sessionInfo()
    R version 4.2.1 (2022-06-23)
    Platform: x86_64-pc-linux-gnu (64-bit)
    Running under: Ubuntu 20.04.4 LTS
    
    Matrix products: default
    BLAS:   /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3
    LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3
    
    locale:
     [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8        LC_COLLATE=C.UTF-8    
     [5] LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8    LC_PAPER=C.UTF-8       LC_NAME=C             
     [9] LC_ADDRESS=C           LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
    [1] shiny_1.7.2           shinytest2_0.1.1.9000 testthat_3.1.4       
    
    loaded via a namespace (and not attached):
     [1] Rcpp_1.0.9        jquerylib_0.1.4   bslib_0.4.0       diffviewer_0.1.1  pillar_1.8.1      compiler_4.2.1   
     [7] later_1.3.0       tools_4.2.1       digest_0.6.29     pkgload_1.3.0     memoise_2.0.1     jsonlite_1.8.0   
    [13] lifecycle_1.0.2   tibble_3.1.8      checkmate_2.1.0   png_0.1-7         pkgconfig_2.0.3   rlang_1.0.5      
    [19] cli_3.4.0         rstudioapi_0.14   diffobj_0.3.5     yaml_2.3.5        curl_4.3.2        fastmap_1.1.0    
    [25] httr_1.4.4        withr_2.5.0       htmlwidgets_1.5.4 sass_0.4.2        desc_1.4.2        fs_1.5.2         
    [31] vctrs_0.4.1       rprojroot_2.0.3   websocket_1.4.1   chromote_0.1.1    glue_1.6.2        R6_2.5.1         
    [37] processx_3.7.0    fansi_1.0.3       callr_3.7.2       magrittr_2.0.3    htmltools_0.5.3   backports_1.4.1  
    [43] ps_1.7.1          promises_1.2.0.1  ellipsis_0.3.2    xtable_1.8-4      mime_0.12         httpuv_1.6.6     
    [49] utf8_1.2.2        cachem_1.0.6      crayon_1.5.1      brio_1.1.3       
    > chromote::ChromoteSession$new()$Browser$getVersion()$product
    [1] "HeadlessChrome/104.0.5112.79"
    
    1. New issue is found as below, it seems the shadows are different on top-left corner. But I'm not sure if the differences relates to new Chrome version (I cannot downgrade to old version to test).
      image
      The generated 2 png files as below:
      002
      002 new
      The difference value, session information, and Chrome version as below:
    > shinytest2::screenshot_max_difference("./tests/testthat/_snaps/windows-4.2/shinytest2/002.png", "./tests/testthat/_snaps/windows-4.2/shinytest2/002.new.png")
    [1] 0.7843137
    > sessionInfo()
    R version 4.2.1 (2022-06-23 ucrt)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 10 x64 (build 19042)
    
    Matrix products: default
    
    locale:
    [1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
    [3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
    [5] LC_TIME=English_United States.utf8    
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
    [1] shiny_1.7.2           shinytest2_0.1.1.9000 testthat_3.1.4       
    
    loaded via a namespace (and not attached):
     [1] Rcpp_1.0.9           jquerylib_0.1.4      bslib_0.4.0          diffviewer_0.1.1    
     [5] pillar_1.8.1         compiler_4.2.1       later_1.3.0          tools_4.2.1         
     [9] digest_0.6.29        pkgload_1.3.0        memoise_2.0.1        praise_1.0.0        
    [13] jsonlite_1.8.0       lifecycle_1.0.2      tibble_3.1.8         checkmate_2.1.0     
    [17] pkgconfig_2.0.3      png_0.1-7            rlang_1.0.5          cli_3.4.0           
    [21] rstudioapi_0.14      diffobj_0.3.5        yaml_2.3.5           curl_4.3.2          
    [25] fastmap_1.1.0        httr_1.4.4           withr_2.5.0          htmlwidgets_1.5.4   
    [29] sass_0.4.2           desc_1.4.2           fs_1.5.2             vctrs_0.4.1         
    [33] shinydashboard_0.7.2 rprojroot_2.0.3      websocket_1.4.1      chromote_0.1.1      
    [37] glue_1.6.2           R6_2.5.1             processx_3.7.0       fansi_1.0.3         
    [41] callr_3.7.2          magrittr_2.0.3       htmltools_0.5.3      backports_1.4.1     
    [45] ps_1.7.1             promises_1.2.0.1     ellipsis_0.3.2       xtable_1.8-4        
    [49] mime_0.12            httpuv_1.6.6         utf8_1.2.2           cachem_1.0.6        
    [53] crayon_1.5.1         brio_1.1.3          
    > chromote::ChromoteSession$new()$Browser$getVersion()$product
    [1] "HeadlessChrome/105.0.5195.127"
    
  10. schloerke commented on Sep 15, 2022

    @schloerke
    CollaboratorAuthor

    @jhen1115 So threshold is an experimental value. I'd guess that a max difference of < 1 means you have fairly similar images. The default max value is 5 * 5 * 4 = 100. So finding a max diff of 0.78 means that "Given a 5x5 pixel kernel and 4 color channels, it found that 0.78 / (5 * 5 * 4) * 100% = 0.78% difference between the two photos".

    Pseudo code:

    screenshot_max_difference <- function(png1, png2) 
      pixel_diffs <- png1 - png2
      max_value <- 0.0
      for each pixel_location in pixel_diffs
        pixel_color_channel_diffs <- pixel_diffs[pixel_location]
        pixel_diff <- sum(abs(pixel_color_channel_diffs))
        if (pixel_diff > max_value) 
          max_value <- pixel_diff
      max_value
    

    My interpretation from small empirical testing of the max value between two screenshots...
    If it is < 5, then it is close enough, otherwise I treat it as a different picture. So I would set the threshold to 5. But each user has different needs and that value could be 20 or it could even be 0 / NULL.


    Odd that Chrome made the whole top left corner different. Haven't seen that before!


    Thank you for testing!!

    If threshold is too annoying to set or things do not make sense, please open a new issue. I am hoping to release {shinytest2} next week

  11. jhen1115 commented on Sep 16, 2022

    @jhen1115

    @schloerke Thank you for your explanation, it's very helpful! The threshold can help us to ignore those small issues (such as rounded corners and top left corner different), and I'm looking forward to the release. Thank you!

  12. bersbersbers commented on Sep 16, 2022

    @bersbersbers
    Contributor

    image

    Hi,
    I have tested the main branch (6610a28, I believe) and it's working fine technically.

    Doing so, I have come across (first time in a while, but that's unrelated, I think) across the issue from #220 (comment). So I played around with the thresholds, and what I find it despite this being small shifts, the intensity change in a single pixel is rather larger. For my full images, I get:

    shinytest2::compare_screenshot_threshold("old.png", "new.png", threshold=0)
    ! Maximum screenshot convolution value `28.1176470588235` > `0` (threshold).
    
    > shinytest2::compare_screenshot_threshold("old.png", "new.png", threshold=0, kernel_size=5)
    ! Maximum screenshot convolution value `28.1176470588235` > `0` (threshold).
    
    > shinytest2::compare_screenshot_threshold("old.png", "new.png", threshold=0, kernel_size=6)
    ! Maximum screenshot convolution value `33.7411764705882` > `0` (threshold).
    
    > shinytest2::compare_screenshot_threshold("old.png", "new.png", threshold=0, kernel_size=7)
    ! Maximum screenshot convolution value `39.3647058823529` > `0` (threshold).
    
    > shinytest2::compare_screenshot_threshold("old.png", "new.png", threshold=0, kernel_size=8)
    ! Maximum screenshot convolution value `44.9882352941177` > `0` (threshold).
    
    > shinytest2::compare_screenshot_threshold("old.png", "new.png", threshold=0, kernel_size=10)
    ! Maximum screenshot convolution value `44.9882352941177` > `0` (threshold).
    
    > shinytest2::compare_screenshot_threshold("old.png", "new.png", threshold=0, kernel_size=15)
    ! Maximum screenshot convolution value `44.9882352941177` > `0` (threshold).
    
    > shinytest2::compare_screenshot_threshold("old.png", "new.png", threshold=0, kernel_size=20)
    ! Maximum screenshot convolution value `44.9882352941177` > `0` (threshold).
    

    which I interpret as 28% for 5x5 and 17% for 8x8 - rather large values to set as a global threshold for comparison.

    Just noting that here, keeping in mind that the original issue is about rounded corners and not subpixel position changes in text (but ultimately these can lead to the same user frustration).

    I wonder if one might set an average threshold as well (averaging the convolution value over the whole image) and comparing that to a threshold, and/or allow a defined number of pixels to fail while still accepting the snapshot (allthough I assume the number if failing pixels is still rather large in my case). Either way, it seems that AppDriver$expect_sreenshot(compare=) gives me/us enough freedom to explore such approaches - thank you :)

  13. schloerke commented on Sep 16, 2022

    @schloerke
    CollaboratorAuthor

    which I interpret as 28% for 5x5 and 17% for 8x8 - rather large values to set as a global threshold for comparison.

    Exactly!

    28.11 / (5 * 5 * 4) = 28.11%

    44.988 / (8 * 8 * 4) = 17.57%

    Agreed, these values are fairly large as the numbers and ticks have shifted. Agreed that it can / will lead to the same user frustration as text shifting.


    Doing so, I have come across (first time in a while, but that's unrelated, I think) across the issue from #220 (comment). So I played around with the thresholds, and what I find it despite this being small shifts, the intensity change in a single pixel is rather larger.

    Would it work to use a large kernel? Something like 30 or 50? If it is a small set of large differences, then the max value should roughly stay the same as a smaller kernel.

    Ex:
    kernel_size = 50;
    Max possible value = 10k
    So a difference of 45 / 10k is 0.45%.

    The speed of the algorithm actually gets faster the larger the kernel size. (O((m - k)(n - k)); m,n is image size; k is kernel_size)


    Either way, it seems that AppDriver$expect_sreenshot(compare=) gives me/us enough freedom to explore such approaches - thank you :)

    Yes! If you settle on a compare_screenshot_pixels(max = ) (or similar) method, please open an issue. I'd be happy to add more comparison methods.

  14. schloerke commented on Sep 16, 2022

    @schloerke
    CollaboratorAuthor

    Thank you both for the quick feedbacks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions