Created
July 15, 2024 00:37
-
-
Save kfischer-okarin/66f5956e07dba9ebfa59efdaa958ac88 to your computer and use it in GitHub Desktop.
Test Suite for Render Area
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe RenderArea do | |
define_helper :build_render_area do |args| | |
# Size of DR default render area (1280x720) | |
# This area is centered inside the allscreen area | |
args.grid.w ||= 1280 | |
args.grid.h ||= 720 | |
# logical pixel size of the whole screen (i.e. size of the render canvas in DR) | |
args.grid.allscreen_w ||= args.grid.w | |
args.grid.allscreen_h ||= args.grid.h | |
# actual pixel size of the whole screen | |
args.grid.allscreen_w_px ||= args.grid.allscreen_w | |
args.grid.allscreen_h_px ||= args.grid.allscreen_h | |
RenderArea.new(grid: args.grid) | |
end | |
# dp is defined as: 1 inch = 160 dp | |
# Screen Density = real screen pixels per inch | |
describe '#dp_to_px' do | |
it 'assumes screen density is 254' do | |
area = build_render_area(args) | |
assert.equal! area.dp_to_px(160), 254 | |
end | |
it 'rounds up to full pixels' do | |
area = build_render_area(args) | |
assert.equal! area.dp_to_px(164), 261 # 164dp -> 260.35px -> 261px | |
end | |
it 'considers scale' do | |
# scale 0.5 | |
args.grid.allscreen_w_px = 500 | |
args.grid.allscreen_w = 1000 | |
area = build_render_area(args) | |
# screen is scaled half so you need double the pixels to get the same physical dp size | |
assert.equal! area.dp_to_px(160), 508 | |
end | |
end | |
# 1 pt is defined as: 1 inch = 72 pt | |
describe '#pt_to_px' do | |
it 'assumes screen density is 254' do | |
area = build_render_area(args) | |
assert.equal! area.pt_to_px(72), 254 | |
end | |
it 'rounds up to full pixels' do | |
area = build_render_area(args) | |
assert.equal! area.pt_to_px(80), 283 # 80pt -> 282.22px -> 283px | |
end | |
it 'considers scale' do | |
# scale 0.5 | |
args.grid.allscreen_w_px = 500 | |
args.grid.allscreen_w = 1000 | |
area = build_render_area(args) | |
# screen is scaled half so you need double the pixels to get the same physical dp size | |
assert.equal! area.pt_to_px(72), 508 | |
end | |
end | |
describe '#px_to_dp' do | |
it 'assumes screen density is 254' do | |
area = build_render_area(args) | |
assert.equal! area.px_to_dp(254), 160 | |
end | |
it 'does not round' do | |
area = build_render_area(args) | |
assert.equal! area.px_to_dp(131).round(2), 82.52 | |
end | |
it 'considers scale' do | |
# scale 0.5 | |
args.grid.allscreen_w_px = 500 | |
args.grid.allscreen_w = 1000 | |
area = build_render_area(args) | |
# screen is scaled half so you need double the pixels to get the same physical dp size | |
assert.equal! area.px_to_dp(508), 160 | |
end | |
end | |
describe '#w' do | |
it 'returns width in dp' do | |
args.grid.allscreen_w = 254 | |
area = build_render_area(args) | |
assert.equal! area.w, 160 | |
end | |
end | |
describe '#h' do | |
it 'returns height in dp' do | |
args.grid.allscreen_h = 254 | |
area = build_render_area(args) | |
assert.equal! area.h, 160 | |
end | |
end | |
describe '#px_coordinates' do | |
it 'converts dp coordinates/rects to px' do | |
args.grid.w = 254 | |
args.grid.h = 254 | |
args.grid.allscreen_w = 254 * 3 | |
args.grid.allscreen_h = 254 * 3 | |
area = build_render_area(args) | |
[ | |
{ value: { x: 0, y: 0 }, expected: { x: -254, y: -254 } }, | |
{ value: { x: 160, y: 160 }, expected: { x: 0, y: 0 } }, | |
{ value: { x: 320, y: 320 }, expected: { x: 254, y: 254 } }, | |
{ value: { x: 0, y: 0, w: 160, h: 160 }, expected: { x: -254, y: -254, w: 254, h: 254 } }, | |
{ value: { x: 160, y: 160, w: 160, h: 160 }, expected: { x: 0, y: 0, w: 254, h: 254 } } | |
].each do |test_case| | |
assert.equal! area.px_coordinates(test_case[:value]), | |
test_case[:expected], | |
"Expected #{test_case[:value]} to be converted to #{test_case[:expected]}" | |
end | |
end | |
end | |
describe '#dp_coordinates' do | |
it 'converts px coordinates/rects to dp' do | |
args.grid.w = 254 | |
args.grid.h = 254 | |
args.grid.allscreen_w = 254 * 3 | |
args.grid.allscreen_h = 254 * 3 | |
area = build_render_area(args) | |
[ | |
{ value: { x: -254, y: -254 }, expected: { x: 0, y: 0 } }, | |
{ value: { x: 0, y: 0 }, expected: { x: 160, y: 160 } }, | |
{ value: { x: 254, y: 254 }, expected: { x: 320, y: 320 } }, | |
{ value: { x: -254, y: -254, w: 254, h: 254 }, expected: { x: 0, y: 0, w: 160, h: 160 } }, | |
{ value: { x: 0, y: 0, w: 254, h: 254 }, expected: { x: 160, y: 160, w: 160, h: 160 } } | |
].each do |test_case| | |
assert.equal! area.dp_coordinates(test_case[:value]), | |
test_case[:expected], | |
"Expected #{test_case[:value]} to be converted to #{test_case[:expected]}" | |
end | |
end | |
end | |
describe '#tick' do | |
it 'applies updated grid values' do | |
area = build_render_area(args) | |
original_rect = { x: 20, y: 30, w: 40, h: 50 } | |
before_change = area.px_coordinates(original_rect) | |
args.grid.allscreen_w_px *= 2 | |
args.grid.allscreen_h_px *= 2 | |
args.grid.w /= 2 | |
args.grid.h /= 2 | |
after_change = area.px_coordinates(original_rect) | |
# Because tick has not yet been called it will be the same | |
assert.equal! before_change, after_change | |
area.tick | |
after_tick = area.px_coordinates(original_rect) | |
assert.not_equal! before_change, after_tick | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment