cookieeditor_test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.CookieEditorTest');
  15. goog.setTestOnly('goog.ui.CookieEditorTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.events.Event');
  18. goog.require('goog.events.EventType');
  19. goog.require('goog.net.cookies');
  20. goog.require('goog.testing.events');
  21. goog.require('goog.testing.jsunit');
  22. goog.require('goog.ui.CookieEditor');
  23. var COOKIE_KEY = 'my_fabulous_cookie';
  24. var COOKIE_VALUES;
  25. goog.net.cookies.get = function(key) {
  26. return COOKIE_VALUES[key];
  27. };
  28. goog.net.cookies.set = function(key, value) {
  29. return COOKIE_VALUES[key] = value;
  30. };
  31. goog.net.cookies.remove = function(key, value) {
  32. delete COOKIE_VALUES[key];
  33. };
  34. function setUp() {
  35. goog.dom.removeChildren(goog.dom.getElement('test_container'));
  36. COOKIE_VALUES = {};
  37. }
  38. function newCookieEditor(opt_cookieValue) {
  39. // Set cookie.
  40. if (opt_cookieValue) {
  41. goog.net.cookies.set(COOKIE_KEY, opt_cookieValue);
  42. }
  43. // Render editor.
  44. var editor = new goog.ui.CookieEditor();
  45. editor.selectCookie(COOKIE_KEY);
  46. editor.render(goog.dom.getElement('test_container'));
  47. assertEquals(
  48. 'wrong text area value', opt_cookieValue || '',
  49. editor.textAreaElem_.value || '');
  50. return editor;
  51. }
  52. function testRender() {
  53. // Render editor.
  54. var editor = newCookieEditor();
  55. // All expected elements created?
  56. var elem = editor.getElement();
  57. assertNotNullNorUndefined('missing element', elem);
  58. assertNotNullNorUndefined('missing clear button', editor.clearButtonElem_);
  59. assertNotNullNorUndefined('missing update button', editor.updateButtonElem_);
  60. assertNotNullNorUndefined('missing text area', editor.textAreaElem_);
  61. }
  62. function testEditCookie() {
  63. // Render editor.
  64. var editor = newCookieEditor();
  65. // Invalid value.
  66. var newValue = 'my bad value;';
  67. editor.textAreaElem_.value = newValue;
  68. goog.testing.events.fireBrowserEvent(
  69. new goog.events.Event(
  70. goog.events.EventType.CLICK, editor.updateButtonElem_));
  71. assertTrue('unexpected cookie value', !goog.net.cookies.get(COOKIE_KEY));
  72. // Valid value.
  73. newValue = 'my fabulous value';
  74. editor.textAreaElem_.value = newValue;
  75. goog.testing.events.fireBrowserEvent(
  76. new goog.events.Event(
  77. goog.events.EventType.CLICK, editor.updateButtonElem_));
  78. assertEquals(
  79. 'wrong cookie value', newValue, goog.net.cookies.get(COOKIE_KEY));
  80. }
  81. function testClearCookie() {
  82. // Render editor.
  83. var value = 'I will be cleared';
  84. var editor = newCookieEditor(value);
  85. // Clear value.
  86. goog.testing.events.fireBrowserEvent(
  87. new goog.events.Event(
  88. goog.events.EventType.CLICK, editor.clearButtonElem_));
  89. assertTrue('unexpected cookie value', !goog.net.cookies.get(COOKIE_KEY));
  90. }