Class: AnimateIt::Verification

Inherits:
Object
  • Object
show all
Defined in:
lib/animate_it/verification.rb

Overview

Pixel-diff harness comparing the client player with legacy filmstrip.

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(composition:, host:, step: 10, threshold: 40.0, alpha_threshold: nil, output_dir: nil, playwright_cli: nil, props: {}, ready_timeout: 30_000) ⇒ Verification

Returns a new instance of Verification.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/animate_it/verification.rb', line 16

def initialize(
  composition:,
  host:,
  step: 10,
  threshold: 40.0,
  alpha_threshold: nil,
  output_dir: nil,
  playwright_cli: nil,
  props: {},
  ready_timeout: 30_000
)
  @composition = composition
  @host = host.delete_suffix("/")
  @step = [step.to_i, 1].max
  @threshold = threshold.to_f
  @alpha_threshold = alpha_threshold.nil? ? @threshold : alpha_threshold.to_f
  @output_dir = Pathname(output_dir || Rails.root.join("tmp/animate_it/verify/#{composition.id}"))
  @playwright_cli = playwright_cli || ENV.fetch("PLAYWRIGHT_CLI_EXECUTABLE_PATH", "npx playwright")
  @props = props.to_h
  @ready_timeout = [ready_timeout.to_i, 1].max
end

Instance Attribute Details

#alpha_thresholdObject (readonly)

Returns the value of attribute alpha_threshold.



14
15
16
# File 'lib/animate_it/verification.rb', line 14

def alpha_threshold
  @alpha_threshold
end

#compositionObject (readonly)

Returns the value of attribute composition.



14
15
16
# File 'lib/animate_it/verification.rb', line 14

def composition
  @composition
end

#hostObject (readonly)

Returns the value of attribute host.



14
15
16
# File 'lib/animate_it/verification.rb', line 14

def host
  @host
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



14
15
16
# File 'lib/animate_it/verification.rb', line 14

def output_dir
  @output_dir
end

#propsObject (readonly)

Returns the value of attribute props.



14
15
16
# File 'lib/animate_it/verification.rb', line 14

def props
  @props
end

#ready_timeoutObject (readonly)

Returns the value of attribute ready_timeout.



14
15
16
# File 'lib/animate_it/verification.rb', line 14

def ready_timeout
  @ready_timeout
end

#stepObject (readonly)

Returns the value of attribute step.



14
15
16
# File 'lib/animate_it/verification.rb', line 14

def step
  @step
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



14
15
16
# File 'lib/animate_it/verification.rb', line 14

def threshold
  @threshold
end

Instance Method Details

#callObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/animate_it/verification.rb', line 38

def call
  require "playwright"
  FileUtils.mkdir_p(output_dir)
  results = []

  Playwright.create(playwright_cli_executable_path: @playwright_cli) do |playwright|
    browser = playwright.chromium.launch(
      headless: true,
      args: [
        "--disable-web-security",
        "--disable-lcd-text",
        "--disable-gpu-compositing",
        "--force-color-profile=srgb"
      ]
    )
    begin
      context = browser.new_context(viewport: { width: composition.width, height: composition.height })
      legacy = open_page(context, "filmstrip")
      candidate = open_page(context, "player")

      sample_frames.each do |frame|
        legacy_shot = screenshot(legacy, frame, "legacy")
        candidate_shot = screenshot(candidate, frame, "player")
        results << result_for(frame, legacy_shot, candidate_shot)
      end
    ensure
      browser&.close
    end
  end

  results
end

#sample_framesObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/animate_it/verification.rb', line 71

def sample_frames
  max = composition.duration_in_frames - 1
  frames = (0..max).step(step).to_a
  composition.structure_layers.each do |layer|
    [layer.from_frame, layer.to_frame].each do |edge|
      frames.push(edge - 1, edge, edge + 1)
    end
  end
  frames.grep(0..max).sort.uniq
end