1# (C) Copyright 2005-2020 Enthought, Inc., Austin, TX
2# All rights reserved.
3#
4# This software is provided without warranty under the terms of the BSD
5# license included in LICENSE.txt and may be redistributed only under
6# the conditions described in the aforementioned license. The license
7# is also available online at http://www.enthought.com/licenses/BSD.txt
8#
9# Thanks for using Enthought open source!
10
11
12import unittest
13
14from ..progress_dialog import ProgressDialog
15from ..toolkit import toolkit_object
16
17GuiTestAssistant = toolkit_object("util.gui_test_assistant:GuiTestAssistant")
18no_gui_test_assistant = GuiTestAssistant.__name__ == "Unimplemented"
19
20ModalDialogTester = toolkit_object(
21    "util.modal_dialog_tester:ModalDialogTester"
22)
23no_modal_dialog_tester = ModalDialogTester.__name__ == "Unimplemented"
24
25
26@unittest.skipIf(no_gui_test_assistant, "No GuiTestAssistant")
27class TestProgressDialog(unittest.TestCase, GuiTestAssistant):
28    def setUp(self):
29        GuiTestAssistant.setUp(self)
30        self.dialog = ProgressDialog()
31
32    def tearDown(self):
33        if self.dialog.control is not None:
34            with self.delete_widget(self.dialog.control):
35                self.dialog.destroy()
36        del self.dialog
37        GuiTestAssistant.tearDown(self)
38
39    def test_create(self):
40        # test that creation and destruction works as expected
41        with self.event_loop():
42            self.dialog._create()
43        with self.event_loop():
44            self.dialog.destroy()
45
46    def test_destroy(self):
47        # test that destroy works even when no control
48        with self.event_loop():
49            self.dialog.destroy()
50
51    def test_can_cancel(self):
52        # test that creation works with can_cancel
53        self.dialog.can_cancel = True
54        with self.event_loop():
55            self.dialog._create()
56        with self.event_loop():
57            self.dialog.destroy()
58
59    def test_can_ok(self):
60        # test that creation works with can_ok
61        self.dialog.can_ok = True
62        with self.event_loop():
63            self.dialog._create()
64        with self.event_loop():
65            self.dialog.destroy()
66
67    def test_show_time(self):
68        # test that creation works with show_time
69        self.dialog.show_time = True
70        with self.event_loop():
71            self.dialog._create()
72        with self.event_loop():
73            self.dialog.destroy()
74
75    @unittest.skip("not implemented in any backend")
76    def test_show_percent(self):
77        # test that creation works with show_percent
78        self.dialog.show_percent = True
79        with self.event_loop():
80            self.dialog._create()
81        with self.event_loop():
82            self.dialog.destroy()
83
84    def test_update(self):
85        self.dialog.min = 0
86        self.dialog.max = 10
87        self.dialog.open()
88        for i in range(11):
89            with self.event_loop():
90                result = self.dialog.update(i)
91
92            self.assertEqual(result, (True, False))
93
94        self.assertIsNone(self.dialog.control)
95
96    @unittest.skip("inconsistent implementations")
97    def test_update_no_control(self):
98        self.dialog.min = 0
99        self.dialog.max = 10
100        with self.event_loop():
101            result = self.dialog.update(1)
102        self.assertEqual(result, (None, None))
103
104    def test_incomplete_update(self):
105        self.dialog.min = 0
106        self.dialog.max = 10
107        self.can_cancel = True
108        self.dialog.open()
109        for i in range(5):
110            with self.event_loop():
111                result = self.dialog.update(i)
112            self.assertEqual(result, (True, False))
113        self.assertIsNotNone(self.dialog.control)
114        with self.event_loop():
115            self.dialog.close()
116
117        self.assertIsNone(self.dialog.control)
118
119    def test_change_message(self):
120        self.dialog.min = 0
121        self.dialog.max = 10
122        self.dialog.open()
123        for i in range(11):
124            with self.event_loop():
125                self.dialog.change_message("Updating {}".format(i))
126                result = self.dialog.update(i)
127
128            self.assertEqual(result, (True, False))
129            self.assertEqual(self.dialog.message, "Updating {}".format(i))
130        self.assertIsNone(self.dialog.control)
131
132    def test_update_show_time(self):
133        self.dialog.min = 0
134        self.dialog.max = 10
135        self.dialog.show_time = True
136        self.dialog.open()
137        for i in range(11):
138            with self.event_loop():
139                result = self.dialog.update(i)
140
141            self.assertEqual(result, (True, False))
142        self.assertIsNone(self.dialog.control)
143
144    def test_update_degenerate(self):
145        self.dialog.min = 0
146        self.dialog.max = 0
147        self.dialog.open()
148        for i in range(10):
149            with self.event_loop():
150                result = self.dialog.update(i)
151
152            self.assertEqual(result, (True, False))
153
154        with self.event_loop():
155            self.dialog.close()
156        # XXX not really sure what correct behaviour is here
157
158    def test_update_negative(self):
159        self.dialog.min = 0
160        self.dialog.max = -10
161        with self.assertRaises(AttributeError):
162            with self.event_loop():
163                self.dialog.open()
164
165        self.assertIsNone(self.dialog.control)
166