summaryrefslogtreecommitdiff
path: root/platform/darwin/test/MGLResourceTests.mm
blob: cf6a997ceae1781a8823c56f9eb45514cc806de8 (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
#import <Mapbox/Mapbox.h>
#import <XCTest/XCTest.h>
#import <mbgl/storage/resource.hpp>

namespace mbgl {
    extern NSURL *resourceURLWithAccountType(const Resource& resource, NSInteger accountType);
}
    
@interface MGLResourceTests : XCTestCase
@end

@implementation MGLResourceTests

- (void)testOfflineQueryParameterIsAddedForOfflineResource {
    
    using namespace mbgl;
    
    std::string testURL = "test://mapbox.com/testing_offline_query?a=one&b=two";
    
    // Is our test URL "correct" for subsequent checks?
    {
        NSURL *url = [NSURL URLWithString:@(testURL.c_str())];
        NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
        NSArray<NSURLQueryItem *> *items = components.queryItems;
        XCTAssert(items.count == 2);
    }

    Resource resource(Resource::Kind::Unknown, testURL);

    // By default, resources are NOT offline
    {
        bool skuTokenQueryItemFound;
        NSURL *url = resourceURLWithAccountType(resource, 0);
        NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
        for (NSURLQueryItem *item in components.queryItems) {
            XCTAssertFalse([item.name isEqualToString:@"offline"]);
            if ([item.name isEqualToString:@"sku"]) {
                skuTokenQueryItemFound = YES;
            }
        }

#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
        XCTAssertTrue(skuTokenQueryItemFound, "Default resource URL should have SKU token query item");
#else
        XCTAssertFalse(skuTokenQueryItemFound, "Non-iOS platforms should not have a SKU token query item");
#endif
    }
    
    // Now check offline
    resource.setUsage(Resource::Usage::Offline);
    
    {
        NSURL *url = resourceURLWithAccountType(resource, 0);
        NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
        
        // For offline, we expect a single offline query item
        NSInteger foundCount = 0;
        
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
        for (NSURLQueryItem *item in components.queryItems) {
            if (([item.name isEqualToString:@"offline"] && [item.value isEqualToString:@"true"]) ||
                ([item.name isEqualToString:@"a"] && [item.value isEqualToString:@"one"]) ||
                ([item.name isEqualToString:@"b"] && [item.value isEqualToString:@"two"]) || [item.name isEqualToString:@"sku"]) {
                foundCount++;
            }
        }

        XCTAssert(foundCount == 4);
#else
        // NOTE: Currently the macOS SDK does not supply the sku or offline query parameters
        for (NSURLQueryItem *item in components.queryItems) {
            if (([item.name isEqualToString:@"a"] && [item.value isEqualToString:@"one"]) ||
                ([item.name isEqualToString:@"b"] && [item.value isEqualToString:@"two"])) {
                foundCount++;
            }
            XCTAssertFalse([item.name isEqualToString:@"sku"]);
        }
        
        XCTAssert(foundCount == 2);
#endif
    }
}

@end