Skip to content

Commit 0cadc0f

Browse files
authored
Merge pull request #6051 from tommy9/AddingFakes
Adding more test fakes
2 parents c5b603b + 5d6ac51 commit 0cadc0f

File tree

38 files changed

+1165
-351
lines changed

38 files changed

+1165
-351
lines changed

Rubberduck.Core/Runtime/BeepInterceptor.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public interface IBeepInterceptor : IDisposable
1515
{
1616
void SuppressBeep(double millisecondsToSuppress);
1717
event EventHandler<BeepEventArgs> Beep;
18+
void NativeCall();
1819
}
1920

2021
public sealed class BeepInterceptor : IBeepInterceptor
@@ -24,6 +25,7 @@ public sealed class BeepInterceptor : IBeepInterceptor
2425
private readonly IVbeNativeApi _vbeApi;
2526
private readonly LocalHook _hook;
2627
private readonly Timer _timer;
28+
private VbaBeepDelegate nativeCall;
2729

2830
public BeepInterceptor(IVbeNativeApi vbeApi)
2931
{
@@ -40,12 +42,19 @@ public void SuppressBeep(double millisecondsToSuppress)
4042
_timer.Enabled = true;
4143
}
4244

45+
public void NativeCall()
46+
{
47+
nativeCall();
48+
}
49+
4350
private LocalHook HookVbaBeep()
4451
{
4552
var processAddress = LocalHook.GetProcAddress(_vbeApi.DllName, "rtcBeep");
4653
var callbackDelegate = new VbaBeepDelegate(VbaBeepCallback);
4754
var hook = LocalHook.Create(processAddress, callbackDelegate, null);
4855
hook.ThreadACL.SetInclusiveACL(new[] { 0 });
56+
var nativeFunctionAddress = hook.HookBypassAddress;
57+
nativeCall = Marshal.GetDelegateForFunctionPointer<VbaBeepDelegate>(nativeFunctionAddress);
4958
return hook;
5059
}
5160

@@ -65,7 +74,7 @@ private void VbaBeepCallback()
6574

6675
if (!e.Handled)
6776
{
68-
_vbeApi.Beep();
77+
nativeCall();
6978
}
7079
}
7180

Rubberduck.Main/ComClientLibrary/Abstract/UnitTesting/IFakesProvider.cs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ namespace Rubberduck.UnitTesting
1313
public interface IFakesProvider
1414
{
1515
[DispId(1)]
16-
[Description("Configures VBA.Interactions.MsgBox calls.")]
16+
[Description("Configures VBA.Interaction.MsgBox calls.")]
1717
IFake MsgBox { get; }
1818

1919
[DispId(2)]
20-
[Description("Configures VBA.Interactions.InputBox calls.")]
20+
[Description("Configures VBA.Interaction.InputBox calls.")]
2121
IFake InputBox { get; }
2222

2323
[DispId(3)]
@@ -79,5 +79,61 @@ public interface IFakesProvider
7979
[DispId(17)]
8080
[Description("Configures VBA.DateTime.Date calls.")]
8181
IFake Date { get; }
82+
83+
[DispId(18)]
84+
[Description("Configures VBA.Math.Rnd calls.")]
85+
IFake Rnd { get; }
86+
87+
[DispId(19)]
88+
[Description("Configures VBA.Interaction.DeleteSetting calls.")]
89+
IStub DeleteSetting { get; }
90+
91+
[DispId(20)]
92+
[Description("Configures VBA.Interaction.SaveSetting calls.")]
93+
IStub SaveSetting { get; }
94+
95+
[DispId(21)]
96+
[Description("Configures VBA.Interaction.GetSetting calls.")]
97+
IFake GetSetting { get; }
98+
99+
[DispId(22)]
100+
[Description("Configures VBA.Math.Randomize calls.")]
101+
IStub Randomize { get; }
102+
103+
[DispId(23)]
104+
[Description("Configures VBA.Interaction.GetAllSettings calls.")]
105+
IFake GetAllSettings { get; }
106+
107+
[DispId(24)]
108+
[Description("Configures VBA.FileSystem.SetAttr calls.")]
109+
IStub SetAttr { get; }
110+
111+
[DispId(25)]
112+
[Description("Configures VBA.FileSystem.GetAttr calls.")]
113+
IFake GetAttr { get; }
114+
115+
[DispId(26)]
116+
[Description("Configures VBA.FileSystem.FileLen calls.")]
117+
IFake FileLen { get; }
118+
119+
[DispId(27)]
120+
[Description("Configures VBA.FileSystem.FileDateTime calls.")]
121+
IFake FileDateTime { get; }
122+
123+
[DispId(28)]
124+
[Description("Configures VBA.FileSystem.FreeFile calls.")]
125+
IFake FreeFile { get; }
126+
127+
[DispId(29)]
128+
[Description("Configures VBA.Information.IMEStatus calls.")]
129+
IFake IMEStatus { get; }
130+
131+
[DispId(30)]
132+
[Description("Configures VBA.FileSystem.Dir calls.")]
133+
IFake Dir { get; }
134+
135+
[DispId(31)]
136+
[Description("Configures VBA.FileSystem.FileCopy calls.")]
137+
IStub FileCopy { get; }
82138
}
83139
}

