kiwi-bdd / Kiwi

Simple BDD for iOS
BSD 3-Clause "New" or "Revised" License
4.14k stars 512 forks source link

How to test cadisplaylink #710

Open jeremyzj opened 6 years ago

jeremyzj commented 6 years ago

HI, friends

//
//  PPSFPSLabel.m
//  rninit
//
//  Created by 魔笛 on 2018/7/30.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "PPSFPSLabel.h"
#import "PPSWeakProxy.h"

static const NSInteger kFPSLabelHeight = 30.f;

@interface PPSFPSLabel() {
  NSUInteger _count;
  NSTimeInterval _lastTime;
  UIFont *_font;
  UIFont *_subFont;
}

@end

@implementation PPSFPSLabel

+ (id)fpsLabel {
    UIScreen *mainScreen = [UIScreen mainScreen];
    PPSFPSLabel *fpsLabel = [[PPSFPSLabel alloc] initWithFrame:CGRectMake(0, mainScreen.bounds.size.height - kFPSLabelHeight, 100 , kFPSLabelHeight)];
    return fpsLabel;
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        self.layer.cornerRadius = 5;
        self.clipsToBounds = YES;
        self.textAlignment = NSTextAlignmentCenter;
        self.userInteractionEnabled = NO;
        self.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.700];

        _font = [UIFont fontWithName:@"Courier" size:14];
        _subFont = [UIFont fontWithName:@"Courier" size:4];
        [self addTimer];
    }

    return self;
}

- (void)addTimer {
    self.fpsLink = [CADisplayLink displayLinkWithTarget:[PPSWeakProxy proxyWithTarget:self] selector:@selector(tick:)];
    [self.fpsLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)dealloc {
  [self.fpsLink invalidate];
  NSLog(@"timer release");
}

- (void)tick:(CADisplayLink *)link {
    NSLog(@"tick: %f", _lastTime);
  if (_lastTime == 0) {
    _lastTime = link.timestamp;
    return;
  }

  _count++;
  NSTimeInterval delta = link.timestamp - _lastTime;
  if (delta < 1) return;
  _lastTime = link.timestamp;
  float fps = _count / delta;
  _count = 0;

  CGFloat progress = fps / 60.0;
  UIColor *color = [UIColor colorWithHue:0.27 * (progress - 0.2) saturation:1 brightness:0.9 alpha:1];

  NSString *text1 = [NSString stringWithFormat:@"%d FPS",(int)round(fps)];
  NSLog(@"%@", text1);

  NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d FPS",(int)round(fps)]];
  [text addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, text.length - 3)];
  [text addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(text.length - 3, 3)];
  [text addAttribute:NSFontAttributeName value:_font range:NSMakeRange(0, text.length)];
  [text addAttribute:NSFontAttributeName value:_subFont range:NSMakeRange(text.length - 4, 1)];
  self.attributedText = text;
}

@end

How to write unit test for CADisplayLink.

I write the kiwi is

describe(@"PPSFPSLabel", ^{
    let(fpsLabel, ^id{
        return [PPSFPSLabel fpsLabel];
    });

    it(@"layout on the left-bottom screen", ^{
        UIScreen *mainScreen = [UIScreen mainScreen];
        [[theValue([fpsLabel frame]) should] equal:theValue(CGRectMake(0, mainScreen.bounds.size.height - 30, 100, 30))];
    });

    context(@"should add a timer", ^{
        PPSFPSLabel *label = [PPSFPSLabel fpsLabel];
        [[label should] receive:@selector(addTimer)];
        [[label.fpsLink shouldNot] beNil];
    });

    it(@"time target", ^{
        PPSFPSLabel *label = [PPSFPSLabel fpsLabel];

        while(true) {

        }
    });
});

But the function tick cannot code coverage