aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/base_test.html
blob: 309be786d45c18d6942aedc25cbb4f1fff524fe0 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
<!DOCTYPE html>
<html>
<!--
Copyright 2006 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.*</title>
<script src="base.js"></script>
<script>
goog.require('goog.functions');
// Used to test dynamic loading works, see testRequire*
goog.require('goog.Timer');
goog.require('goog.testing.PropertyReplacer');
goog.require('goog.testing.jsunit');
goog.require('goog.testing.recordFunction');
goog.require('goog.userAgent');
</script>
</head>
<body>
<div id="elem">
  <span>One</span>
  <span id="text">Two</span>
  <span>Three</span>
</div>

<iframe name="f1" id="f1" src=""></iframe>
<iframe name="f2" id="f2" src=""></iframe>
<script>

function getFramedVars(name) {
  var w = window.frames[name];
  var doc = w.document;
  doc.open();
  doc.write('<script>' +
            'var a = [0, 1, 2];' +
            'var o = {a: 0, b: 1};' +
            'var n = 42;' +
            'var b = true;' +
            'var s = "string";' +
            'var nv = null;' +
            'var u = undefined;' +
            'var fv = function(){};' +
            '</' + 'script>')
  doc.close();
  return {
    'array': w.a,
    'object': w.o,
    'number': w.n,
    'boolean': w.b,
    'string': w.s,
    'functionVar': w.fv,
    'nullVar': w.nv,
    'undefinedVar': w.u
  };
}

var framedVars = getFramedVars('f1');
var framedVars2 = getFramedVars('f2');
// remove iframe
var iframeElement = document.getElementById('f2');
iframeElement.parentNode.removeChild(iframeElement);

</script>

<script>

var stubs = new goog.testing.PropertyReplacer();
var originalGoogBind = goog.bind;

function tearDown() {
  goog.setCssNameMapping(undefined);
  stubs.reset();
  goog.bind = originalGoogBind;
}

function testLibrary() {
  assertNotUndefined("'goog' not loaded", goog);
}

function testProvide() {
  goog.provide('goog.test.name.space');
  assertNotUndefined('provide failed: goog.test', goog.test);
  assertNotUndefined('provide failed: goog.test.name', goog.test.name);
  assertNotUndefined('provide failed: goog.test.name.space',
      goog.test.name.space);

  // ensure that providing 'goog.test.name' doesn't throw an exception
  goog.provide('goog.test');
  goog.provide('goog.test.name');

  delete goog.test;
}

function testProvideStrictness() {
  goog.provide('goog.xy');
  assertProvideFails('goog.xy');

  goog.provide('goog.xy.z');
  assertProvideFails('goog.xy');

  window['goog']['xyz'] = 'Bob';
  assertProvideFails('goog.xyz');

  delete goog.xy;
  delete goog.xyz;
}

function assertProvideFails(namespace) {
  assertThrows('goog.provide(' + namespace + ') should have failed',
      goog.partial(goog.provide, namespace));
}

function testIsProvided() {
  goog.provide('goog.explicit');
  assertTrue(goog.isProvided_('goog.explicit'));
  goog.provide('goog.implicit.explicit');
  assertFalse(goog.isProvided_('goog.implicit'));
  assertTrue(goog.isProvided_('goog.implicit.explicit'));
}

function testGlobalize() {
  goog.globalize(goog);
  assertNotUndefined('Globalize goog', provide);

  var a = {a: 1, b: 2, c: 3};
  var b = {};
  goog.globalize(a, b);
  assertNotUndefined('Globalize to arbitrary object', b.a);
  assertNotUndefined('Globalize to arbitrary object', b.b);
  assertNotUndefined('Globalize to arbitrary object', b.c);
}

function testExportSymbol() {
  var date = new Date();

  assertTrue(typeof nodots == 'undefined');
  goog.exportSymbol('nodots', date);
  assertEquals(date, nodots);
  nodots = undefined;

  assertTrue(typeof gotcher == 'undefined');
  goog.exportSymbol('gotcher.dots.right.Here', date);
  assertEquals(date, gotcher.dots.right.Here);
  gotcher = undefined;

  goog.provide('an.existing.path');
  assertNotNull(an.existing.path);
  goog.exportSymbol('an.existing.path', date);
  assertEquals(date, an.existing.path);
  an = undefined;

  var foo = {foo: 'foo'};
  var bar = {bar: 'bar'};
  var baz = {baz: 'baz'};
  goog.exportSymbol('one.two.three.Four', foo);
  goog.exportSymbol('one.two.three.five', bar);
  goog.exportSymbol('one.two.six', baz);
  assertEquals(foo, one.two.three.Four);
  assertEquals(bar, one.two.three.five);
  assertEquals(baz, one.two.six);

  var win = {};
  var fooBar = {foo: 'foo', bar: 'bar'};
  goog.exportSymbol('one.two.four', fooBar, win);
  assertEquals(fooBar, win.one.two.four);
  assertTrue('four' in win.one.two);
  assertFalse('four' in one.two);
  one = undefined;
}