Rubberduck.Main/ComClientLibrary/UnitTesting/Fakes/Date.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ public void DateCallback(IntPtr retVal)
2424
}
2525
if (PassThrough)
2626
{
27-
object result;
28-
VbeProvider.VbeNativeApi.GetDateVar(out result);
29-
Marshal.GetNativeVariantForObject(result, retVal);
27+
var nativeCall = Marshal.GetDelegateForFunctionPointer<DateDelegate>(NativeFunctionAddress);
28+
nativeCall(retVal);
3029
return;
3130
}
3231
Marshal.GetNativeVariantForObject(ReturnValue ?? 0, retVal);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using Rubberduck.Parsing.Grammar;
4+
5+
namespace Rubberduck.UnitTesting.Fakes
6+
{
7+
internal class Dir : FakeBase
8+
{
9+
public Dir()
10+
{
11+
var processAddress = EasyHook.LocalHook.GetProcAddress(VbeProvider.VbeNativeApi.DllName, "rtcDir");
12+
13+
InjectDelegate(new DirDelegate(DirCallback), processAddress);
14+
}
15+
16+
[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
17+
[return: MarshalAs(UnmanagedType.BStr)]
18+
private delegate string DirDelegate(IntPtr pathname, short attributes);
19+
20+
public string DirCallback(IntPtr pathname, short attributes)
21+
{
22+
OnCallBack();
23+
24+
TrackUsage("pathname", pathname);
25+
TrackUsage("attributes", attributes, Tokens.Integer);
26+
if (PassThrough)
27+
{
28+
var nativeCall = Marshal.GetDelegateForFunctionPointer<DirDelegate>(NativeFunctionAddress);
29+
return nativeCall(pathname, attributes);
30+
}
31+
32+
return ReturnValue?.ToString() ?? string.Empty;
33+
}
34+
}
35+
}

Rubberduck.Main/ComClientLibrary/UnitTesting/Fakes/DoEvents.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public int DoEventsCallback()
2929

3030
if (PassThrough)
3131
{
32-
return VbeProvider.VbeNativeApi.DoEvents();
32+
var nativeCall = Marshal.GetDelegateForFunctionPointer<DoEventsDelegate>(NativeFunctionAddress);
33+
return nativeCall();
3334
}
3435
return (int)(ReturnValue ?? 0);
3536
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using Rubberduck.Parsing.Grammar;
4+
5+
namespace Rubberduck.UnitTesting.Fakes
6+
{
7+
internal class FileDateTime : FakeBase
8+
{
9+
public FileDateTime()
10+
{
11+
var processAddress = EasyHook.LocalHook.GetProcAddress(VbeProvider.VbeNativeApi.DllName, "rtcFileDateTime");
12+
13+
InjectDelegate(new FileDateTimeDelegate(FileDateTimeCallback), processAddress);
14+
}
15+
16+
[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
17+
private delegate void FileDateTimeDelegate(IntPtr retVal, IntPtr pathname);
18+
19+
public void FileDateTimeCallback(IntPtr retVal, IntPtr pathname)
20+
{
21+
OnCallBack();
22+
23+
var pathNameArg = Marshal.PtrToStringBSTR(pathname);
24+
TrackUsage("pathname", pathNameArg, Tokens.String);
25+
if (PassThrough)
26+
{
27+
var nativeCall = Marshal.GetDelegateForFunctionPointer<FileDateTimeDelegate>(NativeFunctionAddress);
28+
nativeCall(retVal, pathname);
29+
}
30+
31+
Marshal.GetNativeVariantForObject(ReturnValue ?? 0, retVal);
32+
}
33+
}
34+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using Rubberduck.Parsing.Grammar;
4+
5+
namespace Rubberduck.UnitTesting.Fakes
6+
{
7+
internal class FileLen : FakeBase
8+
{
9+
public FileLen()
10+
{
11+
var processAddress = EasyHook.LocalHook.GetProcAddress(VbeProvider.VbeNativeApi.DllName, "rtcFileLen");
12+
13+
InjectDelegate(new FileLenDelegate(FileLenCallback), processAddress);
14+
}
15+
16+
[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
17+
[return: MarshalAs(UnmanagedType.I4)]
18+
private delegate int FileLenDelegate(IntPtr pathname);
19+
20+
public int FileLenCallback(IntPtr pathname)
21+
{
22+
OnCallBack();
23+
24+
var pathNameArg = Marshal.PtrToStringBSTR(pathname);
25+
TrackUsage("pathname", pathNameArg, Tokens.String);
26+
if (PassThrough)
27+
{
28+
var nativeCall = Marshal.GetDelegateForFunctionPointer<FileLenDelegate>(NativeFunctionAddress);
29+
return nativeCall(pathname);
30+
}
31+
32+
return Convert.ToInt32(ReturnValue ?? 0);
33+
}
34+
}
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace Rubberduck.UnitTesting.Fakes
5+
{
6+
internal class FreeFile : FakeBase
7+
{
8+
public FreeFile()
9+
{
10+
var processAddress = EasyHook.LocalHook.GetProcAddress(VbeProvider.VbeNativeApi.DllName, "rtcFreeFile");
11+
12+
InjectDelegate(new FreeFileDelegate(FreeFileCallback), processAddress);
13+
}
14+
15+
[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
16+
[return: MarshalAs(UnmanagedType.I2)]
17+
private delegate short FreeFileDelegate(IntPtr rangenumber);
18+
19+
public short FreeFileCallback(IntPtr rangenumber)
20+
{
21+
OnCallBack();
22+
23+
TrackUsage("rangenumber", rangenumber);
24+
if (PassThrough)
25+
{
26+
var nativeCall = Marshal.GetDelegateForFunctionPointer<FreeFileDelegate>(NativeFunctionAddress);
27+
return nativeCall(rangenumber);
28+
}
29+
30+
return Convert.ToInt16(ReturnValue ?? 0);
31+
}
32+
}
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using Rubberduck.Parsing.Grammar;
4+
5+
namespace Rubberduck.UnitTesting.Fakes
6+
{
7+
internal class GetAllSettings : FakeBase
8+
{
9+
public GetAllSettings()
10+
{
11+
var processAddress = EasyHook.LocalHook.GetProcAddress(VbeProvider.VbeNativeApi.DllName, "rtcGetAllSettings");
12+
13+
InjectDelegate(new GetAllSettingsDelegate(GetAllSettingsCallback), processAddress);
14+
}
15+
16+
[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
17+
private delegate void GetAllSettingsDelegate(IntPtr retVal, IntPtr appName, IntPtr section);
18+
19+
public void GetAllSettingsCallback(IntPtr retVal, IntPtr appName, IntPtr section)
20+
{
21+
OnCallBack();
22+
23+
var appNameArg = Marshal.PtrToStringBSTR(appName);
24+
var sectionArg = Marshal.PtrToStringBSTR(section);
25+
TrackUsage("appname", appNameArg, Tokens.String);
26+
TrackUsage("section", sectionArg, Tokens.String);
27+
if (PassThrough)
28+
{
29+
var nativeCall = Marshal.GetDelegateForFunctionPointer<GetAllSettingsDelegate>(NativeFunctionAddress);
30+
nativeCall(retVal, appName, section);
31+
return;
32+
}
33+
Marshal.GetNativeVariantForObject(ReturnValue ?? 0, retVal);
34+
}
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using Rubberduck.Parsing.Grammar;
4+
5+
namespace Rubberduck.UnitTesting.Fakes
6+
{
7+
internal class GetAttr : FakeBase
8+
{
9+
public GetAttr()
10+
{
11+
var processAddress = EasyHook.LocalHook.GetProcAddress(VbeProvider.VbeNativeApi.DllName, "rtcGetFileAttr");
12+
13+
InjectDelegate(new GetAttrDelegate(GetAttrCallback), processAddress);
14+
}
15+
16+
[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
17+
[return: MarshalAs(UnmanagedType.I2)]
18+
private delegate short GetAttrDelegate(IntPtr pathname);
19+
20+
public short GetAttrCallback(IntPtr pathname)
21+
{
22+
OnCallBack();
23+
24+
var pathNameArg = Marshal.PtrToStringBSTR(pathname);
25+
TrackUsage("pathname", pathNameArg, Tokens.String);
26+
if (PassThrough)
27+
{
28+
var nativeCall = Marshal.GetDelegateForFunctionPointer<GetAttrDelegate>(NativeFunctionAddress);
29+
return nativeCall(pathname);
30+
}
31+
32+
return Convert.ToInt16(ReturnValue ?? 0);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)