summaryrefslogtreecommitdiff
path: root/test/js/testMainloop.js
blob: 991f3e7e318862f1185560ddefb917d1d3ef4311 (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
const Mainloop = imports.mainloop;

function testTimeout() {
    var trackTimeout = {
        runTenTimes : 0,
        runOnlyOnce: 0,
        neverRun: 0
    };

    Mainloop.timeout_add(10,
                         function() {
                             if (trackTimeout.runTenTimes == 10) {
                                 Mainloop.quit('testtimeout');
                                 return false;
                             }

                             trackTimeout.runTenTimes += 1;
                             return true;
                         });

    Mainloop.timeout_add(10,
                         function () {
                             trackTimeout.runOnlyOnce += 1;
                             return false;
                         });

    Mainloop.timeout_add(15000,
                       function() {
                           trackTimeout.neverRun += 1;
                           return false;
                       });

    Mainloop.run('testtimeout');

    with (trackTimeout) {
        assertEquals("run ten times", 10, runTenTimes);
        assertEquals("run only once", 1, runOnlyOnce);
        assertEquals("never run", 0, neverRun);
    }
}

function testIdle() {
    var trackIdles = {
        runTwiceCount : 0,
        runOnceCount : 0,
        neverRunsCount : 0,
        quitAfterManyRunsCount : 0
    };
    Mainloop.idle_add(function() {
                          trackIdles.runTwiceCount += 1;
                          if (trackIdles.runTwiceCount == 2)
                              return false;
                          else
                              return true;
                      });
    Mainloop.idle_add(function() {
                          trackIdles.runOnceCount += 1;
                          return false;
                      });
    var neverRunsId =
        Mainloop.idle_add(function() {
                              trackIdles.neverRunsCount += 1;
                              return false;
                          });
    Mainloop.idle_add(function() {
                          trackIdles.quitAfterManyRunsCount += 1;
                          if (trackIdles.quitAfterManyRunsCount > 10) {
                              Mainloop.quit('foobar');
                              return false;
                          } else {
                              return true;
                          }
                      });

    Mainloop.source_remove(neverRunsId);

    Mainloop.run('foobar');

    assertEquals("one-shot ran once", 1, trackIdles.runOnceCount);
    assertEquals("two-shot ran twice", 2, trackIdles.runTwiceCount);
    assertEquals("removed never ran", 0, trackIdles.neverRunsCount);
    assertEquals("quit after many ran 11", 11, trackIdles.quitAfterManyRunsCount);

    // check re-entrancy of removing closures while they
    // are being invoked

    trackIdles.removeId = Mainloop.idle_add(function() {
                                                Mainloop.source_remove(trackIdles.removeId);
                                                Mainloop.quit('foobar');
                                                return false;
                                            });
    Mainloop.run('foobar');

    // Add an idle before exit, then never run main loop again.
    // This is to test that we remove idle callbacks when the associated
    // JSContext is blown away. The leak check in gjs-unit will
    // fail if the idle function is not garbage collected.
    Mainloop.idle_add(function() {
                          fail("This should never have been called");
                          return true;
                      });
}

gjstestRun();