Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Add support for non-standard content/images scales #166

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

djrtl
Copy link

@djrtl djrtl commented Jun 7, 2016

Currently the framework uses [[UIScreen mainScreen] scale] for saving and comparing snapshots of views and layers.

If the view or layer are meant to be displayed at a different scale that doesn't match the main screen's one, the screenshots get resized (minified / magnified) and the comparisons are not correct any more from the point of view of the user, since the images are different from what will be shown to the user.

This PR tries to remove this limitation for those special cases by allowing the test developer to manually override the scale that will be used.

@ghost
Copy link

ghost commented Jun 7, 2016

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks!

@ghost
Copy link

ghost commented Jun 7, 2016

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@ghost ghost added the CLA Signed label Jun 7, 2016
@lyahdav
Copy link

lyahdav commented Jun 7, 2016

I'd like to write two tests for the same screen where there's both the 2x and 3x device scale cases. Is the pull request required for that?

@djrtl
Copy link
Author

djrtl commented Jun 8, 2016

@lyahdav I think this is what you're looking for.

The idea here is that the developer can indeed define views with different content scale factors or layers with different contents scales that are needed by the same instance of the app, and test them on any simulator/device regardless of what that device's main screen's scale is.

So inside a FBSnapshotTestCase you could write something like:

 - (void)testViewScale1
 {
      UIView* view = [self createMyViewWithScale:1];
      self.manualScale = 1;
      FBSnapshotVerifyView(view, nil);
 }

 - (void)testViewScale3
 {
      UIView* view = [self createMyViewWithScale:3];
      self.manualScale = 3;
      FBSnapshotVerifyView(view, nil);
 }

The two tests will go look for reference images called respectively testViewScale1.png and testViewScale3@3x.png, where the contents of the latter are the same of the former rendered at 3x3 times the resolution as you'd expect.

@victorBaro
Copy link

victorBaro commented Oct 4, 2017

Hello @djrtl
I really like your addition. I am using your fork right now but I run into a problem.

I have a very simple test:

func testProfile() {
    let customSizeFrame = ...
    let myView = ...
    view.frame = customSizeFrame
    manualScale = 2
    FBSnapshotVerifyView(view)
}

However, when I run it in a @2x scale device with recordMode = true to generate the image, and then switch to a @3x device (iPhone Plus basically) turning off recordMode, the test fails. It is very strange because both images are exactly the same (both 640x1136):

reference image

failed_testsimpleview2

failed image

failed_testsimpleview2

diff

diff_testsimpleview2

It seems like it is applying a wrong scale to the reference image when comparing both. Any idea how to fix this?

Thank you!

@djrtl
Copy link
Author

djrtl commented Oct 5, 2017

Hello @victorBaro,

I made this quite some time ago and I thought I had tested that situation, but then again maybe not. I'm sorry for that.

Right now it will be hard for me to look into this issue, I'll try to find the time in a few days.

In the meantime if you want to look into it yourself, you might want to take a look at where the comparisons between the pixel resolutions of the reference and new snapshot are made to try and understand why they fail in that use case, as my guess is that that is what is happening.

* Manual scale wasn't propagated properly to image capture and reference image selection methods
@djrtl
Copy link
Author

djrtl commented Oct 30, 2017

Hello @victorBaro,

I think my colleague might have fixed the issue you reported. Could you please let us know if the version that includes his changes works correctly for you?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants