Skip to content

Commit 28155f3

Browse files
Add unit tests for mainframe
1 parent d86d094 commit 28155f3

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

utest/ui/test_mainframe.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Copyright 2024- Robot Framework Foundation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
import os
17+
import pytest
18+
19+
DISPLAY = os.getenv('DISPLAY')
20+
if not DISPLAY: # Avoid failing unit tests in system without X11
21+
pytest.skip("Skipped because of missing DISPLAY", allow_module_level=True)
22+
import wx
23+
from wx.lib.agw.aui import AuiManager
24+
25+
from robotide.robotapi import (TestDataDirectory, TestCaseFile, ResourceFile,
26+
TestCase, UserKeyword)
27+
from robotide.spec.librarymanager import LibraryManager
28+
from robotide.ui.mainframe import ActionRegisterer, ToolBar
29+
from robotide.ui.actiontriggers import MenuBar, ShortcutRegistry
30+
from robotide.application import Project
31+
from robotide.application.pluginloader import PluginLoader
32+
from robotide.controller.filecontrollers import (TestDataDirectoryController, ResourceFileController)
33+
from utest.resources import FakeSettings
34+
from robotide.editor.texteditor import TextEditorPlugin
35+
from robotide.log import LogPlugin
36+
from robotide.ui import mainframe
37+
from robotide.publish import PUBLISHER
38+
from robotide.ui.treeplugin import Tree
39+
from robotide.namespace.namespace import Namespace
40+
41+
42+
app = wx.App()
43+
44+
45+
class _BaseDialogTest(unittest.TestCase):
46+
47+
def setUp(self):
48+
self.app = wx.App()
49+
self.app.settings = FakeSettings()
50+
font_size = self.app.settings['General'].get('font size', 12)
51+
font_face = self.app.settings['General'].get('font face', 'Helvetica')
52+
self.app.fontinfo = wx.FontInfo(font_size).FaceName(font_face).Bold(False)
53+
self.app.namespace = Namespace(FakeSettings())
54+
self.model = self._create_model()
55+
self.frame = mainframe.RideFrame(self.app, self.model)
56+
self.app.frame = self.frame
57+
txtplugin = TextEditorPlugin
58+
logplugin = LogPlugin
59+
plugins_dir = [os.path.join(os.path.dirname(__file__), 'plugins_for_loader')]
60+
self.loader = PluginLoader(self.app, plugins_dir, [txtplugin, logplugin])
61+
self.app.get_plugins = lambda: self.loader.plugins
62+
self.frame.tree = Tree(self.frame, ActionRegisterer(AuiManager(self.frame),
63+
MenuBar(self.frame), ToolBar(self.frame),
64+
ShortcutRegistry(self.frame)), self.app.settings)
65+
# self.frame.Show()
66+
67+
def tearDown(self):
68+
PUBLISHER.unsubscribe_all()
69+
self.app.ExitMainLoop()
70+
self.app.Destroy()
71+
self.app = None
72+
# app.MainLoop() # With this here, there is no Segmentation fault
73+
74+
def _create_model(self):
75+
suite = self._create_directory_suite('/top_suite')
76+
suite.children = [self._create_file_suite('sub_suite_%d.robot' % i)
77+
for i in range(3)]
78+
res = ResourceFile()
79+
res.source = 'resource.robot'
80+
res.keyword_table.keywords.append(UserKeyword(res, 'Resource Keyword', ['en']))
81+
library_manager = LibraryManager(':memory:')
82+
library_manager.create_database()
83+
model = Project(self.app.namespace, library_manager=library_manager)
84+
model.controller = TestDataDirectoryController(suite)
85+
rfc = ResourceFileController(res, project=model)
86+
model.resources.append(rfc)
87+
model.insert_into_suite_structure(rfc)
88+
return model
89+
90+
def _create_directory_suite(self, source):
91+
return self._create_suite(TestDataDirectory, source, is_dir=True)
92+
93+
def _create_file_suite(self, source):
94+
suite = self._create_suite(TestCaseFile, source)
95+
suite.testcase_table.tests = [TestCase(suite, '%s Fake Test %d' % (suite.name, i)) for i in range(16)]
96+
return suite
97+
98+
@staticmethod
99+
def _create_suite(suite_class, source, is_dir=False):
100+
suite = suite_class()
101+
suite.source = source
102+
if is_dir:
103+
suite.directory = source
104+
suite.keyword_table.keywords = [UserKeyword(suite.keyword_table, '%s Fake UK %d' % (suite.name, i),
105+
['en'])
106+
for i in range(5)]
107+
return suite
108+
109+
110+
class TestMainFrame(_BaseDialogTest):
111+
112+
def test_show_mainframe(self):
113+
self.frame.Show()
114+
wx.CallLater(2000, self.app.ExitMainLoop)
115+
self.app.MainLoop()
116+
117+
def test_show_plugins_manager(self):
118+
self.frame.Show()
119+
plugins = self.loader.plugins
120+
self.frame._plugin_manager.show(plugins)
121+
wx.CallLater(5000, self.app.ExitMainLoop)
122+
wx.CallLater(4000, self.frame._plugin_manager._panel.Close)
123+
self.app.MainLoop()
124+
125+
126+
if __name__ == '__main__':
127+
unittest.main()
128+
app.Destroy()

0 commit comments

Comments
 (0)