Repository navigation
Expand file tree
/
Copy pathtest_view.cpp
More file actions
7912 lines (6874 loc) · 385 KB
/
Copy pathtest_view.cpp
File metadata and controls
7912 lines (6874 loc) · 385 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of the Diffractor photo and video organizer
// Copyright 2026 Zac Walker
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// Purpose: View-geometry tests. Covers the zoom model (fit, stepping, anchoring, panning and the
// navigator), the item selector / view scroller widgets, the hosts' pointer gestures - the items
// view's drag and the task lists' column dividers among them - the parts the keyboard reaches among
// drawn elements, and the Movie view's pure decisions (clip stepping, session retirement, stale
// completions).
#include "pch.h"
#include "test_fixtures.h"
#include "test_runner.h"
#include "files.h"
#include "model_zoom.h"
#include "ui_elements.h"
#include "ui_document.h"
#include "libdesktop/ui/views.h"
#include <numeric>
#include "model_document.h"
#include "ui_dialog.h"
#include "ui_panorama.h"
#include "app_sidebar.h"
#include "view_items.h"
#include "view_edit.h"
#include "view_list.h"
#include "view_locate.h"
#include "view_media.h"
#include "view_movie.h"
#include "view_movie_audio.h"
#include "view_selector.h"
#include "view_sync.h"
#include "view_tags.h"
#include "ui_sid.h"
#include "ui_tracker.h"
#include "ui_c64.h"
#include "libdesktop/ui/table_element.h"
#include "app_commands.h"
#include "libdesktop/render/software.h"
#include "libdesktop/ui/test_support.h"
extern std::function<void()> test_during_detach_file_handles;
static void assert_zoom_near(const double expected, const double actual, const std::string_view message)
{
assert_equal(true, std::abs(expected - actual) < 0.000001, message);
}
static void should_decide_when_movie_clip_sessions_are_retired()
{
assert_equal(false, should_retire_movie_clip_session_for_stop(movie_view_state::playing_t::nothing),
"stopping nothing leaves the clip session alone");
assert_equal(true, should_retire_movie_clip_session_for_stop(movie_view_state::playing_t::clip),
"stopping clip playback retires its session");
assert_equal(true, should_retire_movie_clip_session_for_stop(movie_view_state::playing_t::movie),
"stopping movie playback also releases the clip-player slot");
assert_equal(false, should_retire_movie_clip_session_for_document_focus_change(false),
"a document operation that cannot change focus need not close the clip session");
assert_equal(true, should_retire_movie_clip_session_for_document_focus_change(true),
"removing or replacing the focused clip must close the clip session first");
assert_equal(true, should_rewind_movie_clip_after_document_focus_change(true),
"and then park the new focused clip at its in point");
auto clip = make_movie_clip(core::file_path(core::windows_path_semantics ? "c:\\movie\\b.mp4" : "/movie/b.mp4"));
clip.start = 12.0;
assert_equal(12.0, rewound_movie_clip_playhead(&clip), "rewind parks at the focused clip's in point");
assert_equal(0.0, rewound_movie_clip_playhead(nullptr), "an empty timeline rewinds to zero");
}
// A playing movie's preview is composed at the movie's frames, as the render composes them, so its view is
// repainted when the playhead reaches another one rather than on every tick of the display.
static void should_repaint_a_playing_movie_once_a_movie_frame()
{
auto repaints = 0;
int64_t painted = -1;
for (auto tick = 0; tick < 60; ++tick)
{
const auto frame = movie_frame_at(tick / 60.0, 30);
if (frame != painted) ++repaints;
painted = frame;
}
assert_equal(30, repaints, "a second of a 60 Hz display repaints a 30 fps movie thirty times");
assert_equal(true, movie_frame_at(1.0 / 30.0 * 0.999, 30) == 0,
"a playhead short of the next frame is on this one");
assert_equal(true, movie_frame_at(1.0 / 30.0 * 1.001, 30) == 1, "and past it is on the next");
assert_equal(true, movie_frame_at(-1.0, 30) == 0, "a playhead before the start is on the first frame");
assert_equal(true, movie_frame_at(2.5, 0) == 2, "and a rate the project never set counts whole seconds");
}
static void should_step_movie_clips_from_the_playhead_while_playing()
{
std::vector<movie_clip> clips;
for (auto i = 0; i < 9; ++i)
{
auto clip = make_movie_clip(
core::file_path(core::windows_path_semantics ? "c:\\movie\\c.mp4" : "/movie/c.mp4"));
clip.start = 0.0;
clip.end = 10.0;
clips.emplace_back(clip);
}
// Clip i starts at 9i and its crossfade into clip i + 1 runs from 9(i + 1) to 9(i + 1) + 1.
const auto starts = calc_movie_timing(clips).starts;
constexpr size_t focus = 0;
// Counting from the focus, as the transport used to, makes the first answer 1.
assert_equal(size_t{7}, movie_step_target(starts, focus, true, 58.0, true),
"Next while clip 6 plays goes to clip 7, whatever is focused");
assert_equal(size_t{5}, movie_step_target(starts, focus, true, 58.0, false), "and Previous to clip 5");
// Counting from the clip that dominates the picture -- the outgoing one -- makes these 7: the
// incoming clip's start is still the outgoing clip at full weight, so Next would land there again.
assert_equal(size_t{8}, movie_step_target(starts, focus, true, starts[7], true),
"a second Next from the start Next landed on moves on");
assert_equal(size_t{8}, movie_step_target(starts, focus, true, starts[7] + 0.5, true),
"inside a crossfade the incoming clip is the one playing");
assert_equal(size_t{8}, movie_step_target(starts, focus, true, 80.0, true), "Next stops at the last clip");
assert_equal(size_t{0}, movie_step_target(starts, focus, true, 1.0, false), "and Previous at the first");
assert_equal(size_t{1}, movie_step_target(starts, focus, false, 58.0, true),
"stopped, stepping counts from the focus");
assert_equal(size_t{3}, movie_step_target(starts, 4, false, 58.0, false), "in both directions");
assert_equal(size_t{4}, movie_step_target({}, 4, true, 58.0, true), "an empty timeline leaves the focus");
}
static void should_reject_stale_movie_cache_completions_before_state_changes()
{
const auto stale_frame = decide_movie_frame_cache_completion(2, 1, true);
assert_equal(false, stale_frame.accept, "a stale frame answer is rejected");
assert_equal(false, stale_frame.clear_in_flight, "before it clears the current in-flight latch");
assert_equal(false, stale_frame.dispatch_next, "and before it dispatches a current queued request");
const auto current_frame = decide_movie_frame_cache_completion(2, 2, true);
assert_equal(true, current_frame.accept, "a current frame answer is accepted");
assert_equal(true, current_frame.clear_in_flight, "clears its own in-flight latch");
assert_equal(true, current_frame.dispatch_next, "and then dispatches the next queued request");
const auto current_last_frame = decide_movie_frame_cache_completion(2, 2, false);
assert_equal(true, current_last_frame.clear_in_flight, "a current final answer still clears the latch");
assert_equal(false, current_last_frame.dispatch_next, "but has no queue to dispatch");
const auto stale_peak = decide_movie_peak_completion(4, 3, true, true);
assert_equal(false, stale_peak.accept, "a stale peak answer is rejected");
assert_equal(false, stale_peak.clear_pending, "before it releases the new generation's pending scan");
const auto current_peak = decide_movie_peak_completion(4, 4, true, true);
assert_equal(true, current_peak.accept, "a current peak answer is accepted");
assert_equal(true, current_peak.clear_pending, "releases its own pending scan");
assert_equal(true, current_peak.store, "and is kept");
// The level track is drawn for one clip, so moving focus cancels the scan for the last. What a
// cancelled scan measured is part of a file, and kept it would read as a file with no sound.
const auto cancelled_peak = decide_movie_peak_completion(4, 4, true, false);
assert_equal(true, cancelled_peak.clear_pending, "a cancelled scan releases the request it answered");
assert_equal(false, cancelled_peak.store, "but what it measured is not kept");
const auto superseded_peak = decide_movie_peak_completion(4, 4, false, true);
assert_equal(false, superseded_peak.clear_pending, "a scan finishing after another was asked for leaves that one");
assert_equal(true, superseded_peak.store, "and is kept, having read the whole file");
}
// A paused preview was left on the frame the movie had played on to: the session had once been sent to
// the clip's in point, and that was still remembered, so parking there again sent nothing.
static void should_seek_a_movie_preview_back_to_where_it_was()
{
std::optional<double> sought = 2.0;
assert_equal(false, decide_movie_preview_seek(2.0, 2.0, 0.01, sought), "a session where it is wanted stays");
assert_equal(false, sought.has_value(), "and where it was sent is forgotten once it is there");
assert_equal(true, decide_movie_preview_seek(7.0, 2.0, 0.01, sought),
"parked back at the in point after playing on, it is sent there again");
assert_equal(false, decide_movie_preview_seek(7.0, 2.0, 0.01, sought), "once, while it is on its way");
assert_equal(true, decide_movie_preview_seek(7.0, 4.0, 0.01, sought), "a new destination is sought at once");
assert_equal(false, decide_movie_preview_seek(4.3, 4.0, 0.5, sought),
"and while playing, a session a little off its mark walks there rather than seeking");
}
// The out point is the first instant not kept, so the frame shown for it is the last one before it --
// one of the clip's own frames. A fixed 40 ms step back showed one two frames early at 60 fps.
static void should_show_the_movie_frame_at_a_trim_handle()
{
movie_clip clip;
clip.frame_rate = 60;
const auto nearest_frame = [](const double time)
{
return static_cast<int>(std::lround(time * 60));
};
assert_equal(119, nearest_frame(movie_last_kept_frame_time(clip, 0, 2.0)),
"an out point on a frame's start shows the frame before it");
assert_equal(120, nearest_frame(movie_last_kept_frame_time(clip, 0, 2.005)),
"one inside a frame shows that frame, which the movie keeps part of");
assert_equal(1.995, movie_last_kept_frame_time(clip, 1.995, 2.0), "and never one before the in point");
assert_near(1.0 / 120, movie_frame_period(clip) / 2, 1e-12, "it is decoded to the nearest frame of the clip's own");
clip.frame_rate = 0;
assert_near(0.04, movie_frame_period(clip), 1e-12, "a clip not yet measured uses the preview's step");
}
// Dragging the out handle to the start of a long clip leaves both handles on one pixel. The in handle
// won every tie, and against an in point of zero it cannot move, so the trim could not be dragged out.
static void should_let_either_movie_trim_handle_be_taken()
{
assert_equal(0, pick_movie_trim_handle(2, 5, false, 1, 9, 10), "the nearer handle is taken");
assert_equal(1, pick_movie_trim_handle(5, 2, true, 1, 9, 10), "whichever it is");
assert_equal(1, pick_movie_trim_handle(3, 3, true, 0, 0.1, 600),
"trimmed to the start, the out handle is the one that can move");
assert_equal(0, pick_movie_trim_handle(3, 3, false, 599.9, 600, 600), "trimmed to the end, the in handle is");
assert_equal(0, pick_movie_trim_handle(3, 3, true, 300, 300.1, 600), "between, the side of the pointer decides");
assert_equal(1, pick_movie_trim_handle(3, 3, false, 300, 300.1, 600), "either way");
}
// Play, Pause and Play again before the file had opened adopted the first session and then overwrote it
// without closing it; a clip whose file failed to open left the player "playing" for ever.
static void should_adopt_only_the_movie_clip_session_asked_for()
{
assert_equal(true, decide_movie_clip_session(true, true, true) == movie_clip_session_answer::adopt,
"the session for the latest Play is adopted");
assert_equal(true, decide_movie_clip_session(true, false, true) == movie_clip_session_answer::close,
"one a later Play replaced is closed, not adopted");
assert_equal(true, decide_movie_clip_session(true, true, false) == movie_clip_session_answer::close,
"as is one the player stopped wanting");
assert_equal(true, decide_movie_clip_session(false, true, true) == movie_clip_session_answer::stop_playing,
"a latest Play that could not open stops the player");
assert_equal(true, decide_movie_clip_session(false, false, true) == movie_clip_session_answer::ignore,
"but a failure nobody is waiting on changes nothing");
}
static void should_bound_movie_probe_retries_for_moving_sources()
{
assert_equal(true, decide_movie_changed_probe_action(1, 2) == movie_probe_changed_action::retry,
"the first changed-during-probe result is retried");
assert_equal(true, decide_movie_changed_probe_action(2, 2) == movie_probe_changed_action::retry,
"the last budgeted retry is still retryable");
assert_equal(true, decide_movie_changed_probe_action(3, 2) == movie_probe_changed_action::mark_missing,
"past the retry budget the clip reaches an explicit unresolved state");
const auto shared = core::file_path(core::windows_path_semantics ? "c:\\movie\\shared.mp4" : "/movie/shared.mp4");
std::vector<movie_probe_result> results(3);
for (auto& result : results)
{
result.path = shared;
result.changed_during_probe = true;
}
core::hash_map<core::file_path, int, core::ihash, core::ieq> retries;
const auto first = decide_movie_changed_probe_retries(results, retries, 2);
assert_equal(1_z, first.retry_paths.size(), "one changed source costs one retry, even with several clips");
assert_equal(0_z, first.missing_paths.size(), "so no shared clip is marked missing on the first movement");
assert_equal(1, retries[shared], "the retry counter is per source probe, not per clip");
const auto second = decide_movie_changed_probe_retries(results, retries, 2);
assert_equal(1_z, second.retry_paths.size(), "the second source movement is still retryable");
const auto third = decide_movie_changed_probe_retries(results, retries, 2);
assert_equal(0_z, third.retry_paths.size(), "the third movement exhausts the budget");
assert_equal(1_z, third.missing_paths.size(), "and records one missing source decision for every matching clip");
assert_equal(true, should_erase_movie_probe_retry_after_missing(true),
"an exhausted retry entry is erased once the source is recorded missing");
assert_equal(true, should_clear_movie_probe_retries_after_document_replace(true),
"a replaced Movie document starts with a fresh probe retry budget");
}
// A project's clip that runs to the end of its source -- written "-" or "2.5-" -- arrives with no end of
// its own. The probe is what measures its source, so the probe is what must give it the rest of that
// source -- the same rule as a video just added, shared by the view and the command line.
static void should_apply_a_movie_probe_to_the_clip_it_measured()
{
const auto path = core::file_path(core::windows_path_semantics ? "c:\\movie\\clip.mp4" : "/movie/clip.mp4");
movie_probe_result measured;
measured.path = path;
measured.found = true;
measured.duration = 30.0;
measured.extent = {640, 480};
measured.frame_rate = 25.0;
movie_clip untrimmed;
untrimmed.path = path;
const auto whole = apply_movie_probe(untrimmed, measured);
assert_equal(true, whole.is_probed && !whole.is_missing, "a measured source is present");
assert_equal(30.0, whole.end, "a clip with no end runs to the end of its source");
assert_equal(false, whole.is_trimmed(), "and is not shown as trimmed");
assert_equal(25.0, whole.frame_rate, "and takes the measured rate");
movie_clip start_only;
start_only.path = path;
start_only.start = 4.5;
const auto rest = apply_movie_probe(start_only, measured);
assert_equal(4.5, rest.start, "a clip trimmed only at its start keeps its in point");
assert_equal(30.0, rest.end, "and runs to the end of its source");
movie_clip overlong;
overlong.path = path;
overlong.start = 2.0;
overlong.end = 50.0;
const auto clamped = apply_movie_probe(overlong, measured);
assert_equal(2.0, clamped.start, "a stored trim keeps its in point");
assert_equal(30.0, clamped.end, "and is clamped to the source it measured");
auto gone = measured;
gone.found = false;
const auto lost = apply_movie_probe(overlong, gone);
assert_equal(true, lost.is_lost(), "a source that is not there is lost rather than unexamined");
assert_equal(50.0, lost.end, "and keeps its stored times for a relink");
auto still = measured;
still.is_photo = true;
still.duration = 0;
movie_clip photo;
photo.path = path;
const auto held = apply_movie_probe(photo, still);
assert_equal(true, held.is_photo, "a measured photo is a photo");
assert_equal(4.0, held.end, "and a photo with no hold takes the default one");
// A source probed afresh -- a relinked file, another of a SID file's tunes -- is heard again rather
// than judged by what the one before it sounded like.
auto heard = untrimmed;
heard.sound_checked_at = 0;
heard.sound_is_quiet = true;
const auto afresh = apply_movie_probe(heard, measured);
assert_equal(-1.0, afresh.sound_checked_at, "a source probed afresh is heard again");
assert_equal(false, afresh.has_no_sound(), "so a verdict about another source keeps no sound up over it");
}
// Two requests for one frame can both complete. Counting the second store without releasing the
// first leaked bytes until the cache sat permanently over budget, evicted every frame it stored, and
// the strip and preview decoded the same photo in a loop, holding a core at full load.
static void should_count_movie_cache_bytes_once_per_frame()
{
constexpr size_t frame = 4'000'000;
auto bytes = movie_cache_bytes_after_store(0, 0, frame);
assert_equal(frame, bytes, "a new frame adds its bytes");
bytes = movie_cache_bytes_after_store(bytes, frame, frame);
assert_equal(frame, bytes, "storing the same frame again replaces it rather than adding to it");
bytes = movie_cache_bytes_after_store(bytes, frame, frame / 2);
assert_equal(frame / 2, bytes, "a smaller replacement releases the difference");
assert_equal(10_z, movie_cache_bytes_after_store(5, 20, 10), "a stale replaced size cannot underflow");
}
// Movie drew and rendered the stored pixels of an Exif-rotated photo or a portrait phone video, so
// those clips came out sideways in the strip, the preview and the rendered file.
static void should_turn_movie_frames_upright()
{
assert_equal(true, sizei{1200, 1800} == upright_movie_extent({1800, 1200}, core::orientation::right_top),
"a quarter-turned source is measured upright");
assert_equal(true, sizei{1800, 1200} == upright_movie_extent({1800, 1200}, core::orientation::bottom_right),
"a half-turned source keeps its shape");
const auto source = std::make_shared<core::surface>();
source->alloc(3, 2, core::texture_format::RGB, core::orientation::right_top);
for (auto y = 0; y < 2; ++y)
{
for (auto x = 0; x < 3; ++x) source->set_pixel(x, y, core::rgba(10 + x + y * 3, 0, 0));
}
const auto turned = upright_movie_frame(source);
assert_equal(true, sizei{2, 3} == turned->dimensions(), "the frame is turned upright");
assert_equal(static_cast<int>(core::orientation::top_left), static_cast<int>(turned->orientation()),
"and no longer asks to be turned again");
// Orientation 6 stores the visual top as column 0 and the visual right as row 0, so each displayed
// corner is a known stored pixel - an expectation that does not borrow the transform under test.
assert_equal(source->get_pixel(0, 1), turned->get_pixel(0, 0), "top left is the stored bottom of column 0");
assert_equal(source->get_pixel(0, 0), turned->get_pixel(1, 0), "top right is the stored top of column 0");
assert_equal(source->get_pixel(2, 1), turned->get_pixel(0, 2), "bottom left is the stored bottom of column 2");
assert_equal(source->get_pixel(2, 0), turned->get_pixel(1, 2), "bottom right is the stored top of column 2");
const auto upright = std::make_shared<core::surface>();
upright->alloc(3, 2, core::texture_format::RGB, core::orientation::top_left);
assert_equal(true, upright_movie_frame(upright) == core::const_surface_ptr(upright),
"an upright frame is used as it is");
// The mirrored orientations were left as stored: unmirrored, and sideways for 5 and 7, though the
// extent measured them upright. Each displayed corner against the stored pixel EXIF puts there.
struct mirrored
{
core::orientation orientation;
sizei extent;
pointi top_left, top_right, bottom_left;
};
for (const auto& m : {mirrored{core::orientation::top_right, {3, 2}, {2, 0}, {0, 0}, {2, 1}},
mirrored{core::orientation::bottom_left, {3, 2}, {0, 1}, {2, 1}, {0, 0}},
mirrored{core::orientation::left_top, {2, 3}, {0, 0}, {0, 1}, {2, 0}},
mirrored{core::orientation::right_bottom, {2, 3}, {2, 1}, {2, 0}, {0, 1}}})
{
const auto name = std::format("orientation {}", static_cast<int>(m.orientation));
source->orientation(m.orientation);
const auto shown = upright_movie_frame(source);
assert_equal(true, m.extent == shown->dimensions(), name + " is measured upright");
assert_equal(true, m.extent == upright_movie_extent({3, 2}, m.orientation), name + " as the extent says");
assert_equal(static_cast<int>(core::orientation::top_left), static_cast<int>(shown->orientation()),
name + " is no longer to be turned");
assert_equal(source->get_pixel(m.top_left.x, m.top_left.y), shown->get_pixel(0, 0), name + " top left");
assert_equal(source->get_pixel(m.top_right.x, m.top_right.y), shown->get_pixel(m.extent.cx - 1, 0),
name + " top right");
assert_equal(source->get_pixel(m.bottom_left.x, m.bottom_left.y), shown->get_pixel(0, m.extent.cy - 1),
name + " bottom left");
}
const auto target = recti(0, 0, 20, 30);
assert_equal(true, upright_movie_destination(target, core::orientation::top_left) == quadd(target),
"an upright texture draws into its target unchanged");
assert_equal(false, upright_movie_destination(target, core::orientation::right_top) == quadd(target),
"a turned texture is drawn through a turned quad");
}
static void should_retire_movie_source_caches_only_after_successful_replacement()
{
assert_equal(false, should_retire_movie_sources_after_project_change(false),
"a cancelled or failed Open leaves the current source generation alone");
assert_equal(true, should_retire_movie_sources_after_project_change(true),
"a successful reload or relink retires source caches and preview sessions");
assert_equal(true, should_clear_movie_preview_audio_after_source_retire(true),
"retiring source caches also clears same-path preview PCM windows");
const auto old_generation = 7;
const auto new_generation = next_movie_preview_audio_source_generation(old_generation);
assert_equal(false, should_accept_movie_preview_pcm_loaded(new_generation, old_generation),
"a PCM read that started before source retirement cannot refill the same key");
assert_equal(true, should_accept_movie_preview_pcm_loaded(new_generation, new_generation),
"a read from the current source generation can fill the window");
}
static df::item_element_ptr make_versioned_photo_item(const core::file_path path, const core::date_t file_modified,
const core::date_t thumbnail_modified)
{
df::index_file_item info;
info.ft = files::file_type_from_name(path.name());
info.file_created = file_modified;
info.file_modified = file_modified;
auto result = std::make_shared<df::item_element>(path, info);
result->thumbnail(nullptr, nullptr, thumbnail_modified);
return result;
}
static void update_versioned_photo_item(const df::item_element_ptr& item, const core::file_path path,
const core::date_t file_modified, const core::date_t thumbnail_modified)
{
df::index_file_item info;
info.ft = files::file_type_from_name(path.name());
info.file_created = file_modified;
info.file_modified = file_modified;
item->update(path, info);
item->thumbnail(nullptr, nullptr, thumbnail_modified);
}
// MOD-016 - display freshness compares the source file stamp for inequality, not a monotone maximum
// or the late-arriving thumbnail stamp.
static void should_reload_displayed_photo_when_source_version_moves_backward()
{
deferred_async_strategy as;
const auto path = core::file_path("c:\\source-version.jpg");
const auto item = make_versioned_photo_item(path, core::date_t(200), core::date_t(200));
auto texture = std::make_shared<texture_state>(as, item);
texture->load_image(item);
assert_equal(1_z, as.pending_worker_count(async_queue::load), "the initial load is queued");
update_versioned_photo_item(item, path, core::date_t(100), core::date_t(200));
texture->refresh(item);
assert_equal(2_z, as.pending_worker_count(async_queue::load),
"a changed file stamp reloads even when the maximum timestamp is unchanged");
texture->refresh(item);
assert_equal(2_z, as.pending_worker_count(async_queue::load), "the same tuple does not reload again");
update_versioned_photo_item(item, path, core::date_t(100), core::date_t(100));
texture->refresh(item);
assert_equal(2_z, as.pending_worker_count(async_queue::load),
"thumbnail stamp changes alone do not full-decode the displayed photo again");
texture->mark_visuals_current();
update_versioned_photo_item(item, path, core::date_t(50), core::date_t(100));
texture->refresh(item);
assert_equal(2_z, as.pending_worker_count(async_queue::load),
"a metadata-only write keeps the current visual across its new stamp");
texture->refresh(item);
assert_equal(2_z, as.pending_worker_count(async_queue::load),
"the retained metadata-only version is adopted rather than reloading later");
file_load_result written;
written.success = true;
const auto written_stamp = core::date_t(2026, 1, 5, 12, 0, 0);
texture->publish_written_image(path, std::move(written), written_stamp);
update_versioned_photo_item(item, path, written_stamp, {});
texture->refresh(item);
assert_equal(2_z, as.pending_worker_count(async_queue::load),
"a written image adopted from the save is not read back again");
}
// MOD-017 - texture-budget eviction skips displayed entries and keeps looking for eligible offscreen
// entries rather than stopping at a displayed tail.
static void should_skip_displayed_textures_when_evicting_retained_texture_budget()
{
const std::array entries{
retained_texture_budget_entry{false, 10},
retained_texture_budget_entry{false, 20},
retained_texture_budget_entry{false, 30},
retained_texture_budget_entry{true, 0},
};
const auto evicted = retained_texture_eviction_indexes(entries, 30);
assert_equal(1_z, evicted.size(), "only enough offscreen entries are evicted");
assert_equal(2_z, evicted.front(), "the oldest displayed tail is skipped for the next eligible entry");
const std::array two_displayed{
retained_texture_budget_entry{false, 10},
retained_texture_budget_entry{true, 0},
retained_texture_budget_entry{false, 30},
retained_texture_budget_entry{true, 0},
};
const auto evicted_with_two_displayed = retained_texture_eviction_indexes(two_displayed, 10);
assert_equal(1_z, evicted_with_two_displayed.size(), "displayed entries do not count against the budget");
assert_equal(2_z, evicted_with_two_displayed.front(), "the active entries survive while offscreen cache shrinks");
}
struct texture_release_counts
{
std::atomic_int worker = 0;
int ui = 0;
};
static texture_state_ptr make_counted_texture(async_strategy& as, const df::item_element_ptr& item,
texture_release_counts& counts)
{
return texture_state_ptr(new texture_state(as, item), [&counts](texture_state* texture)
{
if (platform::is_ui_thread()) ++counts.ui;
else ++counts.worker;
delete texture;
});
}
class media_preview_test_async final : public null_async_strategy
{
std::deque<std::function<void()>> _ui;
std::function<void(media_preview_state&)> _preview;
public:
void queue_ui(const std::function<void()> f) override { _ui.emplace_back(f); }
void queue_media_preview(std::function<void(media_preview_state&)> f, bool) override { _preview = std::move(f); }
void drain_ui()
{
while (!_ui.empty())
{
auto f = std::move(_ui.front());
_ui.pop_front();
f();
}
}
size_t pending_ui_count() const { return _ui.size(); }
void drop_preview_on_worker()
{
auto preview = std::move(_preview);
std::thread worker([preview = std::move(preview)]() mutable {});
worker.join();
}
void run_preview_on_worker()
{
auto preview = std::move(_preview);
std::thread worker([preview = std::move(preview)]() mutable
{
media_preview_state state;
preview(state);
});
worker.join();
}
};
// MOD-018 - comparison preview carries UI-owned texture identity through success, stale rejection,
// and dropped worker-task exits, so final texture release is posted back to the UI queue.
static void should_release_comparison_preview_textures_on_ui_after_worker_exit()
{
const auto path1 = core::file_path("c:\\compare-a.jpg");
const auto path2 = core::file_path("c:\\compare-b.jpg");
const auto run_case = [&](const bool run_preview, const bool keep_current_until_ui, const std::string_view label)
{
media_preview_test_async as;
common_display_state_t common;
auto display = std::make_shared<display_state_t>(as, common);
texture_release_counts counts;
auto texture1 = make_counted_texture(as, make_versioned_photo_item(path1, core::date_t(1), {}), counts);
auto texture2 = make_counted_texture(as, make_versioned_photo_item(path2, core::date_t(1), {}), counts);
display->_selected_texture1 = texture1;
display->_selected_texture2 = texture2;
display->calc_pixel_difference();
texture1.reset();
texture2.reset();
if (!keep_current_until_ui)
{
display->_selected_texture1.reset();
display->_selected_texture2.reset();
}
if (run_preview) as.run_preview_on_worker();
else as.drop_preview_on_worker();
assert_equal(0, counts.worker.load(), std::format("{}: worker releases no texture", label));
assert_equal(true, as.pending_ui_count() >= (run_preview ? 1_z : 2_z),
std::format("{}: hand-back reaches UI queue", label));
as.drain_ui();
if (keep_current_until_ui)
{
assert_equal(0, counts.worker.load(),
std::format("{}: successful callback still releases no worker texture", label));
assert_equal(0, counts.ui, std::format("{}: display still owns the successful textures", label));
display->_selected_texture1.reset();
display->_selected_texture2.reset();
}
assert_equal(0, counts.worker.load(), std::format("{}: final release is not on the worker", label));
assert_equal(2, counts.ui, std::format("{}: both texture identities release on UI", label));
};
run_case(false, false, "early return");
run_case(true, false, "stale rejection");
run_case(true, true, "success");
}
static void should_select_settled_zoom_sampler()
{
assert_equal(true,
calc_sampler({1000, 500}, {1000, 500}, core::orientation::top_left) == render::texture_sampler::point,
"one-to-one is exact");
assert_equal(true,
calc_sampler({1003, 500}, {1000, 500}, core::orientation::top_left) ==
render::texture_sampler::bicubic,
"just above one-to-one is settled smooth");
assert_equal(true,
calc_sampler({1500, 750}, {1000, 500}, core::orientation::top_left) ==
render::texture_sampler::bicubic,
"one and a half times is settled smooth");
assert_equal(true,
calc_sampler({3000, 1500}, {1000, 500}, core::orientation::top_left) ==
render::texture_sampler::bicubic,
"three times remains smooth");
assert_equal(true,
calc_sampler({3001, 1500}, {1000, 500}, core::orientation::top_left) == render::texture_sampler::point,
"above three times keeps source pixels exact");
assert_equal(true,
calc_sampler({3001, 1500}, {1000, 500}, core::orientation::top_left, false, true) ==
render::texture_sampler::bicubic,
"a magnified stand-in stays smooth rather than blocky");
assert_equal(true,
calc_sampler({1000, 500}, {1000, 500}, core::orientation::top_left, false, true) ==
render::texture_sampler::point,
"a stand-in at one-to-one is still exact");
assert_equal(true,
calc_sampler({1003, 500}, {1000, 500}, core::orientation::top_left, true) ==
render::texture_sampler::bilinear,
"interactive magnification just above one-to-one uses fast sampler");
assert_equal(true,
calc_sampler({1500, 750}, {1000, 500}, core::orientation::top_left, true) ==
render::texture_sampler::bilinear,
"interactive magnification uses fast sampler");
assert_equal(true,
calc_sampler({3001, 1500}, {1000, 500}, core::orientation::top_left, true) ==
render::texture_sampler::point,
"interactive magnification above three times keeps source pixels exact");
}
// A video frame is drawn at the size it was decoded, so a picture smaller than it is minified by the sampler
// on every frame, where Catmull-Rom aliases as bilinear does at five times the texture reads. The picture's
// own extent decides, not the pane it is fitted into.
static void should_select_video_sampler_by_picture_size()
{
assert_equal(true,
calc_video_sampler({960, 540}, {3840, 2160}, core::orientation::top_left) ==
render::texture_sampler::bilinear,
"a 4K frame drawn at a quarter of its size is bilinear");
assert_equal(true,
calc_video_sampler({1400, 787}, {1920, 1080}, core::orientation::top_left) ==
render::texture_sampler::bilinear,
"below three quarters is bilinear");
assert_equal(true,
calc_video_sampler({1500, 844}, {1920, 1080}, core::orientation::top_left) ==
render::texture_sampler::bicubic,
"just below one-to-one keeps its edges");
assert_equal(true,
calc_video_sampler({1920, 1080}, {1920, 1080}, core::orientation::top_left) ==
render::texture_sampler::point,
"a picture at its decoded size is exact, whatever pane it is fitted into");
assert_equal(true,
calc_video_sampler({2880, 1620}, {1920, 1080}, core::orientation::top_left) ==
render::texture_sampler::bicubic,
"a magnified picture is smooth");
assert_equal(true,
calc_video_sampler({2880, 1620}, {1920, 1080}, core::orientation::top_left, true) ==
render::texture_sampler::bilinear,
"and fast while interacting");
}
static void should_accumulate_precision_wheel_deltas()
{
// The frame boundary accumulates the raw platform delta, one detent being 120.
auto pending = 0.0;
assert_equal(0, platform::accumulate_wheel_steps(pending, 40.0), "first partial detent");
assert_equal(0, platform::accumulate_wheel_steps(pending, 40.0), "second partial detent");
assert_equal(0, platform::accumulate_wheel_steps(pending, 39.0), "detent not rounded early");
assert_equal(1, platform::accumulate_wheel_steps(pending, 1.0), "exact detent steps once");
assert_zoom_near(0.0, pending, "positive detent consumed exactly");
assert_equal(-2, platform::accumulate_wheel_steps(pending, -240.0), "multiple reverse detents");
assert_zoom_near(0.0, pending, "negative detents consumed exactly");
// The scroll answer is quantised to whole logical units by the same carry, so a device reporting
// a single raw unit at a time scrolls rather than halving to nothing on every event.
auto scroll = 0.0;
assert_equal(0, platform::accumulate_wheel_steps(scroll, 1.0 / 2.0, 1.0), "half a unit is carried, not dropped");
assert_equal(1, platform::accumulate_wheel_steps(scroll, 1.0 / 2.0, 1.0), "two halves make a unit");
assert_equal(60, platform::accumulate_wheel_steps(scroll, 120.0 / 2.0, 1.0),
"a whole detent scrolls its full amount");
}
static void should_validate_zoom_navigator_mode()
{
assert_equal(true, is_valid_zoom_navigator_mode(static_cast<uint32_t>(zoom_navigator_mode::auto_hide)),
"auto-hide is valid");
assert_equal(true, is_valid_zoom_navigator_mode(static_cast<uint32_t>(zoom_navigator_mode::pinned)),
"pinned is valid");
assert_equal(true, is_valid_zoom_navigator_mode(static_cast<uint32_t>(zoom_navigator_mode::off)), "off is valid");
assert_equal(false, is_valid_zoom_navigator_mode(3), "unknown mode is rejected");
}
static void should_keep_comparison_zoom_panes_matched()
{
df::comparison_zoom_state zoom;
zoom.mutate([](df::zoom_view_state& state) { state.set_explicit(2.0, {0.25, 0.75}); });
assert_zoom_near(2.0, zoom.state(df::zoom_pane::primary).explicit_scale(), "primary scale changes");
assert_zoom_near(2.0, zoom.state(df::zoom_pane::secondary).explicit_scale(), "secondary follows");
assert_zoom_near(0.25, zoom.state(df::zoom_pane::secondary).center().X, "secondary adopts center");
zoom.active(df::zoom_pane::secondary);
assert_equal(true, zoom.active() == df::zoom_pane::secondary, "active pane switches");
zoom.mutate([](df::zoom_view_state& state) { state.set_explicit(4.0, {0.75, 0.25}); });
assert_zoom_near(4.0, zoom.state(df::zoom_pane::primary).explicit_scale(), "primary follows the active pane");
assert_zoom_near(0.75, zoom.state(df::zoom_pane::primary).center().X, "primary adopts center");
assert_equal(true, df::comparison_zoom_state::other(df::zoom_pane::primary) == df::zoom_pane::secondary,
"flip target from primary");
assert_equal(true, df::comparison_zoom_state::other(df::zoom_pane::secondary) == df::zoom_pane::primary,
"flip target from secondary");
}
static void should_calculate_zoom_fit_without_enlarging()
{
assert_zoom_near(0.5, df::zoom_view_state::fit_scale({4000, 2000}, {2000, 1200}), "large image fit");
assert_zoom_near(1.0, df::zoom_view_state::fit_scale({200, 100}, {2000, 1200}), "small image fit");
assert_zoom_near(10.0, df::zoom_view_state::fit_scale({200, 100}, {2000, 1200}, true), "fit and enlarge");
}
static void should_recalculate_zoom_fit_variants()
{
df::zoom_view_state zoom;
constexpr sized source{4000, 3000};
zoom.fit_width(source, {1000, 500});
assert_equal(true, zoom.mode() == df::zoom_scale_mode::fit_width, "fit width mode");
assert_zoom_near(0.25, zoom.effective_scale(0.125), "fit width scale");
zoom.update_fit_variant(source, {2000, 500});
assert_zoom_near(0.5, zoom.effective_scale(0.1666666667), "fit width follows viewport");
zoom.fill(source, {1000, 1000});
assert_equal(true, zoom.mode() == df::zoom_scale_mode::fill, "fill mode");
assert_zoom_near(1.0 / 3.0, zoom.effective_scale(0.25), "fill covers viewport");
const auto geometry = zoom.geometry(source, {1000, 1000}, 0.25);
assert_zoom_near(1000.0, geometry.destination.Height, "fill height");
assert_zoom_near(4000.0 / 3.0, geometry.destination.Width, "fill crops width");
}
// update_fit_variant runs on every layout - a resize, a panel opening, a sidebar toggle. It has to
// follow the viewport with the scale, but where the user panned to is theirs: recentring here threw
// a Fit width or Fill pan away every time the window changed shape. Choosing the command still
// recentres, because that is what the command says it does.
static void should_keep_the_pan_when_a_fit_variant_refits()
{
df::zoom_view_state zoom;
constexpr sized source{4000, 3000};
zoom.fit_width(source, {1000, 500});
assert_zoom_near(0.5, zoom.center().X, "the command centres the picture");
assert_zoom_near(0.5, zoom.center().Y, "on both axes");
zoom.set_center({0.5, 0.8});
assert_zoom_near(0.8, zoom.center().Y, "the user pans down");
zoom.update_fit_variant(source, {2000, 500});
assert_zoom_near(0.5, zoom.effective_scale(0.1666666667), "the scale follows the new viewport");
assert_zoom_near(0.8, zoom.center().Y, "and the pan survives the relayout");
// The command itself still recentres.
zoom.fit_width(source, {2000, 500});
assert_zoom_near(0.5, zoom.center().Y, "choosing fit width again recentres");
// Fill behaves the same way.
zoom.fill(source, {1000, 1000});
zoom.set_center({0.3, 0.5});
zoom.update_fit_variant(source, {1200, 1000});
assert_zoom_near(0.3, zoom.center().X, "a fill pan survives a relayout too");
}
static void should_step_zoom_reversibly_through_fit()
{
df::zoom_view_state zoom;
constexpr sized source{4000, 2000};
constexpr sized viewport{1600, 1000};
const auto fit = zoom.fit_scale(source, viewport);
constexpr pointd anchor{800, 500};
zoom.step(1, fit, source, viewport, anchor);
assert_zoom_near(0.5, zoom.effective_scale(fit), "first step above inserted fit");
zoom.step(1, fit, source, viewport, anchor);
assert_zoom_near(0.67, zoom.effective_scale(fit), "second ladder step");
zoom.step(-1, fit, source, viewport, anchor);
zoom.step(-1, fit, source, viewport, anchor);
assert_equal(true, zoom.is_fit(), "step out returns to fit");
assert_zoom_near(fit, zoom.effective_scale(fit), "fit scale restored");
}
static void should_keep_zoom_anchor_still()
{
df::zoom_view_state zoom;
constexpr sized source{4000, 3000};
constexpr sized viewport{1000, 800};
constexpr pointd anchor{750, 600};
const auto fit = zoom.fit_scale(source, viewport);
constexpr pointd old_center{0.5, 0.5};
const pointd source_anchor{old_center.X * source.Width + (anchor.X - viewport.Width / 2.0) / fit,
old_center.Y * source.Height + (anchor.Y - viewport.Height / 2.0) / fit};
zoom.set_anchored(1.0, fit, source, viewport, anchor);
const auto center = zoom.center();
const pointd projected{viewport.Width / 2.0 + (source_anchor.X - center.X * source.Width),
viewport.Height / 2.0 + (source_anchor.Y - center.Y * source.Height)};
assert_zoom_near(anchor.X, projected.X, "anchor x");
assert_zoom_near(anchor.Y, projected.Y, "anchor y");
}
static void should_step_zoom_at_pointer_anchor()
{
df::zoom_view_state zoom;
constexpr sized source{4000, 3000};
constexpr sized viewport{1000, 800};
constexpr pointd anchor{750, 600};
const auto fit = zoom.fit_scale(source, viewport);
const pointd source_anchor{zoom.center().X * source.Width + (anchor.X - viewport.Width / 2.0) / fit,
zoom.center().Y * source.Height + (anchor.Y - viewport.Height / 2.0) / fit};
zoom.step(1, fit, source, viewport, anchor);
const auto scale = zoom.effective_scale(fit);
const auto center = zoom.center();
const pointd projected{viewport.Width / 2.0 + (source_anchor.X - center.X * source.Width) * scale,
viewport.Height / 2.0 + (source_anchor.Y - center.Y * source.Height) * scale};
assert_zoom_near(anchor.X, projected.X, "stepped anchor x");
assert_zoom_near(anchor.Y, projected.Y, "stepped anchor y");
}
static void should_preserve_zoom_center_across_dimensions()
{
df::zoom_view_state zoom;
zoom.set_explicit(4.0, {0.25, 0.75});
const auto first = zoom.geometry({6000, 4000}, {1200, 800}, 0.2);
const auto second = zoom.geometry({3000, 2000}, {1200, 800}, 0.4);
assert_zoom_near(first.center.X, second.center.X, "normalized center x");
assert_zoom_near(first.center.Y, second.center.Y, "normalized center y");
}
static void should_suspend_carried_zoom_below_fit()
{
df::zoom_view_state zoom;
zoom.update_source({6000, 4000}, 0.2);
zoom.set_explicit(0.5, {0.25, 0.75});
zoom.update_source({1000, 800}, 1.0);
assert_equal(true, zoom.is_carried_fit(), "smaller image is temporarily fitted");
assert_equal(false, zoom.is_magnified(1.0), "carried fit does not permit pan");
assert_equal(true, zoom.mode() == df::zoom_scale_mode::explicit_scale, "carried fit remains durable Zoom mode");
assert_zoom_near(1.0, zoom.effective_scale(1.0), "fit is displayed");
assert_zoom_near(0.5, zoom.explicit_scale(), "carried scale is retained");
zoom.update_source({6000, 4000}, 0.2);
assert_equal(false, zoom.is_carried_fit(), "carried scale resumes on a large image");
assert_zoom_near(0.5, zoom.effective_scale(0.2), "carried scale is restored");
assert_zoom_near(0.25, zoom.center().X, "source center x is retained");
assert_zoom_near(0.75, zoom.center().Y, "source center y is retained");
}
static void should_clamp_zoom_model_pan_to_edges()
{
df::zoom_view_state zoom;
constexpr sized source{4000, 3000};
constexpr sized viewport{1000, 800};
zoom.set_explicit(2.0);
zoom.pan_source({-source.Width, -source.Height}, source, viewport, 0.25);
assert_zoom_near(viewport.Width / (2.0 * source.Width * 2.0), zoom.center().X,
"stored source center clamps to the visible left edge");
assert_zoom_near(viewport.Height / (2.0 * source.Height * 2.0), zoom.center().Y,
"stored source center clamps to the visible top edge");
const auto first = zoom.geometry(source, viewport, 0.25);
assert_zoom_near(viewport.Width / (2.0 * source.Width * 2.0), first.center.X, "draw center clamps left");
assert_zoom_near(viewport.Height / (2.0 * source.Height * 2.0), first.center.Y, "draw center clamps top");
zoom.pan_source({1.0 / 2.0, 0.0}, source, viewport, 0.25);
assert_zoom_near(first.center.X + 1.0 / (2.0 * source.Width), zoom.center().X,
"one displayed pixel of reversal moves immediately");
zoom.pan_source({source.Width * 2.0, source.Height * 2.0}, source, viewport, 0.25);
assert_zoom_near(1.0 - viewport.Width / (2.0 * source.Width * 2.0), zoom.center().X,
"stored source center clamps to the visible right edge");
assert_zoom_near(1.0 - viewport.Height / (2.0 * source.Height * 2.0), zoom.center().Y,
"stored source center clamps to the visible bottom edge");
const auto second = zoom.geometry(source, viewport, 0.25);
assert_zoom_near(1.0 - viewport.Width / (2.0 * source.Width * 2.0), second.center.X, "draw center clamps right");
assert_zoom_near(1.0 - viewport.Height / (2.0 * source.Height * 2.0), second.center.Y, "draw center clamps bottom");
df::zoom_view_state narrow;
narrow.set_explicit(1.0);
narrow.pan_source({source.Width, source.Height}, {100, 100}, viewport, 1.0);
assert_zoom_near(0.5, narrow.center().X, "an axis smaller than the viewport remains centred");
assert_zoom_near(0.5, narrow.center().Y, "both small axes remain centred");
}
static void should_toggle_fit_to_last_explicit_zoom()
{
df::zoom_view_state zoom;
zoom.set_explicit(4.0, {0.25, 0.75});
zoom.toggle_fit();
assert_equal(true, zoom.is_fit(), "first toggle fits");
zoom.toggle_fit();
assert_equal(false, zoom.is_fit(), "second toggle restores explicit mode");
assert_zoom_near(4.0, zoom.explicit_scale(), "last explicit scale restored");
assert_zoom_near(0.25, zoom.center().X, "last explicit center x restored");
assert_zoom_near(0.75, zoom.center().Y, "last explicit center y restored");
}
// Toggling Fit and back returned a Fit width or Fill as the fixed scale it had last computed, so the
// restored view stopped following the window. The variant is what comes back, not its number.
static void should_toggle_fit_back_to_a_fit_variant()
{
df::zoom_view_state zoom;
constexpr sized source{4000, 3000};
zoom.fit_width(source, {1000, 500});
zoom.toggle_fit();
assert_equal(true, zoom.is_fit(), "the first toggle fits");
zoom.toggle_fit();
assert_equal(true, zoom.mode() == df::zoom_scale_mode::fit_width, "the second toggle restores fit width");
zoom.update_fit_variant(source, {2000, 500});
assert_zoom_near(0.5, zoom.effective_scale(0.1666666667), "which still follows the viewport");
}
// The five cells are whole divisions of the width, so a width that is not a multiple of five left a
// few pixels past the last cell, and the inclusive right edge sits past it too. Both divided out to a
// sixth star, which is a rating nothing can hold.
static void should_hit_test_ratings_within_five_stars()
{
const recti bounds(0, 0, 104, 20);
assert_equal(1, rating_control::to_rating(bounds, {0, 10}), "the left edge is one star");
assert_equal(5, rating_control::to_rating(bounds, {99, 10}), "the last cell is five stars");
assert_equal(5, rating_control::to_rating(bounds, {103, 10}), "and so are the pixels past it");
assert_equal(5, rating_control::to_rating(bounds, {104, 10}), "and the inclusive right edge");
}
static void should_keep_zoom_anchor_through_layout_change()
{
df::zoom_view_state zoom;
constexpr sized source{4000, 3000};
constexpr sized old_viewport{1000, 800};
constexpr sized new_viewport{1800, 1000};
constexpr pointd old_origin{700, 100};
constexpr pointd new_origin{100, 100};
constexpr pointd old_anchor{750, 600};
const auto absolute_anchor = old_origin + old_anchor;
const auto old_fit = zoom.fit_scale(source, old_viewport);
const auto source_anchor = zoom.source_point_at(source, old_viewport, old_fit, old_anchor);
zoom.set_anchored(1.0, old_fit, source, old_viewport, old_anchor);