1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  */
9 
10 #include <test/sheet/xactivationbroadcaster.hxx>
11 
12 #include <com/sun/star/lang/EventObject.hpp>
13 #include <com/sun/star/sheet/ActivationEvent.hpp>
14 #include <com/sun/star/sheet/XActivationBroadcaster.hpp>
15 #include <com/sun/star/sheet/XActivationEventListener.hpp>
16 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
17 #include <com/sun/star/uno/Reference.hxx>
18 
19 #include <cppuhelper/implbase.hxx>
20 #include <rtl/ref.hxx>
21 
22 #include <cppunit/TestAssert.h>
23 
24 using namespace com::sun::star;
25 using namespace com::sun::star::uno;
26 
27 namespace apitest
28 {
29 namespace
30 {
31 class MockedActivationEventListener : public ::cppu::WeakImplHelper<sheet::XActivationEventListener>
32 {
33 public:
MockedActivationEventListener()34     MockedActivationEventListener()
35         : mbListenerCalled(false)
36     {
37     }
38     bool mbListenerCalled;
39     virtual void SAL_CALL
activeSpreadsheetChanged(const sheet::ActivationEvent &)40     activeSpreadsheetChanged(const sheet::ActivationEvent& /* xEvent */) override
41     {
42         mbListenerCalled = true;
43     }
disposing(const lang::EventObject &)44     virtual void SAL_CALL disposing(const lang::EventObject& /* xEventObj */) override {}
45 };
46 }
47 
testAddRemoveActivationEventListener()48 void XActivationBroadcaster::testAddRemoveActivationEventListener()
49 {
50     uno::Reference<sheet::XActivationBroadcaster> xAB(init(), UNO_QUERY_THROW);
51     xAB->addActivationEventListener(nullptr);
52 
53     rtl::Reference<MockedActivationEventListener> xListener = new MockedActivationEventListener();
54     xAB->addActivationEventListener(uno::Reference<sheet::XActivationEventListener>(xListener));
55 
56     uno::Reference<sheet::XSpreadsheetView> xView(xAB, UNO_QUERY_THROW);
57     uno::Reference<sheet::XSpreadsheet> xSheet1(xView->getActiveSheet(), UNO_SET_THROW);
58     uno::Reference<sheet::XSpreadsheet> xSheet2(getXSpreadsheet(1), UNO_QUERY_THROW);
59 
60     xView->setActiveSheet(xSheet2);
61 
62     CPPUNIT_ASSERT_MESSAGE("Listener wasn't called", xListener->mbListenerCalled);
63 
64     xAB->removeActivationEventListener(uno::Reference<sheet::XActivationEventListener>(xListener));
65     xView->setActiveSheet(xSheet1);
66     CPPUNIT_ASSERT_MESSAGE("Listener still called after removal", xListener->mbListenerCalled);
67 }
68 } // namespace apitest
69 
70 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
71