goog.exportSymbol('exceptionTest', function() {
  throw Error('ERROR');
});

function testExportSymbolExceptions() {
  var e = assertThrows('Exception wasn\'t thrown by exported function',
      exceptionTest);
  assertEquals('Unexpected error thrown', 'ERROR', e.message);
}

//=== tests for Require logic ===

function testRequireClosure() {
  assertNotUndefined('goog.Timer should be available', goog.Timer);
  assertNotUndefined('goog.events.EventTarget should be available', goog.events.EventTarget);
}

function testRequireWithExternalDuplicate() {
  // Do a provide without going via goog.require. Then goog.require it
  // indirectly and ensure it doesn't cause a duplicate script.
  goog.addDependency('dup.js', ['dup.base'], []);
  goog.addDependency('dup-child.js', ['dup.base.child'], ['dup.base']);
  goog.provide('dup.base');
  try {
    goog.global.CLOSURE_IMPORT_SCRIPT = function(src) {
      if (src == goog.basePath + 'dup.js') {
        fail('Duplicate script written!');
      } else if (src == goog.basePath + 'dup-child.js') {
        // Allow expected script.
        return true;
      } else {
        // Avoid affecting other state.
        return false;
      }
    };

    // To differentiate this call from the real one.
    var require = goog.require;
    require('dup.base.child');
  } finally {
    goog.global.CLOSURE_IMPORT_SCRIPT = undefined;
  }
}

//=== tests for language enhancements ===

function testTypeOf() {
  assertEquals('array', goog.typeOf([]));
  assertEquals('string', goog.typeOf('string'));
  assertEquals('number', goog.typeOf(123));
  assertEquals('null', goog.typeOf(null));
  assertEquals('undefined', goog.typeOf(undefined));
  assertEquals('object', goog.typeOf({}));
  assertEquals('function', goog.typeOf(function(){}));

  // Make sure that NodeList is not treated as an array... NodeLists should
  // be of type object but Safari incorrectly reports it as function so a not
  // equals test will have to suffice here.
  assertNotEquals('array', goog.typeOf(document.getElementsByName('*')));
  assertNotEquals('function', goog.typeOf(document.getElementsByName('*')));
  assertEquals('object', goog.typeOf(document.getElementsByName('*')));
}

function testTypeOfFramed() {
  assertEquals('array', goog.typeOf(framedVars.array));
  assertEquals('string', goog.typeOf(framedVars.string));
  assertEquals('number', goog.typeOf(framedVars.number));
  assertEquals('null', goog.typeOf(framedVars.nullVar));
  assertEquals('undefined', goog.typeOf(framedVars.undefinedVar));
  assertEquals('object', goog.typeOf(framedVars.object));
  assertEquals('function', goog.typeOf(framedVars.functionVar));

  // Opera throws when trying to do cross frame typeof on node lists.
  // IE behaves very strange when it comes to DOM nodes on disconnected frames.
}

function testTypeOfFramed2() {
  assertEquals('array', goog.typeOf(framedVars2.array));
  assertEquals('string', goog.typeOf(framedVars2.string));
  assertEquals('number', goog.typeOf(framedVars2.number));
  assertEquals('null', goog.typeOf(framedVars2.nullVar));
  assertEquals('undefined', goog.typeOf(framedVars2.undefinedVar));
  assertEquals('object', goog.typeOf(framedVars2.object));
  assertEquals('function', goog.typeOf(framedVars2.functionVar));

  // Opera throws when trying to do cross frame typeof on node lists.
  // IE behaves very strange when it comes to DOM nodes on disconnected frames.
}

function testIsDef() {
  var defined = 'foo';
  var nullVar = null;
  var notDefined;

  assertTrue('defined should be defined', goog.isDef(defined));
  assertTrue('null should be defined', goog.isDef(defined));
  assertFalse('undefined should not be defined', goog.isDef(notDefined));
}

function testIsDefAndNotNull() {
  assertTrue('string is defined and non-null', goog.isDefAndNotNull(''));
  assertTrue('object is defined and non-null', goog.isDefAndNotNull({}));
  assertTrue('function is defined and non-null',
      goog.isDefAndNotNull(goog.nullFunction));
  assertTrue('zero is defined and non-null',
      goog.isDefAndNotNull(0));
  assertFalse('null', goog.isDefAndNotNull(null));
  assertFalse('undefined', goog.isDefAndNotNull(undefined));
}

