FolderTests.cpp 9.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

23
#include "Tests.h"
24

25
#include "Media.h"
26
#include "File.h"
27
#include "Folder.h"
28
#include "IMediaLibrary.h"
29
#include "utils/Filename.h"
30
#include "mocks/FileSystem.h"
31
#include "mocks/DiscovererCbMock.h"
32
33

#include <memory>
34

35
36

class FoldersNoDiscover : public Tests
37
{
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
protected:
    std::shared_ptr<mock::FileSystemFactory> fsMock;
    std::unique_ptr<mock::WaitForDiscoveryComplete> cbMock;

public:
    virtual void SetUp() override
    {
        unlink("test.db");
        fsMock.reset( new mock::FileSystemFactory );
        cbMock.reset( new mock::WaitForDiscoveryComplete );
        Reload();
    }

    virtual void InstantiateMediaLibrary() override
    {
        ml.reset( new MediaLibraryWithoutParser );
    }

    virtual void Reload()
    {
        Tests::Reload( fsMock, cbMock.get() );
    }
};
61

62
63
class Folders : public FoldersNoDiscover
{
64
    protected:
65
        virtual void SetUp() override
66
        {
67
68
69
70
71
            FoldersNoDiscover::SetUp();
            cbMock->prepareForWait( 1 );
            ml->discover( mock::FileSystemFactory::Root );
            bool discovered = cbMock->wait();
            ASSERT_TRUE( discovered );
72
        }
73
};
74
75
76

TEST_F( Folders, Add )
{
77
    auto files = ml->files();
78

79
    ASSERT_EQ( files.size(), 3u );
80
81
82
83
}

TEST_F( Folders, Delete )
{
84
    auto f = ml->folder( mock::FileSystemFactory::Root );
85
86
    auto folderPath = f->path();

87
    auto files = ml->files();
88
    ASSERT_EQ( files.size(), 3u );
89

90
    ml->deleteFolder( *f );
91

92
93
94
    f = ml->folder( folderPath );
    ASSERT_EQ( nullptr, f );

95
    files = ml->files();
96
    ASSERT_EQ( files.size(), 0u );
97

98
    cbMock->prepareForReload();
99
    Reload();
100
101
    bool reloaded = cbMock->waitForReload();
    ASSERT_TRUE( reloaded );
102
103
104
105

    // Recheck folder deletion from DB:
    f = ml->folder( folderPath );
    ASSERT_EQ( nullptr, f );
106
107
108
109
}

TEST_F( Folders, Load )
{
110
    cbMock->prepareForReload();
111
    Reload();
112
113
    bool reloaded = cbMock->waitForReload();
    ASSERT_TRUE( reloaded );
114

115
116
    auto files = ml->files();
    ASSERT_EQ( files.size(), 3u );
117
}
118

119
TEST_F( FoldersNoDiscover, InvalidPath )
120
{
121
    cbMock->prepareForWait( 1 );
122
    ml->discover( "/invalid/path" );
123
124
    bool discovered = cbMock->wait();
    ASSERT_TRUE( discovered );
125

126
    auto files = ml->files();
127
128
    ASSERT_EQ( files.size(), 0u );
}
129
130
131

TEST_F( Folders, List )
{
132
    auto f = ml->folder( mock::FileSystemFactory::Root );
133
    ASSERT_NE( f, nullptr );
134
135
136
    auto files = f->files();
    ASSERT_EQ( files.size(), 2u );

137
    cbMock->prepareForReload();
138
    Reload();
139
140
    bool reloaded = cbMock->waitForReload();
    ASSERT_TRUE( reloaded );
141
142
143
144
145

    f = ml->folder( f->path() );
    files = f->files();
    ASSERT_EQ( files.size(), 2u );
}
146

