1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package ifc.sdb;
29 
30 import lib.MultiMethodTest;
31 import lib.Status;
32 import lib.StatusException;
33 
34 import com.sun.star.lang.EventObject;
35 import com.sun.star.sdb.RowChangeEvent;
36 import com.sun.star.sdb.XRowSetApproveBroadcaster;
37 import com.sun.star.sdb.XRowSetApproveListener;
38 
39 /**
40 * <code>com.sun.star.sdb.XRowSetApproveBroadcaster</code> interface test. <p>
41 * Required object relations :
42 * <ul>
43 * <li> <code>'XRowSetApproveBroadcaster.ApproveChecker'</code>:
44 *      implementation of inner interface <code>RowSetApproveChecker</code>
45 *      which can move cursor within a rowset, change row, and change the
46 *      whole rowset. </li>
47 * </ul> <p>
48 * It is better to recreate the object after test, because of unknown
49 * actions made by <code>RowSetApproveChecker</code> interface implementation.
50 *
51 * @see com.sun.star.sdb.XRowSetApproveBroadcaster
52 * @see _XRowSetApproveBroadcaster.RowSetApproveChecker
53 */
54 public class _XRowSetApproveBroadcaster extends MultiMethodTest {
55 
56     // oObj filled by MultiMethodTest
57     public XRowSetApproveBroadcaster oObj = null ;
58 
59     /**
60     * The purpose of this interface is to pass to this test
61     * relation which can make some operations with row set
62     * on which <code>XRowSetApproveListener</code>s can react.
63     * @see com.sun.star.sdb.XRowSetApproveListener
64     */
65     public static interface RowSetApproveChecker {
66         /**
67         * Moves cursor within row set. Method <code>approveCursorMove</code>
68         * of <code>XRowSetApproveListener</code> must be called.
69         */
70         public void moveCursor() ;
71         /**
72         * Change rows in row set. Method <code>approveRowChange</code>
73         * of <code>XRowSetApproveListener</code> must be called.
74         * @return <code>RowChangeEvent</code> structure which contains
75         * what type of change was made and how many rows it affected.
76         * @see com.sun.star.sdb.RowChangeEvent
77         */
78         public RowChangeEvent changeRow() ;
79         /**
80         * Change the whole row set. Method <code>approveRowSetChange</code>
81         * of <code>XRowSetApproveListener</code> must be called.
82         */
83         public void changeRowSet() ;
84     }
85 
86     /**
87     * Implementation of <code>XRowSetApproveListener</code> interface
88     * which just detects and stores approve requipements. They are checked
89     * later.
90     */
91     private class TestListener implements XRowSetApproveListener {
92         public boolean approveRequests = true ;
93         public boolean approveCursorMoveCalled = false ;
94         public boolean approveRowChangeCalled = false ;
95         public RowChangeEvent approveRowChangeEvent = null ;
96         public boolean approveRowSetChangeCalled = false ;
97 
98         public TestListener(boolean approve) {
99             approveRequests = approve ;
100         }
101 
102         public void reset() {
103             approveCursorMoveCalled = false ;
104             approveRowChangeCalled = false ;
105             approveRowSetChangeCalled = false ;
106         }
107         public boolean approveCursorMove(EventObject ev) {
108             approveCursorMoveCalled = true ;
109             return approveRequests ;
110         }
111         public boolean approveRowChange(RowChangeEvent ev) {
112             approveRowChangeCalled = true ;
113             approveRowChangeEvent = ev ;
114             return approveRequests ;
115         }
116         public boolean approveRowSetChange(EventObject ev) {
117             approveRowSetChangeCalled = true ;
118             return approveRequests ;
119         }
120         public void disposing(EventObject ev) {}
121     }
122     private TestListener listener1 = null ;
123 
124     private RowSetApproveChecker checker = null ;
125 
126     /**
127     * Tries to retrieve object relation.
128     */
129     public void before() {
130         checker = (RowSetApproveChecker) tEnv.getObjRelation
131             ("XRowSetApproveBroadcaster.ApproveChecker") ;
132 
133         if (checker == null) {
134             log.println("!!! Relation for test not found !!!") ;
135             throw new StatusException(Status.failed
136                 ("!!! Relation for test not found !!!")) ;
137         }
138     }
139 
140     /**
141     * Creates and adds listener, then call <code>RowSetApproveChecker</code>
142     * methods for listener methods to be called. Then checks if
143     * listener methods were called on appropriate actions. <p>
144     * Has OK status : If and only if appropriate listener methods called,
145     * and listener <code>approveRowChange</code> method has write parameter,
146     * i.e. type and rows number expected.
147     */
148     public void _addRowSetApproveListener() {
149         listener1 = new TestListener(true) ;
150         oObj.addRowSetApproveListener(listener1) ;
151         log.println("Listener added.") ;
152 
153         boolean result = true ;
154 
155         checker.moveCursor() ;
156         log.println("Cursor moved.") ;
157         result &= listener1.approveCursorMoveCalled ;
158 
159         listener1.reset() ;
160         RowChangeEvent actualEvent = checker.changeRow() ;
161         log.println("Row changed.") ;
162 
163         RowChangeEvent event = listener1.approveRowChangeEvent ;
164         result &= listener1.approveRowChangeCalled ;
165 
166         boolean eventOK = event.Action == actualEvent.Action &&
167                           event.Rows == actualEvent.Rows ;
168 
169         result &= eventOK ;
170 
171         listener1.reset() ;
172         checker.changeRowSet();
173         log.println("Row set changed.") ;
174         result &= listener1.approveRowSetChangeCalled ;
175 
176         tRes.tested("addRowSetApproveListener()", result) ;
177     }
178 
179     /**
180     * Removes listener inserted before, then perform all actions
181     * on which listener must react. <p>
182     * Has OK status if no listener methods were called. <p>
183     * Methods required to pass before :
184     * <ul>
185     * <li> <code>_addRowSetApproveListener</code> </li>
186     * </ul>
187     */
188     public void _removeRowSetApproveListener() {
189         requiredMethod("addRowSetApproveListener()") ;
190 
191         listener1.reset() ;
192 
193         oObj.removeRowSetApproveListener(listener1) ;
194 
195         checker.moveCursor() ;
196         checker.changeRow() ;
197         checker.changeRowSet() ;
198 
199         tRes.tested("removeRowSetApproveListener()",
200             !listener1.approveCursorMoveCalled &&
201             !listener1.approveRowChangeCalled &&
202             !listener1.approveRowSetChangeCalled) ;
203     }
204 
205     /**
206     * Disposes object environment.
207     */
208     public void after() {
209         disposeEnvironment() ;
210     }
211 
212 }  // finish class _XRowSetApproveBroadcaster
213 
214 
215