test: Test thumbnail & subtitle cases
[profile/ivi/rygel.git] / tests / rygel-http-byte-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 private errordomain Rygel.TestError {
25     SKIP
26 }
27
28 private class Rygel.HTTPIdentityHandler : GLib.Object {}
29
30 private class Rygel.MediaItem : GLib.Object {
31     public int64 size = 2048;
32 }
33
34 private class Rygel.Thumbnail : GLib.Object {
35     public int64 size = 1024;
36 }
37
38 private class Rygel.Subtitle : GLib.Object {
39     public int64 size = 512;
40 }
41
42 private class Rygel.HTTPGet : GLib.Object {
43     public const string ITEM_URI = "http://DoesntMatterWhatThisIs";
44
45     public Soup.Message msg;
46     public MediaItem item;
47     public Thumbnail thumbnail;
48     public Subtitle subtitle;
49
50     public HTTPIdentityHandler handler;
51
52     public HTTPGet (Thumbnail? thumbnail, Subtitle? subtitle) {
53         this.msg = new Soup.Message ("HTTP", ITEM_URI);
54         this.item = new MediaItem ();
55         this.handler = new HTTPIdentityHandler ();
56         this.thumbnail = thumbnail;
57         this.subtitle = subtitle;
58     }
59
60     public HTTPGet.seek_start (int64      start,
61                                Thumbnail? thumbnail,
62                                Subtitle?  subtitle) {
63         this (thumbnail, subtitle);
64
65         this.add_headers (start, -1);
66     }
67
68     public HTTPGet.seek_stop (int64      stop,
69                               Thumbnail? thumbnail,
70                               Subtitle?  subtitle) {
71         this (thumbnail, subtitle);
72
73         this.add_headers (0, stop);
74     }
75
76     public HTTPGet.seek_start_stop (int64      start,
77                                     int64      stop,
78                                     Thumbnail? thumbnail,
79                                     Subtitle?  subtitle) {
80         this (thumbnail, subtitle);
81
82         this.add_headers (start, stop);
83     }
84
85     private void add_headers (int64 start, int64 stop) {
86         this.msg.request_headers.set_range (start, stop);
87     }
88 }
89
90 private class Rygel.HTTPByteSeekTest : GLib.Object {
91     private Regex range_regex;
92
93     public static int main (string[] args) {
94         try {
95             var test = new HTTPByteSeekTest ();
96
97             test.run ();
98         } catch (TestError.SKIP error) {
99             return 77;
100         } catch (Error error) {
101             critical ("%s", error.message);
102
103             return -1;
104         }
105
106         return 0;
107     }
108
109     public void run () throws HTTPSeekError {
110         var thumbnails = new Thumbnail[] { null, new Thumbnail () };
111         var subtitles = new Subtitle[] { null, new Subtitle () };
112
113         foreach (var thumbnail in thumbnails) {
114             foreach (var subtitle in subtitles) {
115                 this.test_no_seek (thumbnail, subtitle);
116                 this.test_start_only_seek (thumbnail, subtitle);
117                 this.test_stop_only_seek (thumbnail, subtitle);
118                 this.test_start_stop_seek (thumbnail, subtitle);
119             }
120         }
121     }
122
123     private HTTPByteSeekTest () {
124         this.range_regex = new Regex ("bytes +[0-9]+-[0-9]+/[0-9]+",
125                                       RegexCompileFlags.CASELESS);
126     }
127
128     private void test_no_seek (Thumbnail? thumbnail,
129                                Subtitle?  subtitle) throws HTTPSeekError {
130         var request = new HTTPGet (thumbnail, subtitle);
131
132         int64 size;
133         if (request.thumbnail != null) {
134             size = request.thumbnail.size;
135         } else if (request.subtitle != null) {
136             size = request.subtitle.size;
137         } else {
138             size = request.item.size;
139         }
140
141         this.test_seek (request, 0, size - 1);
142     }
143
144     private void test_start_only_seek (Thumbnail? thumbnail,
145                                        Subtitle?  subtitle)
146                                        throws HTTPSeekError {
147         var request = new HTTPGet.seek_start (128, thumbnail, subtitle);
148
149         int64 size;
150         if (request.thumbnail != null) {
151             size = request.thumbnail.size;
152         } else if (request.subtitle != null) {
153             size = request.subtitle.size;
154         } else {
155             size = request.item.size;
156         }
157
158         this.test_seek (request, 128, size - 1);
159     }
160
161     private void test_stop_only_seek (Thumbnail? thumbnail,
162                                       Subtitle?  subtitle)
163                                       throws HTTPSeekError {
164         var request = new HTTPGet.seek_stop (128, thumbnail, subtitle);
165
166         this.test_seek (request, 0, 128);
167     }
168
169     private void test_start_stop_seek (Thumbnail? thumbnail,
170                                        Subtitle?  subtitle)
171                                        throws HTTPSeekError {
172         var request = new HTTPGet.seek_start_stop (128,
173                                                    256,
174                                                    thumbnail,
175                                                    subtitle);
176
177         this.test_seek (request, 128, 256);
178     }
179
180     private void test_seek (HTTPGet request,
181                             int64   start,
182                             int64   stop) throws HTTPSeekError {
183         assert (HTTPByteSeek.needed (request));
184
185         var seek = new HTTPByteSeek (request);
186         seek.add_response_headers ();
187
188         assert (seek != null);
189         assert (seek.start == start);
190         assert (seek.stop == stop);
191
192         if (request.thumbnail != null) {
193             assert (seek.length == request.thumbnail.size);
194         } else if (request.subtitle != null) {
195             assert (seek.length == request.subtitle.size);
196         } else {
197             assert (seek.length == request.item.size);
198         }
199
200         var header = request.msg.response_headers.get ("Accept-Ranges");
201         assert (header == "bytes");
202         header = request.msg.response_headers.get ("Content-Range");
203         assert (header != null);
204         assert (this.range_regex.match (header));
205
206         assert (request.msg.response_headers.get_content_length () ==
207                 seek.stop + 1 - seek.start);
208     }
209 }