ios - How can animation be unit tested? -
modern user interfaces, macos , ios, have lots of “casual” animation -- views appear through brief animated sequences largely orchestrated system.
[[mynewview animator] setframe: rect]
occasionally, might have more elaborate animation, animation grouping , completion block.
now, can imagine bug reports this:
hey -- nice animation when mynewview appears isn't happening in new release!
so, we'd want unit tests simple things:
confirm animation happens check duration of animation check frame rate of animationbut of course of study these tests have simple write , mustn't create code worse; don’t want spoil simplicity of implicit animations ton of test-driven complexity!
so, what tdd-friendly approach implementing tests casual animations?
justifications unit testinglet's take concrete illustration illustrate why we'd want unit test. let's have view contains bunch of widgetviews. when user makes new widget double-clicking, it’s supposed appear tiny , transparent, expanding total size during animation.
now, it's true don't want need unit test scheme behavior. here things might go wrong because fouled things up:
the animation called on wrong thread, , doesn't drawn. in course of study of animation, phone call setneedsdisplay, widget gets drawn.
we're recycling disused widgets pool of discarded widgetcontrollers. new widgetviews transparent, views in recycle pool still opaque. fade doesn't happen.
some additional animation starts on new widget before animation finishes. result, widget begins appear, , starts jerking , flashing briefly before settles down.
you made alter widget's drawrect: method, , new drawrect slow. old animation fine, it's mess.
all of these going show in back upwards log as, "the create-widget animation isn't working anymore." , experience has been that, 1 time used animation, it’s hard developer notice right away unrelated alter has broken animation. that's recipe unit testing, right?
the animation called on wrong thread, , doesn't drawn. in course of study of animation, phone call setneedsdisplay, widget gets drawn.
don't unit test directly. instead utilize assertions and/or raise exceptions when animation on wrong thread. unit test assertion raise exception appropriately. apple aggressively frameworks. keeps shooting in foot. , know when using object outside of valid parameters.
we're recycling disused widgets pool of discarded widgetcontrollers. new widgetviews transparent, views in recycle pool still opaque. fade doesn't happen.
this why see methods dequeuereusablecellwithidentifier
in uitableview. need public method reused widgetview chance test properties alpha reset appropriately.
some additional animation starts on new widget before animation finishes. result, widget begins appear, , starts jerking , flashing briefly before settles down.
same number 1. utilize assertions impose rules on code. unit test assertions can triggered.
you made alter widget's drawrect: method, , new drawrect slow. old animation fine, it's mess.
a unit test can timing method. calculations ensure remain within reasonable time limit.
-(void)testanimationtime { nsdate * start = [nsdate date]; nsview * view = [[nsview alloc]init]; (int = 0; < 10; i++) { [view display]; } nstimeinterval timespent = [start timeintervalsincenow] * -1.0; if (timespent > 1.5) { stfail(@"view took %f seconds calculate 10 times", timespent); } }
ios cocoa design-patterns animation tdd
No comments:
Post a Comment