Skip to content

Commit

Permalink
feat(ios): add unitest for FontLoaderModule
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyunong committed Oct 24, 2024
1 parent 3b67eac commit c13fda7
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default class LoadFontExample extends React.Component {
fillExample() {
this.setState({ inputFontFamily: 'HYHuaXianZi J' });
this.setState({ fontUrl: 'https://zf.sc.chinaz.com/Files/DownLoad/upload/2024/1009/hanyihuaxianzijianti.ttf' });
// this.setState({ fontUrl: 'hpfile://./assets/hanyihuaxianzijianti.ttf' }
}

async loadFont() {
Expand Down
11 changes: 10 additions & 1 deletion framework/ios/module/fontLoader/HippyFontLoaderModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ typedef NS_ENUM(NSInteger, HippyFontUrlState) {
*
* @param fontFamily - The font family needs to be registered
*/
+ (void)registerFontIfNeeded:(NSString *)fontFamily;
+ (BOOL)registerFontIfNeeded:(NSString *)fontFamily;

/**
* Whether the font is downloading from the url.
Expand All @@ -65,6 +65,15 @@ typedef NS_ENUM(NSInteger, HippyFontUrlState) {
*/
+ (dispatch_queue_t) getFontSerialQueue;

/**
* Load font from the url.
*
* @param urlString - The url where font file is downloaded.
* @param resolve - The callback block for downloading successful.
* @param reject - The callback block for downloading failed.
*/
- (void) load:(NSString *)fontFamily from:(NSString *)urlString resolver:(nullable HippyPromiseResolveBlock)resolve rejecter:(nullable HippyPromiseRejectBlock)reject;

@end

NS_ASSUME_NONNULL_END
5 changes: 3 additions & 2 deletions framework/ios/module/fontLoader/HippyFontLoaderModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
static dispatch_queue_t serialQueue;
static NSMutableDictionary *urlToFilePath;
static NSMutableDictionary *fontFamilyToFiles;
static NSMutableDictionary *urlLoadState;
static NSMutableDictionary *urlLoadState = [NSMutableDictionary dictionary];
static NSMutableArray *fontRegistered = [NSMutableArray array];
static NSString *fontDirPath;
static NSString *fontUrlSavePath;
Expand Down Expand Up @@ -122,7 +122,7 @@ + (NSString *)getFontPath:(NSString *)url {
return fontFilePath;
}

+ (void)registerFontIfNeeded:(NSString *)fontFamily {
+ (BOOL)registerFontIfNeeded:(NSString *)fontFamily {
[self initDictIfNeeded];
NSMutableArray *fontFiles = [fontFamilyToFiles objectForKey:fontFamily];
BOOL isFontRegistered = NO;
Expand All @@ -149,6 +149,7 @@ + (void)registerFontIfNeeded:(NSString *)fontFamily {
[[NSNotificationCenter defaultCenter] postNotificationName:HippyFontChangeTriggerNotification object:nil];
}
}
return isFontRegistered;
}


Expand Down
119 changes: 119 additions & 0 deletions tests/ios/HippyFontLoaderTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*!
* iOS SDK
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


#import <XCTest/XCTest.h>
#import <hippy/HippyFontLoaderModule.h>
#import <hippy/HippyBridge.h>

@interface HippyFontLoaderTest : XCTestCase

@property (nonatomic, strong) HippyFontLoaderModule *fontLoader;

@end

@implementation HippyFontLoaderTest

- (void)setUp {
// Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

- (void)testHippyFontLoaderModule {
NSString* invalidURL = @"https://example.url";
// set arbitrary valid font file url
NSString* validURL = @"https://zf.sc.chinaz.com/Files/DownLoad/upload/2024/1009/hanyihuaxianzijianti.ttf";
NSString* fontFamily = @"HYHuaXianZi J";
HippyBridge *bridge = [[HippyBridge alloc] initWithDelegate:nil moduleProvider:nil launchOptions:nil executorKey:nil];
HippyFontLoaderModule *fontLoader = [[HippyFontLoaderModule alloc] init];
[fontLoader setValue:bridge forKey:@"bridge"];

// test fetch from invalidURL
XCTestExpectation *invalidURLExpectation = [self expectationWithDescription:@"Fetch data from invalid url expectation"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),
[HippyFontLoaderModule getFontSerialQueue], ^{
[fontLoader load:fontFamily from:invalidURL resolver:^(id result) {
[invalidURLExpectation fulfill];
} rejecter:^(NSString *code, NSString *message, NSError *error) {
// test whether the url is loading
XCTAssertTrue([HippyFontLoaderModule isUrlLoading:invalidURL]);
XCTAssertEqual(message, @"font request error");
[invalidURLExpectation fulfill];
}];
});
[self waitForExpectationsWithTimeout:5 handler:nil];

// test fetch from validURL
XCTestExpectation *validURLExpectation = [self expectationWithDescription:@"Fetch data from valid url expectation"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),
[HippyFontLoaderModule getFontSerialQueue], ^{
[fontLoader load:fontFamily from:validURL resolver:^(id result) {
// test whether the url is loading
XCTAssertTrue([HippyFontLoaderModule isUrlLoading:validURL]);
[validURLExpectation fulfill];
} rejecter:^(NSString *code, NSString *message, NSError *error) {
XCTAssert(true, @"fetch valid url failed");
[validURLExpectation fulfill];
}];
});
[self waitForExpectationsWithTimeout:5 handler:nil];

__block NSString *fontPath;
// test get font path using undownloaded font
XCTestExpectation *undownloadedExpectation = [self expectationWithDescription:@"get undownloaded font path expectation"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),
[HippyFontLoaderModule getFontSerialQueue], ^{
fontPath = [HippyFontLoaderModule getFontPath:invalidURL];
XCTAssertNil(fontPath);
[undownloadedExpectation fulfill];
});
[self waitForExpectationsWithTimeout:5 handler:nil];

// test get font path using downloaded font
XCTestExpectation *downloadedExpectation = [self expectationWithDescription:@"get downloaded font path expectation"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),
[HippyFontLoaderModule getFontSerialQueue], ^{
fontPath = [HippyFontLoaderModule getFontPath:validURL];
XCTAssertNotNil(fontPath);
[downloadedExpectation fulfill];
});
[self waitForExpectationsWithTimeout:5 handler:nil];

// test whether font registered successfully in load method
BOOL needRegister = [HippyFontLoaderModule registerFontIfNeeded:fontFamily];
XCTAssertFalse(needRegister);

// delete font directory
[[NSFileManager defaultManager] removeItemAtPath:[fontPath stringByDeletingLastPathComponent] error:nil];
}

- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
}

@end

0 comments on commit c13fda7

Please sign in to comment.