summaryrefslogtreecommitdiff
path: root/tests/test_BaseView.py
blob: afe39da7706319c4eab30f45d9d054984c5ea7f8 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
import unittest
import gtk
from gtk import keysyms

from utils import refresh_gui

from kiwi.controllers import BaseController
from kiwi.ui.gadgets import set_foreground, get_foreground, \
                            set_background, get_background
from kiwi.ui.views import BaseView

class FooView(BaseView):
    widgets = [ "vbox", "label" ]
    def __init__(self):
        self.build_ui()
        BaseView.__init__(self, toplevel_name='win')

    def build_ui(self):
        self.win = gtk.Window()
        vbox = gtk.VBox()
        self.label = gtk.Label("Pick one noogie")
        vbox.add(self.label)
        self.button = gtk.Button(label="Noogie!")
        vbox.add(self.button)
        self.foo__button = gtk.Button(label="Boogie!")
        vbox.add(self.foo__button)
        self.win.add(vbox)
        self.vbox = vbox
        return vbox

class FooController(BaseController):
    def __init__(self, view):
        keyactions = {
            keysyms.A: self.on_button__clicked,
            keysyms.a: self.on_button__clicked,
            keysyms.B: self.on_foo__button__clicked,
            keysyms.b: self.on_foo__button__clicked
        }
        BaseController.__init__(self, view, keyactions)

    def on_button__clicked(self, *args):
        self.bar = Bar()

    def on_foo__button__clicked(self, *args):
        # This is subclassed
        self.view.label.set_text("Good click!")

class Bar(BaseView, BaseController):
    def __init__(self):
        self.win = gtk.Window()
        self.label = gtk.Label("foobar!")
        self.win.add(self.label)
        BaseView.__init__(self, toplevel=self.win)
        BaseController.__init__(self, view=self)
        set_foreground(self.label, "#CC99FF")
        set_background(self.win, "#001100")

# these classes are bad and should trigger exceptions

class NoWinFoo(BaseView, BaseController):
    def __init__(self):
        self.win = 0
        BaseView.__init__(self)
        BaseController.__init__(self, view=self)


class NotWidgetFoo(FooView, BaseController):
    def __init__(self):
        self.vbox = self.build_ui()
        # It's dumb, and it breaks
        self.noogie = NotWidgetFoo
        FooView.__init__(self)
        BaseController.__init__(self, view=self)

    def on_noogie__haxored(self, *args):
        print "I AM NOT A NUMBER I AM A FREE MAN"

class BaseViewTest(unittest.TestCase):

    def setUp(self):
        self.view = FooView()
        self.foo = FooController(self.view)
        refresh_gui()

    def tearDown(self):
        self.view.win.destroy()

    def testFooButton(self):
        self.foo.view.foo__button.clicked()
        refresh_gui()
        self.assertEqual(self.foo.view.label.get_text(),
                         "Good click!")

    def testSubView(self):
        self.foo.view.button.clicked()
        refresh_gui()
        self.assertEqual(self.foo.bar, self.foo.bar.view)
        self.assertEqual(self.foo.bar.toplevel, self.foo.bar.win)
        # setting None as transient window should be an error
        self.assertRaises(TypeError, self.foo.bar.set_transient_for, None)

    def testColors(self):
        self.foo.view.button.clicked()
        refresh_gui()
        win = self.foo.bar.win
        win.realize()
        color = get_background(win)
        self.assertEqual(color, "#001100")
        label = self.foo.bar.label
        label.realize()
        color = get_foreground(label)
        self.assertEqual(color, "#CC99FF")


class BrokenViewsTest(unittest.TestCase):

    def testNoWindow(self):
        # A View requires an instance variable called toplevel that
        # specifies the toplevel widget in it
        self.assertRaises(TypeError, NoWinFoo)

    def testNotAWidget(self):
        # noogie (__main__.NotWidgetFoo) is not a widget and
        # can't be connected to
        self.assertRaises(AttributeError, NotWidgetFoo)

if __name__ == '__main__':
    unittest.main()