test_constraints.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-2008 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 
31 function runTest () {
32 
33  Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
34  Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
35 
36  var values;
37  var outValues;
38  var outValuesCount = {};
39 
40  var builder = Cc["@songbirdnest.com/Songbird/Library/ConstraintBuilder;1"]
41  .createInstance(Ci.sbILibraryConstraintBuilder);
42 
43  try {
44  builder.get();
45  fail("did not throw");
46  }
47  catch(e) {
48  assertEqual(e.result, Cr.NS_ERROR_UNEXPECTED);
49  }
50 
51  try {
52  builder.intersect();
53  fail("did not throw");
54  }
55  catch(e) {
56  assertEqual(e.result, Cr.NS_ERROR_UNEXPECTED);
57  }
58 
59  try {
60  builder.include(SBProperties.artistName, "The Beatles")
61  .intersect()
62  .get();
63  fail("did not throw");
64  }
65  catch(e) {
66  assertEqual(e.result, Cr.NS_ERROR_UNEXPECTED);
67  }
68 
69  builder = Cc["@songbirdnest.com/Songbird/Library/ConstraintBuilder;1"]
70  .createInstance(Ci.sbILibraryConstraintBuilder);
71 
72  var c = builder.include(SBProperties.artistName, "The Beatles").get();
73  assertEqual(c.groupCount, 1);
74  var g = c.getGroup(0);
75  assertEqual(g.properties.getNext(), SBProperties.artistName);
76  assertEqual(g.getValues(SBProperties.artistName).getNext(), "The Beatles");
77 
78  c = builder.include(SBProperties.artistName, "The Beatles")
79  .intersect()
80  .include(SBProperties.albumName, "Abbey Road")
81  .include(SBProperties.albumName, "Let It Be").get();
82 
83  assertEqual(c.groupCount, 2);
84  var g = c.getGroup(0);
85  assertEqual(g.properties.getNext(), SBProperties.artistName);
86  assertEqual(g.getValues(SBProperties.artistName).getNext(), "The Beatles");
87  g = c.getGroup(1);
88  assertEqual(g.properties.getNext(), SBProperties.albumName);
89  var values = g.getValues(SBProperties.albumName);
90  assertEqual(values.getNext(), "Abbey Road");
91  assertEqual(values.getNext(), "Let It Be");
92 
93  // Test equality
94  var c1 = LibraryUtils.createConstraint([
95  [
96  [SBProperties.artistName, ["hello"]],
97  [SBProperties.artistName, ["world"]]
98  ]
99  ]);
100 
101  var c2 = LibraryUtils.createConstraint([
102  [
103  [SBProperties.artistName, ["world", "hello"]]
104  ]
105  ]);
106  assertTrue(c1.equals(c2));
107 
108  c1 = LibraryUtils.createConstraint([
109  [
110  [SBProperties.artistName, ["hello"]]
111  ],
112  [
113  [SBProperties.albumName, ["world"]],
114  [SBProperties.trackName, ["trackName"]],
115  [SBProperties.genre, ["rock", "pop", "jazz"]]
116  ]
117  ]);
118 
119  c2 = LibraryUtils.createConstraint([
120  [
121  [SBProperties.genre, ["rock", "pop", "jazz"]],
122  [SBProperties.albumName, ["world"]],
123  [SBProperties.trackName, ["trackName"]]
124  ],
125  [
126  [SBProperties.artistName, ["hello"]],
127  ]
128  ]);
129  assertTrue(c1.equals(c2));
130 
131  c2 = LibraryUtils.createConstraint([
132  [
133  [SBProperties.genre, ["rock", "pop", "jazz"]],
134  [SBProperties.trackName, ["trackName"]]
135  ],
136  [
137  [SBProperties.artistName, ["hello"]],
138  ]
139  ]);
140  assertFalse(c1.equals(c2));
141 
142  var newC2 = writeAndRead(c2);
143  assertTrue(c2.equals(newC2));
144 
145  var sort = Cc["@songbirdnest.com/Songbird/Library/Sort;1"]
146  .createInstance(Ci.sbILibrarySort);
147 
148  try {
149  sort.property;
150  fail("Did not throw");
151  }
152  catch(e) {
153  assertEqual(e.result, Cr.NS_ERROR_UNEXPECTED);
154  }
155 
156  sort.init(SBProperties.artistName, true);
157 
158  try {
159  sort.init(SBProperties.artistName, true);
160  fail("Did not throw");
161  }
162  catch(e) {
163  assertEqual(e.result, Cr.NS_ERROR_UNEXPECTED);
164  }
165 
166  assertEqual(sort.property, SBProperties.artistName);
167  assertEqual(sort.isAscending, true);
168 
169  var newSort = writeAndRead(sort);
170  assertEqual(newSort.property, SBProperties.artistName);
171  assertEqual(sort.isAscending, true);
172 
173 }
174 
175 
176 function writeAndRead(o) {
177  var pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe);
178  pipe.init(false, false, 0, 0xffffffff, null);
179 
180  var oos = Cc["@mozilla.org/binaryoutputstream;1"]
181  .createInstance(Ci.nsIObjectOutputStream);
182  oos.setOutputStream(pipe.outputStream);
183  oos.writeObject(o, true);
184  oos.close();
185 
186  var ois = Cc["@mozilla.org/binaryinputstream;1"]
187  .createInstance(Ci.nsIObjectInputStream);
188  ois.setInputStream(pipe.inputStream);
189  var read = ois.readObject(true)
190  ois.close();
191 
192  return read;
193 }
194 
const Cc
function fail(aMessage)
function runTest()
Test file.
function assertTrue(aTest, aMessage)
function assertEqual(aExpected, aActual, aMessage)
function writeAndRead(o)
return null
Definition: FeedWriter.js:1143
function assertFalse(aTest, aMessage)
const Cr
const Ci
Javascript wrappers for common library tasks.