yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
yaze::test::ScreenshotAssertion Class Reference

Utilities for screenshot-based testing assertions. More...

#include <screenshot_assertion.h>

Collaboration diagram for yaze::test::ScreenshotAssertion:

Public Types

using CaptureCallback
 

Public Member Functions

 ScreenshotAssertion ()
 
 ~ScreenshotAssertion ()=default
 
void SetConfig (const ComparisonConfig &config)
 
const ComparisonConfigGetConfig () const
 
void SetCaptureCallback (CaptureCallback callback)
 
absl::StatusOr< ComparisonResultAssertMatchesReference (const std::string &reference_path)
 Assert current screen matches a reference image file.
 
absl::StatusOr< ComparisonResultAssertMatchesScreenshot (const Screenshot &expected)
 Assert current screen matches another Screenshot object.
 
absl::StatusOr< ComparisonResultAssertRegionMatches (const std::string &reference_path, const ScreenRegion &region)
 Assert a specific region matches reference.
 
absl::StatusOr< ComparisonResultAssertChanged (const std::string &baseline_name)
 Assert screen has changed since baseline.
 
absl::StatusOr< ComparisonResultAssertUnchanged (const std::string &baseline_name)
 Assert screen has NOT changed since baseline.
 
absl::Status CaptureBaseline (const std::string &name)
 Capture and store a baseline screenshot.
 
absl::Status SaveAsReference (const std::string &path)
 Save current screen as a new reference image.
 
absl::StatusOr< ScreenshotLoadReference (const std::string &path)
 Load a reference image from file.
 
absl::StatusOr< ScreenshotGetBaseline (const std::string &name) const
 Get a previously captured baseline.
 
void ClearBaselines ()
 Clear all captured baselines.
 
ComparisonResult Compare (const Screenshot &actual, const Screenshot &expected)
 Compare two screenshots.
 
ComparisonResult CompareRegion (const Screenshot &actual, const Screenshot &expected, const ScreenRegion &region)
 Compare specific regions of two screenshots.
 
absl::StatusOr< std::string > GenerateDiffImage (const Screenshot &actual, const Screenshot &expected, const std::string &output_path)
 Generate a visual diff image.
 
absl::StatusOr< bool > AssertPixelColor (int x, int y, uint8_t r, uint8_t g, uint8_t b, int tolerance=10)
 Assert pixel at (x, y) has expected color.
 
absl::StatusOr< bool > AssertRegionContainsColor (const ScreenRegion &region, uint8_t r, uint8_t g, uint8_t b, float min_coverage=0.1f)
 Assert region contains a specific color.
 
absl::StatusOr< bool > AssertRegionExcludesColor (const ScreenRegion &region, uint8_t r, uint8_t g, uint8_t b, int tolerance=10)
 Assert region does NOT contain a specific color.
 
absl::StatusOr< ScreenshotCaptureScreen ()
 Capture current screen and return it.
 

Static Public Member Functions

static absl::Status SaveScreenshot (const Screenshot &screenshot, const std::string &path)
 Save screenshot to file (PNG format).
 
static absl::StatusOr< ScreenshotLoadScreenshot (const std::string &path)
 Load screenshot from file.
 

Private Member Functions

ComparisonResult ComparePixelExact (const Screenshot &actual, const Screenshot &expected, const ScreenRegion &region)
 
ComparisonResult ComparePerceptualHash (const Screenshot &actual, const Screenshot &expected)
 
ComparisonResult CompareStructural (const Screenshot &actual, const Screenshot &expected)
 
bool ColorsMatch (uint8_t r1, uint8_t g1, uint8_t b1, uint8_t r2, uint8_t g2, uint8_t b2, int threshold) const
 
std::vector< ScreenRegionFindDifferingRegions (const Screenshot &actual, const Screenshot &expected, int threshold)
 

Private Attributes

ComparisonConfig config_
 
CaptureCallback capture_callback_
 
std::unordered_map< std::string, Screenshotbaselines_
 

Detailed Description

Utilities for screenshot-based testing assertions.

Provides tools for capturing, comparing, and asserting screenshot content for visual regression testing and AI-assisted verification.

Usage:

assertion.SetCaptureCallback(my_capture_func);
// Compare to reference
auto result = assertion.AssertMatchesReference("expected_state.png");
EXPECT_TRUE(result.passed) << result.error_message;
// Compare specific regions
auto result = assertion.AssertRegionMatches(
"canvas_expected.png",
ScreenRegion::At(100, 100, 256, 256));
// Check for visual changes
assertion.CaptureBaseline("before_edit");
// ... make changes ...
auto result = assertion.AssertChanged("before_edit");
Utilities for screenshot-based testing assertions.
absl::StatusOr< ComparisonResult > AssertMatchesReference(const std::string &reference_path)
Assert current screen matches a reference image file.
absl::StatusOr< ComparisonResult > AssertRegionMatches(const std::string &reference_path, const ScreenRegion &region)
Assert a specific region matches reference.
absl::Status CaptureBaseline(const std::string &name)
Capture and store a baseline screenshot.
absl::StatusOr< ComparisonResult > AssertChanged(const std::string &baseline_name)
Assert screen has changed since baseline.
void SetCaptureCallback(CaptureCallback callback)
static ScreenRegion At(int x, int y, int w, int h)

