| 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 <test/unoapi_test.hxx> |
| 11 |
|
| 12 |
#include <com/sun/star/frame/XFrame.hpp> |
| 13 |
#include <com/sun/star/frame/XComponentLoader.hpp> |
| 14 |
#include <com/sun/star/frame/FrameSearchFlag.hpp> |
| 15 |
#include <com/sun/star/util/URLTransformer.hpp> |
| 16 |
|
| 17 |
#include <comphelper/propertyvalue.hxx> |
| 18 |
#include <salhelper/thread.hxx> |
| 19 |
#include <vcl/svapp.hxx> |
| 20 |
#include <vcl/scheduler.hxx> |
| 21 |
#include <vcl/wrkwin.hxx> |
| 22 |
|
| 23 |
using namespace ::com::sun::star; |
| 24 |
|
| 25 |
namespace |
| 26 |
{ |
| 27 |
/// Covers framework/source/services/ fixes. |
| 28 |
class Test : public UnoApiTest |
| 29 |
{ |
| 30 |
public: |
| 31 |
Test() |
| 32 |
: UnoApiTest(u"/framework/qa/cppunit/data/"_ustr) |
| 33 |
{ |
| 34 |
} |
| 35 |
}; |
| 36 |
|
| 37 |
/// Invokes XFrameImpl::loadComponentFromURL() on a thread. |
| 38 |
class TestThread : public salhelper::Thread |
| 39 |
{ |
| 40 |
uno::Reference<frame::XComponentLoader> mxComponentLoader; |
| 41 |
uno::Reference<lang::XComponent>& mrComponent; |
| 42 |
|
| 43 |
public: |
| 44 |
TestThread(const uno::Reference<frame::XComponentLoader>& xComponentLoader, |
| 45 |
uno::Reference<lang::XComponent>& rComponent); |
| 46 |
void execute() override; |
| 47 |
}; |
| 48 |
|
| 49 |
TestThread::TestThread(const uno::Reference<frame::XComponentLoader>& xComponentLoader, |
| 50 |
uno::Reference<lang::XComponent>& rComponent) |
| 51 |
: salhelper::Thread("TestThread") |
| 52 |
, mxComponentLoader(xComponentLoader) |
| 53 |
, mrComponent(rComponent) |
| 54 |
{ |
| 55 |
} |
| 56 |
|
| 57 |
void TestThread::execute() |
| 58 |
{ |
| 59 |
sal_Int32 nSearchFlags = frame::FrameSearchFlag::AUTO; |
| 60 |
uno::Sequence<beans::PropertyValue> aArguments = { |
| 61 |
comphelper::makePropertyValue(u"OnMainThread"_ustr, true), |
| 62 |
}; |
| 63 |
// Note how this is invoking loadComponentFromURL() on a frame, not on the desktop, as usual. |
| 64 |
mrComponent = mxComponentLoader->loadComponentFromURL(u"private:factory/swriter"_ustr, |
| 65 |
u"_self"_ustr, nSearchFlags, aArguments); |
| 66 |
} |
| 67 |
|
| 68 |
CPPUNIT_TEST_FIXTURE(Test, testLoadComponentFromURL) |
| 69 |
{ |
| 70 |
// Without the accompanying fix in place, this test would have failed with: |
| 71 |
// thread 1: comphelper::SolarMutex::doRelease end: m_nCount is 1 |
| 72 |
// thread 2: vcl::SolarThreadExecutor::execute: before SolarMutexReleaser ctor |
| 73 |
// thread 2: comphelper::SolarMutex::doRelease start: m_nCount is 1, bUnlockAll is 1 |
| 74 |
// thread 2: comphelper::SolarMutex::doRelease: failed IsCurrentThread() check, will abort |
| 75 |
// Notice how thread 2 attempts to release the solar mutex while thread 1 holds it. |
| 76 |
|
| 77 |
// Create a default window, so by the time the thread would post a user event, it doesn't need |
| 78 |
// the solar mutex to process a SendMessageW() call on Windows. |
| 79 |
ScopedVclPtrInstance<WorkWindow> xWindow(nullptr, WB_APP | WB_STDWORK); |
| 80 |
// Variable is not used, it holds the default window. |
| 81 |
(void)xWindow; |
| 82 |
|
| 83 |
rtl::Reference<TestThread> xThread; |
| 84 |
{ |
| 85 |
// Start the thread that will load the component, but hold the solar mutex for now, so we |
| 86 |
// can see if it blocks. |
| 87 |
SolarMutexGuard guard; |
| 88 |
uno::Reference<frame::XFrame> xFrame |
| 89 |
= mxDesktop->findFrame(u"_blank"_ustr, /*nSearchFlags=*/0); |
| 90 |
uno::Reference<frame::XComponentLoader> xComponentLoader(xFrame, uno::UNO_QUERY); |
| 91 |
xThread = new TestThread(xComponentLoader, mxComponent); |
| 92 |
xThread->launch(); |
| 93 |
// If loadComponentFromURL() doesn't lock the solar mutex, the test will abort here. |
| 94 |
osl::Thread::wait(std::chrono::seconds(1)); |
| 95 |
} |
| 96 |
{ |
| 97 |
// Now release the solar mutex, so the thread can post the task on the main loop. |
| 98 |
SolarMutexReleaser releaser; |
| 99 |
osl::Thread::wait(std::chrono::seconds(1)); |
| 100 |
} |
| 101 |
{ |
| 102 |
// Spin the main loop. |
| 103 |
SolarMutexGuard guard; |
| 104 |
Scheduler::ProcessEventsToIdle(); |
| 105 |
} |
| 106 |
{ |
| 107 |
// Stop the thread. |
| 108 |
SolarMutexReleaser releaser; |
| 109 |
xThread->join(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
CPPUNIT_TEST_FIXTURE(Test, testURLTransformer_parseSmart) |
| 114 |
{ |
| 115 |
// Without the accompanying fix in place, this test would have failed with |
| 116 |
// "www.example.com:" treated as scheme, "/8080/foo/" as path, "bar?q=baz" |
| 117 |
// as name, and "F" as fragment. |
| 118 |
|
| 119 |
css::util::URL aURL; |
| 120 |
aURL.Complete = "www.example.com:8080/foo/bar?q=baz#F"; |
| 121 |
css::uno::Reference xParser(css::util::URLTransformer::create(m_xContext)); |
| 122 |
CPPUNIT_ASSERT(xParser->parseSmart(aURL, u"http:"_ustr)); |
| 123 |
CPPUNIT_ASSERT_EQUAL(u"http://www.example.com:8080/foo/bar?q=baz#F"_ustr, aURL.Complete); |
| 124 |
CPPUNIT_ASSERT_EQUAL(u"http://www.example.com:8080/foo/bar"_ustr, aURL.Main); |
| 125 |
CPPUNIT_ASSERT_EQUAL(u"http://"_ustr, aURL.Protocol); |
| 126 |
CPPUNIT_ASSERT(aURL.User.isEmpty()); |
| 127 |
CPPUNIT_ASSERT(aURL.Password.isEmpty()); |
| 128 |
CPPUNIT_ASSERT_EQUAL(u"www.example.com"_ustr, aURL.Server); |
| 129 |
CPPUNIT_ASSERT_EQUAL(sal_Int16(8080), aURL.Port); |
| 130 |
CPPUNIT_ASSERT_EQUAL(u"/foo/"_ustr, aURL.Path); |
| 131 |
CPPUNIT_ASSERT_EQUAL(u"bar"_ustr, aURL.Name); |
| 132 |
CPPUNIT_ASSERT_EQUAL(u"q=baz"_ustr, aURL.Arguments); |
| 133 |
CPPUNIT_ASSERT_EQUAL(u"F"_ustr, aURL.Mark); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
CPPUNIT_PLUGIN_IMPLEMENT(); |
| 138 |
|
| 139 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |