Skip to content

Commit 3595980

Browse files
committed
Rewriting hotkeys - tests
1 parent 993301b commit 3595980

File tree

6 files changed

+125
-54
lines changed

6 files changed

+125
-54
lines changed

RetailCoder.VBE/Common/RubberduckHooks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void HookHotkeys()
3232
var config = _config.LoadConfiguration();
3333
var settings = config.UserSettings.HotkeySettings;
3434

35-
foreach (var hotkeySetting in settings.Settings.Where(hotkey => hotkey.IsEnabled))
35+
foreach (var hotkeySetting in settings.Settings.Where(hotkeySetting => hotkeySetting.IsEnabled))
3636
{
3737
var hotkey = _hotkeyFactory.Create(hotkeySetting, Hwnd);
3838
if (hotkey != null)

RetailCoder.VBE/Settings/HotkeySettings.cs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using Rubberduck.Common.Hotkeys;
5+
using Rubberduck.Parsing.VBA;
56

67
namespace Rubberduck.Settings
78
{
@@ -13,7 +14,7 @@ public interface IHotkeySettings
1314
public class HotkeySettings : IHotkeySettings, IEquatable<HotkeySettings>
1415
{
1516
private readonly IEnumerable<HotkeySetting> _defaultSettings;
16-
private HashSet<HotkeySetting> _settings;
17+
private HashSet<HotkeySetting> _settings = new HashSet<HotkeySetting>();
1718

1819
public HotkeySetting[] Settings
1920
{
@@ -23,7 +24,11 @@ public HotkeySetting[] Settings
2324
// Enable loading user settings during deserialization
2425
if (_defaultSettings == null)
2526
{
26-
_settings = value == null ? new HashSet<HotkeySetting>() : new HashSet<HotkeySetting>(value);
27+
if (value != null)
28+
{
29+
AddUnique(value);
30+
}
31+
2732
return;
2833
}
2934

@@ -34,19 +39,15 @@ public HotkeySetting[] Settings
3439
_settings = new HashSet<HotkeySetting>(defaults);
3540
return;
3641
}
42+
3743
_settings = new HashSet<HotkeySetting>();
44+
3845
var incoming = value.ToList();
3946
//Make sure settings are valid to keep trash out of the config file.
4047
var hotkeyCommandTypeNames = defaults.Select(h => h.CommandTypeName);
4148
incoming.RemoveAll(h => !hotkeyCommandTypeNames.Contains(h.CommandTypeName) || !IsValid(h));
4249

43-
//Only take the first setting if multiple definitions are found.
44-
foreach (var setting in incoming.GroupBy(s => s.CommandTypeName).Select(hotkey => hotkey.First()))
45-
{
46-
//Only allow one hotkey to be enabled with the same key combination.
47-
setting.IsEnabled &= !IsDuplicate(setting);
48-
_settings.Add(setting);
49-
}
50+
AddUnique(incoming);
5051

5152
//Merge any hotkeys that weren't found in the input.
5253
foreach (var setting in defaults.Where(setting => _settings.FirstOrDefault(s => s.CommandTypeName.Equals(setting.CommandTypeName)) == null))
@@ -67,17 +68,7 @@ public HotkeySettings()
6768
public HotkeySettings(IEnumerable<HotkeySetting> defaultSettings)
6869
{
6970
_defaultSettings = defaultSettings;
70-
}
71-
72-
private bool IsDuplicate(HotkeySetting candidate)
73-
{
74-
return _settings.FirstOrDefault(
75-
s =>
76-
s.Key1 == candidate.Key1 &&
77-
s.Key2 == candidate.Key2 &&
78-
s.HasAltModifier == candidate.HasAltModifier &&
79-
s.HasCtrlModifier == candidate.HasCtrlModifier &&
80-
s.HasShiftModifier == candidate.HasShiftModifier) != null;
71+
_settings = defaultSettings.ToHashSet();
8172
}
8273

8374
public bool Equals(HotkeySettings other)
@@ -99,5 +90,27 @@ private static bool IsValid(HotkeySetting candidate)
9990
return false;
10091
}
10192
}
93+
94+
private void AddUnique(IEnumerable<HotkeySetting> settings)
95+
{
96+
//Only take the first setting if multiple definitions are found.
97+
foreach (var setting in settings.GroupBy(s => s.CommandTypeName).Select(hotkey => hotkey.First()))
98+
{
99+
//Only allow one hotkey to be enabled with the same key combination.
100+
setting.IsEnabled &= !IsDuplicate(setting);
101+
_settings.Add(setting);
102+
}
103+
}
104+
105+
private bool IsDuplicate(HotkeySetting candidate)
106+
{
107+
return _settings.FirstOrDefault(
108+
s =>
109+
s.Key1 == candidate.Key1 &&
110+
s.Key2 == candidate.Key2 &&
111+
s.HasAltModifier == candidate.HasAltModifier &&
112+
s.HasCtrlModifier == candidate.HasCtrlModifier &&
113+
s.HasShiftModifier == candidate.HasShiftModifier) != null;
114+
}
102115
}
103116
}

