Merge pull request #4630 from Red-F/gotham-resume-pvr-lastplayedposition
[vuplus_xbmc] / xbmc / test / TestBasicEnvironment.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "TestBasicEnvironment.h"
22 #include "TestUtils.h"
23 #include "filesystem/Directory.h"
24 #include "filesystem/File.h"
25 #include "filesystem/SpecialProtocol.h"
26 #include "powermanagement/PowerManager.h"
27 #include "settings/AdvancedSettings.h"
28 #include "settings/Settings.h"
29 #include "Util.h"
30
31 #include <cstdio>
32 #include <cstdlib>
33 #include <climits>
34
35 void TestBasicEnvironment::SetUp()
36 {
37   char *tmp;
38   CStdString xbmcTempPath;
39   XFILE::CFile *f;
40
41   /* NOTE: The below is done to fix memleak warning about unitialized variable
42    * in xbmcutil::GlobalsSingleton<CAdvancedSettings>::getInstance().
43    */
44   g_advancedSettings.Initialize();
45
46   if (!CXBMCTestUtils::Instance().SetReferenceFileBasePath())
47     SetUpError();
48   CXBMCTestUtils::Instance().setTestFileFactoryWriteInputFile(
49     XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt")
50   );
51
52 //for darwin set framework path - else we get assert
53 //in guisettings init below
54 #ifdef TARGET_DARWIN
55   CStdString frameworksPath = CUtil::GetFrameworksPath();
56   CSpecialProtocol::SetXBMCFrameworksPath(frameworksPath);    
57 #endif
58   /* TODO: Something should be done about all the asserts in GUISettings so
59    * that the initialization of these components won't be needed.
60    */
61   g_powerManager.Initialize();
62   CSettings::Get().Initialize();
63
64   /* Create a temporary directory and set it to be used throughout the
65    * test suite run.
66    */
67 #ifdef TARGET_WINDOWS
68   TCHAR lpTempPathBuffer[MAX_PATH];
69   if (!GetTempPath(MAX_PATH, lpTempPathBuffer))
70     SetUpError();
71   xbmcTempPath = lpTempPathBuffer;
72   if (!GetTempFileName(xbmcTempPath.c_str(), "xbmctempdir", 0, lpTempPathBuffer))
73     SetUpError();
74   DeleteFile(lpTempPathBuffer);
75   if (!CreateDirectory(lpTempPathBuffer, NULL))
76     SetUpError();
77   CSpecialProtocol::SetTempPath(lpTempPathBuffer);
78 #else
79   char buf[MAX_PATH];
80   (void)xbmcTempPath;
81   strcpy(buf, "/tmp/xbmctempdirXXXXXX");
82   if ((tmp = mkdtemp(buf)) == NULL)
83     SetUpError();
84   CSpecialProtocol::SetTempPath(tmp);
85 #endif
86
87   /* Create and delete a tempfile to initialize the VFS (really to initialize
88    * CLibcdio). This is done so that the initialization of the VFS does not
89    * affect the performance results of the test cases.
90    */
91   /* TODO: Make the initialization of the VFS here optional so it can be
92    * testable in a test case.
93    */
94   f = XBMC_CREATETEMPFILE("");
95   if (!f || !XBMC_DELETETEMPFILE(f))
96   {
97     TearDown();
98     SetUpError();
99   }
100 }
101
102 void TestBasicEnvironment::TearDown()
103 {
104   CStdString xbmcTempPath = CSpecialProtocol::TranslatePath("special://temp/");
105   XFILE::CDirectory::Remove(xbmcTempPath);
106 }
107
108 void TestBasicEnvironment::SetUpError()
109 {
110   fprintf(stderr, "Setup of basic environment failed.\n");
111   exit(EXIT_FAILURE);
112 }