Updated French translation
[profile/ivi/rygel.git] / tests / rygel-http-item-uri-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 Gee;
25
26 private errordomain Rygel.HTTPRequestError {
27     UNACCEPTABLE = Soup.Status.NOT_ACCEPTABLE,
28     BAD_REQUEST = Soup.Status.BAD_REQUEST,
29     NOT_FOUND = Soup.Status.NOT_FOUND
30 }
31
32 private errordomain Rygel.TestError {
33     SKIP
34 }
35
36 private class Rygel.Transcoder : GLib.Object {
37     public string extension { get; protected set; }
38
39     public Transcoder (string extension) {
40         this.extension = extension;
41     }
42 }
43
44 private class Rygel.HTTPServer : GLib.Object {
45     private const string SERVER_PATH = "/Test";
46
47     public string path_root { get; private set; }
48
49     public GUPnP.Context context;
50
51     public HTTPServer () throws TestError {
52         this.path_root = SERVER_PATH;
53
54         try {
55             this.context = new GUPnP.Context (null, "lo", 0);
56         } catch (Error error) {
57             throw new TestError.SKIP ("Network context not available");
58         }
59
60         assert (this.context != null);
61         assert (this.context.host_ip != null);
62         assert (this.context.port > 0);
63     }
64
65     public Transcoder get_transcoder (string  target) throws Error {
66         if (target == "MP3") {
67             return new Transcoder ("mp3");
68         }
69         throw new HTTPRequestError.NOT_FOUND (
70                             "No transcoder available for target format '%s'",
71                             target);
72     }
73 }
74
75 private class Rygel.MediaObject : GLib.Object {
76     public string id;
77 }
78
79 private class Rygel.MediaItem : Rygel.MediaObject {
80     public ArrayList<string> uris = new ArrayList<string> ();
81     public string mime_type;
82 }
83
84 private class Rygel.Thumbnail {
85     public string file_extension;
86 }
87
88 private class Rygel.VisualItem : MediaItem {
89     public ArrayList<Thumbnail> thumbnails = new ArrayList<Thumbnail> ();
90 }
91
92 private class Rygel.Subtitle : GLib.Object {
93     public string caption_type;
94 }
95
96 private class Rygel.VideoItem : VisualItem {
97     public ArrayList<Subtitle> subtitles = new ArrayList<Subtitle> ();
98 }
99
100 private class Rygel.MusicItem : MediaItem {
101     public Thumbnail album_art;
102 }
103
104 private class Rygel.HTTPItemURITest : GLib.Object {
105     private const int THUMBNAIL_INDEX = 1;
106     private const int SUBTITLE_INDEX = 1;
107     private const string TRANSCODE_TARGET = "MP3";
108     private VisualItem item = new VisualItem ();
109     private HTTPServer server;
110
111     public static int main (string[] args) {
112         try {
113             var test = new HTTPItemURITest ();
114
115             test.run ();
116         } catch (TestError.SKIP error) {
117             return 77;
118         } catch (Error error) {
119             critical ("%s", error.message);
120
121             return -1;
122         }
123
124         return 0;
125     }
126
127     public void run () throws Error {
128         Thumbnail thumb = new Thumbnail ();
129
130         thumb.file_extension = "png";
131         this.item.thumbnails.add (thumb);
132         this.item.id = "HELLO";
133         this.item.uris.add ("foo.mp3");
134         this.item.mime_type = "audio/mp3";
135
136         var uris = new HTTPItemURI[] {
137             this.test_construction (),
138             this.test_construction_with_thumbnail (),
139             this.test_construction_with_subtitle (),
140             this.test_construction_with_transcoder () };
141
142         foreach (var uri in uris) {
143             var str = this.test_to_string (uri);
144             this.test_construction_from_string (str);
145         }
146         this.test_error_construction ("/Ttt", Soup.Status.BAD_REQUEST);
147     }
148
149     private HTTPItemURITest () throws TestError {
150         this.server = new HTTPServer ();
151     }
152
153     private HTTPItemURI test_construction () {
154         var uri = new HTTPItemURI (this.item, this.server);
155         assert (uri != null);
156
157         return uri;
158     }
159
160     private HTTPItemURI test_construction_with_subtitle () {
161         var uri = new HTTPItemURI (this.item,
162                                    this.server,
163                                    -1,
164                                    SUBTITLE_INDEX);
165         assert (uri != null);
166
167         return uri;
168     }
169
170     private HTTPItemURI test_construction_with_thumbnail () {
171         var uri = new HTTPItemURI (this.item,
172                                    this.server,
173                                    THUMBNAIL_INDEX);
174         assert (uri != null);
175
176         return uri;
177     }
178
179     private HTTPItemURI test_construction_with_transcoder () {
180         var uri = new HTTPItemURI (this.item,
181                                    this.server,
182                                    THUMBNAIL_INDEX,
183                                    -1,
184                                    TRANSCODE_TARGET);
185         assert (uri != null);
186
187         return uri;
188     }
189
190     private HTTPItemURI test_construction_from_string (string str)
191                                                        throws Error {
192         var uri = new HTTPItemURI.from_string (str, this.server);
193         assert (uri != null);
194         assert (uri.to_string () == str);
195
196         return uri;
197     }
198
199     private void test_error_construction (string str,
200                                           Soup.Status error_code) {
201         try {
202             var uri = new HTTPItemURI.from_string (str, this.server);
203             assert (uri == null);
204         } catch (HTTPRequestError error) {
205             assert (error.code == error_code);
206         }
207     }
208
209     private string test_to_string (HTTPItemURI uri) {
210         var str = uri.to_string ();
211         assert (str != null);
212
213         return str;
214     }
215 }