test_servicepanehelper.js
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
29 Components.utils.import("resource://app/jsmodules/ServicePaneHelper.jsm");
30 
31 function DBG(s) { log('DBG:test_servicepanehelper: '+s); }
32 
33 function testBadges(SPS, aRoot) {
34  function getBadgeIDs() {
35  return [badge.id for (badge in ServicePaneHelper.getAllBadges(aRoot))];
36  }
37 
38  assertArraysEqual(getBadgeIDs(), []);
39 
40  let badge = ServicePaneHelper.getBadge(aRoot, "testBadge");
41  assertTrue(badge, "Getting a non-existent badge doesn't create it");
42  assertEqual(badge.id, "testBadge", "getBadge didn't initialize badge ID");
43  assertEqual(badge.node, aRoot, "getBadge didn't initialize badge node");
44  assertFalse(badge.visible, "Badge isn't invisible by default");
45 
46  assertArraysEqual(getBadgeIDs(), []);
47 
48  badge.label = "label";
49  badge.image = "chrome://example/skin/image.png";
50 
51  assertEqual(badge.label, "label", "Setting badge label didn't work");
52  assertEqual(badge.image, "chrome://example/skin/image.png",
53  "Setting badge image didn't work");
54 
55  assertArraysEqual(getBadgeIDs(), []);
56 
57  badge = ServicePaneHelper.getBadge(aRoot, "testBadge");
58  assertEqual(badge.label, "label", "Re-getting badge cleared badge label");
59  assertEqual(badge.image, "chrome://example/skin/image.png",
60  "Re-getting badge cleared badge image");
61 
62  assertArraysEqual(getBadgeIDs(), []);
63 
64  badge.append();
65  assertArraysEqual(getBadgeIDs(), ["testBadge"]);
66 
67  assertEqual(badge.id, "testBadge", "getBadge didn't initialize badge ID");
68  assertEqual(badge.node, aRoot, "getBadge didn't initialize badge node");
69  assertTrue(badge.visible, "Badge isn't invisible by default");
70  assertEqual(badge.label, "label", "Appending badge cleared badge label");
71  assertEqual(badge.image, "chrome://example/skin/image.png",
72  "Appending badge cleared badge image");
73 
74  badge = ServicePaneHelper.getBadge(aRoot, "testBadge");
75  assertTrue(badge.visible, "Re-getting badge cleared visibility flag");
76  assertEqual(badge.label, "label", "Re-getting badge cleared badge label");
77  assertEqual(badge.image, "chrome://example/skin/image.png",
78  "Re-getting badge cleared badge image");
79 
80  let badge2 = ServicePaneHelper.getBadge(aRoot);
81  assertTrue(badge2, "Getting badge without ID doesn't create it");
82  assertTrue(badge2.id, "getBadge didn't generate badge ID");
83 
84  assertArraysEqual(getBadgeIDs(), ["testBadge"]);
85 
86  badge2.insertBefore("testBadge");
87  assertArraysEqual(getBadgeIDs(), [badge2.id, "testBadge"]);
88 
89  assertEqual(badge2.label, null, "Badge label isn't null by default");
90  assertEqual(badge2.image, null, "Badge image isn't null by default");
91 
92  badge2.label = "label2";
93  assertEqual(badge2.label, "label2",
94  "Changing label of visible badge didn't persist");
95  assertEqual(badge.label, "label", "Label of unrelated badge was modified");
96 
97  badge.insertBefore(badge2.id);
98  assertArraysEqual(getBadgeIDs(), ["testBadge", badge2.id]);
99 
100  badge.visible = false;
101  assertArraysEqual(getBadgeIDs(), [badge2.id]);
102  assertFalse(badge.visible, "Badge is still visible after hiding");
103  assertEqual(badge.label, "label", "Hiding badge cleared badge label");
104  assertEqual(badge.image, "chrome://example/skin/image.png",
105  "Hiding badge cleared badge image");
106 
107  badge.remove();
108  assertArraysEqual(getBadgeIDs(), [badge2.id]);
109  assertEqual(badge.label, null, "Removing badge didn't clear badge label");
110  assertEqual(badge.image, null, "Removing badge didn't clear badge image");
111 
112  badge.remove();
113  assertArraysEqual(getBadgeIDs(), [badge2.id]);
114 
115  badge2.remove();
116  assertArraysEqual(getBadgeIDs(), []);
117  assertFalse(badge2.visible, "Badge is still visible after removing");
118 
119  // Root node should still have no attributes after the test
120  assertEqual(serializeTree(aRoot), '[node/]');
121 }
122 
123 function runTest() {
124  let SPS = Cc["@songbirdnest.com/servicepane/service;1"]
125  .getService(Ci.sbIServicePaneService);
126 
127  // okay, did we get it?
128  assertNotEqual(SPS, null, "Failed to get service pane service");
129 
130  // Can we create test node?
131  let testRoot = SPS.createNode();
132  assertTrue(testRoot instanceof Ci.sbIServicePaneNode,
133  "created node is not a service pane node");
134 
135  SPS.root.appendChild(testRoot);
136 
137  // Test badges
138  testBadges(SPS, testRoot);
139 
140  // Clean up
141  SPS.root.removeChild(testRoot);
142 }
const Cc
function assertNotEqual(aExpected, aActual, aMessage)
function log(s)
function assertTrue(aTest, aMessage)
function assertEqual(aExpected, aActual, aMessage)
function assertArraysEqual(a1, a2)
function DBG(s)
return null
Definition: FeedWriter.js:1143
function assertFalse(aTest, aMessage)
const Ci
function serializeTree(aNode)
Common service pane helper functions.
function runTest()
Advanced DataRemote unit tests.
function testBadges(SPS, aRoot)
var ServicePaneHelper