function testIsNull() {
  var notNull = 'foo';
  var nullVar = null;
  var notDefined;

  assertFalse('defined should not be null', goog.isNull(notNull));
  assertTrue('null should be null', goog.isNull(nullVar));
  assertFalse('undefined should not be null', goog.isNull(notDefined))
}

function testIsArray() {
  var array = [1, 2, 3];
  var arrayWithLengthSet = [1, 2, 3];
  arrayWithLengthSet.length = 2;
  var objWithArrayFunctions = {slice: function() {}, length: 0};
  var object = {a: 1, b: 2, c: 3};
  var nullVar = null;
  var notDefined;
  var elem = document.getElementById('elem');
  var text = document.getElementById('text').firstChild;
  var impostor = document.body.getElementsByTagName('BOGUS');
  impostor.push = Array.prototype.push;
  impostor.pop = Array.prototype.pop;
  impostor.slice = Array.prototype.slice;
  impostor.splice = Array.prototype.splice;

  assertTrue('array should be an array', goog.isArray(array));
  assertTrue('arrayWithLengthSet should be an array',
             goog.isArray(arrayWithLengthSet));
  assertFalse('object with array functions should not be an array unless ' +
              'length is not enumerable', goog.isArray(objWithArrayFunctions));
  assertFalse('object should not be an array', goog.isArray(object));
  assertFalse('null should not be an array', goog.isArray(nullVar));
  assertFalse('undefined should not be an array', goog.isArray(notDefined));
  assertFalse('NodeList should not be an array', goog.isArray(elem.childNodes));
  assertFalse('TextNode should not be an array', goog.isArray(text));
  assertTrue('Array of nodes should be an array',
             goog.isArray([elem.firstChild, elem.lastChild]));
  assertFalse('An impostor should not be an array', goog.isArray(impostor));
}

function testTypeOfAcrossWindow() {
  if (goog.userAgent.WEBKIT && goog.userAgent.MAC) {
    // The server farm has issues with new windows on Safari Mac.
    return;
  }

  var w = window.open('', 'blank');
  if (w) {
    try {
      try {
        var d = w.document;
        d.open();
        d.write('<script>function fun(){};' +
                'var arr = [];' +
                'var x = 42;' +
                'var s = "";' +
                'var b = true;' +
                'var obj = {length: 0, splice: {}, call: {}};' +
                '</' + 'script>');
        d.close();
      } catch (ex) {
        // In Firefox Linux on the server farm we don't have access to
        // w.document.
        return;
      }

      assertEquals('function', goog.typeOf(w.fun));
      assertEquals('array', goog.typeOf(w.arr));
      assertEquals('number', goog.typeOf(w.x));
      assertEquals('string', goog.typeOf(w.s));
      assertEquals('boolean', goog.typeOf(w.b));
      assertEquals('object', goog.typeOf(w.obj));
    } finally {
      w.close();
    }
  }
}

function testIsArrayLike() {
  var array = [1, 2, 3];
  var objectWithNumericLength = {length: 2};
  var objectWithNonNumericLength = {length: 'a'};
  var object = {a: 1, b: 2};
  var nullVar = null;
  var notDefined;
  var elem = document.getElementById('elem');
  var text = document.getElementById('text').firstChild;

  assertTrue('array should be array-like', goog.isArrayLike(array));
  assertTrue('obj w/numeric length should be array-like',
      goog.isArrayLike(objectWithNumericLength));
  assertFalse('obj w/non-numeric length should not be array-like',
      goog.isArrayLike(objectWithNonNumericLength));
  assertFalse('object should not be array-like', goog.isArrayLike(object));
  assertFalse('null should not be array-like', goog.isArrayLike(nullVar));
  assertFalse('undefined should not be array-like',
      goog.isArrayLike(notDefined));
  assertTrue('NodeList should be array-like',
      goog.isArrayLike(elem.childNodes));
  // TODO(attila): Fix isArrayLike to return false for text nodes!
  // assertFalse('TextNode should not be array-like', goog.isArrayLike(text));
  assertTrue('Array of nodes should be array-like',
      goog.isArrayLike([elem.firstChild, elem.lastChild]));
}


// Use mock date in testIsDateLike() rather than a real goog.date.Date to
// minimize dependencies in this unit test.
function MockGoogDate() {}

MockGoogDate.prototype.getFullYear = function() {
  return 2007;
};


