|
| 1 | +using System.Windows.Controls; |
| 2 | +using Text_Grab.Models; |
| 3 | +using Text_Grab.Views; |
| 4 | +using Wpf.Ui.Controls; |
| 5 | +using MenuItem = System.Windows.Controls.MenuItem; |
| 6 | + |
| 7 | +namespace Tests; |
| 8 | + |
| 9 | +public class FullscreenGrabPostGrabActionTests |
| 10 | +{ |
| 11 | + [Fact] |
| 12 | + public void GetPostGrabActionKey_UsesTemplateIdForTemplateActions() |
| 13 | + { |
| 14 | + ButtonInfo action = new("Template Action", "ApplyTemplate_Click", SymbolRegular.Apps24, DefaultCheckState.Off) |
| 15 | + { |
| 16 | + TemplateId = "template-123" |
| 17 | + }; |
| 18 | + |
| 19 | + string key = FullscreenGrab.GetPostGrabActionKey(action); |
| 20 | + |
| 21 | + Assert.Equal("template:template-123", key); |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public void GetPostGrabActionKey_FallsBackToButtonTextWhenClickEventMissing() |
| 26 | + { |
| 27 | + ButtonInfo action = new() |
| 28 | + { |
| 29 | + ButtonText = "Custom action" |
| 30 | + }; |
| 31 | + |
| 32 | + string key = FullscreenGrab.GetPostGrabActionKey(action); |
| 33 | + |
| 34 | + Assert.Equal("text:Custom action", key); |
| 35 | + } |
| 36 | + |
| 37 | + [WpfFact] |
| 38 | + public void GetActionablePostGrabMenuItems_ExcludesUtilityEntriesAndPreservesOrder() |
| 39 | + { |
| 40 | + ContextMenu contextMenu = new(); |
| 41 | + MenuItem firstAction = new() |
| 42 | + { |
| 43 | + Header = "First action", |
| 44 | + Tag = new ButtonInfo("First action", "First_Click", SymbolRegular.Apps24, DefaultCheckState.Off) |
| 45 | + }; |
| 46 | + MenuItem utilityItem = new() |
| 47 | + { |
| 48 | + Header = "Customize", |
| 49 | + Tag = "EditPostGrabActions" |
| 50 | + }; |
| 51 | + MenuItem secondAction = new() |
| 52 | + { |
| 53 | + Header = "Second action", |
| 54 | + Tag = new ButtonInfo("Second action", "Second_Click", SymbolRegular.Apps24, DefaultCheckState.Off) |
| 55 | + }; |
| 56 | + |
| 57 | + contextMenu.Items.Add(firstAction); |
| 58 | + contextMenu.Items.Add(new Separator()); |
| 59 | + contextMenu.Items.Add(utilityItem); |
| 60 | + contextMenu.Items.Add(secondAction); |
| 61 | + contextMenu.Items.Add(new MenuItem |
| 62 | + { |
| 63 | + Header = "Close this menu", |
| 64 | + Tag = "ClosePostGrabMenu" |
| 65 | + }); |
| 66 | + |
| 67 | + List<MenuItem> actionableItems = FullscreenGrab.GetActionablePostGrabMenuItems(contextMenu); |
| 68 | + |
| 69 | + Assert.Collection(actionableItems, |
| 70 | + item => Assert.Same(firstAction, item), |
| 71 | + item => Assert.Same(secondAction, item)); |
| 72 | + } |
| 73 | + |
| 74 | + [WpfFact] |
| 75 | + public void BuildPostGrabActionSnapshot_KeepsChangedTemplateCheckedAndUnchecksOthers() |
| 76 | + { |
| 77 | + ButtonInfo regularAction = new("Trim each line", "TrimEachLine_Click", SymbolRegular.Apps24, DefaultCheckState.Off); |
| 78 | + ButtonInfo firstTemplate = new("Template A", "ApplyTemplate_Click", SymbolRegular.Apps24, DefaultCheckState.Off) |
| 79 | + { |
| 80 | + TemplateId = "template-a" |
| 81 | + }; |
| 82 | + ButtonInfo secondTemplate = new("Template B", "ApplyTemplate_Click", SymbolRegular.Apps24, DefaultCheckState.Off) |
| 83 | + { |
| 84 | + TemplateId = "template-b" |
| 85 | + }; |
| 86 | + |
| 87 | + Dictionary<string, bool> snapshot = FullscreenGrab.BuildPostGrabActionSnapshot( |
| 88 | + [ |
| 89 | + new MenuItem { Tag = regularAction, IsCheckable = true, IsChecked = true }, |
| 90 | + new MenuItem { Tag = firstTemplate, IsCheckable = true, IsChecked = true }, |
| 91 | + new MenuItem { Tag = secondTemplate, IsCheckable = true, IsChecked = false } |
| 92 | + ], |
| 93 | + FullscreenGrab.GetPostGrabActionKey(secondTemplate), |
| 94 | + true); |
| 95 | + |
| 96 | + Assert.True(snapshot[FullscreenGrab.GetPostGrabActionKey(regularAction)]); |
| 97 | + Assert.False(snapshot[FullscreenGrab.GetPostGrabActionKey(firstTemplate)]); |
| 98 | + Assert.True(snapshot[FullscreenGrab.GetPostGrabActionKey(secondTemplate)]); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void ShouldPersistLastUsedState_ForForcedSourceAction_ReturnsTrue() |
| 103 | + { |
| 104 | + ButtonInfo lastUsedAction = new("Remove duplicate lines", "RemoveDuplicateLines_Click", SymbolRegular.Apps24, DefaultCheckState.LastUsed); |
| 105 | + |
| 106 | + bool shouldPersist = FullscreenGrab.ShouldPersistLastUsedState( |
| 107 | + lastUsedAction, |
| 108 | + previousChecked: true, |
| 109 | + isChecked: true, |
| 110 | + forcePersistActionKey: FullscreenGrab.GetPostGrabActionKey(lastUsedAction)); |
| 111 | + |
| 112 | + Assert.True(shouldPersist); |
| 113 | + } |
| 114 | + |
| 115 | + [Fact] |
| 116 | + public void ShouldPersistLastUsedState_DoesNotPersistUnchangedNonSourceAction() |
| 117 | + { |
| 118 | + ButtonInfo lastUsedAction = new("Remove duplicate lines", "RemoveDuplicateLines_Click", SymbolRegular.Apps24, DefaultCheckState.LastUsed); |
| 119 | + |
| 120 | + bool shouldPersist = FullscreenGrab.ShouldPersistLastUsedState( |
| 121 | + lastUsedAction, |
| 122 | + previousChecked: true, |
| 123 | + isChecked: true); |
| 124 | + |
| 125 | + Assert.False(shouldPersist); |
| 126 | + } |
| 127 | +} |
0 commit comments