public async Task WnsToastPushTest() { PushWatcher watcher = new PushWatcher(); var client = this.GetClient(); var push = this.GetClient().GetPush(); string pushChannelUri = await GetChannelUri(); //Build push payload XElement expectedResult = BuildXmlToastPayload("sendToastText01", "Hello World"); var xmlPayload = "<?xml version=\"1.0\"?>" + expectedResult; JObject body = new JObject(); body["method"] = "send"; body["type"] = "wns"; body["payload"] = xmlPayload; body["token"] = "dummy"; body["wnsType"] = "toast"; try { //Register for Push await push.RegisterAsync(pushChannelUri); //Invoke API to send push await this.GetClient().InvokeApiAsync("push", body); var notificationResult = await watcher.WaitForPush(TimeSpan.FromSeconds(10)); if (notificationResult == null) { Assert.Fail("Error, push not received on the timeout allowed"); } else { Log("Push notification received:"); XElement receivedPushInfo = XElement.Parse(notificationResult.ToastNotification.Content.GetXml()); Log(" {0}: {1}", notificationResult.NotificationType, receivedPushInfo); if (expectedResult.ToString(SaveOptions.DisableFormatting) == receivedPushInfo.ToString(SaveOptions.DisableFormatting)) { Log("Received notification is the expected one."); } else { Assert.Fail(string.Format("Received notification is not the expected one. \r\nExpected:{0} \r\nActual:{1}", expectedResult.ToString(), receivedPushInfo.ToString())); } } } finally { push.UnregisterAsync().Wait(); } }
private static ZumoTest CreatePushTest(string wnsMethod, JToken payload, XElement expectedResult) { string testName = "Test for " + wnsMethod + ": "; string payloadString = payload.ToString(Formatting.None); testName += payloadString.Length < 15 ? payloadString : (payloadString.Substring(0, 15) + "..."); return new ZumoTest(testName, async delegate(ZumoTest test) { test.AddLog("Test for method {0}, with payload {1}", wnsMethod, payload); var client = ZumoTestGlobals.Instance.Client; var table = client.GetTable(ZumoTestGlobals.PushTestTableName); PushWatcher watcher = new PushWatcher(); var item = new JObject(); item.Add("method", wnsMethod); item.Add("channelUri", pushChannel.Uri); item.Add("payload", payload); var pushResult = await table.InsertAsync(item); test.AddLog("Push result: {0}", pushResult); var notificationResult = await watcher.WaitForPush(TimeSpan.FromSeconds(10)); if (notificationResult == null) { test.AddLog("Error, push not received on the timeout allowed"); return false; } else { test.AddLog("Push notification received:"); XElement receivedPushInfo = null; switch (notificationResult.NotificationType) { case PushNotificationType.Raw: receivedPushInfo = new XElement("raw", new XText(notificationResult.RawNotification.Content)); break; case PushNotificationType.Toast: receivedPushInfo = XElement.Parse(notificationResult.ToastNotification.Content.GetXml()); break; case PushNotificationType.Badge: receivedPushInfo = XElement.Parse(notificationResult.BadgeNotification.Content.GetXml()); break; case PushNotificationType.Tile: receivedPushInfo = XElement.Parse(notificationResult.TileNotification.Content.GetXml()); break; } test.AddLog(" {0}: {1}", notificationResult.NotificationType, receivedPushInfo); bool passed; if (expectedResult.ToString(SaveOptions.DisableFormatting) == receivedPushInfo.ToString(SaveOptions.DisableFormatting)) { test.AddLog("Received notification is the expected one."); passed = true; } else { test.AddLog("Received notification is not the expected one. Expected:"); test.AddLog(expectedResult.ToString()); test.AddLog("Actual:"); test.AddLog(receivedPushInfo.ToString()); passed = false; } await Task.Delay(5000); // leave some time between pushes return passed; } }); }
private static ZumoTest CreatePushTest(string wnsMethod, JToken payload, XElement expectedResult) { string testName = "Test for " + wnsMethod + ": "; string payloadString = payload.ToString(Formatting.None); testName += payloadString.Length < 15 ? payloadString : (payloadString.Substring(0, 15) + "..."); return(new ZumoTest(testName, async delegate(ZumoTest test) { test.AddLog("Test for method {0}, with payload {1}", wnsMethod, payload); var client = ZumoTestGlobals.Instance.Client; var table = client.GetTable(ZumoTestGlobals.PushTestTableName); PushWatcher watcher = new PushWatcher(); var item = new JObject(); item.Add("method", wnsMethod); item.Add("channelUri", pushChannel.Uri); item.Add("payload", payload); var pushResult = await table.InsertAsync(item); test.AddLog("Push result: {0}", pushResult); var notificationResult = await watcher.WaitForPush(TimeSpan.FromSeconds(10)); if (notificationResult == null) { test.AddLog("Error, push not received on the timeout allowed"); return false; } else { test.AddLog("Push notification received:"); XElement receivedPushInfo = null; switch (notificationResult.NotificationType) { case PushNotificationType.Raw: receivedPushInfo = new XElement("raw", new XText(notificationResult.RawNotification.Content)); break; case PushNotificationType.Toast: receivedPushInfo = XElement.Parse(notificationResult.ToastNotification.Content.GetXml()); break; case PushNotificationType.Badge: receivedPushInfo = XElement.Parse(notificationResult.BadgeNotification.Content.GetXml()); break; case PushNotificationType.Tile: receivedPushInfo = XElement.Parse(notificationResult.TileNotification.Content.GetXml()); break; } test.AddLog(" {0}: {1}", notificationResult.NotificationType, receivedPushInfo); bool passed; if (expectedResult.ToString(SaveOptions.DisableFormatting) == receivedPushInfo.ToString(SaveOptions.DisableFormatting)) { test.AddLog("Received notification is the expected one."); passed = true; } else { test.AddLog("Received notification is not the expected one. Expected:"); test.AddLog(expectedResult.ToString()); test.AddLog("Actual:"); test.AddLog(receivedPushInfo.ToString()); passed = false; } await Task.Delay(5000); // leave some time between pushes return passed; } })); }