-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathPushTests.cs
131 lines (97 loc) · 4.52 KB
/
PushTests.cs
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
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Parse.Abstractions.Infrastructure;
using Parse.Abstractions.Platform.Push;
using Parse.Infrastructure.Utilities;
using Parse.Infrastructure;
using Parse.Platform.Push;
namespace Parse.Tests;
[TestClass]
public class PushTests
{
private ParseClient Client { get; } = new ParseClient(new ServerConnectionData { Test = true });
private IParsePushController GetMockedPushController(IPushState expectedPushState)
{
var mockedController = new Mock<IParsePushController>(MockBehavior.Strict);
mockedController
.Setup(obj => obj.SendPushNotificationAsync(It.Is<IPushState>(s => s.Equals(expectedPushState)), It.IsAny<IServiceHub>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(false));
return mockedController.Object;
}
private IParsePushChannelsController GetMockedPushChannelsController(IEnumerable<string> channels)
{
var mockedChannelsController = new Mock<IParsePushChannelsController>(MockBehavior.Strict);
// Setup for SubscribeAsync to accept any IServiceHub instance
mockedChannelsController
.Setup(obj => obj.SubscribeAsync(It.Is<IEnumerable<string>>(it => it.CollectionsEqual(channels)), It.IsAny<IServiceHub>(), It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask); // Ensure it returns a completed task
// Setup for UnsubscribeAsync to accept any IServiceHub instance
mockedChannelsController
.Setup(obj => obj.UnsubscribeAsync(It.Is<IEnumerable<string>>(it => it.CollectionsEqual(channels)), It.IsAny<IServiceHub>(), It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask); // Ensure it returns a completed task
return mockedChannelsController.Object;
}
[TestCleanup]
public void TearDown() => (Client.Services as ServiceHub).Reset();
[TestMethod]
public async Task TestSendPushAsync()
{
// Arrange
var hub = new MutableServiceHub();
var client = new ParseClient(new ServerConnectionData { Test = true }, hub);
var state = new MutablePushState
{
Query = Client.GetInstallationQuery()
};
var thePush = new ParsePush(client);
hub.PushController = GetMockedPushController(state);
// Act
thePush.Alert = "Alert";
state.Alert = "Alert";
await thePush.SendAsync();
thePush.Channels = new List<string> { "channel" };
state.Channels = new List<string> { "channel" };
await thePush.SendAsync();
var query = new ParseQuery<ParseInstallation>(client, "aClass");
thePush.Query = query;
state.Query = query;
await thePush.SendAsync();
// Assert
Assert.IsTrue(true); // Reaching here means no exceptions occurred
}
[TestMethod]
public async Task TestSubscribeAsync()
{
// Arrange
var hub = new MutableServiceHub();
var client = new ParseClient(new ServerConnectionData { Test = true }, hub);
var channels = new List<string> { "test" };
hub.PushChannelsController = GetMockedPushChannelsController(channels);
// Act
await client.SubscribeToPushChannelAsync("test");
await client.SubscribeToPushChannelsAsync(new List<string> { "test" });
using var cancellationTokenSource = new CancellationTokenSource();
await client.SubscribeToPushChannelsAsync(new List<string> { "test" }, cancellationTokenSource.Token);
// Assert
Assert.IsTrue(true); // Reaching here means no exceptions occurred
}
[TestMethod]
public async Task TestUnsubscribeAsync()
{
// Arrange
var hub = new MutableServiceHub();
var client = new ParseClient(new ServerConnectionData { Test = true }, hub);
var channels = new List<string> { "test" }; // Corrected to ensure we have the "test" channel
hub.PushChannelsController = GetMockedPushChannelsController(channels);
// Act
await client.UnsubscribeToPushChannelAsync("test");
await client.UnsubscribeToPushChannelsAsync(new List<string> { "test" });
using var cancellationTokenSource = new CancellationTokenSource();
await client.UnsubscribeToPushChannelsAsync(new List<string> { "test" }, cancellationTokenSource.Token);
// Assert
Assert.IsTrue(true); // Reaching here means no exceptions occurred
}
}