RubberduckTests/RubberduckTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
<Compile Include="QuickFixes\UseSetKeywordForObjectAssignmentQuickFixTests.cs" />
180180
<Compile Include="QuickFixes\WriteOnlyPropertyQuickFixTests.cs" />
181181
<Compile Include="QuickFixes\QuickFixBaseTests.cs" />
182+
<Compile Include="Settings\HotkeyFactoryTests.cs" />
182183
<Compile Include="SmartIndenter\VerticalSpacingTests.cs" />
183184
<Compile Include="SourceControl\SynchrounouslyConstructedDeclarationFinderFactory.cs" />
184185
<Compile Include="Stats\ParseTreeMetricsAnalystTests.cs" />

RubberduckTests/Settings/GeneralSettingsTests.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@ private Configuration GetDefaultConfig()
2626
//Delimiter = '.'
2727
};
2828

29-
var hotkeySettings = new HotkeySettings()
29+
var hotkeySettings = new HotkeySettings(new[]
3030
{
31-
Settings = new[]
32-
{
33-
new HotkeySetting {CommandTypeName = "IndentProcedure", IsEnabled = true, Key1 = "CTRL-P"},
34-
new HotkeySetting {CommandTypeName = "IndentModule", IsEnabled = true, Key1 = "CTRL-M"}
35-
}
36-
};
31+
new HotkeySetting {CommandTypeName = "FooCommand", IsEnabled = true, Key1 = "A"},
32+
new HotkeySetting {CommandTypeName = "BarCommand", IsEnabled = true, Key1 = "B"}
33+
});
3734

3835
var userSettings = new UserSettings(generalSettings, hotkeySettings, null, null, null, null, null);
3936
return new Configuration(userSettings);
@@ -49,20 +46,19 @@ private Configuration GetNondefaultConfig()
4946
//Delimiter = '/'
5047
};
5148

52-
var hotkeySettings = new HotkeySettings()
49+
var hotkeySettings = new HotkeySettings
5350
{
5451
Settings = new[]
5552
{
56-
new HotkeySetting{CommandTypeName="IndentProcedure", IsEnabled=false, Key1="CTRL-C"},
57-
new HotkeySetting{CommandTypeName="IndentModule", IsEnabled=false, Key1="CTRL-X"}
53+
new HotkeySetting{CommandTypeName="FooCommand", IsEnabled=false, Key1="C"},
54+
new HotkeySetting{CommandTypeName="BarCommand", IsEnabled=false, Key1="D"}
5855
}
5956
};
6057

6158
var userSettings = new UserSettings(generalSettings, hotkeySettings, null, null, null, null, null);
6259
return new Configuration(userSettings);
6360
}
6461

65-
[Ignore]
6662
[TestCategory("Settings")]
6763
[TestMethod]
6864
public void SaveConfigWorks()
@@ -80,7 +76,6 @@ public void SaveConfigWorks()
8076
() => Assert.AreEqual(config.UserSettings.GeneralSettings.AutoSavePeriod, viewModel.AutoSavePeriod));
8177
}
8278

83-
[Ignore]
8479
[TestCategory("Settings")]
8580
[TestMethod]
8681
public void SetDefaultsWorks()
@@ -97,7 +92,6 @@ public void SetDefaultsWorks()
9792
() => Assert.AreEqual(defaultConfig.UserSettings.GeneralSettings.AutoSavePeriod, viewModel.AutoSavePeriod));
9893
}
9994

