focus_test.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2009 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.editor.focusTest');
  15. goog.setTestOnly('goog.editor.focusTest');
  16. goog.require('goog.dom.selection');
  17. goog.require('goog.editor.BrowserFeature');
  18. goog.require('goog.editor.focus');
  19. goog.require('goog.testing.jsunit');
  20. function setUp() {
  21. // Make sure focus is not in the input to begin with.
  22. var dummy = document.getElementById('dummyLink');
  23. dummy.focus();
  24. }
  25. /**
  26. * Tests that focusInputField() puts focus in the input field and sets the
  27. * cursor to the end of the text cointained inside.
  28. */
  29. function testFocusInputField() {
  30. var input = document.getElementById('myInput');
  31. assertNotEquals(
  32. 'Input should not be focused initially', input, document.activeElement);
  33. goog.editor.focus.focusInputField(input);
  34. if (goog.editor.BrowserFeature.HAS_ACTIVE_ELEMENT) {
  35. assertEquals(
  36. 'Input should be focused after call to focusInputField', input,
  37. document.activeElement);
  38. }
  39. assertEquals(
  40. 'Selection should start at the end of the input text', input.value.length,
  41. goog.dom.selection.getStart(input));
  42. assertEquals(
  43. 'Selection should end at the end of the input text', input.value.length,
  44. goog.dom.selection.getEnd(input));
  45. }