Debian Codegraph @

framework/qa/cppunit/accelerators.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <com/sun/star/awt/Key.hpp>
11 #include <com/sun/star/container/NoSuchElementException.hpp>
12 #include <com/sun/star/ui/XAcceleratorConfiguration.hpp>
13 #include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
14
15 #include <test/unoapi_test.hxx>
16
17 #include <array>
18
19 using namespace css;
20
21 class AcceleratorsTest : public UnoApiTest
22 {
23 public:
24 AcceleratorsTest()
25 : UnoApiTest(u"/framework/qa/cppunit/data"_ustr)
26 {
27 }
28
29 void configNotification();
30
31 CPPUNIT_TEST_SUITE(AcceleratorsTest);
32 CPPUNIT_TEST(configNotification);
33 CPPUNIT_TEST_SUITE_END();
34 };
35
36 void AcceleratorsTest::configNotification()
37 {
38 uno::Reference<lang::XMultiServiceFactory> xFactory = getMultiServiceFactory();
39
40 uno::Reference<ui::XAcceleratorConfiguration> xGlobalAccelCfg(
41 xFactory->createInstance(u"com.sun.star.ui.GlobalAcceleratorConfiguration"_ustr),
42 uno::UNO_QUERY);
43 CPPUNIT_ASSERT(xGlobalAccelCfg.is());
44
45 // Create two instances of the Writer module accelerator config so
46 // we can test that updates are copied between them
47 std::array<uno::Reference<ui::XAcceleratorConfiguration>, 2> xSwAccelCfgs;
48
49 for (auto& xSwAccelCfg : xSwAccelCfgs)
50 {
51 uno::Sequence<uno::Any> args(1);
52 args.getArray()[0] <<= u"com.sun.star.text.TextDocument"_ustr;
53
54 xSwAccelCfg.set(xFactory->createInstanceWithArguments(
55 u"com.sun.star.ui.ModuleAcceleratorConfiguration"_ustr, args),
56 uno::UNO_QUERY);
57 CPPUNIT_ASSERT(xSwAccelCfg.is());
58 }
59
60 // The two modules configs shouldn’t be the same object, otherwise
61 // the test is not really testing anything
62 CPPUNIT_ASSERT(xSwAccelCfgs[0] != xSwAccelCfgs[1]);
63
64 // Create an instance of another module
65 uno::Sequence<uno::Any> args(1);
66 args.getArray()[0] <<= u"com.sun.star.sheet.SpreadsheetDocument"_ustr;
67 uno::Reference<ui::XAcceleratorConfiguration> xScAccelCfg(
68 xFactory->createInstanceWithArguments(
69 u"com.sun.star.ui.ModuleAcceleratorConfiguration"_ustr, args),
70 uno::UNO_QUERY);
71 CPPUNIT_ASSERT(xScAccelCfg.is());
72
73 awt::KeyEvent aKeyEvent;
74 aKeyEvent.KeyCode = awt::Key::F16;
75
76 // Make sure the key we’re going to test isn’t already defined
77 CPPUNIT_ASSERT_THROW(xGlobalAccelCfg->getCommandByKeyEvent(aKeyEvent),
78 css::container::NoSuchElementException);
79 for (const auto& xSwAccelCfg : xSwAccelCfgs)
80 {
81 CPPUNIT_ASSERT_THROW(xSwAccelCfg->getCommandByKeyEvent(aKeyEvent),
82 css::container::NoSuchElementException);
83 }
84 CPPUNIT_ASSERT_THROW(xScAccelCfg->getCommandByKeyEvent(aKeyEvent),
85 css::container::NoSuchElementException);
86
87 // Set the key event on the module
88 xSwAccelCfgs[0]->setKeyEvent(aKeyEvent, u".uno:Bold"_ustr);
89
90 // Store the changed configuration. This should trigger a change event
91 uno::Reference<ui::XUIConfigurationPersistence> xConfPersistence(xSwAccelCfgs[0],
92 uno::UNO_QUERY);
93 xConfPersistence->store();
94
95 // The key shouldn’t have been copied to the global configuration
96 CPPUNIT_ASSERT_THROW(xGlobalAccelCfg->getCommandByKeyEvent(aKeyEvent),
97 css::container::NoSuchElementException);
98 // Nor the Calc module
99 CPPUNIT_ASSERT_THROW(xScAccelCfg->getCommandByKeyEvent(aKeyEvent),
100 css::container::NoSuchElementException);
101
102 // Make sure the key was copied to all of the Writer module
103 // configurations
104 for (const auto& xSwAccelCfg : xSwAccelCfgs)
105 CPPUNIT_ASSERT_EQUAL(u".uno:Bold"_ustr, xSwAccelCfg->getCommandByKeyEvent(aKeyEvent));
106
107 // Modify the key to a different command
108 xSwAccelCfgs[0]->setKeyEvent(aKeyEvent, u".uno:Italic"_ustr);
109 xConfPersistence->store();
110
111 // Check the propagation worked correctly
112 CPPUNIT_ASSERT_THROW(xGlobalAccelCfg->getCommandByKeyEvent(aKeyEvent),
113 css::container::NoSuchElementException);
114 CPPUNIT_ASSERT_THROW(xScAccelCfg->getCommandByKeyEvent(aKeyEvent),
115 css::container::NoSuchElementException);
116 for (const auto& xSwAccelCfg : xSwAccelCfgs)
117 CPPUNIT_ASSERT_EQUAL(u".uno:Italic"_ustr, xSwAccelCfg->getCommandByKeyEvent(aKeyEvent));
118
119 // Remove the key binding
120 xSwAccelCfgs[0]->removeKeyEvent(aKeyEvent);
121 xConfPersistence->store();
122
123 // Now none of the configs should have the key
124 CPPUNIT_ASSERT_THROW(xGlobalAccelCfg->getCommandByKeyEvent(aKeyEvent),
125 css::container::NoSuchElementException);
126 CPPUNIT_ASSERT_THROW(xScAccelCfg->getCommandByKeyEvent(aKeyEvent),
127 css::container::NoSuchElementException);
128 for (const auto& xSwAccelCfg : xSwAccelCfgs)
129 {
130 CPPUNIT_ASSERT_THROW(xSwAccelCfg->getCommandByKeyEvent(aKeyEvent),
131 css::container::NoSuchElementException);
132 }
133 }
134
135 CPPUNIT_TEST_SUITE_REGISTRATION(AcceleratorsTest);
136
137 CPPUNIT_PLUGIN_IMPLEMENT();
138
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */