summaryrefslogtreecommitdiff
path: root/render-test/ios/iosTestRunner.mm
blob: 4c3f7b87f135bb269da0e60a3e27a50f2552030e (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
#import "iosTestRunner.h"

#include "ios_test_runner.hpp"

#import "SSZipArchive.h"

#include <string>

@interface IosTestRunner ()

@property (nullable) TestRunner* runner;

@property (copy, nullable) NSString *styleResultPath;

@property (copy, nullable) NSString *metricResultPath;

@property (copy, nullable) NSString *metricPath;

@property BOOL testStatus;

@end

@implementation IosTestRunner

-(instancetype)init
{
    self = [super init];
    if (self) {
        self.testStatus = NO;
        self.runner = new TestRunner();
        NSString *path = nil;
        NSError *error;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
        NSArray *bundleContents = [fileManager contentsOfDirectoryAtPath: bundleRoot error: &error];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [paths objectAtIndex: 0];
        
        for (uint32_t i = 0; i < bundleContents.count; i++) {
            NSString *dirName = [bundleContents objectAtIndex: i];
            if ([dirName isEqualToString:@"test-data"]) {
                NSString *destinationPath = [documentsDir stringByAppendingPathComponent: dirName];
                //Using NSFileManager we can perform many file system operations.
                BOOL success = [fileManager fileExistsAtPath: destinationPath];
                if (success) {
                    success = [fileManager removeItemAtPath:destinationPath error:NULL];
                }
                break;
            }
        }
        
        for (uint32_t i = 0; i < bundleContents.count; i++) {
            NSString *dirName = [bundleContents objectAtIndex: i];
            if ([dirName isEqualToString:@"test-data"]) {
                NSString *destinationPath = [documentsDir stringByAppendingPathComponent: dirName];
                BOOL success = [fileManager fileExistsAtPath: destinationPath];
                if (!success) {
                    NSString *copyDirPath  = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: dirName];

                    success = [fileManager copyItemAtPath: copyDirPath toPath: destinationPath error: &error];
              
                    if (!success){
                        NSAssert1(0, @"Failed to copy file '%@'.", [error localizedDescription]);
                        NSLog(@"Failed to copy %@ file, error %@", dirName, [error localizedDescription]);
                    }
                    else {
                        path = destinationPath;
                        NSLog(@"File copied %@ OK", dirName);
                    }
                }
                else {
                    NSLog(@"File exits %@, skip copy", dirName);
                }
                break;
            }
        }
        if (path) {

            std::string basePath = std::string([path UTF8String]);
            self.testStatus = self.runner->startTest(basePath) ? YES : NO;
            self.styleResultPath =  [path stringByAppendingPathComponent:@"/ios-render-test-runner-style.html"];
            self.metricResultPath =  [path stringByAppendingPathComponent:@"/ios-render-test-runner-metrics.html"];

            BOOL fileFound = [fileManager fileExistsAtPath: self.styleResultPath];
            if (fileFound == NO) {
                NSLog(@"Style test result file '%@' doese not exit ", self.styleResultPath);
                self.testStatus = NO;
            }

            fileFound = [fileManager fileExistsAtPath: self.metricResultPath];
            if (fileFound == NO) {
                NSLog(@"Metric test result file '%@' doese not exit ", self.metricResultPath);
                self.testStatus = NO;
            }

            NSString *rebaselinePath = [path stringByAppendingPathComponent:@"/baselines"];
            BOOL needArchiving = NO;
            BOOL isDir = NO;
            NSArray *subpaths = [[NSArray alloc] init];
            if ([fileManager fileExistsAtPath:rebaselinePath isDirectory: &isDir] && isDir){
                subpaths = [fileManager subpathsAtPath:rebaselinePath];
                for(NSString *path in subpaths)
                {
                    NSString *longPath = [rebaselinePath stringByAppendingPathComponent:path];
                    if([fileManager fileExistsAtPath:longPath isDirectory:&isDir] && !isDir)
                    {
                        needArchiving = YES;
                        break;
                    }
                }
            }
            else {
               NSLog(@"Metric path '%@' doese not exit ", rebaselinePath);
            }

            if (needArchiving) {
                NSString *archivePath = [path stringByAppendingString:@"/metrics.zip"];
                BOOL success = [SSZipArchive createZipFileAtPath:archivePath
                withContentsOfDirectory:rebaselinePath
                    keepParentDirectory:NO
                       compressionLevel:-1
                               password:nil
                                    AES:YES
                        progressHandler:nil];

                if (success) {
                    NSLog(@"Successfully archive all of the metrics into metrics.zip");
                    self.metricPath =  archivePath;
                }
                else {
                    NSLog(@"Failed to archive rebaselined metrics into metrics.zip");
                }
            }
        }

        delete self.runner;
        self.runner = nullptr;
    }
    return self;
}

- (NSString*) getStyleResultPath {
    return self.styleResultPath;
}

- (NSString*) getMetricResultPath {
    return self.metricResultPath;
}

- (NSString*) getMetricPath {
    return self.metricPath;
}

- (BOOL) getTestStatus {
    return self.testStatus;
}
@end