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 os
13import unittest
14
15from ..file_dialog import FileDialog
16from ..gui import GUI
17from ..toolkit import toolkit_object
18
19GuiTestAssistant = toolkit_object("util.gui_test_assistant:GuiTestAssistant")
20no_gui_test_assistant = GuiTestAssistant.__name__ == "Unimplemented"
21
22ModalDialogTester = toolkit_object(
23    "util.modal_dialog_tester:ModalDialogTester"
24)
25no_modal_dialog_tester = ModalDialogTester.__name__ == "Unimplemented"
26
27
28@unittest.skipIf(no_gui_test_assistant, "No GuiTestAssistant")
29class TestFileDialog(unittest.TestCase, GuiTestAssistant):
30    def setUp(self):
31        GuiTestAssistant.setUp(self)
32        self.dialog = FileDialog()
33
34    def tearDown(self):
35        if self.dialog.control is not None:
36            with self.delete_widget(self.dialog.control):
37                self.dialog.destroy()
38        del self.dialog
39        GuiTestAssistant.tearDown(self)
40
41    def test_create_wildcard(self):
42        wildcard = FileDialog.create_wildcard("Python", "*.py")
43        self.assertTrue(len(wildcard) != 0)
44
45    def test_create_wildcard_multiple(self):
46        wildcard = FileDialog.create_wildcard(
47            "Python", ["*.py", "*.pyo", "*.pyc", "*.pyd"]
48        )
49        self.assertTrue(len(wildcard) != 0)
50
51    def test_create(self):
52        # test that creation and destruction works as expected
53        with self.event_loop():
54            self.dialog._create()
55        with self.event_loop():
56            self.dialog.destroy()
57
58    def test_destroy(self):
59        # test that destroy works even when no control
60        with self.event_loop():
61            self.dialog.destroy()
62
63    def test_close(self):
64        # test that close works
65        with self.event_loop():
66            self.dialog._create()
67        with self.event_loop():
68            self.dialog.close()
69
70    def test_default_path(self):
71        # test that default path works
72        self.dialog.default_path = os.path.join("images", "core.png")
73        with self.event_loop():
74            self.dialog._create()
75        with self.event_loop():
76            self.dialog.close()
77
78    def test_default_dir_and_file(self):
79        # test that default dir and path works
80        self.dialog.default_directory = "images"
81        self.dialog.default_filename = "core.png"
82        with self.event_loop():
83            self.dialog._create()
84        with self.event_loop():
85            self.dialog.close()
86
87    def test_open_files(self):
88        # test that open files action works
89        self.dialog.action = "open files"
90        with self.event_loop():
91            self.dialog._create()
92        with self.event_loop():
93            self.dialog.close()
94
95    def test_save_as(self):
96        # test that open files action works
97        self.dialog.action = "save as"
98        with self.event_loop():
99            self.dialog._create()
100        with self.event_loop():
101            self.dialog.close()
102
103    # XXX would be nice to actually test with an open dialog, but not right now
104