tests: fix Search calls in tests
[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 uint child_count = 10;
45     public async MediaObjects? get_children (
46                                             uint offset,
47                                             uint max_count,
48                                             string sort_criteria,
49                                             Cancellable? cancellable)
50                                             throws Error {
51         Idle.add ( () => { get_children.callback (); return false; });
52         yield;
53
54         var result = new MediaObjects ();
55         for (int i = 0; i < 10; ++i) {
56             result.add (new MediaObject ());
57         }
58
59         return result;
60     }
61 }
62
63 public class TestContainer : MediaContainer, Rygel.SearchableContainer {
64     public MainLoop loop;
65     public Gee.ArrayList<string> search_classes { get; set; default = new
66         Gee.ArrayList<string> ();}
67
68     public async void test_search_no_limit () {
69         uint total_matches;
70
71         // check corners
72         var result = yield search (null, 0, 0, out total_matches, "", null);
73         assert (total_matches == 10);
74         assert (result.size == 10);
75
76         result = yield search (null, 10, 0, out total_matches, "",  null);
77         assert (total_matches == 10);
78         assert (result.size == 0);
79
80         for (int i = 1; i < 10; ++i) {
81             result = yield search (null, i, 0, out total_matches, "", null);
82             assert (total_matches == 10);
83             assert (result.size == 10 - i);
84         }
85
86         this.loop.quit ();
87     }
88
89     public async void test_search_with_limit () {
90         uint total_matches;
91
92         // check corners
93         var result = yield search (null, 0, 4, out total_matches, "", null);
94         assert (total_matches == 0);
95         assert (result.size == 4);
96
97         result = yield search (null, 10, 4, out total_matches, "", null);
98         assert (total_matches == 0);
99         assert (result.size == 0);
100
101         for (int i = 1; i < 10; ++i) {
102             result = yield search (null, i, 3, out total_matches, "", null);
103             assert (total_matches == 0);
104             assert (result.size == int.min (10 - i, 3));
105         }
106
107         this.loop.quit ();
108     }
109
110
111     public async MediaObjects? search (SearchExpression? expression,
112                                        uint              offset,
113                                        uint              max_count,
114                                        out uint          total_matches,
115                                        string            sort_criteria,
116                                        Cancellable?      cancellable)
117                                        throws Error {
118         return yield this.simple_search (expression,
119                                          offset,
120                                          max_count,
121                                          out total_matches,
122                                          sort_criteria,
123                                          cancellable);
124     }
125
126 }
127
128 public class MediaObjects : Gee.ArrayList<MediaObject> {
129     public override Gee.List<MediaObject>? slice (int start, int stop) {
130         var slice = base.slice (start, stop);
131         var ret = new MediaObjects ();
132
133         ret.add_all (slice);
134
135         return ret;
136     }
137 }
138
139 int main ()
140 {
141     var loop = new MainLoop ();
142     var c = new TestContainer ();
143     c.loop = new MainLoop ();
144     c.test_search_no_limit.begin ();
145     c.loop.run ();
146     c.test_search_with_limit.begin ();
147     c.loop.run ();
148
149     return 0;
150 }