summaryrefslogtreecommitdiff
path: root/widget/tests/test_bug343416.xul
blob: 1c31bdf543fc7f352bd9b539aae58c7552f9ffb7 (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
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
                 type="text/css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=343416
-->
<window title="Mozilla Bug 343416"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />

<body  xmlns="http://www.w3.org/1999/xhtml">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=343416">Mozilla Bug 343416</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
</pre>
</body>

<script class="testbody" type="application/javascript">
<![CDATA[

/** Test for Bug 343416 **/
SimpleTest.waitForExplicitFinish();

// Observer:
var idleObserver =
{
    QueryInterface: function _qi(iid)
    {
        if (iid.equals(Components.interfaces.nsISupports) ||
            iid.equals(Components.interfaces.nsIObserver))
        {
            return this;
        }
        throw Components.results.NS_ERROR_NO_INTERFACE;
    },
    observe: function _observe(subject, topic, data)
    {
        if (topic != "idle")
            return;
        
        var diff = Math.abs(data - newIdleSeconds * 1000);

//        ok (diff < 5000, "The idle time should have increased by roughly 6 seconds, " +
//                         "as that's when we told this listener to fire.");
//        if (diff >= 5000)
//            alert(data + "  " + newIdleSeconds);

        // Attempt to get to the nsIIdleService        
        var subjectOK = false;
        try {
            var idleService = subject.QueryInterface(nsIIdleService);
            subjectOK = true;
        }
        catch (ex)
        {}
        ok(subjectOK, "The subject of the notification should be the " +
                      "nsIIdleService.");

        // Attempt to remove ourselves.
        var removedObserver = false;
        try {
            idleService.removeIdleObserver(this, newIdleSeconds);
            removedObserver = true;
        }
        catch (ex)
        {}
        ok(removedObserver, "We should be able to remove our observer here.");
        finishedListenerOK = true;
        if (finishedTimeoutOK)
        {
            clearTimeout(testBailout);
            finishThisTest();
        }
    }
};


const nsIIdleService = Components.interfaces.nsIIdleService;
const nsIISCID = "@mozilla.org/widget/idleservice;1";
var idleService = null;
try
{
    idleService = Components.classes[nsIISCID].getService(nsIIdleService);
}
catch (ex)
{}

ok(idleService, "nsIIdleService should exist and be implemented on all tier 1 platforms.");

var idleTime = null;
var gotIdleTime = false;
try
{
    idleTime = idleService.idleTime;
    gotIdleTime = true;
}
catch (ex)
{}

ok (gotIdleTime, "Getting the idle time should not fail " +
                 "in normal circumstances on any tier 1 platform.");

// Now we set up a timeout to sanity-test the idleTime after 5 seconds
setTimeout(testIdleTime, 5000);
var startTimeStamp = Date.now();

// Now we add the listener:
var newIdleSeconds = Math.floor(idleTime / 1000) + 6;
var addedObserver = false;
try
{
    idleService.addIdleObserver(idleObserver, newIdleSeconds);
    addedObserver = true;
}
catch (ex)
{}

ok(addedObserver, "The nsIIdleService should allow us to add an observer.");

addedObserver = false;
try
{
    idleService.addIdleObserver(idleObserver, newIdleSeconds);
    addedObserver = true;
}
catch (ex)
{}

ok(addedObserver, "The nsIIdleService should allow us to add the same observer again.");

var removedObserver = false;
try
{
    idleService.removeIdleObserver(idleObserver, newIdleSeconds);
    removedObserver = true;
}
catch (ex)
{}

ok(removedObserver, "The nsIIdleService should allow us to remove the observer just once.");

function testIdleTime()
{
    var gotIdleTime = false
    try
    {
        var newIdleTime = idleService.idleTime;
    gotIdleTime = true
    }
    catch (ex)
    {}
    ok(gotIdleTime, "Getting the idle time should not fail " +
                    "in normal circumstances on any tier 1 platform.");
    // Get the time difference, remove the approx. 5 seconds that we've waited,
    // should be very close to 0 left.
    var timeDiff = Math.abs((newIdleTime - idleTime) -
                            (Date.now() - startTimeStamp));

    var timePassed = Date.now() - startTimeStamp;
    var idleTimeDiff = newIdleTime - idleTime;
    // 1.5 second leniency.
    ok(timeDiff < 1500, "The idle time should have increased by roughly the " +
                        "amount of time it took for the timeout to fire. " +
                        "You didn't touch the mouse or keyboard during the " +
                        "test did you?");
    finishedTimeoutOK = true;
}

// make sure we still exit when the listener and/or setTimeout don't fire:
var testBailout = setTimeout(finishThisTest, 12000);
var finishedTimeoutOK = false, finishedListenerOK = false;
function finishThisTest()
{
    ok(finishedTimeoutOK, "We set a timeout and it should have fired by now.");
    ok(finishedListenerOK, "We added a listener and it should have been called by now.");
    if (!finishedListenerOK)
    {
        var removedListener = false;
        try
        {
            idleService.removeIdleObserver(idleObserver, newIdleSeconds);
            removedListener = true;
        }
        catch (ex)
        {}
    
        ok(removedListener, "We added a listener and we should be able to remove it.");
    }
    // Done:
    SimpleTest.finish();
}

]]>
</script>

</window>