public void TextBox_Reveal_Password_Reset_When_Lost_Focus()
        {
            using (Start(FocusServices))
            {
                var target1 = new MaskedTextBox
                {
                    Template     = CreateTemplate(),
                    Text         = "1234",
                    PasswordChar = '*'
                };
                var target2 = new MaskedTextBox
                {
                    Template = CreateTemplate(),
                    Text     = "5678"
                };
                var sp = new StackPanel();
                sp.Children.Add(target1);
                sp.Children.Add(target2);

                target1.ApplyTemplate();
                target2.ApplyTemplate();

                var root = new TestRoot {
                    Child = sp
                };

                target1.Focus();
                target1.RevealPassword = true;

                target2.Focus();

                Assert.False(target1.RevealPassword);
            }
        }
        public void Typing_Beginning_With_0_Should_Not_Modify_Text_When_Bound_To_Int()
        {
            using (Start())
            {
                var source = new Class1();
                var target = new MaskedTextBox
                {
                    DataContext = source,
                    Template    = CreateTemplate(),
                };

                target.ApplyTemplate();
                target.Bind(TextBox.TextProperty, new Binding(nameof(Class1.Foo), BindingMode.TwoWay));

                Assert.Equal("0", target.Text);

                target.CaretIndex = 1;
                target.RaiseEvent(new TextInputEventArgs
                {
                    RoutedEvent = InputElement.TextInputEvent,
                    Text        = "2",
                });

                Assert.Equal("02", target.Text);
            }
        }
        public void TextBox_CaretIndex_Persists_When_Focus_Lost()
        {
            using (Start(FocusServices))
            {
                var target1 = new MaskedTextBox
                {
                    Template = CreateTemplate(),
                    Text     = "1234"
                };
                var target2 = new MaskedTextBox
                {
                    Template = CreateTemplate(),
                    Text     = "5678"
                };
                var sp = new StackPanel();
                sp.Children.Add(target1);
                sp.Children.Add(target2);

                target1.ApplyTemplate();
                target2.ApplyTemplate();

                var root = new TestRoot {
                    Child = sp
                };

                target2.Focus();
                target2.CaretIndex = 2;
                Assert.False(target1.IsFocused);
                Assert.True(target2.IsFocused);

                target1.Focus();

                Assert.Equal(2, target2.CaretIndex);
            }
        }
        public void Press_Enter_Add_Default_Newline()
        {
            using (Start())
            {
                var target = new MaskedTextBox
                {
                    Template      = CreateTemplate(),
                    AcceptsReturn = true
                };

                target.ApplyTemplate();

                RaiseKeyEvent(target, Key.Enter, 0);

                Assert.Equal(Environment.NewLine, target.Text);
            }
        }
        public void Press_Enter_Add_Custom_Newline()
        {
            using (Start())
            {
                var target = new MaskedTextBox
                {
                    Template      = CreateTemplate(),
                    AcceptsReturn = true,
                    NewLine       = "Test"
                };

                target.ApplyTemplate();

                RaiseKeyEvent(target, Key.Enter, 0);

                Assert.Equal("Test", target.Text);
            }
        }
        public void Press_Ctrl_A_Select_All_Text()
        {
            using (Start())
            {
                var target = new MaskedTextBox
                {
                    Template = CreateTemplate(),
                    Text     = "1234"
                };

                target.ApplyTemplate();

                RaiseKeyEvent(target, Key.A, KeyModifiers.Control);

                Assert.Equal(0, target.SelectionStart);
                Assert.Equal(4, target.SelectionEnd);
            }
        }
        public void Opening_Context_Flyout_Does_not_Lose_Selection()
        {
            using (Start(FocusServices))
            {
                var target1 = new MaskedTextBox
                {
                    Template      = CreateTemplate(),
                    Text          = "1234",
                    ContextFlyout = new MenuFlyout
                    {
                        Items = new List <MenuItem>
                        {
                            new MenuItem {
                                Header = "Item 1"
                            },
                            new MenuItem {
                                Header = "Item 2"
                            },
                            new MenuItem {
                                Header = "Item 3"
                            }
                        }
                    }
                };


                target1.ApplyTemplate();

                var root = new TestRoot()
                {
                    Child = target1
                };

                target1.SelectionStart = 0;
                target1.SelectionEnd   = 3;

                target1.Focus();
                Assert.True(target1.IsFocused);

                target1.ContextFlyout.ShowAt(target1);

                Assert.Equal("123", target1.SelectedText);
            }
        }
        public void TextBox_GotFocus_And_LostFocus_Work_Properly()
        {
            using (Start(FocusServices))
            {
                var target1 = new MaskedTextBox
                {
                    Template = CreateTemplate(),
                    Text     = "1234"
                };
                var target2 = new MaskedTextBox
                {
                    Template = CreateTemplate(),
                    Text     = "5678"
                };
                var sp = new StackPanel();
                sp.Children.Add(target1);
                sp.Children.Add(target2);

                target1.ApplyTemplate();
                target2.ApplyTemplate();

                var root = new TestRoot {
                    Child = sp
                };

                var gfcount = 0;
                var lfcount = 0;

                target1.GotFocus  += (s, e) => gfcount++;
                target2.LostFocus += (s, e) => lfcount++;

                target2.Focus();
                Assert.False(target1.IsFocused);
                Assert.True(target2.IsFocused);

                target1.Focus();
                Assert.False(target2.IsFocused);
                Assert.True(target1.IsFocused);

                Assert.Equal(1, gfcount);
                Assert.Equal(1, lfcount);
            }
        }
        public void CaretIndex_Can_Moved_To_Position_After_The_End_Of_Text_With_Arrow_Key()
        {
            using (Start())
            {
                var target = new MaskedTextBox
                {
                    Template = CreateTemplate(),
                    Text     = "1234"
                };

                target.ApplyTemplate();
                target.CaretIndex = 3;
                target.Measure(Size.Infinity);

                RaiseKeyEvent(target, Key.Right, 0);

                Assert.Equal(4, target.CaretIndex);
            }
        }
