loggerclient_test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.messaging.LoggerClientTest');
  15. goog.setTestOnly('goog.messaging.LoggerClientTest');
  16. goog.require('goog.debug');
  17. goog.require('goog.debug.Logger');
  18. goog.require('goog.messaging.LoggerClient');
  19. goog.require('goog.testing.MockControl');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.testing.messaging.MockMessageChannel');
  22. var mockControl;
  23. var channel;
  24. var client;
  25. var logger;
  26. function setUp() {
  27. goog.debug.FORCE_SLOPPY_STACKS = false;
  28. mockControl = new goog.testing.MockControl();
  29. channel = new goog.testing.messaging.MockMessageChannel(mockControl);
  30. client = new goog.messaging.LoggerClient(channel, 'log');
  31. logger = goog.debug.Logger.getLogger('test.logging.Object');
  32. }
  33. function tearDown() {
  34. channel.dispose();
  35. client.dispose();
  36. }
  37. function testCommand() {
  38. channel.send('log', {
  39. name: 'test.logging.Object',
  40. level: goog.debug.Logger.Level.WARNING.value,
  41. message: 'foo bar',
  42. exception: undefined
  43. });
  44. mockControl.$replayAll();
  45. logger.warning('foo bar');
  46. mockControl.$verifyAll();
  47. }
  48. function testCommandWithException() {
  49. var ex = Error('oh no');
  50. ex.stack = ['one', 'two'];
  51. ex.message0 = 'message 0';
  52. ex.message1 = 'message 1';
  53. ex.ignoredProperty = 'ignored';
  54. channel.send('log', {
  55. name: 'test.logging.Object',
  56. level: goog.debug.Logger.Level.WARNING.value,
  57. message: 'foo bar',
  58. exception: {
  59. name: 'Error',
  60. message: ex.message,
  61. stack: ex.stack,
  62. lineNumber: ex.lineNumber || ex.line || 'Not available',
  63. fileName: ex.fileName || ex.sourceURL || window.location.href,
  64. message0: ex.message0,
  65. message1: ex.message1
  66. }
  67. });
  68. mockControl.$replayAll();
  69. logger.warning('foo bar', ex);
  70. mockControl.$verifyAll();
  71. }
  72. function testCommandWithStringException() {
  73. // NOTE: the stack traces won't match with the strict mode compatible
  74. // stack traces as they are recorded in different locations.
  75. goog.debug.FORCE_SLOPPY_STACKS = true;
  76. channel.send('log', {
  77. name: 'test.logging.Object',
  78. level: goog.debug.Logger.Level.WARNING.value,
  79. message: 'foo bar',
  80. exception: {
  81. name: 'Unknown error',
  82. message: 'oh no',
  83. stack: '[Anonymous](object, foo bar, oh no)\n' +
  84. '[Anonymous](foo bar, oh no)\n' + goog.debug.getStacktrace(),
  85. lineNumber: 'Not available',
  86. fileName: window.location.href
  87. }
  88. });
  89. mockControl.$replayAll();
  90. logger.warning('foo bar', 'oh no');
  91. mockControl.$verifyAll();
  92. }