2 This file is part of the LibreOffice project.
4 This Source Code Form is subject to the terms of the Mozilla Public
5 License, v. 2.0. If a copy of the MPL was not distributed with this
6 file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 This file incorporates work covered by the following license notice:
10 Licensed to the Apache Software Foundation (ASF) under one or more
11 contributor license agreements. See the NOTICE file distributed
12 with this work for additional information regarding copyright
13 ownership. The ASF licenses this file to you under the Apache
14 License, Version 2.0 (the "License"); you may not use this file
15 except in compliance with the License. You may obtain a copy of
16 the License at http://www.apache.org/licenses/LICENSE-2.0 .
25 from com
.sun
.star
.lang
import EventObject
26 from com
.sun
.star
.lang
import XMultiServiceFactory
27 from com
.sun
.star
.lang
import XComponent
28 from com
.sun
.star
.beans
import PropertyValue
29 from com
.sun
.star
.frame
import XGlobalEventBroadcaster
30 from com
.sun
.star
.frame
import XStorable
31 from com
.sun
.star
.document
import DocumentEvent
32 from com
.sun
.star
.document
import XDocumentEventListener
33 from org
.libreoffice
.unotest
import UnoInProcess
34 from urllib
.parse
import quote
37 class LoadSaveTest(unittest
.TestCase
):
39 def mkPropertyValue(self
, name
, value
):
40 return uno
.createUnoStruct("com.sun.star.beans.PropertyValue", name
, 0, value
, 0)
44 cls
._uno
= UnoInProcess()
46 cls
.xDoc
= cls
._uno
.openEmptyWriterDoc()
47 cls
.xContext
= cls
._uno
.getContext()
49 cls
.m_fileURL
= "file:/"
50 cls
.m_SourceDir
= "FIXME"
51 cls
.m_TargetDir
= "/tmp/out/"
57 def tearDownClass(cls
):
60 def testLoadStore(self
):
61 self
.dirs
, self
.files
= self
.getDirAndFile(self
.m_SourceDir
)
62 self
.makeDirs(self
.m_TargetDir
)
63 for self
.file_name
in self
.files
:
68 props
= [("ReadOnly", True)]
69 load_props
= tuple([self
.mkPropertyValue(name
, value
) for (name
, value
) in props
])
71 m_xMSF
= self
.xContext
.ServiceManager
72 desktop
= m_xMSF
.createInstanceWithContext('com.sun.star.frame.Desktop', self
.xContext
)
74 filepath
= os
.path
.abspath("FIXME")
76 source_file
= ''.join(("file:///", filepath
, "/", quote(self
.file_name
)))
78 source_file
= ''.join(("file://", quote(filepath
), "/", quote(self
.file_name
)))
80 self
.xDoc
= desktop
.loadComponentFromURL(source_file
, "_blank", 0, load_props
)
84 target_file
= ''.join(("file:///", self
.m_TargetDir
, quote(self
.m_SourceDir
), "/", quote(self
.file_name
)))
86 target_file
= ''.join(("file://", quote(self
.m_TargetDir
), quote(self
.m_SourceDir
), "/", quote(self
.fileName
)))
89 PropValue
= uno
.Any("[]com.sun.star.beans.PropertyValue", (p1
,))
90 uno
.invoke(self
.xDoc
, "storeToURL", (target_file
, PropValue
))
95 def getDirAndFile(self
, dir):
98 root_path
= ''.join((dir, "/", dir, ".odt"))
99 root
= open(root_path
, 'a')
101 self
.getDirAndFileNames(dir)
102 return self
.dirs
, self
.files
104 def getDirAndFileNames(self
, fdName
):
106 if os
.path
.isdir(fdName
):
107 self
.dirs
.append(fdName
)
108 subfiles
= os
.listdir(fdName
)
110 if not fdName
[-1] == "/":
113 for subfile
in subfiles
:
114 subfile_name
= fdName
+ subfile
115 self
.getDirAndFileNames(subfile_name
)
117 if os
.path
.isfile(fdName
):
118 self
.files
.append(fdName
.split('/')[-1])
120 def makeDirs(self
, target
):
121 if not os
.path
.exists(target
):
123 self
.assertTrue(os
.path
.exists(target
))
125 for dir in self
.dirs
:
126 if not os
.path
.exists(target
+ dir):
127 f
= os
.mkdir(target
+ dir)
128 self
.assertTrue(os
.path
.exists(target
+ dir))
130 target_path
= ''.join((target
, dir, "/", self
.m_SourceDir
, ".odt"))
131 root
= open(target_path
, 'a')
132 filepath
= os
.path
.abspath(target_path
)