100-
[Ignore]
10195
[TestCategory("Settings")]
10296
[TestMethod]
10397
public void LanguageIsSetInCtor()
@@ -108,7 +102,6 @@ public void LanguageIsSetInCtor()
108102
Assert.AreEqual(defaultConfig.UserSettings.GeneralSettings.Language, viewModel.SelectedLanguage);
109103
}
110104

111-
[Ignore]
112105
[TestCategory("Settings")]
113106
[TestMethod]
114107
public void HotkeysAreSetInCtor()
@@ -119,7 +112,6 @@ public void HotkeysAreSetInCtor()
119112
Assert.IsTrue(defaultConfig.UserSettings.HotkeySettings.Settings.SequenceEqual(viewModel.Hotkeys));
120113
}
121114

122-
[Ignore]
123115
[TestCategory("Settings")]
124116
[TestMethod]
125117
public void AutoSaveEnabledIsSetInCtor()
@@ -130,7 +122,6 @@ public void AutoSaveEnabledIsSetInCtor()
130122
Assert.AreEqual(defaultConfig.UserSettings.GeneralSettings.AutoSaveEnabled, viewModel.AutoSaveEnabled);
131123
}
132124

133-
[Ignore]
134125
[TestCategory("Settings")]
135126
[TestMethod]
136127
public void AutoSavePeriodIsSetInCtor()
@@ -141,7 +132,6 @@ public void AutoSavePeriodIsSetInCtor()
141132
Assert.AreEqual(defaultConfig.UserSettings.GeneralSettings.AutoSavePeriod, viewModel.AutoSavePeriod);
142133
}
143134

144-
[Ignore]
145135
[TestCategory("Settings")]
146136
[TestMethod]
147137
public void SourceControlEnabledIsSetInCtor()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Moq;
4+
using Rubberduck.Common.Hotkeys;
5+
using Rubberduck.Settings;
6+
using Rubberduck.UI.Command;
7+
8+
namespace RubberduckTests.Settings
9+
{
10+
[TestClass]
11+
public class HotkeyFactoryTests
12+
{
13+
[TestMethod]
14+
public void CreatingHotkeyReturnsNullWhenNoSettingProvided()
15+
{
16+
var factory = new HotkeyFactory(null);
17+
18+
var hotkey = factory.Create(null, IntPtr.Zero);
19+
20+
Assert.IsNull(hotkey);
21+
}
22+
23+
[TestMethod]
24+
public void CreatingHotkeyReturnsNullWhenNoMatchingCommandExists()
25+
{
26+
var mockCommand = new Mock<CommandBase>(null).Object;
27+
var factory = new HotkeyFactory(new[] {mockCommand});
28+
var setting = new HotkeySetting { CommandTypeName = "Foo" };
29+
30+
var hotkey = factory.Create(setting, IntPtr.Zero);
31+
32+
Assert.IsNull(hotkey);
33+
}
34+
35+
[TestMethod]
36+
public void CreatingHotkeyReturnsCorrectResult()
37+
{
38+
var mockCommand = new Mock<CommandBase>(null).Object;
39+
var factory = new HotkeyFactory(new[] {mockCommand});
40+
var setting = new HotkeySetting
41+
{
42+
CommandTypeName = mockCommand.GetType().Name,
43+
Key1 = "X",
44+
HasCtrlModifier = true
45+
};
46+
47+
var hotkey = factory.Create(setting, IntPtr.Zero);
48+
49+
MultiAssert.Aggregate(
50+
() => Assert.AreEqual(mockCommand, hotkey.Command),
51+
() => Assert.AreEqual(setting.ToString(), hotkey.Key));
52+
}
53+
}
54+
}

