test_shutdownservice.js
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2009 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
28 
29 //------------------------------------------------------------------------------
30 
31 //
32 // \brief This test will test the shutdown service by creating three dummy
33 // shutdown tasks and invoking the shutdown service manually.
34 //
35 function runTest()
36 {
37  LOG("Starting the shutdown service unit test");
38 
39  var controller = new sbShutdownTestController();
40  controller.startTest();
41 
42  testPending();
43 }
44 
45 //------------------------------------------------------------------------------
46 // Shutdown test controller class
47 
49 {
50 }
51 
53  _listeners: [],
54 
55  //
56  // \brief Init the test
57  //
58  startTest: function() {
59  this._listeners.push(new sbTestTask("Test Task 1"));
60  this._listeners.push(new sbTestTask("Test Task 2"));
61  this._listeners.push(new sbTestTask("Test Task 3"));
62 
63  // Listen to the shutdown service via |sbIJobProgressListener|.
64  var shutdownService = Cc["@songbirdnest.com/shutdown-service;1"]
65  .getService(Ci.sbIJobProgress);
66  shutdownService.addJobProgressListener(this);
67 
68  // Trick invoke the shutdown service
69  shutdownService.QueryInterface(Ci.nsIObserver);
70  var fakeBool = Cc["@mozilla.org/supports-PRBool;1"]
71  .createInstance(Ci.nsISupportsPRBool);
72  shutdownService.observe(fakeBool,
73  "quit-application-requested",
74  "is-unit-test");
75 
76  // Make sure that the service modified the bool to cancel shutdown
77  assertTrue(fakeBool.data);
78  },
79 
80  // sbIJobProgressListener
81  onJobProgress: function(aJobProgress) {
82  // Abort if the shutdown service failed.
83  assertNotEqual(aJobProgress.status, Ci.sbIJobProgress.STATUS_FAILED);
84 
85  if (aJobProgress.status == Ci.sbIJobProgress.STATUS_SUCCEEDED) {
86  LOG("The shutdown service has successfully completed");
87 
88  // Remove ourselves as a job listener
89  var shutdownService = Cc["@songbirdnest.com/shutdown-service;1"]
90  .getService(Ci.sbIJobProgress);
91  shutdownService.removeJobProgressListener(this);
92 
93  // Cleanup the tasks
94  this._listeners.splice(0);
95 
96  testFinished();
97  }
98  },
99 
100  QueryInterface: XPCOMUtils.generateQI([Ci.sbIJobProgressListener])
101 };
102 
103 //------------------------------------------------------------------------------
104 // Dummy test shutdown task
105 
106 function sbTestTask(aTitleText)
107 {
108  this._titleText = aTitleText;
109 
110  var observerService = Cc["@mozilla.org/observer-service;1"]
111  .getService(Ci.nsIObserverService);
112  observerService.addObserver(this, "songbird-shutdown", false);
113 }
114 
115 sbTestTask.prototype =
116 {
117  _jobListener : null,
118  _timer : null,
119  status : Ci.sbIJobProgress.STATUS_RUNNING,
120  blocked : false,
121  statusText : "Testing Shutdown Job",
122  titleText : "",
123  progress : 0,
124  total : 0,
125 
126  // sbIJobProgress
127  get errorCount() {
128  return 0;
129  },
130 
131  getErrorMessages: function() {
132  return null;
133  },
134 
135  addJobProgressListener: function(aJobListener) {
136  // Don't bother with tracking multiple listeners right now.
137  this._jobListener = aJobListener;
138  },
139 
140  removeJobProgressListener: function(aJobListener) {
141  this._jobListener = null;
142  },
143 
144  // sbIShutdownTask
145  get needsToRunTask() {
146  return true;
147  },
148 
149  startTask: function() {
150  LOG("Starting shutdown task: " + this._titleText);
151 
152  this.status = Ci.sbIJobProgress.STATUS_RUNNING;
153  this._jobListener.onJobProgress(this);
154 
155  this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
156  this._timer.initWithCallback(this, 3000, Ci.nsITimerCallback.TYPE_ONE_SHOT);
157  },
158 
159  // nsIObserver
160  observe: function(aSubject, aTopic, aData) {
161  // Do nothing
162  },
163 
164  // nsITimerCallback
165  notify: function(aTimer) {
166  LOG("Completed shutdown task: " + this._titleText);
167 
168  // Cleanup with the observer service
169  var observerService = Cc["@mozilla.org/observer-service;1"]
170  .getService(Ci.nsIObserverService);
171  observerService.removeObserver(this, "songbird-shutdown");
172 
173  this.status = Ci.sbIJobProgress.STATUS_SUCCEEDED;
174  this._jobListener.onJobProgress(this);
175  },
176 
177  QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
178  Ci.nsITimerCallback,
179  Ci.sbIJobProgress,
180  Ci.sbIShutdownJob])
181 };
182 
183 //------------------------------------------------------------------------------
184 // Logging utility function
185 
186 function LOG(aMessage)
187 {
188  dump("----------------------------------------------------------\n");
189  dump(" " + aMessage + "\n");
190  dump("----------------------------------------------------------\n");
191 }
192 
var total
const Cc
static nsCOMPtr< nsIObserverService > observerService
Definition: UnityProxy.cpp:6
function LOG(aMessage)
function assertNotEqual(aExpected, aActual, aMessage)
function testFinished()
sbOSDControlService prototype QueryInterface
function assertTrue(aTest, aMessage)
function sbTestTask(aTitleText)
function sbShutdownTestController()
sbDeviceFirmwareAutoCheckForUpdate prototype _timer
TimerLoop prototype notify
function runTest()
Advanced DataRemote unit tests.
function startTest()
return null
Definition: FeedWriter.js:1143
const Ci
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe
function testPending()