aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/labs/observe/observer_test.html
blob: 3b3e5d2c7d4ee885324fc3285921ca91a6a0e6b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!DOCTYPE html>
<html>
<!--
Copyright 2012 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.labs.observe.Observer</title>
<script src="../../base.js"></script>
<script>
  goog.require('goog.labs.observe.Notice');
  goog.require('goog.labs.observe.Observer');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.recordFunction');
</script>
</head>
<body>
<script>
var TYPE = 'testtype';


function testToObserver() {
  var expectedScope = this;
  var notice = new goog.labs.observe.Notice(null, TYPE);
  var fn = goog.testing.recordFunction(function(n) {
    assertEquals('Function receives wrong notice object.', notice, n);
    assertEquals('Function is executed with wrong scope.', expectedScope, this);
  });
  var observer = goog.labs.observe.Observer.fromFunction(fn);
  observer.notify(notice);
  assertEquals(1, fn.getCallCount());
}


function testToObserverWithScope() {
  var expectedScope = {};
  var notice = new goog.labs.observe.Notice(null, TYPE);
  var fn = goog.testing.recordFunction(function(n) {
    assertEquals('Function receives wrong notice object.', notice, n);
    assertEquals('Function is executed with wrong scope.', expectedScope, this);
  });
  var observer = goog.labs.observe.Observer.fromFunction(fn, expectedScope);
  observer.notify(notice);
  assertEquals(1, fn.getCallCount());
}


function testToObserverEquals() {
  var fn = function() {};
  assertTrue(
      goog.labs.observe.Observer.fromFunction(fn).equals(
          goog.labs.observe.Observer.fromFunction(fn)));

  var scope = {};
  assertTrue(
      goog.labs.observe.Observer.fromFunction(fn, scope).equals(
          goog.labs.observe.Observer.fromFunction(fn, scope)));
}
</script>
</body>