summaryrefslogtreecommitdiff
path: root/tests/test_gicon.py
blob: 5ce7ece771ce91c11ba2c7f22cb411c0535dc9f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- Mode: Python -*-

import os
import unittest

from common import gio, glib


class TestIcon(unittest.TestCase):
    def test_eq(self):
        self.assertEquals(gio.FileIcon(gio.File('foo.png')),
                          gio.FileIcon(gio.File('foo.png')))
        self.assertEquals(gio.ThemedIcon('foo'),
                          gio.ThemedIcon('foo'))

        self.assertNotEqual(gio.FileIcon(gio.File('foo.png')),
                            gio.FileIcon(gio.File('bar.png')))
        self.assertNotEquals(gio.ThemedIcon('foo'),
                             gio.ThemedIcon('bar'))
        self.assertNotEquals(gio.FileIcon(gio.File('foo.png')),
                             gio.ThemedIcon('foo'))

    def test_hash(self):
        self.assertEquals(hash(gio.FileIcon(gio.File('foo.png'))),
                          hash(gio.FileIcon(gio.File('foo.png'))))
        self.assertEquals(hash(gio.ThemedIcon('foo')),
                          hash(gio.ThemedIcon('foo')))

class TestLoadableIcon(unittest.TestCase):
    def setUp(self):
        self.file = open('temp.svg', 'w')
        self.svg = ('<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
                    '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" '
                    '"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">'
                    '<svg width="32" height="32"/>')
        self.file.write(self.svg)
        self.file.close()
        self.icon = gio.FileIcon(gio.File('temp.svg'))

    def tearDown(self):
        if os.path.exists('temp.svg'):
            os.unlink('temp.svg')

    def test_load(self):
        stream, type = self.icon.load()
        try:
            self.assert_(isinstance(stream, gio.InputStream))
            self.assertEquals(self.svg, stream.read())
        finally:
            stream.close()

    def test_load_async(self):
        def callback(icon, result):
            try:
                stream, type = icon.load_finish(result)
                self.assert_(isinstance(stream, gio.InputStream))
                self.assertEquals(self.svg, stream.read())
            finally:
                loop.quit()
                stream.close()

        self.icon.load_async(callback)

        loop = glib.MainLoop()
        loop.run()

class TestFileIcon(unittest.TestCase):
    def test_constructor(self):
        file = gio.File('foo.png')
        self.assert_(file is gio.FileIcon(file).get_file())

class TestThemedIcon(unittest.TestCase):
    def setUp(self):
        self.icon = gio.ThemedIcon("open")

    def test_constructor(self):
        have_use_default_fallbacks = ('use_default_fallbacks'
                                      in gio.ThemedIcon.props.__members__)
        icon = gio.ThemedIcon('foo')
        self.assertEquals(['foo'], icon.props.names)
        self.assert_(not have_use_default_fallbacks
                     or not icon.props.use_default_fallbacks)

        icon = gio.ThemedIcon(['foo', 'bar', 'baz'])
        self.assertEquals(['foo', 'bar', 'baz'], icon.props.names)
        self.assert_(not have_use_default_fallbacks
                     or not icon.props.use_default_fallbacks)

        icon = gio.ThemedIcon('xxx-yyy-zzz', True)
        self.assertEquals(['xxx-yyy-zzz', 'xxx-yyy', 'xxx'], icon.props.names)
        self.assert_(not have_use_default_fallbacks
                     or icon.props.use_default_fallbacks)

    def test_constructor_illegals(self):
        self.assertRaises(TypeError, lambda: gio.ThemedIcon(42))
        self.assertRaises(TypeError, lambda: gio.ThemedIcon(['foo', 42, 'bar']))

    def test_get_names(self):
        self.assertEquals(self.icon.get_names(), ['open'])

    def test_append_name(self):
        self.assertEquals(self.icon.get_names(), ['open'])
        self.icon.append_name('close')
        self.assertEquals(self.icon.get_names(), ['open', 'close'])

class TestEmblemedIcon(unittest.TestCase):
    def test_emblemed_icon(self):
        icon = gio.FileIcon(gio.File('foo.png'))
        emblem = gio.Emblem(icon)
        emblemed = gio.EmblemedIcon(icon, emblem)
        self.assertEquals(emblemed.get_emblems()[0], emblem)