function testIsDateLike() {
  var jsDate = new Date();
  var googDate = new MockGoogDate();
  var string = 'foo';
  var number = 1;
  var nullVar = null;
  var notDefined;

  assertTrue('js Date should be date-like', goog.isDateLike(jsDate));
  assertTrue('goog Date should be date-like', goog.isDateLike(googDate));
  assertFalse('string should not be date-like', goog.isDateLike(string));
  assertFalse('number should not be date-like', goog.isDateLike(number));
  assertFalse('nullVar should not be date-like', goog.isDateLike(nullVar));
  assertFalse('undefined should not be date-like', goog.isDateLike(notDefined));
}

function testIsString() {
  var string = 'foo';
  var number = 2;
  var nullVar = null;
  var notDefined;

  assertTrue('string should be a string', goog.isString(string));
  assertFalse('number should not be a string', goog.isString(number));
  assertFalse('null should not be a string', goog.isString(nullVar));
  assertFalse('undefined should not be a string', goog.isString(notDefined));
}

function testIsBoolean() {
  var b = true;
  var s = 'true';
  var num = 1;
  var nullVar = null;
  var notDefined;

  assertTrue('boolean should be a boolean', goog.isBoolean(b));
  assertFalse('string should not be a boolean', goog.isBoolean(s));
  assertFalse('number should not be a boolean', goog.isBoolean(num));
  assertFalse('null should not be a boolean', goog.isBoolean(nullVar));
  assertFalse('undefined should not be a boolean', goog.isBoolean(notDefined));
}

function testIsNumber() {
  var number = 1;
  var string = '1';
  var nullVar = null;
  var notDefined;

  assertTrue('number should be a number', goog.isNumber(number));
  assertFalse('string should not be a number', goog.isNumber(string));
  assertFalse('null should not be a number', goog.isNumber(nullVar));
  assertFalse('undefined should not be a number', goog.isNumber(notDefined));
}

function testIsFunction() {
  var func = function() { return 1; };
  var object = {a: 1, b: 2};
  var nullVar = null;
  var notDefined;

  assertTrue('function should be a function', goog.isFunction(func));
  assertFalse('object should not be a function', goog.isFunction(object));
  assertFalse('null should not be a function', goog.isFunction(nullVar));
  assertFalse('undefined should not be a function', goog.isFunction(notDefined));
}

function testIsObject() {
  var object = {a: 1, b: 2};
  var string = 'b';
  var nullVar = null;
  var notDefined;
  var array = [0, 1, 2];
  var fun = function() {};

  assertTrue('object should be an object', goog.isObject(object));
  assertTrue('array should be an object', goog.isObject(array));
  assertTrue('function should be an object', goog.isObject(fun));
  assertFalse('string should not be an object', goog.isObject(string));
  assertFalse('null should not be an object', goog.isObject(nullVar));
  assertFalse('undefined should not be an object', goog.isObject(notDefined));
}


//=== tests for unique ID methods ===

function testGetUid() {
  var a = {};
  var b = {};
  var c = {};

  var uid1 = goog.getUid(a);
  var uid2 = goog.getUid(b);
  var uid3 = goog.getUid(c);

  assertNotEquals('Unique IDs must be unique', uid1, uid2);
  assertNotEquals('Unique IDs must be unique', uid1, uid3);
  assertNotEquals('Unique IDs must be unique', uid2, uid3);
}

function testRemoveUidFromPlainObject() {
  var a = {};
  var uid = goog.getUid(a);
  goog.removeUid(a);
  assertNotEquals("An object's old and new unique IDs should be different",
      uid, goog.getUid(a));
}

function testRemoveUidFromObjectWithoutUid() {
  var a = {};
  // Removing a unique ID should not fail even if it did not exist
  goog.removeUid(a);
}

function testRemoveUidFromNode() {
  var node = document.createElement('div');
  var nodeUid = goog.getUid(node);
  goog.removeUid(node);
  assertNotEquals("A node's old and new unique IDs should be different",
      nodeUid, goog.getUid(node));
}

function testConstructorUid() {
  function BaseClass() {};
  function SubClass() {};
  goog.inherits(SubClass, BaseClass);

  var baseClassUid = goog.getUid(BaseClass);
  var subClassUid = goog.getUid(SubClass);

  assertTrue('Unique ID of BaseClass must be a number',
      typeof baseClassUid == 'number');
  assertTrue('Unique ID of SubClass must be a number',
      typeof subClassUid == 'number');
  assertNotEquals('Unique IDs of BaseClass and SubClass must differ',
      baseClassUid, subClassUid);
  assertNotEquals('Unique IDs of BaseClass and SubClass instances must differ',
      goog.getUid(new BaseClass), goog.getUid(new SubClass));

  assertEquals('Unique IDs of BaseClass.prototype and SubClass.prototype ' +
      'should differ, but to keep the implementation simple, we do not ' +
      'handle this edge case.',
      goog.getUid(BaseClass.prototype), goog.getUid(SubClass.prototype));
}

