bump product version to 5.0.4.1
[LibreOffice.git] / vcl / qa / cppunit / dndtest.cxx
blob7e502faf2372b4f5f4e0fd64a167544471a06cbc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <unotest/filters-test.hxx>
21 #include <test/bootstrapfixture.hxx>
23 #include <vcl/event.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/wrkwin.hxx>
26 #include <vcl/msgbox.hxx>
27 #include <vcl/lstbox.hxx>
29 #include <cppuhelper/implbase1.hxx>
30 #include <cppuhelper/implbase3.hxx>
32 #include <com/sun/star/lang/XComponent.hpp>
33 #include <com/sun/star/datatransfer/XTransferable.hpp>
34 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
35 #include <com/sun/star/datatransfer/dnd/XDropTarget.hpp>
36 #include <com/sun/star/datatransfer/dnd/XDragSourceListener.hpp>
37 #include <com/sun/star/datatransfer/dnd/XDropTargetListener.hpp>
38 #include <com/sun/star/datatransfer/dnd/XDragGestureRecognizer.hpp>
39 #include <com/sun/star/datatransfer/dnd/XDragGestureListener.hpp>
41 using namespace ::com::sun::star::io;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::datatransfer;
45 using namespace ::com::sun::star::datatransfer::clipboard;
46 using namespace ::com::sun::star::datatransfer::dnd;
48 class MyWin : public WorkWindow
50 public:
51 MyWin( vcl::Window* pParent, WinBits nWinStyle );
53 void MouseMove( const MouseEvent& rMEvt );
54 void MouseButtonDown( const MouseEvent& rMEvt );
55 void MouseButtonUp( const MouseEvent& rMEvt );
56 void KeyInput( const KeyEvent& rKEvt );
57 void KeyUp( const KeyEvent& rKEvt );
58 void Paint( const Rectangle& rRect );
59 void Resize();
62 class MyDragAndDropListener: public ::cppu::WeakImplHelper3 < XDropTargetListener, XDragGestureListener, XDragSourceListener >
64 vcl::Window * m_pWindow;
66 public:
68 MyDragAndDropListener( vcl::Window * pWindow ) : m_pWindow( pWindow ) {};
70 virtual void SAL_CALL dragGestureRecognized( const DragGestureEvent& dge ) throw(RuntimeException);
71 virtual void SAL_CALL drop( const DropTargetDropEvent& dtde ) throw(RuntimeException);
72 virtual void SAL_CALL dragEnter( const DropTargetDragEnterEvent& dtde ) throw(RuntimeException);
73 virtual void SAL_CALL dragExit( const DropTargetEvent& dte ) throw(RuntimeException);
74 virtual void SAL_CALL dragOver( const DropTargetDragEvent& dtde ) throw(RuntimeException);
75 virtual void SAL_CALL dropActionChanged( const DropTargetDragEvent& dtde ) throw(RuntimeException);
76 virtual void SAL_CALL dragDropEnd( const DragSourceDropEvent& dsde ) throw(RuntimeException);
77 virtual void SAL_CALL dragEnter( const DragSourceDragEvent& dsdee ) throw(RuntimeException);
78 virtual void SAL_CALL dragExit( const DragSourceEvent& dse ) throw(RuntimeException);
79 virtual void SAL_CALL dragOver( const DragSourceDragEvent& dsde ) throw(RuntimeException);
80 virtual void SAL_CALL dropActionChanged( const DragSourceDragEvent& dsde ) throw(RuntimeException);
81 virtual void SAL_CALL disposing( const EventObject& eo ) throw(RuntimeException);
84 class MyInfoBox : public InfoBox
87 public:
89 MyInfoBox( vcl::Window* pParent );
92 class MyListBox : public ListBox
95 public:
97 MyListBox( vcl::Window* pParent );
100 class StringTransferable : public ::cppu::WeakImplHelper1< XTransferable >
102 const OUString m_aData;
103 Sequence< DataFlavor > m_aFlavorList;
105 public:
106 StringTransferable( const OUString& rString ) : m_aData( rString ), m_aFlavorList( 1 )
108 DataFlavor df;
110 df.MimeType = "text/plain;charset=utf-16";
111 df.DataType = cppu::UnoType<OUString>::get();
113 m_aFlavorList[0] = df;
116 virtual Any SAL_CALL getTransferData( const DataFlavor& aFlavor ) throw(UnsupportedFlavorException, IOException, RuntimeException);
117 virtual Sequence< DataFlavor > SAL_CALL getTransferDataFlavors( ) throw(RuntimeException);
118 virtual bool SAL_CALL isDataFlavorSupported( const DataFlavor& aFlavor ) throw(RuntimeException);
121 class VclDnDTest : public test::BootstrapFixture
123 public:
124 VclDnDTest() : BootstrapFixture(true, false) {}
126 /// Play with drag and drop
127 void testDnD();
129 CPPUNIT_TEST_SUITE(VclDnDTest);
130 CPPUNIT_TEST(testDnD);
131 CPPUNIT_TEST_SUITE_END();
134 void VclDnDTest::testDnD()
136 MyWin aMainWin( NULL, WB_APP | WB_STDWORK );
137 aMainWin.SetText( OUString( "Drag And Drop - Workbench" ) );
138 aMainWin.Show();
140 // test the clipboard code
141 Reference< XClipboard > xClipboard = aMainWin.GetClipboard();
142 CPPUNIT_ASSERT_MESSAGE("System clipboard not available",
143 xClipboard.is());
145 MyInfoBox aInfoBox( &aMainWin );
146 aInfoBox.Execute();
148 MyListBox aListBox( &aMainWin );
149 aListBox.setPosSizePixel( 10, 10, 100, 100 );
150 aListBox.InsertEntry( OUString("TestItem"));
151 aListBox.Show();
154 MyWin::MyWin( vcl::Window* pParent, WinBits nWinStyle ) :
155 WorkWindow( pParent, nWinStyle )
157 Reference< XDropTargetListener > xListener = new MyDragAndDropListener( this );
159 Reference< XDropTarget > xDropTarget = GetDropTarget();
160 if( xDropTarget.is() )
162 xDropTarget->addDropTargetListener( xListener );
163 xDropTarget->setActive( true );
166 Reference< XDragGestureRecognizer > xRecognizer = GetDragGestureRecognizer();
167 if( xRecognizer.is() )
168 xRecognizer->addDragGestureListener( Reference< XDragGestureListener > ( xListener, UNO_QUERY ) );
171 void MyWin::MouseMove( const MouseEvent& rMEvt )
173 WorkWindow::MouseMove( rMEvt );
176 void MyWin::MouseButtonDown( const MouseEvent& rMEvt )
178 WorkWindow::MouseButtonDown( rMEvt );
181 void MyWin::MouseButtonUp( const MouseEvent& rMEvt )
183 WorkWindow::MouseButtonUp( rMEvt );
186 void MyWin::KeyInput( const KeyEvent& rKEvt )
188 WorkWindow::KeyInput( rKEvt );
191 void MyWin::KeyUp( const KeyEvent& rKEvt )
193 WorkWindow::KeyUp( rKEvt );
196 void MyWin::Paint( const Rectangle& rRect )
198 WorkWindow::Paint( rRect );
201 void MyWin::Resize()
203 WorkWindow::Resize();
206 void SAL_CALL MyDragAndDropListener::dragGestureRecognized( const DragGestureEvent& dge ) throw(RuntimeException)
208 Reference< XDragSource > xDragSource( dge.DragSource, UNO_QUERY );
209 xDragSource->startDrag( dge, -1, 0, 0, new StringTransferable( OUString("TestString") ), this );
212 void SAL_CALL MyDragAndDropListener::drop( const DropTargetDropEvent& dtde ) throw(RuntimeException)
214 dtde.Context->dropComplete( true );
217 void SAL_CALL MyDragAndDropListener::dragEnter( const DropTargetDragEnterEvent& dtdee ) throw(RuntimeException)
219 dtdee.Context->acceptDrag( dtdee.DropAction );
222 void SAL_CALL MyDragAndDropListener::dragExit( const DropTargetEvent& ) throw(RuntimeException)
226 void SAL_CALL MyDragAndDropListener::dragOver( const DropTargetDragEvent& dtde ) throw(RuntimeException)
228 dtde.Context->acceptDrag( dtde.DropAction );
231 void SAL_CALL MyDragAndDropListener::dropActionChanged( const DropTargetDragEvent& dtde ) throw(RuntimeException)
233 dtde.Context->acceptDrag( dtde.DropAction );
236 void SAL_CALL MyDragAndDropListener::dragDropEnd( const DragSourceDropEvent& ) throw(RuntimeException)
240 void SAL_CALL MyDragAndDropListener::dragEnter( const DragSourceDragEvent& ) throw(RuntimeException)
244 void SAL_CALL MyDragAndDropListener::dragExit( const DragSourceEvent& ) throw(RuntimeException)
248 void SAL_CALL MyDragAndDropListener::dragOver( const DragSourceDragEvent& ) throw(RuntimeException)
252 void SAL_CALL MyDragAndDropListener::dropActionChanged( const DragSourceDragEvent& ) throw(RuntimeException)
256 void SAL_CALL MyDragAndDropListener::disposing( const EventObject& ) throw(RuntimeException)
260 MyInfoBox::MyInfoBox( vcl::Window* pParent ) : InfoBox( pParent,
261 OUString("dragging over this box should result in another window id in the drag log.") )
263 Reference< XDropTargetListener > xListener = new MyDragAndDropListener( this );
265 Reference< XDropTarget > xDropTarget = GetDropTarget();
266 if( xDropTarget.is() )
268 xDropTarget->addDropTargetListener( xListener );
269 xDropTarget->setActive( true );
272 Reference< XDragGestureRecognizer > xRecognizer = GetDragGestureRecognizer();
273 if( xRecognizer.is() )
274 xRecognizer->addDragGestureListener( Reference< XDragGestureListener > ( xListener, UNO_QUERY ) );
277 MyListBox::MyListBox( vcl::Window* pParent ) : ListBox( pParent )
279 Reference< XDropTargetListener > xListener = new MyDragAndDropListener( this );
281 Reference< XDropTarget > xDropTarget = GetDropTarget();
282 if( xDropTarget.is() )
284 xDropTarget->addDropTargetListener( xListener );
285 xDropTarget->setActive( true );
288 Reference< XDragGestureRecognizer > xRecognizer = GetDragGestureRecognizer();
289 if( xRecognizer.is() )
290 xRecognizer->addDragGestureListener( Reference< XDragGestureListener > ( xListener, UNO_QUERY ) );
293 Any SAL_CALL StringTransferable::getTransferData( const DataFlavor& )
294 throw(UnsupportedFlavorException, IOException, RuntimeException)
296 return makeAny( m_aData );
299 Sequence< DataFlavor > SAL_CALL StringTransferable::getTransferDataFlavors( )
300 throw(RuntimeException)
302 return m_aFlavorList;
305 bool SAL_CALL StringTransferable::isDataFlavorSupported( const DataFlavor& )
306 throw(RuntimeException)
308 return true;
311 CPPUNIT_TEST_SUITE_REGISTRATION(VclDnDTest);
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */