summaryrefslogtreecommitdiff
path: root/SmartDeviceLinkTests/SDLCacheFileManagerSpec.m
blob: 836a0391fc7859961d9db4a2f82053546e75106b (plain)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//
//  SDLCacheFileManagerSpec.m
//  SmartDeviceLinkTests
//
//  Created by James Lapinski on 3/23/20.
//  Copyright © 2020 smartdevicelink. All rights reserved.
//

#import <Quick/Quick.h>
#import <Nimble/Nimble.h>
#import <OCMock/OCMock.h>

#import "SDLCacheFileManager.h"
#import "SDLIconArchiveFile.h"
#import "SDLOnSystemRequest.h"

@interface SDLCacheFileManager ()

+ (nullable NSString *)sdl_writeImage:(UIImage *)icon toFilePath:(NSString *)filePath imageURL:(NSString *)urlString;
- (void)sdl_downloadIconFromRequestURL:(NSString *)requestURL withCompletionHandler:(ImageRetrievalCompletionHandler)completion;

@property (weak, nonatomic, nullable) NSURLSession *urlSession;
@property (strong, nonatomic, nullable) NSURLSessionDataTask *dataTask;
@property (strong, nonatomic) NSFileManager *fileManager;

@end

QuickSpecBegin(SDLCacheFileManagerSpec)

describe(@"a cache file manager", ^{
    __block SDLCacheFileManager *testManager = nil;
    __block id testManagerMock = nil;
    __block NSFileManager *mockFileManager = nil;
    __block id mockUnarchiver = nil;
    __block id mockUIImage = nil;
    __block id mockURLSession = nil;
    __block id mockArchiver = nil;
    __block SDLOnSystemRequest *testRequest = nil;
    __block NSString *testURL = nil;
    __block SDLOnSystemRequest *expiredTestRequest = nil;
    __block NSString *testFilePath = nil;
    __block NSString *expiredTestURL = nil;
    __block UIImage *testImage = nil;
    __block SDLIconArchiveFile *testArchiveFile = nil;
    __block SDLLockScreenIconCache *testIconCache = nil;
    __block SDLLockScreenIconCache *testExpiredIconCache = nil;
    __block NSArray<SDLLockScreenIconCache *> *testArchiveFileLockScreenCacheArray = nil;

    beforeEach(^{
        testManager = [[SDLCacheFileManager alloc] init];
        testManagerMock = OCMPartialMock(testManager);

        testFilePath = [[NSBundle bundleForClass:self.class] pathForResource:@"testImagePNG" ofType:@"png"];
        testImage = [UIImage imageNamed:@"testImagePNG" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];

        mockFileManager = OCMStrictClassMock([NSFileManager class]);
        testManager.fileManager = mockFileManager;
        mockUnarchiver = OCMClassMock([NSKeyedUnarchiver class]);

        mockUIImage = OCMClassMock([UIImage class]);
        mockURLSession = OCMClassMock([NSURLSession class]);

        mockArchiver = OCMClassMock([NSKeyedArchiver class]);
    });

    it(@"should initialize properties", ^{
        expect(testManager.urlSession).toNot(beNil());
        expect(testManager.fileManager).toNot(beNil());
    });

    describe(@"request with existing icon", ^{
        beforeEach(^{
            testURL = @"testURL";
            testRequest = [[SDLOnSystemRequest alloc] init];
            testRequest.url = testURL;
            testIconCache = [[SDLLockScreenIconCache alloc] initWithIconUrl:testURL iconFilePath:testFilePath];

            expiredTestURL = @"expiredTestURL";
            expiredTestRequest = [[SDLOnSystemRequest alloc] init];
            expiredTestRequest.url = expiredTestURL;
            testExpiredIconCache = [[SDLLockScreenIconCache alloc] initWithIconUrl:expiredTestURL iconFilePath:testFilePath];
            testExpiredIconCache.lastModifiedDate = [[NSDate date] dateByAddingTimeInterval:-31*24*60*60];

            testArchiveFileLockScreenCacheArray = @[testIconCache,testExpiredIconCache];

            testArchiveFile = [[SDLIconArchiveFile alloc] init];
            testArchiveFile.lockScreenIconCaches = testArchiveFileLockScreenCacheArray;

            OCMStub(ClassMethod([mockUnarchiver unarchiveObjectWithFile:[OCMArg any]])).andReturn(testArchiveFile);
            OCMStub([mockFileManager fileExistsAtPath:[OCMArg any]]).andReturn(YES);
        });

        context(@"icon is not expired", ^{
            __block UIImage *resultImage = nil;
            __block NSError *resultError = nil;

            context(@"failure to retrieve icon", ^{
                beforeEach(^{
                    OCMStub(ClassMethod([mockArchiver archiveRootObject:[OCMArg isNotNil] toFile:[OCMArg isNotNil]])).andReturn(YES);
                    OCMStub(ClassMethod([testManagerMock sdl_writeImage:[OCMArg isNotNil] toFilePath:[OCMArg isNotNil] imageURL:[OCMArg isNotNil]])).andReturn(testFilePath);
                    OCMStub([testManagerMock sdl_downloadIconFromRequestURL:[OCMArg isNotNil] withCompletionHandler:([OCMArg invokeBlockWithArgs:testImage, [NSNull null], nil])]);
                    OCMStub([mockFileManager contentsOfDirectoryAtPath:[OCMArg isNotNil] error:[OCMArg anyObjectRef]]);
                    OCMStub(ClassMethod([mockUIImage imageWithContentsOfFile:[OCMArg isNotNil]])).andReturn(nil);
                    [testManager retrieveImageForRequest:testRequest withCompletionHandler:^(UIImage * _Nullable image, NSError * _Nullable error) {
                        resultImage = image;
                        resultError = error;
                    }];
                });

                it(@"should download and return image with no error", ^{
                    expect(resultImage).to(equal(testImage));
                    expect(resultError).to(beNil());
                });
            });

            context(@"retrieve icon success", ^{
                beforeEach(^{
                    OCMStub(ClassMethod([mockUIImage imageWithContentsOfFile:[OCMArg isNotNil]])).andReturn(testImage);
                    [testManager retrieveImageForRequest:testRequest withCompletionHandler:^(UIImage * _Nullable image, NSError * _Nullable error) {
                        resultImage = image;
                        resultError = error;
                    }];
                });

                it(@"should return retrieved image and no error", ^{
                    expect(resultImage).to(equal(testImage));
                    expect(resultError).to(beNil());
                });
            });
        });

        context(@"request with expired icon", ^{
            __block UIImage *resultImage = nil;
            __block NSError *resultError = nil;

            context(@"image download succeeds", ^{
                context(@"Failed to update archive object", ^{
                    beforeEach(^{
                        OCMStub(ClassMethod([mockArchiver archiveRootObject:[OCMArg isNotNil] toFile:[OCMArg isNotNil]])).andReturn(NO);
                        OCMStub(ClassMethod([testManagerMock sdl_writeImage:[OCMArg isNotNil] toFilePath:[OCMArg isNotNil] imageURL:[OCMArg isNotNil]])).andReturn(testFilePath);
                        OCMStub([testManagerMock sdl_downloadIconFromRequestURL:[OCMArg isNotNil] withCompletionHandler:([OCMArg invokeBlockWithArgs:testImage, [NSNull null], nil])]);

                        [testManager retrieveImageForRequest:expiredTestRequest withCompletionHandler:^(UIImage * _Nullable image, NSError * _Nullable error) {
                            resultImage = image;
                            resultError = error;
                        }];
                    });

                    it(@"it should return downloaded image and no error", ^{
                        expect(resultImage).toEventually(equal(testImage));
                        expect(resultError).toEventually(beNil());
                    });
                });

                context(@"write image to file path succeeds", ^{
                    beforeEach(^{
                        OCMStub(ClassMethod([mockArchiver archiveRootObject:[OCMArg isNotNil] toFile:[OCMArg isNotNil]])).andReturn(YES);
                        OCMStub(ClassMethod([testManagerMock sdl_writeImage:[OCMArg isNotNil] toFilePath:[OCMArg isNotNil] imageURL:[OCMArg isNotNil]])).andReturn(testFilePath);
                        OCMStub([testManagerMock sdl_downloadIconFromRequestURL:[OCMArg isNotNil] withCompletionHandler:([OCMArg invokeBlockWithArgs:testImage, [NSNull null], nil])]);

                        [testManager retrieveImageForRequest:expiredTestRequest withCompletionHandler:^(UIImage * _Nullable image, NSError * _Nullable error) {
                            resultImage = image;
                            resultError = error;
                        }];
                    });

                    it(@"it should return downloaded image and no error", ^{
                        expect(resultImage).toEventually(equal(testImage));
                        expect(resultError).toEventually(beNil());
                    });
                });

                context(@"write image to file path fails", ^{
                    beforeEach(^{
                        OCMStub(ClassMethod([testManagerMock sdl_writeImage:[OCMArg isNotNil] toFilePath:[OCMArg isNotNil] imageURL:[OCMArg isNotNil]])).andReturn(nil);
                        OCMStub([testManagerMock sdl_downloadIconFromRequestURL:[OCMArg isNotNil] withCompletionHandler:([OCMArg invokeBlockWithArgs:testImage, [NSNull null], nil])]);

                        [testManager retrieveImageForRequest:expiredTestRequest withCompletionHandler:^(UIImage * _Nullable image, NSError * _Nullable error) {
                            resultImage = image;
                            resultError = error;
                        }];
                    });

                    it(@"it should return downloaded image and no error", ^{
                        expect(resultImage).to(equal(testImage));
                        expect(resultError).to(beNil());
                    });
                });
            });

            context(@"image download fails", ^{
                __block NSError *imageDownloadError = [NSError errorWithDomain:@"test" code:0 userInfo:nil];

                beforeEach(^{
                    OCMStub([mockURLSession dataTaskWithURL:[OCMArg isNotNil] completionHandler:([OCMArg invokeBlockWithArgs:[NSNull null], [NSNull null], imageDownloadError, nil])]);
                    OCMStub([testManagerMock sdl_downloadIconFromRequestURL:[OCMArg isNotNil] withCompletionHandler:([OCMArg invokeBlockWithArgs:[NSNull null], imageDownloadError, nil])]);

                    [testManager retrieveImageForRequest:expiredTestRequest withCompletionHandler:^(UIImage * _Nullable image, NSError * _Nullable error) {
                        resultImage = image;
                        resultError = error;
                    }];
                });

                it(@"it should return error and no image", ^{
                    expect(resultImage).to(beNil());
                    expect(resultError).toNot(beNil());
                });
            });
        });
    });

    describe(@"Receiving a new icon", ^{
        __block SDLOnSystemRequest *newIconRequest = nil;
        __block NSString *newIconURL = nil;

        beforeEach(^{
            newIconURL = @"newURL";
            newIconRequest = [[SDLOnSystemRequest alloc] init];
            newIconRequest.url = newIconURL;
        });

        context(@"When no directory is present", ^{
            __block UIImage *resultImage = nil;
            __block NSError *resultError = nil;

            beforeEach(^{
                OCMStub(ClassMethod([mockArchiver archiveRootObject:[OCMArg isNotNil] toFile:[OCMArg isNotNil]])).andReturn(YES);
                OCMStub(ClassMethod([testManagerMock sdl_writeImage:[OCMArg isNotNil] toFilePath:[OCMArg isNotNil] imageURL:[OCMArg isNotNil]])).andReturn(testFilePath);
                OCMStub([testManagerMock sdl_downloadIconFromRequestURL:[OCMArg isNotNil] withCompletionHandler:([OCMArg invokeBlockWithArgs:testImage, [NSNull null], nil])]);
                OCMStub([mockFileManager contentsOfDirectoryAtPath:[OCMArg isNotNil] error:[OCMArg anyObjectRef]]);
                OCMStub([mockFileManager createDirectoryAtPath:[OCMArg isNotNil] withIntermediateDirectories:[OCMArg any] attributes:[OCMArg any] error:[OCMArg anyObjectRef]]).andReturn(YES);
                OCMStub([mockFileManager fileExistsAtPath:[OCMArg any]]).andReturn(NO);

                [testManager retrieveImageForRequest:newIconRequest withCompletionHandler:^(UIImage * _Nullable image, NSError * _Nullable error) {
                    resultImage = image;
                    resultError = error;
                }];
            });

            it(@"it should return downloaded image and no error", ^{
                expect(resultImage).to(equal(testImage));
                expect(resultError).to(beNil());
            });
        });

        context(@"When directory is present", ^{
            __block UIImage *resultImage = nil;
            __block NSError *resultError = nil;

            beforeEach(^{
                OCMStub(ClassMethod([mockArchiver archiveRootObject:[OCMArg isNotNil] toFile:[OCMArg isNotNil]])).andReturn(YES);
                OCMStub(ClassMethod([testManagerMock sdl_writeImage:[OCMArg isNotNil] toFilePath:[OCMArg isNotNil] imageURL:[OCMArg isNotNil]])).andReturn(testFilePath);
                OCMStub([testManagerMock sdl_downloadIconFromRequestURL:[OCMArg isNotNil] withCompletionHandler:([OCMArg invokeBlockWithArgs:testImage, [NSNull null], nil])]);

                testArchiveFile = [[SDLIconArchiveFile alloc] init];
                testArchiveFile.lockScreenIconCaches = testArchiveFileLockScreenCacheArray;
            });

            context(@"no archive file present", ^{
                beforeEach(^{
                    OCMStub([mockFileManager contentsOfDirectoryAtPath:[OCMArg isNotNil] error:[OCMArg anyObjectRef]]).andReturn(nil);
                    OCMStub([mockFileManager fileExistsAtPath:[OCMArg any]]).andReturn(YES);

                    [testManager retrieveImageForRequest:newIconRequest withCompletionHandler:^(UIImage * _Nullable image, NSError * _Nullable error) {
                        resultImage = image;
                        resultError = error;
                    }];
                });

                it(@"it should return downloaded image and no error", ^{
                    expect(resultImage).to(equal(testImage));
                    expect(resultError).to(beNil());
                });
            });

            context(@"archive file present", ^{
                context(@"remove item success", ^{
                    beforeEach(^{
                        OCMStub([mockFileManager removeItemAtPath:[OCMArg isNotNil] error:[OCMArg anyObjectRef]]).andReturn(YES);
                        OCMStub([mockFileManager contentsOfDirectoryAtPath:[OCMArg isNotNil] error:[OCMArg anyObjectRef]]).andReturn(@[@"iconArchiveFile"]);
                        OCMStub([mockFileManager fileExistsAtPath:[OCMArg any]]).andReturn(YES);

                        [testManager retrieveImageForRequest:newIconRequest withCompletionHandler:^(UIImage * _Nullable image, NSError * _Nullable error) {
                            resultImage = image;
                            resultError = error;
                        }];
                    });

                    it(@"it should return downloaded image and no error", ^{
                        expect(resultImage).to(equal(testImage));
                        expect(resultError).to(beNil());
                    });
                });

                context(@"remove item fails", ^{
                    beforeEach(^{
                        OCMStub([mockFileManager removeItemAtPath:[OCMArg isNotNil] error:[OCMArg anyObjectRef]]).andReturn(NO);
                        OCMStub([mockFileManager contentsOfDirectoryAtPath:[OCMArg isNotNil] error:[OCMArg anyObjectRef]]).andReturn(@[@"iconArchiveFile"]);
                        OCMStub([mockFileManager fileExistsAtPath:[OCMArg any]]).andReturn(YES);

                        [testManager retrieveImageForRequest:newIconRequest withCompletionHandler:^(UIImage * _Nullable image, NSError * _Nullable error) {
                            resultImage = image;
                            resultError = error;
                        }];
                    });

                    it(@"it should return downloaded image and no error", ^{
                        expect(resultImage).to(equal(testImage));
                        expect(resultError).to(beNil());
                    });
                });
            });
        });
    });
});

QuickSpecEnd