147
148
TEST_F( Folders, ListFolders )
{
149
    auto f = ml->folder( mock::FileSystemFactory::Root );
150
    ASSERT_NE( f, nullptr );
151
152
153
154
155
156
157
    auto subFolders = f->folders();
    ASSERT_EQ( 1u, subFolders.size() );

    auto subFolder = subFolders[0];
    auto subFiles = subFolder->files();
    ASSERT_EQ( 1u, subFiles.size() );

158
    ASSERT_EQ( mock::FileSystemFactory::SubFolder + "subfile.mp4", subFiles[0]->mrl() );
159

160
    // Now again, without cache. No need to wait for fs discovery reload here
161
    Reload();
162
163
164
165
166
167
168
169
170

    f = ml->folder( f->path() );
    subFolders = f->folders();
    ASSERT_EQ( 1u, subFolders.size() );

    subFolder = subFolders[0];
    subFiles = subFolder->files();
    ASSERT_EQ( 1u, subFiles.size() );

171
    ASSERT_EQ( mock::FileSystemFactory::SubFolder + "subfile.mp4", subFiles[0]->mrl() );
172
}
173

174
TEST_F( Folders, NewFolderWithFile )
175
176
177
178
{
    ASSERT_EQ( 3u, ml->files().size() );
    // Do not watch for live changes
    ml.reset();
179
    auto newFolder = mock::FileSystemFactory::Root + "newfolder/";
180
    fsMock->addFolder( newFolder );
181
    fsMock->addFile( newFolder + "newfile.avi" );
182

183
184
    // This will trigger a reload
    cbMock->prepareForReload();
185
    Reload();
186
187
    bool reloaded = cbMock->waitForReload();
    ASSERT_TRUE( reloaded );
188
189

    ASSERT_EQ( 4u, ml->files().size() );
190
191
    auto f = ml->media( newFolder + "newfile.avi" );
    ASSERT_NE( nullptr, f );
192
}
193

194
// This is expected to fail until we fix the file system modifications detection
195
196
TEST_F( Folders, NewFileInSubFolder )
{
197
    auto f = ml->folder( mock::FileSystemFactory::Root );
198
    ASSERT_EQ( 3u, ml->files().size() );
199
200

    f = ml->folder( mock::FileSystemFactory::SubFolder );
201
202
    // Do not watch for live changes
    ml.reset();
203
    fsMock->addFile( mock::FileSystemFactory::SubFolder + "newfile.avi" );
204

205
    cbMock->prepareForReload();
206
    Reload();
207
208
    bool reloaded = cbMock->waitForReload();
    ASSERT_TRUE( reloaded );
209
210

    ASSERT_EQ( 4u, ml->files().size() );
211
    auto media = ml->media( mock::FileSystemFactory::SubFolder + "newfile.avi" );
212
    f = ml->folder( mock::FileSystemFactory::SubFolder );
213
    ASSERT_EQ( 2u, f->files().size() );
214
    ASSERT_NE( nullptr, media );
215
}
216
217
218
219
220
221

