Пример #1
0
        public static void Register(DisplayModeProvider instance)
        {
            var builder = new DisplayModeMatrixBuilder();

            var profiles = builder
                           .AddOptionalFactor("Device", o => o
                                              .Evidence("Tablet", x => Regex.IsMatch(x.GetOverriddenUserAgent(), @"\b(iPad|Tablet)\b"))
                                              .Evidence("Mobile", x => x.GetOverriddenBrowser().IsMobileDevice))
                           .AddOptionalFactor("Theme", o => o
                                              .Evidence("Dark", x => x.Request.Cookies.AllKeys.Contains("Theme") && x.Request.Cookies["Theme"].Value == "dark")
                                              .Evidence("Light", x => x.Request.Cookies.AllKeys.Contains("Theme") && x.Request.Cookies["Theme"].Value == "light"))
                           .AddOptionalFactor("Preview", o => o
                                              .Evidence("Preview", x => x.Request.Cookies.AllKeys.Contains("Preview")))
                           .Build();

            instance.Modes.Clear();

            foreach (var profile in profiles)
            {
                instance.Modes.Add(new DefaultDisplayMode(profile.Name)
                {
                    ContextCondition = profile.ContextCondition
                });
            }

            instance.Modes.Add(new DefaultDisplayMode());
        }
Пример #2
0
        public void Test_DisplayModeMatrixBuilder_Could_Provide_Right_Combonations()
        {
            //Arrange
            var builder = new DisplayModeMatrixBuilder();

            var matrix = builder
                         .AddOptionalFactor("Device", l => l.Evidence("Mobile", x => true).Evidence("Tablet", x => true))
                         .AddOptionalFactor("Theme", l => l.Evidence("Dark", x => true))
                         .AddOptionalFactor("Preview", l => l.Evidence("Preview", x => true))
                         .Build();

            //Act
            var result = matrix.Select(x => x.Name).ToList();

            foreach (var x in result)
            {
                Console.WriteLine(x);
            }

            //Assert
            var expected = new[]
            {
                "Mobile-Dark-Preview",
                "Tablet-Dark-Preview",
                "Mobile-Dark",
                "Tablet-Dark",
                "Mobile-Preview",
                "Tablet-Preview",
                "Dark-Preview",
                "Mobile",
                "Tablet",
                "Dark",
                "Preview",
            };

            CollectionAssert.AreEqual(expected, result);
        }
        public static void Register(DisplayModeProvider instance)
        {
            //instance.Modes.Add(new DefaultDisplayMode("Mobile-Dark-Preview")
            //{
            //    ContextCondition = x => IsMobile(x) && CurrentTheme(x) == "dark" && IsPreview(x)
            //});

            //instance.Modes.Add(new DefaultDisplayMode("Tablet-Dark-Preview")
            //{
            //    ContextCondition = x => IsTablet(x) && CurrentTheme(x) == "dark" && IsPreview(x)
            //});

            //instance.Modes.Add(new DefaultDisplayMode("Mobile-Dark")
            //{
            //    ContextCondition = x => IsMobile(x) && CurrentTheme(x) == "dark"
            //});

            //instance.Modes.Add(new DefaultDisplayMode("Tablet-Dark")
            //{
            //    ContextCondition = x => IsTablet(x) && CurrentTheme(x) == "dark"
            //});

            //instance.Modes.Add(new DefaultDisplayMode("Dark-Preview")
            //{
            //    ContextCondition = x => CurrentTheme(x) == "dark" && IsPreview(x)
            //});

            //instance.Modes.Add(new DefaultDisplayMode("Mobile-Preview")
            //{
            //    ContextCondition = x => IsMobile(x) && IsPreview(x)
            //});

            //instance.Modes.Add(new DefaultDisplayMode("Tablet-Preview")
            //{
            //    ContextCondition = x => IsTablet(x) && IsPreview(x)
            //});

            //instance.Modes.Add(new DefaultDisplayMode("Mobile")
            //{
            //    ContextCondition = x => IsMobile(x)
            //});

            //instance.Modes.Add(new DefaultDisplayMode("Tablet")
            //{
            //    ContextCondition = x => IsTablet(x)
            //});

            //instance.Modes.Add(new DefaultDisplayMode("Dark")
            //{
            //    ContextCondition = x => CurrentTheme(x) == "dark"
            //});

            //instance.Modes.Add(new DefaultDisplayMode("Preview")
            //{
            //    ContextCondition = x => IsPreview(x)
            //});


            var builder = new DisplayModeMatrixBuilder();

            var matrix = builder
                         //.Precondition(x => false)
                         //.SetEvaluateBehavior(EvaluateBehavior.Lazy)
                         .AddOptionalFactor("Device", l => l
                                            .Evidence("Tablet", x => IsTablet(x))
                                            .Evidence("Mobile", x => IsMobile(x)))
                         .AddOptionalFactor("Theme", l => l
                                            .Evidence("Dark", x => CurrentTheme(x) == "dark")
                                            .Evidence("Light", x => CurrentTheme(x) == "light"))
                         .AddOptionalFactor("Preview", l => l
                                            .Evidence("Preview", x => IsPreview(x)))
                         .Build();

            instance.Modes.Clear();

            foreach (var profile in matrix)
            {
                instance.Modes.Add(new DefaultDisplayMode(profile.Name)
                {
                    ContextCondition = profile.ContextCondition
                });
            }

            instance.Modes.Add(new DefaultDisplayMode(""));
        }