File: geary-attachment-test.vala

package info (click to toggle)
geary 46.0-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,092 kB
  • sloc: javascript: 972; ansic: 722; sql: 247; xml: 183; python: 30; makefile: 28; sh: 24
file content (221 lines) | stat: -rw-r--r-- 7,814 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * Copyright 2016 Michael Gratton <mike@vee.net>
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later). See the COPYING file in this distribution.
 */

// Defined by CMake build script.
extern const string _SOURCE_ROOT_DIR;

class Geary.AttachmentTest : TestCase {

    private const string CONTENT_TYPE = "image/svg+xml";
    private const string CONTENT_ID = "test-content-id";
    private const string CONTENT_DESC = "Mea navis volitans anguillis plena est";
    private const string FILE_PATH = "icons/hicolor/scalable/apps/org.gnome.Geary.svg";

    private Mime.ContentType? content_type;
    private Mime.ContentType? default_type;
    private Mime.ContentDisposition? content_disposition;
    private File? file;


    private class TestAttachment : Attachment {
        // A test article

        internal TestAttachment(Mime.ContentType content_type,
                                string? content_id,
                                string? content_description,
                                Mime.ContentDisposition content_disposition,
                                string? content_filename,
                                GLib.File file) {
            base(content_type, content_id, content_description,
                 content_disposition, content_filename);
            set_file_info(file, 742);
        }

    }

    public AttachmentTest() {
        base("Geary.AttachmentTest");
        add_test("get_safe_file_name_with_content_name",
                 get_safe_file_name_with_content_name);
        add_test("get_safe_file_name_with_bad_content_name",
                 get_safe_file_name_with_bad_content_name);
        add_test("get_safe_file_name_with_bad_file_name",
                 get_safe_file_name_with_bad_file_name);
        add_test("get_safe_file_name_with_alt_file_name",
                 get_safe_file_name_with_alt_file_name);
        add_test("get_safe_file_name_with_no_content_name",
                 get_safe_file_name_with_no_content_name);
        add_test("get_safe_file_name_with_no_content_name_or_id",
                 get_safe_file_name_with_no_content_name_or_id);
        add_test("get_safe_file_name_with_default_content_type",
                 get_safe_file_name_with_default_content_type);
        add_test("get_safe_file_name_with_default_content_type_bad_file_name",
                 get_safe_file_name_with_default_content_type_bad_file_name);
        add_test("get_safe_file_name_with_unknown_content_type",
                 get_safe_file_name_with_unknown_content_type);
    }

    public override void set_up() throws GLib.Error {
        this.content_type = Mime.ContentType.parse(CONTENT_TYPE);
        this.default_type = Mime.ContentType.ATTACHMENT_DEFAULT;
        this.content_disposition = new Mime.ContentDisposition("attachment", null);

        File source = File.new_for_path(_SOURCE_ROOT_DIR);
        this.file = source.get_child(FILE_PATH);
    }

    public void get_safe_file_name_with_content_name() throws Error {
        const string TEST_FILENAME = "test-filename.svg";
        Attachment test = new TestAttachment(
            this.content_type,
            CONTENT_ID,
            CONTENT_DESC,
            content_disposition,
            TEST_FILENAME,
            this.file
        );

        test.get_safe_file_name.begin(null, this.async_completion);

        assert(test.get_safe_file_name.end(async_result()) == TEST_FILENAME);
    }

    public void get_safe_file_name_with_bad_content_name() throws Error {
        const string TEST_FILENAME = "test-filename.jpg";
        const string RESULT_FILENAME = "test-filename.jpg.svg";
        Attachment test = new TestAttachment(
            this.content_type,
            CONTENT_ID,
            CONTENT_DESC,
            content_disposition,
            TEST_FILENAME,
            this.file
        );

        test.get_safe_file_name.begin(null, this.async_completion);

        assert(test.get_safe_file_name.end(async_result()) == RESULT_FILENAME);
    }

    public void get_safe_file_name_with_bad_file_name() throws Error {
        const string TEST_FILENAME = "test-filename";
        const string RESULT_FILENAME = "test-filename.svg";
        Attachment test = new TestAttachment(
            this.content_type,
            CONTENT_ID,
            CONTENT_DESC,
            content_disposition,
            TEST_FILENAME,
            this.file
        );

        test.get_safe_file_name.begin(null, this.async_completion);

        assert(test.get_safe_file_name.end(async_result()) == RESULT_FILENAME);
    }

    public void get_safe_file_name_with_no_content_name() throws Error {
        const string RESULT_FILENAME = CONTENT_ID + ".svg";
        Attachment test = new TestAttachment(
            this.content_type,
            CONTENT_ID,
            CONTENT_DESC,
            content_disposition,
            null,
            this.file
        );

        test.get_safe_file_name.begin(null, this.async_completion);

        assert(test.get_safe_file_name.end(async_result()) == RESULT_FILENAME);
    }

    public void get_safe_file_name_with_no_content_name_or_id() throws Error {
        const string RESULT_FILENAME = "attachment.svg";
        Attachment test = new TestAttachment(
            this.content_type,
            null,
            CONTENT_DESC,
            content_disposition,
            null,
            this.file
        );

        test.get_safe_file_name.begin(null, this.async_completion);

        assert(test.get_safe_file_name.end(async_result()) == RESULT_FILENAME);
    }

    public void get_safe_file_name_with_alt_file_name() throws Error {
        const string ALT_TEXT = "some text";
        const string RESULT_FILENAME = "some text.svg";
        Attachment test = new TestAttachment(
            this.content_type,
            null,
            CONTENT_DESC,
            content_disposition,
            null,
            this.file
        );

        test.get_safe_file_name.begin(ALT_TEXT, this.async_completion);

        assert(test.get_safe_file_name.end(async_result()) == RESULT_FILENAME);
    }

    public void get_safe_file_name_with_default_content_type() throws Error {
        const string TEST_FILENAME = "test-filename.svg";
        Attachment test = new TestAttachment(
            this.default_type,
            CONTENT_ID,
            CONTENT_DESC,
            content_disposition,
            TEST_FILENAME,
            this.file
        );

        test.get_safe_file_name.begin(null, this.async_completion);

        assert(test.get_safe_file_name.end(async_result()) == TEST_FILENAME);
    }

    public void get_safe_file_name_with_default_content_type_bad_file_name()
        throws Error {
        const string TEST_FILENAME = "test-filename.jpg";
        const string RESULT_FILENAME = "test-filename.jpg.svg";
        Attachment test = new TestAttachment(
            this.default_type,
            CONTENT_ID,
            CONTENT_DESC,
            content_disposition,
            TEST_FILENAME,
            this.file
        );

        test.get_safe_file_name.begin(null, this.async_completion);

        assert(test.get_safe_file_name.end(async_result()) == RESULT_FILENAME);
    }

    public void get_safe_file_name_with_unknown_content_type()
        throws Error {
        const string TEST_FILENAME = "test-filename.unlikely";
        Attachment test = new TestAttachment(
            this.default_type,
            CONTENT_ID,
            CONTENT_DESC,
            content_disposition,
            TEST_FILENAME,
            File.new_for_path(TEST_FILENAME)
        );

        test.get_safe_file_name.begin(null, this.async_completion);

        assert_equal(test.get_safe_file_name.end(async_result()), TEST_FILENAME);
    }

}