TEST_F( Folders, RemoveFileFromDirectory )
{
    ASSERT_EQ( 3u, ml->files().size() );
    // Do not watch for live changes
    ml.reset();
222
    fsMock->removeFile( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
223

224
    cbMock->prepareForReload();
225
    Reload();
226
227
    bool reloaded = cbMock->waitForReload();
    ASSERT_TRUE( reloaded );
228
229

    ASSERT_EQ( 2u, ml->files().size() );
230
    auto media = ml->media( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
231
232
    auto f = ml->folder( mock::FileSystemFactory::SubFolder );
    ASSERT_EQ( 0u, f->files().size() );
233
    ASSERT_EQ( nullptr, media );
234
}
235
236
237
238
239
240
241
242

TEST_F( Folders, RemoveDirectory )
{
    ASSERT_EQ( 3u, ml->files().size() );
    // Do not watch for live changes
    ml.reset();
    fsMock->removeFolder( mock::FileSystemFactory::SubFolder );

243
    cbMock->prepareForReload();
244
    Reload();
245
246
    bool reloaded = cbMock->waitForReload();
    ASSERT_TRUE( reloaded );
247
248

    ASSERT_EQ( 2u, ml->files().size() );
249
    auto media = ml->media( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
250
251
    auto f = ml->folder( mock::FileSystemFactory::SubFolder );
    ASSERT_EQ( nullptr, f );
252
    ASSERT_EQ( nullptr, media );
253
}
254
255
256

TEST_F( Folders, UpdateFile )
{
257
    auto filePath = mock::FileSystemFactory::SubFolder + "subfile.mp4";
258
    auto f = ml->media( filePath );
259
    ASSERT_NE( f, nullptr );
260
261
262
    auto id = f->id();

    ml.reset();
263
    fsMock->file( filePath )->markAsModified();
264

265
    cbMock->prepareForReload();
266
    Reload();
267
268
269
    bool reloaded = cbMock->waitForReload();
    ASSERT_TRUE( reloaded );

270
    f = ml->media( filePath );
271
272
273
274
275
    ASSERT_NE( nullptr, f );
    // The file is expected to be deleted and re-added since it changed, so the
    // id should have changed
    ASSERT_NE( id, f->id() );
}
276

277
TEST_F( FoldersNoDiscover, Blacklist )
278
279
{
    cbMock->prepareForWait( 1 );
280
    ml->banFolder( mock::FileSystemFactory::SubFolder );
281
    ml->discover( mock::FileSystemFactory::Root );
282
283
284
285
286
287
288
289
290
291
292
293
294
295
    bool discovered = cbMock->wait();
    ASSERT_TRUE( discovered );

    auto f = ml->folder( mock::FileSystemFactory::SubFolder );
    ASSERT_EQ( nullptr, f );
}

TEST_F( Folders, BlacklistAfterDiscovery )
{
    auto f = ml->folder( mock::FileSystemFactory::SubFolder );
    ASSERT_NE( nullptr, f );
    auto files = f->files();
    ASSERT_NE( 0u, files.size() );

296
    ml->banFolder( mock::FileSystemFactory::SubFolder );
297
298
299
    auto f2 = ml->folder( mock::FileSystemFactory::SubFolder );
    ASSERT_EQ( nullptr, f2 );
}
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

TEST_F( FoldersNoDiscover, NoMediaBeforeDiscovery )
{
    auto newFolder = mock::FileSystemFactory::Root + "newfolder/";
    fsMock->addFolder( newFolder );
    fsMock->addFile( newFolder + "newfile.avi" );
    fsMock->addFile( newFolder + ".nomedia" );

    cbMock->prepareForWait( 1 );
    ml->discover( mock::FileSystemFactory::Root );
    bool discovered = cbMock->wait();
    ASSERT_TRUE( discovered );

    auto files = ml->files();
    // We add 3 files before, and the new one shouldn't be account for since there is a .nomedia file
    ASSERT_EQ( 3u, files.size() );
}

TEST_F( Folders, InsertNoMedia )
{
    auto files = ml->files();
    ASSERT_EQ( 3u, files.size() );
    fsMock->addFile( mock::FileSystemFactory::SubFolder + ".nomedia" );

    cbMock->prepareForReload();
    Reload();
    bool reloaded = cbMock->waitForReload();
    ASSERT_TRUE( reloaded );

    files = ml->files();
    ASSERT_EQ( 2u, files.size() );
}

TEST_F( Folders, InsertNoMediaInRoot )
{
    fsMock->addFile( mock::FileSystemFactory::Root + ".nomedia" );

    cbMock->prepareForReload();
    Reload();
    bool reloaded = cbMock->waitForReload();
    ASSERT_TRUE( reloaded );

    auto files = ml->files();
    ASSERT_EQ( 0u, files.size() );
}
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367

TEST_F( Folders, ReloadSubDir )
{
    auto files = ml->files();
    ASSERT_EQ( 3u, files.size() );
    fsMock->addFile( mock::FileSystemFactory::Root + "newmedia.mkv" );

    cbMock->prepareForReload();
    ml->reload( mock::FileSystemFactory::SubFolder );
    bool reloaded = cbMock->waitForReload();
    ASSERT_TRUE( reloaded );

    files = ml->files();
    ASSERT_EQ( 3u, files.size() );

    cbMock->prepareForReload();
    ml->reload();
    reloaded = cbMock->waitForReload();
    ASSERT_TRUE( reloaded );

    files = ml->files();
    ASSERT_EQ( 4u, files.size() );
}