renderer-gst: Deprecate element wrapping
[profile/ivi/rygel.git] / tests / rygel-searchable-container-test.vala
1 /*
2  * Copyright (C) 2012 Jens Georg.
3  *
4  * Author: Jens Georg <mail@jensge.org>
5  *
6  * This file is part of Rygel.
7  *
8  * Rygel is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Rygel is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22
23 public class RelationalExpression : SearchExpression {
24 }
25
26 namespace SearchCriteriaOp {
27     public const string EQ = "=";
28 }
29
30 public class SearchExpression : Object {
31     public string operand1;
32     public string operand2;
33     public string op;
34
35     public bool satisfied_by (MediaObject object) {
36         return true;
37     }
38 }
39
40 public class MediaObject : Object {
41 }
42
43 public class MediaContainer : MediaObject {
44     public string sort_criteria = "+dc:title";
45     public int child_count = 10;
46     public bool create_mode_enabled = false;
47     public int all_child_count {
48         get { return this.child_count; }
49     }
50     public async MediaObjects? get_children (
51                                             uint offset,
52                                             uint max_count,
53                                             string sort_criteria,
54                                             Cancellable? cancellable)
55                                             throws Error {
56         Idle.add ( () => { get_children.callback (); return false; });
57         yield;
58
59         var result = new MediaObjects ();
60         for (int i = 0; i < 10; ++i) {
61             result.add (new MediaObject ());
62         }
63
64         return result;
65     }
66
67     internal void check_search_expression (SearchExpression? expression) {}
68 }
69
70 public class TestContainer : MediaContainer, Rygel.SearchableContainer {
71     public MainLoop loop;
72     public Gee.ArrayList<string> search_classes { get; set; default = new
73         Gee.ArrayList<string> ();}
74
75     public async void test_search_no_limit () {
76         uint total_matches = 0;
77
78         // check corners
79         try {
80             var result = yield search (null, 0, 0, out total_matches, "", null);
81             assert (total_matches == 10);
82             assert (result.size == 10);
83         } catch (GLib.Error error) {
84             assert_not_reached ();
85         }
86
87         try {
88             var result = yield search (null, 10, 0, out total_matches, "",  null);
89             assert (total_matches == 10);
90             assert (result.size == 0);
91         } catch (GLib.Error error) {
92             assert_not_reached ();
93         }
94
95         for (int i = 1; i < 10; ++i) {
96             try {
97                 var result = yield search (null, i, 0, out total_matches, "", null);
98                 assert (total_matches == 10);
99                 assert (result.size == 10 - i);
100             } catch (GLib.Error error) {
101                 assert_not_reached ();
102             }
103         }
104
105         this.loop.quit ();
106     }
107
108     public async void test_search_with_limit () {
109         uint total_matches;
110
111         // check corners
112         try {
113             var result = yield search (null, 0, 4, out total_matches, "", null);
114             assert (total_matches == 0);
115             assert (result.size == 4);
116         } catch (GLib.Error error) {
117             assert_not_reached ();
118         }
119
120         try
121         {
122             var result = yield search (null, 10, 4, out total_matches, "", null);
123             assert (total_matches == 0);
124             assert (result.size == 0);
125         } catch (GLib.Error error) {
126             assert_not_reached ();
127         }
128
129         for (int i = 1; i < 10; ++i) {
130             try {
131                 var result = yield search (null, i, 3, out total_matches, "", null);
132                 assert (total_matches == 0);
133                 assert (result.size == int.min (10 - i, 3));
134             } catch (GLib.Error error) {
135                 assert_not_reached ();
136             }
137         }
138
139         this.loop.quit ();
140     }
141
142     /* TODO: This is just here to avoid a warning about
143      * serialize_search_parameters() not being used.
144      * How should this really be tested?
145      */
146     public void test_serialization() {
147          var writer = new GUPnP.DIDLLiteWriter(null);
148          var didl_container = writer.add_container();
149          serialize_search_parameters(didl_container);
150     }
151
152     public async MediaObjects? search (SearchExpression? expression,
153                                        uint              offset,
154                                        uint              max_count,
155                                        out uint          total_matches,
156                                        string            sort_criteria,
157                                        Cancellable?      cancellable)
158                                        throws Error {
159         return yield this.simple_search (expression,
160                                          offset,
161                                          max_count,
162                                          out total_matches,
163                                          sort_criteria ?? this.sort_criteria,
164                                          cancellable);
165     }
166
167 }
168
169 public class MediaObjects : Gee.ArrayList<MediaObject> {
170     public override Gee.List<MediaObject>? slice (int start, int stop) {
171         var slice = base.slice (start, stop);
172         var ret = new MediaObjects ();
173
174         ret.add_all (slice);
175
176         return ret;
177     }
178 }
179
180 int main ()
181 {
182     var c = new TestContainer ();
183     c.loop = new MainLoop ();
184     c.test_search_no_limit.begin ();
185     c.loop.run ();
186     c.test_search_with_limit.begin ();
187     c.loop.run ();
188
189     return 0;
190 }