RubberduckTests/Settings/HotkeySettingsTests.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,71 @@ namespace RubberduckTests.Settings
77
[TestClass]
88
public class HotkeySettingsTests
99
{
10-
[Ignore]
1110
[TestCategory("Settings")]
1211
[TestMethod]
1312
public void DefaultsSetInCtor()
1413
{
15-
var expected = new HotkeySetting[0];
14+
var expected = new []
15+
{
16+
new HotkeySetting {CommandTypeName = "FooCommand", Key1 = "F"},
17+
new HotkeySetting {CommandTypeName = "BarCommand", Key1 = "B"}
18+
};
19+
20+
var settings = new HotkeySettings(new []
21+
{
22+
new HotkeySetting {CommandTypeName = "FooCommand", Key1 = "F"},
23+
new HotkeySetting {CommandTypeName = "BarCommand", Key1 = "B"}
24+
});
1625

17-
// TODO: Use costructor with parameter
18-
var settings = new HotkeySettings();
1926
var actual = settings.Settings;
2027

2128
Assert.IsTrue(expected.SequenceEqual(actual));
2229
}
2330

24-
[Ignore]
2531
[TestCategory("Settings")]
2632
[TestMethod]
2733
public void InvalidSettingNameWontAdd()
2834
{
29-
var settings = new HotkeySettings();
35+
var settings = new HotkeySettings(new[]
36+
{
37+
new HotkeySetting {CommandTypeName = "FooCommand", Key1 = "F"}
38+
});
39+
3040
var expected = settings.Settings;
3141

32-
settings.Settings = new[] { new HotkeySetting { CommandTypeName = "Foobar", IsEnabled = false, Key1 = "CTRL-C" } };
42+
settings.Settings = new[]
43+
{new HotkeySetting {CommandTypeName = "BarCommand", IsEnabled = false, Key1 = "CTRL-C"}};
3344

3445
var actual = settings.Settings;
3546

3647
Assert.IsTrue(expected.SequenceEqual(actual));
3748
}
3849

39-
[Ignore]
4050
[TestCategory("Settings")]
4151
[TestMethod]
4252
public void InvalidSettingKeyWontAdd()
4353
{
44-
var settings = new HotkeySettings();
54+
var settings = new HotkeySettings(new[]
55+
{
56+
new HotkeySetting {CommandTypeName = "FooCommand", Key1 = "F"}
57+
});
58+
4559
var expected = settings.Settings;
4660

47-
settings.Settings = new[] { new HotkeySetting { CommandTypeName = "ParseAll", IsEnabled = false, Key1 = "Foobar" } };
61+
settings.Settings = new[]
62+
{new HotkeySetting {CommandTypeName = "FooCommand", IsEnabled = false, Key1 = "Foobar"}};
4863

4964
var actual = settings.Settings;
5065

5166
Assert.IsTrue(expected.SequenceEqual(actual));
5267
}
5368

54-
[Ignore]
5569
[TestCategory("Settings")]
5670
[TestMethod]
5771
public void DuplicateKeysAreDeactivated()
5872
{
59-
var duplicate1 = new HotkeySetting { CommandTypeName = "ParseAll", IsEnabled = true, Key1 = "X" };
60-
var duplicate2 = new HotkeySetting { CommandTypeName = "FindSymbol", IsEnabled = true, Key1 = "X" };
73+
var duplicate1 = new HotkeySetting {CommandTypeName = "FooCommand", IsEnabled = true, Key1 = "X"};
74+
var duplicate2 = new HotkeySetting {CommandTypeName = "BarCommand", IsEnabled = true, Key1 = "X"};
6175

6276
// ReSharper disable once UnusedVariable
6377
var settings = new HotkeySettings
@@ -68,13 +82,12 @@ public void DuplicateKeysAreDeactivated()
6882
Assert.IsFalse(duplicate1.IsEnabled == duplicate2.IsEnabled);
6983
}
7084

71-
[Ignore]
7285
[TestCategory("Settings")]
7386
[TestMethod]
7487
public void DuplicateNamesAreIgnored()
7588
{
76-
var expected = new HotkeySetting { CommandTypeName = "ParseAll", IsEnabled = true, Key1 = "X" };
77-
var duplicate = new HotkeySetting { CommandTypeName = "ParseAll", IsEnabled = true, Key1 = "Y" };
89+
var expected = new HotkeySetting {CommandTypeName = "FooCommand", IsEnabled = true, Key1 = "X"};
90+
var duplicate = new HotkeySetting {CommandTypeName = "FooCommand", IsEnabled = true, Key1 = "Y"};
7891

7992
var settings = new HotkeySettings
8093
{

0 commit comments

Comments
 (0)