Add path specification for movement animations.
[kaya.git] / test / test_animations.rb
blob4f166e9484982aba9e95c912c7145c57dc61c7e9
1 require 'test/unit'
2 require 'animations'
3 require 'board/point_converter'
4 require 'helpers/animation_test_helper'
5 require 'helpers/stubs'
6 require 'rubygems'
8 class FakeAnimator
9   include Animations
10   
11   class Board
12     include PointConverter
13     def unit
14       Qt::Point.new(50, 50)
15     end
16     
17     def flipped?
18       false
19     end
20   end
21   
22   attr_reader :board
23   def initialize
24     @board = Board.new
25   end
26 end
28 class TestAnimations < Test::Unit::TestCase
29   def setup
30     @field = FakeAnimationField.new
31     @c = FakeAnimator.new
32   end
33   
34   def test_disappear
35     item = GeneralMock.new
36     @field.run @c.disappear(item)
37     @field.run_test
38     
39     assert_equal [:opacity=, [1.0]], item.calls.shift
40     assert_equal [:visible=, [true]], item.calls.shift
41     
42     old_op = 1.0
43     while old_op > 0.0
44       method, args = item.calls.shift
45       break unless method == :opacity=
46       assert_operator args.first, :<=, old_op
47       old_op = args.first
48     end
49     
50     assert_equal [:remove, []], item.calls.shift
51     assert_equal [], item.calls
52   end
53   
54   def test_appear
55     item = GeneralMock.new
56     @field.run @c.appear(item)
57     @field.run_test
58     
59     assert_equal [:opacity=, [0.0]], item.calls.shift
60     assert_equal [:visible=, [true]], item.calls.shift
61     
62     old_op = 0.0
63     while true
64       method, args = item.calls.shift
65       break unless method == :opacity=
66       assert_operator args.first, :>=, old_op
67       old_op = args.first
68     end
69     
70     assert_equal [], item.calls
71   end
72   
73   def test_movement
74     item = GeneralMock.new
75     @field.run @c.movement(item, Point.new(3, 4), Point.new(5, 6), Path::Linear)
76     @field.run_test
77     
78     old_p = nil
79     while not item.calls.empty?
80       method, args = item.calls.shift
81       assert_equal :pos=, method
82       p = args.first
83       assert_not_nil p
84       if old_p
85         assert_operator old_p.x, :<=, p.x
86         assert_operator old_p.y, :<=, p.y
87         delta = p - old_p
88         assert_in_delta 1.0, (delta.y.to_f / delta.x), 1e-5 if delta.x.abs >= 1e-5
89       end
90       old_p = p
91     end
92   end
93 end