]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | describe("Ext.util.DelayedTask", function() {\r |
2 | \r | |
3 | it('should delay the call', function() {\r | |
4 | var d,\r | |
5 | called = false;\r | |
6 | \r | |
7 | d = new Ext.util.DelayedTask(function() {\r | |
8 | called = true;\r | |
9 | });\r | |
10 | d.delay(100);\r | |
11 | \r | |
12 | // Immediately after the delay call, nothing has been executed\r | |
13 | expect(called).toBe(false);\r | |
14 | \r | |
15 | waits(150);\r | |
16 | runs(function() {\r | |
17 | // At 150ms, the 100ms delay should have been executed\r | |
18 | expect(called).toBe(true);\r | |
19 | });\r | |
20 | });\r | |
21 | \r | |
22 | it('should cancel any previous invocations', function() {\r | |
23 | var d,\r | |
24 | counter = 0,\r | |
25 | lastValue = 0,\r | |
26 | incr = function(value) {\r | |
27 | counter++;\r | |
28 | lastValue = value;\r | |
29 | };\r | |
30 | \r | |
31 | d = new Ext.util.DelayedTask(incr);\r | |
32 | d.delay(500, null, null, [1]);\r | |
33 | d.delay(1500, null, null, [2]);\r | |
34 | d.delay(2500, null, null, [3]);\r | |
35 | waits(1000);\r | |
36 | runs(function() {\r | |
37 | // At 1000 ms, no call should have been made. The 500ms delay should have been superceded\r | |
38 | expect(counter).toBe(0);\r | |
39 | expect(lastValue).toBe(0);\r | |
40 | waits(1000);\r | |
41 | runs(function() {\r | |
42 | // At 2000 ms, no call should have been made. The 1500ms delay should have been superceded\r | |
43 | expect(counter).toBe(0);\r | |
44 | expect(lastValue).toBe(0);\r | |
45 | waits(1000);\r | |
46 | runs(function() {\r | |
47 | // At 3000 ms, one call should have been made. The 2500ms delay with a value of 3 should win\r | |
48 | expect(counter).toBe(1);\r | |
49 | expect(lastValue).toBe(3);\r | |
50 | });\r | |
51 | });\r | |
52 | });\r | |
53 | });\r | |
54 | \r | |
55 | it('should not cancel any previous invocations', function() {\r | |
56 | var d,\r | |
57 | counter = 0,\r | |
58 | lastValue,\r | |
59 | incr = function(value) {\r | |
60 | counter++;\r | |
61 | lastValue = value;\r | |
62 | };\r | |
63 | \r | |
64 | d = new Ext.util.DelayedTask(incr, null, null, false);\r | |
65 | d.delay(100, null, null, [1]);\r | |
66 | d.delay(150, null, null, [2]);\r | |
67 | d.delay(300, null, null, [3]);\r | |
68 | waits(150);\r | |
69 | runs(function() {\r | |
70 | // At 150 ms, the arguments from the last (300ms) delay call should have been used, but\r | |
71 | // the initial invocation delay of 100ms should have been allowed to execute because\r | |
72 | // of specifying cancelOnDelay as false.\r | |
73 | expect(counter).toBe(1);\r | |
74 | expect(lastValue).toBe(3);\r | |
75 | waits(100);\r | |
76 | runs(function() {\r | |
77 | // At 250 ms, no further calls should have been made.\r | |
78 | expect(counter).toBe(1);\r | |
79 | expect(lastValue).toBe(3);\r | |
80 | waits(100);\r | |
81 | runs(function() {\r | |
82 | // At 350 ms, no further calls should have been made.\r | |
83 | expect(counter).toBe(1);\r | |
84 | expect(lastValue).toBe(3);\r | |
85 | });\r | |
86 | });\r | |
87 | });\r | |
88 | });\r | |
89 | \r | |
90 | it("should accept a delay of 0", function(){\r | |
91 | var called = true;\r | |
92 | var d = new Ext.util.DelayedTask(function() {\r | |
93 | called = true; \r | |
94 | });\r | |
95 | d.delay(1000);\r | |
96 | d.delay(0);\r | |
97 | waits(50);\r | |
98 | runs(function(){\r | |
99 | // should fire almost straight away, will still be on a callback though\r | |
100 | // so we need at least some kind of delay\r | |
101 | expect(called).toBe(true);\r | |
102 | });\r | |
103 | })\r | |
104 | }); |