tests: Updated http-time-seek-test.vala
[profile/ivi/rygel.git] / tests / rygel-http-time-seek-test.vala
1 /*
2  * Copyright (C) 2010 Nokia Corporation.
3  *
4  * Author: Zeeshan Ali (Khattak) <zeeshan.ali@nokia.com>
5  *                               <zeeshanak@gnome.org>
6  *
7  * This file is part of Rygel.
8  *
9  * Rygel is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Rygel is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23
24 using Gst;
25
26 private errordomain Rygel.TestError {
27     SKIP
28 }
29
30 private class Rygel.HTTPTranscodeHandler : GLib.Object {}
31
32 private abstract class Rygel.MediaItem : GLib.Object {
33     public int64 size = -1;
34
35     public bool is_live_stream () {
36         return true;
37     }
38 }
39
40 private class Rygel.AudioItem : MediaItem {
41     public int64 duration = 2048;
42 }
43
44 private class Rygel.Thumbnail : GLib.Object {}
45 private class Rygel.Subtitle : GLib.Object {}
46
47 private class Rygel.HTTPGet : GLib.Object {
48     public const string ITEM_URI = "http://DoesntMatterWhatThisIs";
49
50     public Soup.Message msg;
51     public MediaItem item;
52     public Thumbnail thumbnail;
53     public Subtitle subtitle;
54
55     public HTTPTranscodeHandler handler;
56
57     public HTTPGet (Thumbnail? thumbnail, Subtitle? subtitle) {
58         this.msg = new Soup.Message ("HTTP", ITEM_URI);
59         this.item = new AudioItem ();
60         this.handler = new HTTPTranscodeHandler ();
61         this.thumbnail = thumbnail;
62         this.subtitle = subtitle;
63     }
64
65     public HTTPGet.seek_start (int64      start,
66                                Thumbnail? thumbnail,
67                                Subtitle?  subtitle) {
68         this (thumbnail, subtitle);
69
70         this.add_headers (start, -1);
71     }
72
73     public HTTPGet.seek_stop (int64      stop,
74                               Thumbnail? thumbnail,
75                               Subtitle?  subtitle) {
76         this (thumbnail, subtitle);
77
78         this.add_headers (0, stop);
79     }
80
81     public HTTPGet.seek_start_stop (int64      start,
82                                     int64      stop,
83                                     Thumbnail? thumbnail,
84                                     Subtitle?  subtitle) {
85         this (thumbnail, subtitle);
86
87         this.add_headers (start, stop);
88     }
89
90     public HTTPGet.seek_hhmmss (string     start,
91                                 string     stop,
92                                 Thumbnail? thumbnail,
93                                 Subtitle?  subtitle) {
94         this (thumbnail, subtitle);
95
96         this.add_headers_hhmmss (start, stop);
97     }
98
99     private void add_headers (int64 start, int64 stop) requires (start >= 0) {
100         var stop_str = (stop > 0)? stop.to_string (): "";
101         var range = "npt=" + start.to_string () + "-" + stop_str;
102         this.msg.request_headers.append ("TimeSeekRange.dlna.org", range);
103     }
104
105     private void add_headers_hhmmss (string start, string stop) {
106         var range = "npt=" + start + "-" + stop;
107         this.msg.request_headers.append ("TimeSeekRange.dlna.org", range);
108     }
109 }
110
111 private class Rygel.HTTPTimeSeekTest : GLib.Object {
112     private Regex range_regex;
113     private bool  test_hhmmss;
114
115     public static int main (string[] args) {
116         try {
117             var test = new HTTPTimeSeekTest ();
118
119             test.run ();
120         } catch (TestError.SKIP error) {
121             return 77;
122         } catch (Error error) {
123             critical ("%s", error.message);
124
125             return -1;
126         }
127
128         return 0;
129     }
130
131     public void run () throws HTTPSeekError {
132         var thumbnails = new Thumbnail[] { null, new Thumbnail () };
133         var subtitles = new Subtitle[] { null, new Subtitle () };
134
135         foreach (var thumbnail in thumbnails) {
136             foreach (var subtitle in subtitles) {
137                 this.test_hhmmss = false;
138                 this.test_no_seek (thumbnail, subtitle);
139                 this.test_start_only_seek (thumbnail, subtitle);
140                 this.test_stop_only_seek (thumbnail, subtitle);
141                 this.test_start_stop_seek (thumbnail, subtitle);
142                 this.test_hhmmss = true;
143                 this.test_start_only_seek (thumbnail, subtitle);
144                 this.test_stop_only_seek (thumbnail, subtitle);
145                 this.test_start_stop_seek (thumbnail, subtitle);
146             }
147         }
148     }
149
150     private HTTPTimeSeekTest () {
151         var expression = "npt=[0-9]+\\.[0-9][0-9][0-9]-" +
152                          "[0-9]+\\.[0-9][0-9][0-9]/" +
153                          "[0-9]+\\.[0-9][0-9][0-9]";
154
155         this.range_regex = new Regex (expression, RegexCompileFlags.CASELESS);
156     }
157
158     private void test_no_seek (Thumbnail? thumbnail,
159                                Subtitle?  subtitle) throws HTTPSeekError {
160         var request = new HTTPGet (thumbnail, subtitle);
161         var audio_item = request.item as AudioItem;
162         this.test_seek (request, 0, audio_item.duration * SECOND - MSECOND);
163     }
164
165     private void test_start_only_seek (Thumbnail? thumbnail,
166                                        Subtitle?  subtitle)
167                                        throws HTTPSeekError {
168         HTTPGet request = null;
169         if (this.test_hhmmss) {
170             request = new HTTPGet.seek_hhmmss ("00:02:08.000", "", thumbnail, subtitle);
171         } else {
172             request = new HTTPGet.seek_start (128, thumbnail, subtitle);
173         }
174
175         var audio_item = request.item as AudioItem;
176         this.test_seek (request,
177                         128 * SECOND,
178                         audio_item.duration * SECOND - MSECOND);
179     }
180
181     private void test_stop_only_seek (Thumbnail? thumbnail,
182                                       Subtitle?  subtitle)
183                                       throws HTTPSeekError {
184         HTTPGet request = null;
185         if (this.test_hhmmss) {
186             request = new HTTPGet.seek_hhmmss ("00:00:00.000",
187                                                "00:02:08.000",
188                                                thumbnail,
189                                                subtitle);
190         } else {
191             request = new HTTPGet.seek_stop (128, thumbnail, subtitle);
192         }
193
194         this.test_seek (request, 0, 128 * SECOND);
195     }
196
197     private void test_start_stop_seek (Thumbnail? thumbnail,
198                                        Subtitle?  subtitle)
199                                        throws HTTPSeekError {
200         HTTPGet request = null;
201         if (this.test_hhmmss) {
202             request = new HTTPGet.seek_hhmmss ("00:02:08.000",
203                                                "00:04:16.000",
204                                                thumbnail,
205                                                subtitle);
206         } else {
207             request = new HTTPGet.seek_start_stop (128,
208                                                    256,
209                                                    thumbnail,
210                                                    subtitle);
211         }
212
213         this.test_seek (request, 128 * SECOND, 256 * SECOND);
214     }
215
216     private void test_seek (HTTPGet request,
217                             int64   start,
218                             int64   stop) throws HTTPSeekError {
219         assert (HTTPTimeSeek.needed (request));
220
221         var seek = new HTTPTimeSeek (request);
222         seek.add_response_headers ();
223
224         assert (seek != null);
225         assert (seek.start == start);
226         assert (seek.stop == stop);
227         assert (seek.length == seek.stop + MSECOND - seek.start);
228
229         var audio_item = request.item as AudioItem;
230         assert (seek.total_length == audio_item.duration * SECOND);
231
232         var header = request.msg.response_headers.get_one
233                                         ("TimeSeekRange.dlna.org");
234         assert (header != null);
235         assert (this.range_regex.match (header));
236     }
237 }