Definition at line 121 of file screenshot_assertion.h.

Member Typedef Documentation

◆ CaptureCallback

Initial value:
std::function<absl::StatusOr<Screenshot>()>

Definition at line 131 of file screenshot_assertion.h.

Constructor & Destructor Documentation

◆ ScreenshotAssertion()

yaze::test::ScreenshotAssertion::ScreenshotAssertion ( )
default

◆ ~ScreenshotAssertion()

yaze::test::ScreenshotAssertion::~ScreenshotAssertion ( )
default

Member Function Documentation

◆ SetConfig()

void yaze::test::ScreenshotAssertion::SetConfig ( const ComparisonConfig & config)
inline

Definition at line 127 of file screenshot_assertion.h.

References config_.

◆ GetConfig()

const ComparisonConfig & yaze::test::ScreenshotAssertion::GetConfig ( ) const
inline

Definition at line 128 of file screenshot_assertion.h.

References config_.

◆ SetCaptureCallback()

void yaze::test::ScreenshotAssertion::SetCaptureCallback ( CaptureCallback callback)
inline

Definition at line 133 of file screenshot_assertion.h.

References capture_callback_.

◆ AssertMatchesReference()

absl::StatusOr< ComparisonResult > yaze::test::ScreenshotAssertion::AssertMatchesReference ( const std::string & reference_path)

Assert current screen matches a reference image file.

Definition at line 15 of file screenshot_assertion.cc.

References CaptureScreen(), Compare(), config_, LoadReference(), and yaze::test::ComparisonConfig::tolerance.

Here is the call graph for this function:

◆ AssertMatchesScreenshot()

absl::StatusOr< ComparisonResult > yaze::test::ScreenshotAssertion::AssertMatchesScreenshot ( const Screenshot & expected)

Assert current screen matches another Screenshot object.

Definition at line 33 of file screenshot_assertion.cc.

References CaptureScreen(), Compare(), config_, and yaze::test::ComparisonConfig::tolerance.

Here is the call graph for this function:

◆ AssertRegionMatches()

absl::StatusOr< ComparisonResult > yaze::test::ScreenshotAssertion::AssertRegionMatches ( const std::string & reference_path,
const ScreenRegion & region )

Assert a specific region matches reference.

Definition at line 46 of file screenshot_assertion.cc.

References CaptureScreen(), CompareRegion(), config_, LoadReference(), and yaze::test::ComparisonConfig::tolerance.

Here is the call graph for this function:

◆ AssertChanged()

absl::StatusOr< ComparisonResult > yaze::test::ScreenshotAssertion::AssertChanged ( const std::string & baseline_name)

Assert screen has changed since baseline.

Definition at line 64 of file screenshot_assertion.cc.

References CaptureScreen(), Compare(), config_, GetBaseline(), and yaze::test::ComparisonConfig::tolerance.

Here is the call graph for this function:

◆ AssertUnchanged()

absl::StatusOr< ComparisonResult > yaze::test::ScreenshotAssertion::AssertUnchanged ( const std::string & baseline_name)

Assert screen has NOT changed since baseline.

Definition at line 83 of file screenshot_assertion.cc.

References CaptureScreen(), Compare(), config_, GetBaseline(), and yaze::test::ComparisonConfig::tolerance.

Here is the call graph for this function:

◆ CaptureBaseline()

absl::Status yaze::test::ScreenshotAssertion::CaptureBaseline ( const std::string & name)

Capture and store a baseline screenshot.

Definition at line 101 of file screenshot_assertion.cc.

References baselines_, CaptureScreen(), and LOG_DEBUG.

Here is the call graph for this function:

◆ SaveAsReference()

absl::Status yaze::test::ScreenshotAssertion::SaveAsReference ( const std::string & path)

Save current screen as a new reference image.

Definition at line 114 of file screenshot_assertion.cc.

References CaptureScreen(), and SaveScreenshot().

Here is the call graph for this function:

◆ LoadReference()

absl::StatusOr< Screenshot > yaze::test::ScreenshotAssertion::LoadReference ( const std::string & path)

Load a reference image from file.

Definition at line 123 of file screenshot_assertion.cc.

References LoadScreenshot().

Referenced by AssertMatchesReference(), and AssertRegionMatches().

Here is the call graph for this function:

◆ GetBaseline()

absl::StatusOr< Screenshot > yaze::test::ScreenshotAssertion::GetBaseline ( const std::string & name) const

Get a previously captured baseline.

Definition at line 128 of file screenshot_assertion.cc.

References baselines_.

Referenced by AssertChanged(), and AssertUnchanged().