/**
 * Tests against Chrome bug where the re-created element will have the uid
 * property set but undefined. See bug 1252508.
 */
function testUidNotUndefinedOnReusedElement() {
  var div = document.createElement('DIV');
  document.body.appendChild(div);
  div.innerHTML = '<form id="form"></form>';
  var span = div.getElementsByTagName('FORM')[0];
  goog.getUid(span);

  div.innerHTML = '<form id="form"></form>';
  var span2 = div.getElementsByTagName('FORM')[0];
  assertNotUndefined(goog.getUid(span2));
}

function testWindowUid() {
  var uid = goog.getUid(window);
  assertTrue('window unique id is a number', goog.isNumber(uid));
  assertEquals('returns the same id second time', uid, goog.getUid(window));
  goog.removeUid(window);
  assertNotEquals('generates new id after the old one is removed',
      goog.getUid(window));
}

//=== tests for clone method ===

function testClonePrimitive() {
  assertEquals('cloning a primitive should return an equal primitive',
      5, goog.cloneObject(5));
}

function testCloneObjectThatHasACloneMethod() {
  var original = {
    name: 'original',
    clone: function() { return { name: 'clone' } }
  };

  var clone = goog.cloneObject(original);
  assertEquals('original', original.name);
  assertEquals('clone', clone.name);
}

function testCloneFlatObject() {
  var original = {a:1, b:2, c:3};
  var clone = goog.cloneObject(original);
  assertNotEquals(original, clone);
  assertEquals(1, clone.a);
  assertEquals(2, clone.b);
  assertEquals(3, clone.c);
}

function testCloneDeepObject() {
  var original = {
    a: 1,
    b: {c: 2, d: 3},
    e: {f: {g: 4, h: 5}}
  };
  var clone = goog.cloneObject(original);

  assertNotEquals(original, clone);
  assertNotEquals(original.b, clone.b);
  assertNotEquals(original.e, clone.e);

  assertEquals(1, clone.a);
  assertEquals(2, clone.b.c);
  assertEquals(3, clone.b.d);
  assertEquals(4, clone.e.f.g);
  assertEquals(5, clone.e.f.h);
}

function testCloneFunctions() {
  var original = {
    f: function() {
      return 'hi';
    }
  };
  var clone = goog.cloneObject(original);

  assertNotEquals(original, clone);
  assertEquals('hi', clone.f());
  assertEquals(original.f, clone.f);
}


//=== tests for bind() and friends ===

// Function.prototype.bind and Function.prototype.partial are purposefullly
// not defined in open sourced Closure.  These functions sniff for their
// presence.

var foo = 'global';
var obj = {foo: 'obj'};

function getFoo(arg1, arg2) {
  return {foo: this.foo, arg1: arg1, arg2: arg2};
}

function testBindWithoutObj() {
  if (Function.prototype.bind) {
    assertEquals(foo, getFoo.bind()().foo);
  }
}

function testBindWithObj() {
  if (Function.prototype.bind) {
    assertEquals(obj.foo, getFoo.bind(obj)().foo);
  }
}

function testBindWithNullObj() {
  if (Function.prototype.bind) {
    assertEquals(foo, getFoo.bind()().foo);
  }
}

function testBindStaticArgs() {
  if (Function.prototype.bind) {
    var fooprime = getFoo.bind(obj, 'hot', 'dog');
    var res = fooprime();
    assertEquals(obj.foo, res.foo);
    assertEquals('hot', res.arg1);
    assertEquals('dog', res.arg2);
  }
}

function testBindDynArgs() {
  if (Function.prototype.bind) {
    var res = getFoo.bind(obj)('hot', 'dog');
    assertEquals(obj.foo, res.foo);
    assertEquals('hot', res.arg1);
    assertEquals('dog', res.arg2);
  }
}

function testBindCurriedArgs() {
  if (Function.prototype.bind) {
    var res = getFoo.bind(obj, 'hot')('dog');
    assertEquals(obj.foo, res.foo);
    assertEquals('hot', res.arg1);
    assertEquals('dog', res.arg2);
  }
}

function testBindDoubleBind() {
  var getFooP = goog.bind(getFoo, obj, 'hot');
  var getFooP2 = goog.bind(getFooP, null, 'dog');

  var res = getFooP2();
  assertEquals("res.arg1 should be 'hot'", 'hot', res.arg1);
  assertEquals("res.arg2 should be 'dog'", 'dog', res.arg2);
}

function testBindWithCall() {
  var obj = {};
  var obj2 = {};
  var f = function() {
    assertEquals('this should be bound to obj', obj, this);
  };
  var b = goog.bind(f, obj);
  b.call(null);
  b.call(obj2);
}

function testBindJs() {
  assertEquals(1, goog.bindJs_(add, {valueOf: function() { return 1; }})());
  assertEquals(3, goog.bindJs_(add, null, 1, 2)());
}

function testBindNative() {
  if (Function.prototype.bind &&
      Function.prototype.bind.toString().indexOf('native code') != -1) {
    assertEquals(
        1, goog.bindNative_(add, {valueOf: function() { return 1; }})());
    assertEquals(3, goog.bindNative_(add, null, 1, 2)());

    assertThrows(function() {
      goog.bindNative_(null, null);
    });
  }
}

function testBindDefault() {
  assertEquals(
      1, goog.bind(add, {valueOf: function() { return 1; }})());
  assertEquals(3, goog.bind(add, null, 1, 2)());
}

function add(var_args) {
  var sum = Number(this) || 0;
  for (var i = 0; i < arguments.length; i++) {
    sum += arguments[i];
  }
  return sum;
}

function testPartial() {
  var f = function(x, y) {
    return x + y;
  };
  var g = goog.partial(f, 1);
  assertEquals(3, g(2));

  var h = goog.partial(f, 1, 2);
  assertEquals(3, h());

  var i = goog.partial(f);
  assertEquals(3, i(1, 2));
}

function testPartialUsesGlobal() {
  var f = function(x, y) {
    assertEquals(goog.global, this);
    return x + y;
  };
  var g = goog.partial(f, 1);
  var h = goog.partial(g, 2);
  assertEquals(3, h());
}

function testPartialWithCall() {
  var obj = {};
  var f = function(x, y) {
    assertEquals(obj, this);
    return x + y;
  };
  var g = goog.partial(f, 1);
  var h = goog.partial(g, 2);
  assertEquals(3, h.call(obj));
}

function testPartialAndBind() {
  // This ensures that this "survives" through a partial.
  var p = goog.partial(getFoo, 'hot');
  var b = goog.bind(p, obj, 'dog');

  var res = b();
  assertEquals(obj.foo, res.foo);
  assertEquals('hot', res.arg1);
  assertEquals('dog', res.arg2);
}

function testBindAndPartial() {
  // This ensures that this "survives" through a partial.
  var b = goog.bind(getFoo, obj, 'hot');
  var p = goog.partial(b, 'dog');

  var res = p();
  assertEquals(obj.foo, res.foo);
  assertEquals('hot', res.arg1);
  assertEquals('dog', res.arg2);
}

function testGlobalEval() {
  goog.globalEval('var foofoofoo = 125;')
  assertEquals('Var should be globally assigned', 125, goog.global.foofoofoo);
  var foofoofoo = 128;
  assertEquals('Global should not have changed', 125, goog.global.foofoofoo);

  // NOTE(user): foofoofoo would normally be available in the function scope,
  // via the scope chain, but the JsUnit framework seems to do something weird
  // which makes it not work.
}

function testGlobalEvalWithHtml() {
  // Make sure we don't trip on HTML markup in the code
  goog.global.evalTestResult = 'failed';
  goog.global.evalTest = function(arg) {
    goog.global.evalTestResult = arg;
  };

  goog.globalEval('evalTest("<test>")');

  assertEquals('Should be able to evaluate strings with HTML in',
      '<test>', goog.global.evalTestResult);
}


//=== tests for inherits ===

function testInherits() {
  function Foo() {};
  function Bar() {};
  goog.inherits(Bar, Foo);
  var bar = new Bar();

  assert('object should be instance of constructor',
      bar instanceof Bar);
  assert('object should be instance of base constructor',
      bar instanceof Foo) ;
}

function testInherits_constructor() {
  function Foo() {};
  function Bar() {};
  goog.inherits(Bar, Foo);
  var bar = new Bar();

  assertEquals('constructor property should match constructor function',
      Bar, bar.constructor);
  assertEquals('Superclass constructor should match constructor function',
      Foo, Bar.superClass_.constructor);
}


//=== tests for makeSingleton ===
function testMakeSingleton() {
  function Foo() {};
  goog.addSingletonGetter(Foo);

  assertNotNull('Should add get instance function', Foo.getInstance);

  var x = Foo.getInstance();
  assertNotNull('Should successfully create an object', x);

  var y = Foo.getInstance();
  assertEquals('Should return the same object', x, y);

  delete Foo.instance_;

  var z = Foo.getInstance();
  assertNotNull('Should work after clearing for testing', z);

  assertNotEquals('Should return a different object after clearing for testing',
      x, z);
}


//=== tests for now ===