Esempio n. 10
0
        public void Control_Delete_Should_Remove_The_Word_After_The_Caret_If_There_Is_No_Selection()
        {
            using (Start())
            {
                var textBox = new MaskedTextBox
                {
                    Template   = CreateTemplate(),
                    Text       = "First Second Third Fourth",
                    CaretIndex = 19
                };

                textBox.ApplyTemplate();

                // (First Second Third |Fourth)
                RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
                Assert.Equal("First Second Third ", textBox.Text);

                // (First Second |Third )
                textBox.CaretIndex = 13;
                RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
                Assert.Equal("First Second ", textBox.Text);

                // (First Sec|ond )
                textBox.CaretIndex = 9;
                RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
                Assert.Equal("First Sec", textBox.Text);

                // (Fi[rs]t Sec )
                textBox.SelectionStart = 2;
                textBox.SelectionEnd   = 4;

                RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
                Assert.Equal("Fit Sec", textBox.Text);

                // (Fit Sec| )
                textBox.Text      += " ";
                textBox.CaretIndex = 7;
                RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
                Assert.Equal("Fit Sec", textBox.Text);
            }
        }
Esempio n. 11
0
        public void Opening_Context_Menu_Does_not_Lose_Selection()
        {
            using (Start(FocusServices))
            {
                var target1 = new MaskedTextBox
                {
                    Template    = CreateTemplate(),
                    Text        = "1234",
                    ContextMenu = new TestContextMenu()
                };

                var target2 = new MaskedTextBox
                {
                    Template = CreateTemplate(),
                    Text     = "5678"
                };

                var sp = new StackPanel();
                sp.Children.Add(target1);
                sp.Children.Add(target2);

                target1.ApplyTemplate();
                target2.ApplyTemplate();

                var root = new TestRoot()
                {
                    Child = sp
                };

                target1.SelectionStart = 0;
                target1.SelectionEnd   = 3;

                target1.Focus();
                Assert.False(target2.IsFocused);
                Assert.True(target1.IsFocused);

                target2.Focus();

                Assert.Equal("123", target1.SelectedText);
            }
        }
Esempio n. 12
0
        public void MaxLength_Works_Properly(
            string initalText,
            string textInput,
            int maxLength,
            int selectionStart,
            int selectionEnd,
            bool fromClipboard,
            string expected)
        {
            using (Start())
            {
                var target = new MaskedTextBox
                {
                    Template       = CreateTemplate(),
                    Text           = initalText,
                    MaxLength      = maxLength,
                    SelectionStart = selectionStart,
                    SelectionEnd   = selectionEnd
                };

                target.ApplyTemplate();

                if (fromClipboard)
                {
                    AvaloniaLocator.CurrentMutable.Bind <IClipboard>().ToSingleton <ClipboardStub>();

                    var clipboard = AvaloniaLocator.CurrentMutable.GetService <IClipboard>();
                    clipboard.SetTextAsync(textInput).GetAwaiter().GetResult();

                    RaiseKeyEvent(target, Key.V, KeyModifiers.Control);
                    clipboard.ClearAsync().GetAwaiter().GetResult();
                }
                else
                {
                    RaiseTextEvent(target, textInput);
                }

                Assert.Equal(expected, target.Text);
            }
        }
Esempio n. 13
0
        public void Control_Backspace_Should_Remove_The_Word_Before_The_Caret_If_There_Is_No_Selection()
        {
            using (Start())
            {
                MaskedTextBox textBox = new MaskedTextBox
                {
                    Template   = CreateTemplate(),
                    Text       = "First Second Third Fourth",
                    CaretIndex = 5
                };

                textBox.ApplyTemplate();

                // (First| Second Third Fourth)
                RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
                Assert.Equal(" Second Third Fourth", textBox.Text);

                // ( Second |Third Fourth)
                textBox.CaretIndex = 8;
                RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
                Assert.Equal(" Third Fourth", textBox.Text);

                // ( Thi|rd Fourth)
                textBox.CaretIndex = 4;
                RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
                Assert.Equal(" rd Fourth", textBox.Text);

                // ( rd F[ou]rth)
                textBox.SelectionStart = 5;
                textBox.SelectionEnd   = 7;

                RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
                Assert.Equal(" rd Frth", textBox.Text);

                // ( |rd Frth)
                textBox.CaretIndex = 1;
                RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
                Assert.Equal("rd Frth", textBox.Text);
            }
        }