2
# -*- coding: utf-8 -*-
3
# Copyright (C) 2012 Canonical
6
# Francis 'fginther' Ginther <francis.ginther@canonical.com>
7
# Łukasz 'sil2100' Zemczak <lukasz.zemczak@canonical.com>
9
# This program is free software; you can redistribute it and/or modify it under
10
# the terms of the GNU General Public License as published by the Free Software
11
# Foundation; version 3.
13
# This program is distributed in the hope that it will be useful, but WITHOUTa
14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18
# You should have received a copy of the GNU General Public License along with
19
# this program; if not, write to the Free Software Foundation, Inc.,
20
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25
from mock import MagicMock, patch
26
from StringIO import StringIO
33
# Module Under Test (mut)
35
mut = __import__("02_migrate_to_gsettings")
37
sys.exit("Error! 02_migrate_to_gsettings module-under-test not found - perhaps you missed including it in the PYTHONPATH environment variable? Add the migration module directory and re-run the script again.")
39
class MigrateGconfToGsettingsTests(unittest.TestCase):
40
"""Test suite for method migrate_gconf_to_gsettings"""
43
"""Redirects stdout so that tests can assert print statements"""
44
self.stdout = sys.stdout
50
sys.stdout = self.stdout
51
# Dump the content that was sent to stdout
52
# print(self.out.getvalue())
54
def testClientGetDefaultFalse(self):
55
"""Test missing gconf client"""
56
gconf.client_get_default = MagicMock(return_value=False)
57
mut.migrate_gconf_to_gsettings()
58
self.assertEqual(self.out.getvalue().strip(), "WARNING: no gconf client found. No transitionning will be done")
60
def testGetSchemaException(self):
61
"""Test exception handling for get_schema"""
62
gconf_mock = MagicMock(name="gconf-Mock")
63
gconf.client_get_default = MagicMock(return_value=gconf_mock)
64
gconf_schema_mock = MagicMock(name="gconf.Schema-Mock")
65
gconf_mock.get_schema = MagicMock(return_value=gconf_schema_mock,
66
side_effect=glib.GError)
67
self.assertRaises(glib.GError, mut.migrate_gconf_to_gsettings())
69
def testGetSchemaNone(self):
70
"""Test missing schema"""
71
gconf_mock = MagicMock(name="gconf-Mock")
72
gconf.client_get_default = MagicMock(return_value=gconf_mock)
73
#gconf_schema_mock = MagicMock(name="gconf.Schema-Mock")
74
gconf_mock.get_schema = MagicMock(return_value=False)
75
mut.migrate_gconf_to_gsettings()
76
self.assertEqual(self.out.getvalue().strip(), "No current profile set, no migration needed")
78
def setupGetSchema(self, profile):
79
"""Set up mock objects for testing a valid schema"""
80
# gconf.client_get_default
81
gconf_mock = MagicMock(name="gconf-Mock")
82
gconf.client_get_default = MagicMock(return_value=gconf_mock)
84
gconf_schema_mock = MagicMock(name="gconf.Schema-Mock")
85
gconf_mock.get_schema = MagicMock(return_value=gconf_schema_mock)
86
# current_profile_schema.get_default_value
87
gconf_gconfvalue_mock = MagicMock(name="gconf.Schema.gconfvalue-Mock")
88
gconf_schema_mock.get_default_value = MagicMock(return_value=gconf_gconfvalue_mock)
89
# current_profile_gconfvalue.get_string
90
gconf_gconfvalue_mock.get_string = MagicMock(return_value=profile)
93
subprocess_mock = MagicMock(name="subprocess-Mock")
94
popen_mock = MagicMock(name="popen-Mock", return_value=subprocess_mock)
95
subprocess.Popen = popen_mock
98
def testGetSchemaUnity(self):
99
"""Test the 'unity' schema"""
100
popen_mock = self.setupGetSchema("unity")
101
mut.migrate_gconf_to_gsettings()
102
# 2 files should be converted
103
self.assertEqual(len(popen_mock.call_args_list), 2)
104
self.assertEqual(popen_mock.call_args_list[0][0][0][1],
105
'--file=/usr/lib/compiz/migration/compiz-profile-active-unity.convert')
106
self.assertEqual(popen_mock.call_args_list[1][0][0][1],
107
'--file=/usr/lib/compiz/migration/compiz-profile-Default.convert')
109
def testGetSchemaDefaultConvertPathInvalid(self):
110
"""Test the default schema"""
111
popen_mock = self.setupGetSchema("Default")
112
os.path.exists = MagicMock(return_value=False)
113
mut.migrate_gconf_to_gsettings()
114
# 1 file should be converted
115
self.assertEqual(len(popen_mock.call_args_list), 1)
116
self.assertEqual(popen_mock.call_args_list[0][0][0][1],
117
'--file=/usr/lib/compiz/migration/compiz-profile-active-Default.convert')
119
def testGetSchemaDefaultConvertPathValid(self):
120
"""Test the default schema with a valid unity convert file"""
121
popen_mock = self.setupGetSchema("Default")
122
exists_mock = MagicMock(return_value=True)
123
os.path.exists = exists_mock
124
mut.migrate_gconf_to_gsettings()
125
self.assertEqual(exists_mock.call_args_list[0][0][0],
126
mut.CONVERT_PATH + 'compiz-profile-unity.convert')
127
# 2 files should be converted
128
self.assertEqual(len(popen_mock.call_args_list), 2)
129
self.assertEqual(popen_mock.call_args_list[0][0][0][1],
130
'--file=/usr/lib/compiz/migration/compiz-profile-active-Default.convert')
131
self.assertEqual(popen_mock.call_args_list[1][0][0][1],
132
'--file=/usr/lib/compiz/migration/compiz-profile-unity.convert')
135
if __name__ == '__main__':