function testNow() {
  var toleranceMilliseconds = 10;
  var now1 = new Date().getTime();
  var now2 = goog.now();
  assertTrue(Math.abs(now1 - now2) < toleranceMilliseconds);
}


//=== test non-html context ===

function testInHtmlDocument() {
  var savedGoogGlobal = goog.global;
  try {
    goog.global = {};
    assertFalse(goog.inHtmlDocument_());
    goog.global.document = {};
    assertFalse(goog.inHtmlDocument_());
    goog.global.document.write = function() {};
    assertTrue(goog.inHtmlDocument_());
  } finally {
    // Restore context to respect other tests.
    goog.global = savedGoogGlobal;
  }
}

function testLoadInNonHtmlNotThrows() {
  var savedGoogGlobal = goog.global;
  try {
    goog.global = {};
    goog.global.document = {};
    assertFalse(goog.inHtmlDocument_());
    // The goog code which is executed at load.
    goog.findBasePath_();
    goog.writeScriptTag_(goog.basePath + 'deps.js');
  } finally {
    // Restore context to respect other tests.
    goog.global = savedGoogGlobal;
  }
}

function testLoadBaseWithQueryParamOk() {
  var savedGoogGlobal = goog.global;
  try {
    goog.global = {};
    goog.global.document = {
      write: goog.nullFunction,
      getElementsByTagName: goog.functions.constant(
          [{src: '/path/to/base.js?zx=5'}])
    };
    assertTrue(goog.inHtmlDocument_());
    goog.findBasePath_();
    assertEquals('/path/to/', goog.basePath);
  } finally {
    // Restore context to respect other tests.
    goog.global = savedGoogGlobal;
  }
}

//=== tests for getmsg ===
function testGetMsgWithDollarSigns() {
  var msg = goog.getMsg('{$amount} per minute',
      {amount: '$0.15'});
  assertEquals('$0.15 per minute', msg);
  msg = goog.getMsg('{$amount} per minute',
      {amount: '$0.$1$5'});
  assertEquals('$0.$1$5 per minute', msg);

  msg = goog.getMsg('This is a {$rate} sale!',
      {rate: '$$$$$$$$$$10'});
  assertEquals('This is a $$$$$$$$$$10 sale!', msg);
  msg = goog.getMsg('{$name}! Hamburgers: {$hCost}, Hotdogs: {$dCost}.',
      {name: 'Burger Bob',
       hCost: '$0.50',
       dCost: '$100'});
  assertEquals('Burger Bob! Hamburgers: $0.50, Hotdogs: $100.',
    msg);
}


//=== miscellaneous tests ===

function testIdentity() {
  assertEquals(3, goog.identityFunction(3));
  assertEquals(3, goog.identityFunction(3, 4));
  assertEquals(null, goog.identityFunction(null));
  assertTrue(goog.identityFunction() === undefined);
}

function testGetObjectByName() {
  var m = {
    'undefined': undefined,
    'null': null,
    emptyString: '',
    'false': false,
    'true': true,
    zero: 0,
    one: 1,
    two: {
      three: 3,
      four: {
        five: 5
      }
    },
    'six|seven': '6|7',
    'eight.nine': 8.9
  };
  goog.global.m = m;

  assertNull(goog.getObjectByName('m.undefined'));
  assertNull(goog.getObjectByName('m.null'));
  assertEquals(goog.getObjectByName('m.emptyString'), '');
  assertEquals(goog.getObjectByName('m.false'), false);
  assertEquals(goog.getObjectByName('m.true'), true);
  assertEquals(goog.getObjectByName('m.zero'), 0);
  assertEquals(goog.getObjectByName('m.one'), 1);
  assertEquals(goog.getObjectByName('m.two.three'), 3);
  assertEquals(goog.getObjectByName('m.two.four.five'), 5);
  assertEquals(goog.getObjectByName('m.six|seven'), '6|7');
  assertNull(goog.getObjectByName('m.eight.nine'));
  assertNull(goog.getObjectByName('m.notThere'));

  assertEquals(goog.getObjectByName('one', m), 1);
  assertEquals(goog.getObjectByName('two.three', m), 3);
  assertEquals(goog.getObjectByName('two.four.five', m), 5);
  assertEquals(goog.getObjectByName('six|seven', m), '6|7');
  assertNull(goog.getObjectByName('eight.nine', m));
  assertNull(goog.getObjectByName('notThere', m));
}


