1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id$
29   */
30  
31  package palmed.edit.selection;
32  
33  import palmed.edit.selection.ISelectionView;
34  import palmed.edit.selection.Selection;
35  import palmed.edit.text.IText;
36  import palmed.edit.util.Coordinate;
37  import palmed.util.EasyMockTestCase;
38  
39  /***
40   * Selection test case.
41   *
42   * @author Mathieu Champlon
43   * @version $Revision$ $Date$
44   */
45  public class SelectionTest extends EasyMockTestCase
46  {
47      /***
48       * Tested object.
49       */
50      private Selection selection;
51      /***
52       * Mock objects.
53       */
54      private IText mockText;
55      private ISelectionView mockView;
56  
57      protected void setUp()
58      {
59          mockText = (IText)createMock( IText.class );
60          mockView = (ISelectionView)createMock( ISelectionView.class );
61          selection = new Selection( mockText );
62          selection.register( mockView );
63      }
64  
65      public void testSelectionIsCreatedEmpty()
66      {
67          assertTrue( selection.isEmpty() );
68      }
69  
70      public void testStepForwardOnEmptyTextContentIsNoOp()
71      {
72          mockText.getLine( 0 );
73          control( mockText ).setReturnValue( null );
74          mockText.getHeight();
75          control( mockText ).setReturnValue( 0 );
76          replay();
77          selection.forward();
78          assertTrue( selection.isEmpty() );
79      }
80  
81      public void testStepBackwardOnEmptyTextContentIsNoOp()
82      {
83          replay();
84          selection.backward();
85          assertTrue( selection.isEmpty() );
86      }
87  
88      public void testStepForwardAndBackwardResultsInEmptySelection()
89      {
90          mockText.getLine( 0 );
91          control( mockText ).setReturnValue( "this is my test" );
92          mockView.update( new Coordinate( 1, 0 ) );
93          mockView.update( new Coordinate( 0, 0 ) );
94          replay();
95          selection.forward();
96          selection.backward();
97          assertTrue( selection.isEmpty() );
98      }
99  
100     public void testStepForwardSelectsFirstCharacter()
101     {
102         mockText.getLine( 0 );
103         control( mockText ).setReturnValue( "this is my test" );
104         mockView.update( new Coordinate( 1, 0 ) );
105         replay();
106         selection.forward();
107         assertFalse( selection.isEmpty() );
108     }
109 
110     public void testSetPositionSelectsText()
111     {
112         mockText.getHeight();
113         control( mockText ).setReturnValue( 1 );
114         mockText.getLine( 0 );
115         control( mockText ).setReturnValue( "this is my test" );
116         mockView.update( new Coordinate( 3, 0 ) );
117         replay();
118         selection.extend( 3, 0 );
119         assertFalse( selection.isEmpty() );
120     }
121 
122     public void testSetPositionSelectsMultipleTextLines()
123     {
124         mockText.getHeight();
125         control( mockText ).setReturnValue( 7 );
126         mockText.getLine( 5 );
127         control( mockText ).setReturnValue( "this is my test" );
128         mockView.update( new Coordinate( 3, 5 ) );
129         replay();
130         selection.extend( 3, 5 );
131         assertFalse( selection.isEmpty() );
132     }
133 
134     public void testResetEmptySelectionStaysEmpty()
135     {
136         mockView.update( new Coordinate( 0, 0 ) );
137         replay();
138         selection.clear();
139         assertTrue( selection.isEmpty() );
140     }
141 
142     public void testResetMultipleTextLinesSelection()
143     {
144         mockText.getHeight();
145         control( mockText ).setReturnValue( 7 );
146         mockText.getLine( 5 );
147         control( mockText ).setReturnValue( "this is my test" );
148         mockView.update( new Coordinate( 3, 5 ) );
149         replay();
150         selection.extend( 3, 5 );
151         reset();
152         mockView.update( new Coordinate( 3, 5 ) );
153         replay();
154         selection.clear();
155         assertTrue( selection.isEmpty() );
156     }
157 
158     public void testDeleteClearsSelection()
159     {
160         mockText.getHeight();
161         control( mockText ).setReturnValue( 7 );
162         mockText.getLine( 5 );
163         control( mockText ).setReturnValue( "this is my test" );
164         mockView.update( new Coordinate( 3, 5 ) );
165         replay();
166         selection.extend( 3, 5 );
167         reset();
168         mockText.remove( new Coordinate( 0, 0 ), new Coordinate( 3, 5 ) );
169         mockView.update( new Coordinate( 0, 0 ) );
170         replay();
171         selection.delete();
172         assertTrue( selection.isEmpty() );
173     }
174 
175     public void testDeleteBackwardSelectionClearsSelection()
176     {
177         mockText.getHeight();
178         control( mockText ).setReturnValue( 7 );
179         mockText.getLine( 5 );
180         control( mockText ).setReturnValue( "this is my test" );
181         mockView.update( new Coordinate( 3, 5 ) );
182         mockView.update( new Coordinate( 3, 5 ) );
183         replay();
184         selection.extend( 3, 5 );
185         selection.clear();
186         reset();
187         mockText.getHeight();
188         control( mockText ).setReturnValue( 7 );
189         mockText.getLine( 0 );
190         control( mockText ).setReturnValue( "this is my test" );
191         mockView.update( new Coordinate( 0, 0 ) );
192         replay();
193         selection.extend( 0, 0 );
194         reset();
195         mockText.remove( new Coordinate( 0, 0 ), new Coordinate( 3, 5 ) );
196         mockView.update( new Coordinate( 0, 0 ) );
197         replay();
198         selection.delete();
199         assertTrue( selection.isEmpty() );
200     }
201 }