public async Task ActionRendererRegistraton_CustomActionTest()
        {
            var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;

            // Need to move the test to the UI Thread
            await dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                AdaptiveActionParserRegistration actionParserRegistration = new AdaptiveActionParserRegistration();
                List <AdaptiveWarning> warnings = new List <AdaptiveWarning>();

                actionParserRegistration.Set("TestCustomAction", new TestActionParser());

                String testCard =
                    "{" +
                    "   \"type\":\"AdaptiveCard\"," +
                    "   \"version\":\"1.0\"," +
                    "   \"body\":" +
                    "   [" +
                    "   ]," +
                    "   \"actions\":" +
                    "   [" +
                    "       {" +
                    "           \"type\":\"TestCustomAction\"" +
                    "       }," +
                    "       {" +
                    "           \"type\":\"Action.Submit\"" +
                    "       }" +
                    "   ]" +
                    "}";

                AdaptiveCard card = AdaptiveCard.FromJsonString(testCard, null, actionParserRegistration).AdaptiveCard;
                Assert.IsNotNull(card);

                AdaptiveCardRenderer renderer = new AdaptiveCardRenderer();

                renderer.ActionRenderers.Set("TestCustomAction", new TestCustomActionRenderer());
                renderer.ActionRenderers.Set("Action.Submit", new TestSubmitActionRenderer());

                Assert.IsNotNull(renderer.ActionRenderers.Get("TestCustomAction") as TestCustomActionRenderer);
                Assert.IsNotNull(renderer.ActionRenderers.Get("Action.Submit") as TestSubmitActionRenderer);

                FrameworkElement renderedCard = renderer.RenderAdaptiveCard(card).FrameworkElement;

                bool submitFound = false;
                bool customFound = false;
                foreach (var radioButton in RenderTestHelpers.GetAllDescendants(renderedCard).OfType <RadioButton>())
                {
                    customFound |= radioButton.Name == "CustomActionRadioButton";
                    submitFound |= radioButton.Name == "SubmitActionRadioButton";
                }

                Assert.IsTrue(customFound && submitFound);

                renderer.ActionRenderers.Remove("TestCustomAction");
                renderer.ActionRenderers.Remove("Action.Submit");
                renderer.ActionRenderers.Remove("TestCustomActionThatDoesntExist");
            });
        }
Ejemplo n.º 2
0
        public void ActionParserRegistraton_CustomActionTest()
        {
            AdaptiveActionParserRegistration  actionParserRegistration  = new AdaptiveActionParserRegistration();
            AdaptiveElementParserRegistration elementParserRegistration = new AdaptiveElementParserRegistration();
            List <AdaptiveWarning>            warnings = new List <AdaptiveWarning>();

            actionParserRegistration.Set("TestCustomAction", new TestActionParser());
            IAdaptiveActionParser testActionParserRetrieved = actionParserRegistration.Get("TestCustomAction");

            Assert.IsNotNull(testActionParserRetrieved);
            Assert.IsNotNull(testActionParserRetrieved as TestActionParser);

            String testCard =
                "{" +
                "   \"type\":\"AdaptiveCard\"," +
                "   \"version\":\"1.0\"," +
                "   \"body\":" +
                "   [" +
                "   ]," +
                "   \"actions\":" +
                "   [" +
                "       {" +
                "           \"type\":\"TestCustomAction\"," +
                "           \"internalSubmitAction\":" +
                "           {" +
                "               \"type\": \"Action.Submit\"" +
                "           }" +
                "       }" +
                "   ]" +
                "}";

            AdaptiveCard card = AdaptiveCard.FromJsonString(testCard, elementParserRegistration, actionParserRegistration).AdaptiveCard;

            Assert.IsNotNull(card);

            Assert.AreEqual(1, card.Actions.Count);

            IAdaptiveActionElement action = card.Actions[0];

            Assert.IsNotNull(action);

            Assert.AreEqual(ActionType.Custom, action.ActionType);
            Assert.AreEqual("TestCustomAction", action.ActionTypeString);

            TestCustomAction customElement = card.Actions[0] as TestCustomAction;

            Assert.IsNotNull(customElement);

            Assert.AreEqual(ActionType.Submit, customElement.InternalSubmitAction.ActionType);
        }
Ejemplo n.º 3
0
        internal static JsonParseToastResult ParseToast(string json, FeatureSet currFeatureSet)
        {
            JsonToastContent toastContent = null;

            JsonParseToastResult result = new JsonParseToastResult();

            int payloadSize = System.Text.Encoding.UTF8.GetByteCount(json);

            if (payloadSize > PAYLOAD_SIZE_LIMIT)
            {
                result.Errors.Add(new ParseError(ParseErrorType.ErrorButRenderAllowed, $"Your payload exceeds the 5 KB size limit (it is {payloadSize.ToString("N0")} Bytes). Please reduce your payload, or else Windows will not display it."));
            }

            var settings = new JsonSerializerSettings()
            {
                MissingMemberHandling = MissingMemberHandling.Error,
                Error = new EventHandler <ErrorEventArgs>((sender, args) =>
                {
                    HandleError(result.Errors, args);
                }),
                ContractResolver = new DefaultContractResolver()
                {
                    NamingStrategy = new JsonToastNamingStrategy()
                }
            };

            try
            {
                toastContent = JsonConvert.DeserializeObject <JsonToastContent>(json, settings);

                if (toastContent.Type != "AdaptiveCard")
                {
                    result.Errors.Add(new ParseError(ParseErrorType.ErrorButRenderAllowed, "\"type\": \"AdaptiveCard\" must be specified in your payload."));
                }
                if (toastContent.Version == null)
                {
                    result.Errors.Add(new ParseError(ParseErrorType.ErrorButRenderAllowed, "\"version\" property must be specified in your payload."));
                }

                var actionReg = new AdaptiveActionParserRegistration();
                // Can't override the Submit parser so can't know whether dev actually sent -ms-systemActivationType="dismiss" or if we translated it
                actionReg.Set("MsAction.Dismiss", new DismissActionParser(result.Errors));
                actionReg.Set("MsAction.Snooze", new SnoozeActionParser(result.Errors));


                AdaptiveElementParserRegistration elementRegistration = new AdaptiveElementParserRegistration();
                elementRegistration.Set("ActionSet", new ActionSetParser());

                var cardParseResult = AdaptiveCard.FromJsonString(json, elementRegistration, actionReg);
                toastContent.Card = cardParseResult.AdaptiveCard;

                foreach (var error in cardParseResult.Errors)
                {
                    result.Errors.Add(new ParseError(ParseErrorType.ErrorButRenderAllowed, error.Message));
                }
                foreach (var warning in cardParseResult.Warnings)
                {
                    result.Errors.Add(new ParseError(ParseErrorType.Warning, warning.Message));
                }
            }
            catch (Exception ex)
            {
                // Json parse exceptions are handled by the error handler and already reported
                if (!(ex is JsonReaderException))
                {
                    result.Errors.Add(new ParseError(ParseErrorType.Error, ex.Message));
                }
            }

            result.Content = toastContent;

            return(result);
        }