function testGetCssName() {
  assertEquals('classname', goog.getCssName('classname'));
  assertEquals('random-classname', goog.getCssName('random-classname'));
  assertEquals('control-modifier', goog.getCssName('control', 'modifier'));

  goog.setCssNameMapping({
    'goog': 'a',
    'disabled': 'b'
  }, 'BY_PART');
  var g = goog.getCssName('goog');
  assertEquals('a', g);
  assertEquals('a-b', goog.getCssName(g, 'disabled'));
  assertEquals('a-b', goog.getCssName('goog-disabled'));
  assertEquals('a-button', goog.getCssName('goog-button'));

  goog.setCssNameMapping({
    'goog-button': 'a',
    'active': 'b'
  }, 'BY_WHOLE');

  g = goog.getCssName('goog-button');
  assertEquals('a', g);
  assertEquals('a-b', goog.getCssName(g, 'active'));
  assertEquals('goog-disabled', goog.getCssName('goog-disabled'));
}

function testAddDependency() {
  stubs.set(goog, 'writeScriptTag_', goog.nullFunction);

  goog.addDependency(
      'foo.js',
      ['testDep.foo'],
      ['testDep.bar']);

  goog.provide('testDep.bar');

  // To differentiate this call from the real one.
  var require = goog.require;

  // this used to throw an exception
  require('testDep.foo');

  assertTrue(goog.isObject(testDep.bar));

  // Unset provided namespace so the test can be re-run.
  testDep = undefined;
}

function testBaseMethod() {
  function A() {}
  A.prototype.foo = function(x, y) {
    return x + y;
  };

  function B() {}
  goog.inherits(B, A);
  B.prototype.foo = function(x, y) {
    return 2 + goog.base(this, 'foo', x, y);
  };

  function C() {}
  goog.inherits(C, B);
  C.prototype.foo = function(x, y) {
    return 4 + goog.base(this, 'foo', x, y);
  };

  var d = new C();
  d.foo = function(x, y) {
    return 8 + goog.base(this, 'foo', x, y);
  };

  assertEquals(15, d.foo(1, 0));
  assertEquals(16, d.foo(1, 1));
  assertEquals(16, d.foo(2, 0));
  assertEquals(7, (new C()).foo(1, 0));
  assertEquals(3, (new B()).foo(1, 0));
  assertThrows(function() {
    goog.base(d, 'foo', 1, 0);
  });

  delete B.prototype.foo;
  assertEquals(13, d.foo(1, 0));

  delete C.prototype.foo;
  assertEquals(9, d.foo(1, 0));
}

function testBaseMethodAndBaseCtor() {
  // This will fail on FF4.0 if the following bug is not fixed:
  // https://bugzilla.mozilla.org/show_bug.cgi?id=586482
  function A(x, y) {
    this.foo(x, y);
  }
  A.prototype.foo = function(x, y) {
    this.bar = x + y;
  };

  function B(x, y) {
    goog.base(this, x, y);
  }
  goog.inherits(B, A);
  B.prototype.foo = function(x, y) {
    goog.base(this, 'foo', x, y);
    this.bar = this.bar * 2;
  };

  assertEquals(14, new B(3, 4).bar);
}

function testBaseClass() {
  function A(x, y) {
    this.foo = x + y;
  }

  function B(x, y) {
    goog.base(this, x, y);
    this.foo += 2;
  }
  goog.inherits(B, A);

  function C(x, y) {
    goog.base(this, x, y);
    this.foo += 4;
  }
  goog.inherits(C, B);

  function D(x, y) {
    goog.base(this, x, y);
    this.foo += 8;
  }
  goog.inherits(D, C);

  assertEquals(15, (new D(1, 0)).foo);
  assertEquals(16, (new D(1, 1)).foo);
  assertEquals(16, (new D(2, 0)).foo);
  assertEquals(7, (new C(1, 0)).foo);
  assertEquals(3, (new B(1, 0)).foo);
}

function testGoogRequireCheck() {
  stubs.set(goog, 'ENABLE_DEBUG_LOADER', false);
  stubs.set(goog, 'useStrictRequires', true);
  stubs.set(goog, 'implicitNamespaces_', {});

  // Aliased so that build tools do not mistake this for an actual call.
  var require = goog.require;
  assertThrows('Requiring non-required namespace should fail',
    function() {
      require('far.out');
    });

  assertUndefined(goog.global.far );
  assertEvaluatesToFalse(goog.getObjectByName('far.out'));
  assertObjectEquals({}, goog.implicitNamespaces_);
  assertFalse(goog.isProvided_('far.out'));

  goog.provide('far.out');

  assertNotUndefined(far.out);
  assertEvaluatesToTrue(goog.getObjectByName('far.out'));
  assertObjectEquals({'far': true}, goog.implicitNamespaces_);
  assertTrue(goog.isProvided_('far.out'));

  stubs.reset();
  delete far;
}

</script>
</body>
</html>