toolbarfactory_test.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2010 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.ui.editor.ToolbarFactoryTest');
  15. goog.setTestOnly('goog.ui.editor.ToolbarFactoryTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.testing.ExpectedFailures');
  18. goog.require('goog.testing.editor.TestHelper');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.ui.editor.ToolbarFactory');
  21. goog.require('goog.userAgent');
  22. var helper;
  23. var expectedFailures;
  24. function setUpPage() {
  25. helper = new goog.testing.editor.TestHelper(goog.dom.getElement('myField'));
  26. expectedFailures = new goog.testing.ExpectedFailures();
  27. }
  28. function setUp() {
  29. helper.setUpEditableElement();
  30. }
  31. function tearDown() {
  32. helper.tearDownEditableElement();
  33. expectedFailures.handleTearDown();
  34. }
  35. /**
  36. * Makes sure we have the correct conversion table in
  37. * goog.ui.editor.ToolbarFactory.LEGACY_SIZE_TO_PX_MAP_. Can only be tested in
  38. * a browser that takes legacy size values as input to execCommand but returns
  39. * pixel size values from queryCommandValue. That's OK because that's the only
  40. * situation where this conversion table's precision is critical. (When it's
  41. * used to size the labels of the font size menu options it's ok if it's a few
  42. * pixels off.)
  43. */
  44. function testGetLegacySizeFromPx() {
  45. // We will be warned if other browsers start behaving like webkit pre-534.7.
  46. expectedFailures.expectFailureFor(
  47. !goog.userAgent.WEBKIT ||
  48. (goog.userAgent.WEBKIT && goog.userAgent.isVersionOrHigher('534.7')));
  49. try {
  50. var fieldElem = goog.dom.getElement('myField');
  51. // Start from 1 because size 0 is bogus (becomes 16px, legacy size 3).
  52. for (var i = 1;
  53. i < goog.ui.editor.ToolbarFactory.LEGACY_SIZE_TO_PX_MAP_.length; i++) {
  54. helper.select(fieldElem, 0, fieldElem, 1);
  55. document.execCommand('fontSize', false, i);
  56. helper.select('foo', 1);
  57. var value = document.queryCommandValue('fontSize');
  58. assertEquals(
  59. 'Px size ' + value + ' should convert to legacy size ' + i, i,
  60. goog.ui.editor.ToolbarFactory.getLegacySizeFromPx(
  61. parseInt(value, 10)));
  62. }
  63. } catch (e) {
  64. expectedFailures.handleException(e);
  65. }
  66. }