test_medialistviewclone.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 library = createLibrary("test_medialistviewclone", null, false);
37  library.clear();
38 
39  var view = library.createView();
40 
41  var cfs = view.cascadeFilterSet;
42 
43  view.setSort(SBProperties.createArray([
44  [SBProperties.artistName, "a"]
45  ]));
46  view.filterConstraint = LibraryUtils.createConstraint([
47  [
48  [SBProperties.isList, ["0"]]
49  ],
50  [
51  [SBProperties.hidden, ["0"]]
52  ]
53  ]);
54 
55  cfs.appendSearch([
56  SBProperties.artistName,
57  SBProperties.albumName,
58  SBProperties.trackName
59  ], 3);
60  cfs.appendFilter(SBProperties.genre);
61  cfs.appendFilter(SBProperties.artistName);
62  cfs.appendFilter(SBProperties.albumName);
63 
64  cfs.set(0, ["Beat"], 1);
65  cfs.set(1, ["ROCK"], 1);
66  cfs.set(2, ["The Beatles"], 1);
67 
68  var clone = view.clone();
69 
70  assertPropertyArray(clone.currentSort, SBProperties.createArray([
71  [SBProperties.artistName, "a"]
72  ]));
73 
74  assertTrue(clone.searchConstraint.equals(LibraryUtils.createConstraint([
75  [
76  [SBProperties.artistName, ["Beat"]],
77  [SBProperties.albumName, ["Beat"]],
78  [SBProperties.trackName, ["Beat"]]
79  ]
80  ])));
81 
82  assertTrue(clone.filterConstraint.equals(LibraryUtils.createConstraint([
83  [
84  [SBProperties.isList, ["0"]]
85  ],
86  [
87  [SBProperties.hidden, ["0"]]
88  ],
89  [
90  [SBProperties.genre, ["ROCK"]]
91  ],
92  [
93  [SBProperties.artistName, ["The Beatles"]]
94  ]
95  ])));
96 
97  var cloneCfs = clone.cascadeFilterSet;
98  assertEqual(cfs.length, cloneCfs.length);
99  for (let i = 0; i < cfs.length; i++) {
100  assertEqual(cfs.getProperty(i), cloneCfs.getProperty(i));
101  assertEqual(cfs.isSearch(i), cloneCfs.isSearch(i));
102  }
103 
104  var state = view.getState();
105  var pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe);
106  pipe.init(false, false, 0, 0xffffffff, null);
107 
108  var oos = Cc["@mozilla.org/binaryoutputstream;1"]
109  .createInstance(Ci.nsIObjectOutputStream);
110  oos.setOutputStream(pipe.outputStream);
111  oos.writeObject(state, true);
112  oos.close();
113 
114  var ois = Cc["@mozilla.org/binaryinputstream;1"]
115  .createInstance(Ci.nsIObjectInputStream);
116  ois.setInputStream(pipe.inputStream);
117  var newState = ois.readObject(true)
118  ois.close();
119 
120  var newView = library.createView(newState);
121 
122  assertPropertyArray(newView.currentSort, SBProperties.createArray([
123  [SBProperties.artistName, "a"]
124  ]));
125  assertTrue(newView.searchConstraint.equals(LibraryUtils.createConstraint([
126  [
127  [SBProperties.artistName, ["Beat"]],
128  [SBProperties.albumName, ["Beat"]],
129  [SBProperties.trackName, ["Beat"]]
130  ]
131  ])));
132 
133  assertTrue(newView.filterConstraint.equals(LibraryUtils.createConstraint([
134  [
135  [SBProperties.isList, ["0"]]
136  ],
137  [
138  [SBProperties.hidden, ["0"]]
139  ],
140  [
141  [SBProperties.genre, ["ROCK"]]
142  ],
143  [
144  [SBProperties.artistName, ["The Beatles"]]
145  ]
146  ])));
147 
148  var newCfs = newView.cascadeFilterSet;
149  assertEqual(cfs.length, newCfs.length);
150  for (let i = 0; i < cfs.length; i++) {
151  assertEqual(cfs.getProperty(i), newCfs.getProperty(i));
152  assertEqual(cfs.isSearch(i), newCfs.isSearch(i));
153  }
154 }
155 
156 function assertPropertyArray(a1, a2) {
157  if (a1.length != a2.length) {
158  fail("Property array lengths differ, " + a1 +
159  " != " + a2);
160  }
161 
162  for(var i = 0; i < a1.length; i++) {
163  var prop1 = a1.getPropertyAt(i);
164  var prop2 = a2.getPropertyAt(i);
165  if (prop1.id != prop2.id) {
166  fail("Names are different at index " + i + ", " +
167  prop1 + " != " + prop2);
168  }
169  if (prop1.value != prop2.value) {
170  fail("Values are different at index " + i + ", " +
171  prop1 + " != " + prop2);
172  }
173  }
174 
175 }
176 
const Cc
function fail(aMessage)
function assertPropertyArray(a1, a2)
function assertTrue(aTest, aMessage)
function assertEqual(aExpected, aActual, aMessage)
function runTest()
Test file.
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
const Ci
Javascript wrappers for common library tasks.
_getSelectedPageStyle s i