ios - Testing correct drag of UIView in Rubymotion -
i want write spec uiview use. uiview should dragged location test wether calls wright delegate methods. i'm using "drag" method. drag command not seem to dragging. perhaps i'm using in wrong way? used macbacon spec inspiration.
here code:
describe "draggableview" tests specdragviewcontroller before @drag = mydragableview.alloc.initwithframe [[10,200],[100,100]] @drag.backgroundcolor = uicolor.redcolor controller.view.addsubview @drag end "should not accept wrong drop" @drag.frame.should != nil @drag.accessibilitylabel = "drag view" delegate = object.new delegate.mock!(:draggableviewhasbeendroppedatwronglocation) {|view| view.should == @drag } p @drag.center drag "drag view", :from => cgpointmake(15,250), :to => cgpointmake(300,300) wait 10{} # long wait, play view in simulator p @drag.frame end end class specdragviewcontroller < uiviewcontroller end class mydragableview < uiview attr_accessor :destination_view attr_accessor :delegate def initwithframe (rect) if super addbehaviour end self end private def addbehaviour @pangesture = uipangesturerecognizer.alloc.initwithtarget(self, action:'draggesture:'); self.addgesturerecognizer(@pangesture) self.userinteractionenabled=true end def draggesture(pangesture) translation = pangesture.translationinview(self.superview) case pangesture.state when uigesturerecognizerstatebegan @originalcenter = self.center delegate.draggableviewstarteddragging(self) if delegate when uigesturerecognizerstatechanged self.center = cgpointmake(self.center.x + translation.x, self.center.y + translation.y) when uigesturerecognizerstateended if (@destination_view && cgrectcontainspoint(@destination_view.frame, self.center) && delegate.draggableviewcanbedropped?(self)) self.removegesturerecognizer(@pangesture) delegate.draggableviewhasbeendropped(self) else delegate.draggableviewhasbeendroppedatwronglocation(self) end end pangesture.settranslation(cgpointzero, inview:self.superview) end end
i still interested in real solution, working around problem. perhaps interesting other people running same problem.
i decided fake uigestures , send them delegate functions of draggableview. in way can still test correct working of function, without drag of simulator. have stubbed 3 gestures: start gesture, change gesture , end gesture.
before @drag = dragableview.alloc.initwithframe [[10,200],[100,100]] @drag.backgroundcolor = uicolor.redcolor @start_gesture = object.new @start_gesture.stub!(:settranslation) {|v1,v2|} @start_gesture.stub!(:state, :return=> uigesturerecognizerstatebegan) @start_gesture.stub!("translationinview") {|v| [0,0]} @end_gesture = object.new @end_gesture.stub!(:state, :return=> uigesturerecognizerstateended) @end_gesture.stub!("translationinview") {|v| [100,100]} @end_gesture.stub!(:settranslation) {|v1,v2|} controller.view.addsubview @drag end "should not accept wrong drop" @wrong_gesture = object.new @wrong_gesture.stub!(:state, :return=> uigesturerecognizerstatechanged) @wrong_gesture.stub!("translationinview") {|v| cgpointmake(100,100)} @wrong_gesture.stub!(:settranslation) {|v1,v2|} delegate = object.new delegate.mock! (:draggableviewstarteddragging) {|view|} delegate.mock!(:draggableviewhasbeendroppedatwronglocation) {|view| view.should == @drag } @drag.delegate = delegate @drag.send :draggesture, @start_gesture @drag.send :draggesture, @wrong_gesture @drag.send :draggesture, @end_gesture cgrectequaltorect(@drag.frame, [[10,200], [100,100]]).should.be.true end
Comments
Post a Comment