◆ ClearBaselines()

void yaze::test::ScreenshotAssertion::ClearBaselines ( )
inline

Clear all captured baselines.

Definition at line 195 of file screenshot_assertion.h.

References baselines_.

◆ Compare()

ComparisonResult yaze::test::ScreenshotAssertion::Compare ( const Screenshot & actual,
const Screenshot & expected )

Compare two screenshots.

Definition at line 138 of file screenshot_assertion.cc.

References CompareRegion(), and yaze::test::ScreenRegion::FullScreen().

Referenced by AssertChanged(), AssertMatchesReference(), AssertMatchesScreenshot(), and AssertUnchanged().

Here is the call graph for this function:

◆ CompareRegion()

◆ GenerateDiffImage()

absl::StatusOr< std::string > yaze::test::ScreenshotAssertion::GenerateDiffImage ( const Screenshot & actual,
const Screenshot & expected,
const std::string & output_path )

◆ AssertPixelColor()

absl::StatusOr< bool > yaze::test::ScreenshotAssertion::AssertPixelColor ( int x,
int y,
uint8_t r,
uint8_t g,
uint8_t b,
int tolerance = 10 )

Assert pixel at (x, y) has expected color.

Parameters
xX coordinate
yY coordinate
rExpected red (0-255)
gExpected green (0-255)
bExpected blue (0-255)
tolerancePer-channel tolerance

Definition at line 325 of file screenshot_assertion.cc.

References CaptureScreen(), and ColorsMatch().

Here is the call graph for this function:

◆ AssertRegionContainsColor()

absl::StatusOr< bool > yaze::test::ScreenshotAssertion::AssertRegionContainsColor ( const ScreenRegion & region,
uint8_t r,
uint8_t g,
uint8_t b,
float min_coverage = 0.1f )

◆ AssertRegionExcludesColor()

absl::StatusOr< bool > yaze::test::ScreenshotAssertion::AssertRegionExcludesColor ( const ScreenRegion & region,
uint8_t r,
uint8_t g,
uint8_t b,
int tolerance = 10 )

Assert region does NOT contain a specific color.

Definition at line 371 of file screenshot_assertion.cc.

References AssertRegionContainsColor().

Here is the call graph for this function:

◆ CaptureScreen()

absl::StatusOr< Screenshot > yaze::test::ScreenshotAssertion::CaptureScreen ( )

◆ SaveScreenshot()

absl::Status yaze::test::ScreenshotAssertion::SaveScreenshot ( const Screenshot & screenshot,
const std::string & path )
static

Save screenshot to file (PNG format).

Todo
Implement proper PNG encoding

Definition at line 388 of file screenshot_assertion.cc.

References yaze::test::Screenshot::data, yaze::test::Screenshot::height, LOG_DEBUG, and yaze::test::Screenshot::width.

Referenced by GenerateDiffImage(), and SaveAsReference().

◆ LoadScreenshot()

absl::StatusOr< Screenshot > yaze::test::ScreenshotAssertion::LoadScreenshot ( const std::string & path)
static

◆ ComparePixelExact()

◆ ComparePerceptualHash()

ComparisonResult yaze::test::ScreenshotAssertion::ComparePerceptualHash ( const Screenshot & actual,
const Screenshot & expected )
private
Todo
Implement proper pHash algorithm

Definition at line 256 of file screenshot_assertion.cc.

References yaze::test::ComparisonResult::error_message.

Referenced by CompareRegion().

◆ CompareStructural()

ComparisonResult yaze::test::ScreenshotAssertion::CompareStructural ( const Screenshot & actual,
const Screenshot & expected )
private
Todo
Implement proper SSIM algorithm

Definition at line 265 of file screenshot_assertion.cc.

References yaze::test::ComparisonResult::error_message.

Referenced by CompareRegion().

◆ ColorsMatch()

bool yaze::test::ScreenshotAssertion::ColorsMatch ( uint8_t r1,
uint8_t g1,
uint8_t b1,
uint8_t r2,
uint8_t g2,
uint8_t b2,
int threshold ) const
private

◆ FindDifferingRegions()

std::vector< ScreenRegion > yaze::test::ScreenshotAssertion::FindDifferingRegions ( const Screenshot & actual,
const Screenshot & expected,
int threshold )
private
Todo
Implement region clustering for difference visualization

Definition at line 441 of file screenshot_assertion.cc.

Member Data Documentation

◆ config_

◆ capture_callback_

CaptureCallback yaze::test::ScreenshotAssertion::capture_callback_
private

Definition at line 270 of file screenshot_assertion.h.

Referenced by CaptureScreen(), and SetCaptureCallback().

◆ baselines_

std::unordered_map<std::string, Screenshot> yaze::test::ScreenshotAssertion::baselines_
private

Definition at line 271 of file screenshot_assertion.h.

Referenced by CaptureBaseline(), ClearBaselines(), and GetBaseline().


The documentation for this class was generated from the following files: