Merge pull request #26166 from ksooo/improve-plugin-ctx-menus
[xbmc.git] / xbmc / test / TestBasicEnvironment.cpp
blob7a33e9d8517612523437cc83eec76ba771ca908b
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "TestBasicEnvironment.h"
11 #include "FileItem.h"
12 #include "FileItemList.h"
13 #include "ServiceBroker.h"
14 #include "ServiceManager.h"
15 #include "TestUtils.h"
16 #include "application/AppEnvironment.h"
17 #include "application/AppParams.h"
18 #include "application/Application.h"
19 #include "filesystem/Directory.h"
20 #include "filesystem/File.h"
21 #include "filesystem/SpecialProtocol.h"
22 #include "messaging/ApplicationMessenger.h"
23 #include "platform/Filesystem.h"
24 #include "profiles/ProfileManager.h"
25 #include "settings/SettingsComponent.h"
26 #include "windowing/WinSystem.h"
28 #ifdef TARGET_DARWIN
29 #include "Util.h"
30 #endif
32 #include <cstdio>
33 #include <cstdlib>
34 #include <climits>
35 #include <system_error>
37 namespace fs = KODI::PLATFORM::FILESYSTEM;
39 TestBasicEnvironment::TestBasicEnvironment() = default;
41 void TestBasicEnvironment::SetUp()
43 const auto params = std::make_shared<CAppParams>();
44 params->SetPlatformDirectories(false);
46 CAppEnvironment::SetUp(params);
48 CServiceBroker::RegisterAppMessenger(std::make_shared<KODI::MESSAGING::CApplicationMessenger>());
50 XFILE::CFile *f;
52 g_application.m_ServiceManager.reset(new CServiceManager());
54 if (!CXBMCTestUtils::Instance().SetReferenceFileBasePath())
55 SetUpError();
56 CXBMCTestUtils::Instance().setTestFileFactoryWriteInputFile(
57 XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt")
60 //for darwin set framework path - else we get assert
61 //in guisettings init below
62 #ifdef TARGET_DARWIN
63 std::string frameworksPath = CUtil::GetFrameworksPath();
64 CSpecialProtocol::SetXBMCFrameworksPath(frameworksPath);
65 #endif
66 /**
67 * @todo Something should be done about all the asserts in GUISettings so
68 * that the initialization of these components won't be needed.
71 /* Create a temporary directory and set it to be used throughout the
72 * test suite run.
75 std::error_code ec;
76 m_tempPath = fs::create_temp_directory(ec);
77 if (ec)
79 TearDown();
80 SetUpError();
83 CSpecialProtocol::SetTempPath(m_tempPath);
84 CSpecialProtocol::SetProfilePath(m_tempPath);
86 /* Create and delete a tempfile to initialize the VFS (really to initialize
87 * CLibcdio). This is done so that the initialization of the VFS does not
88 * affect the performance results of the test cases.
90 /** @todo Make the initialization of the VFS here optional so it can be
91 * testable in a test case.
93 f = XBMC_CREATETEMPFILE("");
94 if (!f || !XBMC_DELETETEMPFILE(f))
96 TearDown();
97 SetUpError();
100 const CProfile profile("special://temp");
101 CServiceBroker::GetSettingsComponent()->GetProfileManager()->AddProfile(profile);
102 CServiceBroker::GetSettingsComponent()->GetProfileManager()->CreateProfileFolders();
104 if (!g_application.m_ServiceManager->InitForTesting())
105 exit(1);
108 void TestBasicEnvironment::TearDown()
110 XFILE::CDirectory::RemoveRecursive(m_tempPath);
112 g_application.m_ServiceManager->DeinitTesting();
114 CServiceBroker::UnregisterAppMessenger();
116 CAppEnvironment::TearDown();
119 void TestBasicEnvironment::SetUpError()
121 fprintf(stderr, "Setup of basic environment failed.\n");
122 exit(EXIT_FAILURE);