Example #1
0
 /// <summary>
 /// Synthesize a key event to the window. The event types supported are:
 /// keydown, keyup, keypress
 ///
 /// Key events generally end up being sent to the focused node.
 ///
 /// Cannot be accessed from unprivileged context (not content-accessible)
 /// Will throw a DOM security error if called without UniversalXPConnect
 /// privileges.
 ///
 /// @param aType event type
 /// @param aKeyCode key code
 /// @param aCharCode character code
 /// @param aModifiers modifiers pressed, using constants defined in nsIDOMNSEvent
 /// @param aPreventDefault if true, preventDefault() the event before dispatch
 ///
 /// @return false if the event had preventDefault() called on it,
 /// true otherwise.  In other words, true if and only if the
 /// default action was taken.
 /// </summary>
 ///
 public bool SendKeyEvent(string aType, int aKeyCode, int aCharCode, int aModifiers, bool aPreventDefault)
 {
     using (nsAString type = new nsAString(aType))
     {
         return(_windowUtils.SendKeyEvent(type, aKeyCode, aCharCode, aModifiers, aPreventDefault));
     }
 }
        public void DomContentChanged_ChangeContentOfTextInputWithKeyPressAndMoveToSecondInput_DomContentChangedShouldFire()
        {
            string html = "<input id=\"one\" type=\"text\" value=\"hello\" /><input id=\"two\" type=\"text\"  value=\"world\" />";

            LoadHtml(html);

            // Place browser on a form and show it. This is need to make the gecko accept the key press.
            Form f = new Form();

            f.Controls.Add(browser);
            browser.Visible = true;
            f.Show();

            // Focus first input box
            browser.Document.GetElementById("one").Focus();
            GeckoRange range = browser.Document.CreateRange();

            range.SelectNode(browser.Document.GetElementById("one"));
            browser.Window.Selection.AddRange(range);

            // record if DomContentChanged event happened.
            bool contentChangedEventReceived = false;

            browser.DomContentChanged += (sender, e) => contentChangedEventReceived = true;


            // Modify first input by sending a keypress.
            // TODO: create wrapper for nsIDOMWindowUtils
            nsIDOMWindowUtils utils = Xpcom.QueryInterface <nsIDOMWindowUtils>(browser.Window.DomWindow);

            using (nsAString type = new nsAString("keypress"))
            {
                utils.SendKeyEvent(type, 0, 102, 0, false);
            }

            // DomContentChanged Event should fire when we move we move to next element.
            browser.Document.GetElementById("two").Focus();
            range.SelectNode(browser.Document.GetElementById("two"));
            browser.Window.Selection.RemoveAllRanges();
            browser.Window.Selection.AddRange(range);

            Assert.IsTrue(contentChangedEventReceived);
        }