2014-04-24 3 views
0

내가 사용하는 UIView에 대한 스펙을 작성하고 싶습니다. UIView는 특정 위치로 드래그하여 Wright 대리자 메서드를 호출하도록 테스트해야합니다. "끌기"방법을 사용하고 있습니다. 그러나 드래그 명령은 드래그하는 것으로 보이지 않습니다. 아마도 나는 그것을 잘못된 방식으로 사용하고 있는가? 영감을 얻으려고 macbacon spec을 사용했습니다. 여기 Rubymotion에서 UIView의 올바른 드래그 테스트하기

코드입니다 :

describe "DraggableView" do 
    tests SpecDragViewController 

    before do 
    @drag = MyDragableView.alloc.initWithFrame [[10,200],[100,100]] 
    @drag.backgroundColor = UIColor.redColor 

    controller.view.addSubview @drag 
    end 

    it "should not accept wrong drop" do 
    @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, just so I could play with the view in the 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 

답변

0

내가 진짜 솔루션에 관심이 여전히,하지만 나는 지금 내가 문제를 해결하고 있어요. 아마 같은 문제를 겪고있는 다른 사람들에게도 흥미로울 것입니다.

나는 UIGesture를 위조하여 DraggableView의 위임 기능에 보내기로 결정했습니다. 이 방법으로 시뮬레이터를 끌지 않고도 함수의 올바른 작동을 테스트 할 수 있습니다. 나는 시작 제스처, 변경 제스처 및 종료 제스처와 같은 세 가지 제스처를 내미는 것을했습니다.

before do 
    @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 
    it "should not accept wrong drop" do 

    @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