environment_test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // Copyright 2014 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.labs.testing.environmentTest');
  15. goog.setTestOnly('goog.labs.testing.environmentTest');
  16. goog.require('goog.Promise');
  17. goog.require('goog.labs.testing.Environment');
  18. goog.require('goog.testing.MockClock');
  19. goog.require('goog.testing.MockControl');
  20. goog.require('goog.testing.PropertyReplacer');
  21. goog.require('goog.testing.TestCase');
  22. goog.require('goog.testing.jsunit');
  23. goog.require('goog.testing.testSuite');
  24. var testCase = null;
  25. var mockControl = null;
  26. var replacer = null;
  27. // Use this flag to control whether the global JsUnit lifecycle events are being
  28. // called as part of the test lifecycle or as part of the "mocked" environment.
  29. var testing = false;
  30. function setUp() {
  31. // These methods end up being called by the test framework for these tests
  32. // as well as the part of the environment that is being tested as part
  33. // of the test. Bail if the test is already running.
  34. if (testing) {
  35. // This value is used by the testSetupReturnsValue test below
  36. return 'hello';
  37. }
  38. // Temporarily override the initializeTestRunner method to avoid installing
  39. // our "test" TestCase.
  40. var initFn = goog.testing.TestCase.initializeTestRunner;
  41. goog.testing.TestCase.initializeTestRunner = function() {};
  42. testCase = new goog.labs.testing.EnvironmentTestCase_();
  43. goog.labs.testing.EnvironmentTestCase_.getInstance = function() {
  44. return testCase;
  45. };
  46. goog.testing.TestCase.initializeTestRunner = initFn;
  47. mockControl = new goog.testing.MockControl();
  48. replacer = new goog.testing.PropertyReplacer();
  49. }
  50. function tearDown() {
  51. if (testing) {
  52. return;
  53. }
  54. replacer.reset();
  55. mockControl.$resetAll();
  56. mockControl.$tearDown();
  57. }
  58. function testLifecycle() {
  59. testing = true;
  60. var envOne = mockControl.createStrictMock(goog.labs.testing.Environment);
  61. var envTwo = mockControl.createStrictMock(goog.labs.testing.Environment);
  62. var envThree = mockControl.createStrictMock(goog.labs.testing.Environment);
  63. var testMethod = mockControl.createFunctionMock('testMethod');
  64. testCase.addNewTest('testFake', testMethod);
  65. testCase.registerEnvironment_(envOne);
  66. testCase.registerEnvironment_(envTwo);
  67. testCase.registerEnvironment_(envThree);
  68. envOne.setUpPage();
  69. envTwo.setUpPage();
  70. envThree.setUpPage();
  71. envOne.setUp();
  72. envTwo.setUp();
  73. envThree.setUp();
  74. testMethod();
  75. envThree.tearDown();
  76. envTwo.tearDown();
  77. envOne.tearDown();
  78. envThree.tearDownPage();
  79. envTwo.tearDownPage();
  80. envOne.tearDownPage();
  81. mockControl.$replayAll();
  82. testCase.runTests();
  83. mockControl.$verifyAll();
  84. testing = false;
  85. }
  86. function testLifecycle_withPromises() {
  87. var mockClock = new goog.testing.MockClock(true /* autoinstall */);
  88. testing = true;
  89. var envOne = mockControl.createStrictMock(goog.labs.testing.Environment);
  90. testCase.registerEnvironment_(envOne);
  91. var envTwo = mockControl.createStrictMock(goog.labs.testing.Environment);
  92. testCase.registerEnvironment_(envTwo);
  93. var testObj = {
  94. 'configureEnvironment':
  95. mockControl.createFunctionMock('configureEnvironment'),
  96. 'setUpPage': mockControl.createFunctionMock('setUpPage'),
  97. 'setUp': mockControl.createFunctionMock('setUp'),
  98. 'tearDown': mockControl.createFunctionMock('tearDown'),
  99. 'tearDownPage': mockControl.createFunctionMock('tearDownPage'),
  100. 'testFoo': mockControl.createFunctionMock('testFoo')
  101. };
  102. testCase.setTestObj(testObj);
  103. // Make the testCase.runTestsReturningPromise() a pending operation so we can
  104. // use assertNextCall also for checking the first call.
  105. var resultPromise;
  106. var pendingOp = goog.Promise.withResolver();
  107. pendingOp.promise.then(function() {
  108. resultPromise = testCase.runTestsReturningPromise();
  109. });
  110. var nextOp = null;
  111. var finishPendingOp = function() {
  112. pendingOp.resolve();
  113. pendingOp = nextOp;
  114. nextOp = null;
  115. mockClock.tick();
  116. };
  117. var expectCallTo = function(expectedCall) {
  118. assertNull(nextOp);
  119. mockControl.$resetAll();
  120. nextOp = goog.Promise.withResolver();
  121. expectedCall().$returns(nextOp.promise);
  122. mockControl.$replayAll();
  123. finishPendingOp();
  124. mockControl.$verifyAll();
  125. };
  126. // Make sure there are no hanging expecations dropped by the first $resetAll.
  127. mockControl.$replayAll();
  128. mockControl.$verifyAll();
  129. expectCallTo(envOne.setUpPage);
  130. expectCallTo(envTwo.setUpPage);
  131. expectCallTo(testObj.setUpPage);
  132. expectCallTo(testObj.configureEnvironment);
  133. expectCallTo(envOne.setUp);
  134. expectCallTo(envTwo.setUp);
  135. expectCallTo(testObj.setUp);
  136. expectCallTo(testObj.testFoo);
  137. mockControl.$resetAll();
  138. testObj.tearDown();
  139. envTwo.tearDown();
  140. envOne.tearDown();
  141. testObj.tearDownPage();
  142. envTwo.tearDownPage();
  143. envOne.tearDownPage();
  144. mockControl.$replayAll();
  145. finishPendingOp();
  146. var result = mockClock.tickPromise(resultPromise);
  147. mockControl.$verifyAll();
  148. assertTrue(result.complete);
  149. assertEquals(1, result.totalCount);
  150. assertEquals(1, result.runCount);
  151. assertEquals(1, result.successCount);
  152. assertEquals(0, result.errors.length);
  153. testing = false;
  154. mockClock.uninstall();
  155. }
  156. function testTearDownWithMockControl() {
  157. testing = true;
  158. var envWith = new goog.labs.testing.Environment();
  159. var envWithout = new goog.labs.testing.Environment();
  160. var mockControlMock = mockControl.createStrictMock(goog.testing.MockControl);
  161. var mockControlCtorMock =
  162. mockControl.createMethodMock(goog.testing, 'MockControl');
  163. mockControlCtorMock().$times(1).$returns(mockControlMock);
  164. // Expecting verify / reset calls twice since two environments use the same
  165. // mockControl, but only one created it and is allowed to tear it down.
  166. mockControlMock.$verifyAll();
  167. mockControlMock.$replayAll();
  168. mockControlMock.$verifyAll();
  169. mockControlMock.$resetAll();
  170. mockControlMock.$tearDown().$times(1);
  171. mockControlMock.$verifyAll();
  172. mockControlMock.$replayAll();
  173. mockControlMock.$verifyAll();
  174. mockControlMock.$resetAll();
  175. mockControl.$replayAll();
  176. envWith.withMockControl();
  177. envWithout.mockControl = mockControlMock;
  178. envWith.tearDown();
  179. envWithout.tearDown();
  180. mockControl.$verifyAll();
  181. mockControl.$resetAll();
  182. testing = false;
  183. }
  184. function testAutoDiscoverTests() {
  185. testing = true;
  186. var setUpPageFn = testCase.setUpPage;
  187. var setUpFn = testCase.setUp;
  188. var tearDownFn = testCase.tearDownFn;
  189. var tearDownPageFn = testCase.tearDownPageFn;
  190. testCase.autoDiscoverTests();
  191. assertEquals(setUpPageFn, testCase.setUpPage);
  192. assertEquals(setUpFn, testCase.setUp);
  193. assertEquals(tearDownFn, testCase.tearDownFn);
  194. assertEquals(tearDownPageFn, testCase.tearDownPageFn);
  195. // Note that this number changes when more tests are added to this file as
  196. // the environment reflects on the window global scope for JsUnit.
  197. assertEquals(9, testCase.tests_.length);
  198. testing = false;
  199. }
  200. // Verify "goog.testing.testSuite" integration
  201. function testTestSuiteTests() {
  202. testing = true;
  203. // don't try to reinitialize the test runner, while a test is running.
  204. replacer.set(goog.testing.TestCase, 'initializeTestRunner', function() {});
  205. // with an active environment.
  206. var envOne = new goog.labs.testing.Environment();
  207. var setUpPageFn = testCase.setUpPage;
  208. var setUpFn = testCase.setUp;
  209. var tearDownFn = testCase.tearDownFn;
  210. var tearDownPageFn = testCase.tearDownPageFn;
  211. goog.testing.testSuite({
  212. // These lifecycle methods should not override the environment testcase
  213. // methods but they should be called, when the test runs.
  214. setUp: function() {},
  215. tearDown: function() {},
  216. setUpPage: function() {},
  217. tearDownPage: function() {},
  218. // This test method should be added.
  219. testMe: function() {}
  220. });
  221. assertEquals(setUpPageFn, testCase.setUpPage);
  222. assertEquals(setUpFn, testCase.setUp);
  223. assertEquals(tearDownFn, testCase.tearDownFn);
  224. assertEquals(tearDownPageFn, testCase.tearDownPageFn);
  225. // Note that this number changes when more tests are added to this file as
  226. // the environment reflects on the window global scope for JsUnit.
  227. assertEquals(1, testCase.tests_.length);
  228. testing = false;
  229. }
  230. function testSetupReturnsValue() {
  231. testing = true;
  232. var env = new goog.labs.testing.Environment();
  233. // Expect the environment to pass on any value returned by the user defined
  234. // setUp method.
  235. assertEquals('hello', testCase.setUp());
  236. testCase.tearDown();
  237. testing = false;
  238. }
  239. function testMockClock() {
  240. testing = true;
  241. var env = new goog.labs.testing.Environment().withMockClock();
  242. testCase.addNewTest('testThatThrowsEventually', function() {
  243. setTimeout(function() { throw new Error('LateErrorMessage'); }, 200);
  244. });
  245. testCase.runTests();
  246. assertTestFailure(testCase, 'testThatThrowsEventually', 'LateErrorMessage');
  247. testing = false;
  248. }
  249. function testMockControl() {
  250. testing = true;
  251. var env = new goog.labs.testing.Environment().withMockControl();
  252. var test = env.mockControl.createFunctionMock('test');
  253. testCase.addNewTest('testWithoutVerify', function() {
  254. test();
  255. env.mockControl.$replayAll();
  256. test();
  257. });
  258. testCase.runTests();
  259. assertNull(env.mockClock);
  260. testing = false;
  261. }
  262. function testMock() {
  263. testing = true;
  264. var env = new goog.labs.testing.Environment().withMockControl();
  265. var mock = env.mock({test: function() {}});
  266. testCase.addNewTest('testMockCalled', function() {
  267. mock.test().$times(2);
  268. env.mockControl.$replayAll();
  269. mock.test();
  270. mock.test();
  271. env.mockControl.verifyAll();
  272. });
  273. testCase.runTests();
  274. testing = false;
  275. }
  276. function assertTestFailure(testCase, name, message) {
  277. assertContains(message, testCase.result_.resultsByName[name][0].toString());
  278. }