Skip to content

Commit d0969d6

Browse files
authored
bpo-30981: IDLE -- Add more configdialog font page tests. (#2794)
1 parent a54a8f1 commit d0969d6

File tree

3 files changed

+54
-63
lines changed

3 files changed

+54
-63
lines changed

Lib/idlelib/configdialog.py

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ def create_page_font_tab(self):
154154
"""Return frame of widgets for Font/Tabs tab.
155155
156156
Tk Variables:
157+
font_name: Font face.
157158
font_size: Font size.
158159
font_bold: Select font bold or not.
159-
font_name: Font face.
160160
Note: these 3 share var_changed_font callback.
161161
space_num: Indentation width.
162162
@@ -167,8 +167,7 @@ def create_page_font_tab(self):
167167
load_font_cfg: Set vars and fontlist.
168168
on_fontlist_select: Bound to fontlist button release
169169
or key release.
170-
set_font_sample: Command for opt_menu_font_size and
171-
check_font_bold.
170+
set_samples: Notify both samples of any font change.
172171
load_tab_cfg: Get current.
173172
174173
Widget Structure: (*) widgets bound to self
@@ -181,7 +180,7 @@ def create_page_font_tab(self):
181180
frame_font_param: Frame
182181
font_size_title: Label
183182
(*)opt_menu_font_size: DynOptionMenu - font_size
184-
check_font_bold: Checkbutton - font_bold
183+
bold_toggle: Checkbutton - font_bold
185184
frame_font_sample: Frame
186185
(*)font_sample: Label
187186
frame_indent: LabelFrame
@@ -190,9 +189,9 @@ def create_page_font_tab(self):
190189
(*)scale_indent_size: Scale - space_num
191190
"""
192191
parent = self.parent
192+
self.font_name = StringVar(parent)
193193
self.font_size = StringVar(parent)
194194
self.font_bold = BooleanVar(parent)
195-
self.font_name = StringVar(parent)
196195
self.space_num = IntVar(parent)
197196
self.edit_font = tkFont.Font(parent, ('courier', 10, 'normal'))
198197

@@ -218,10 +217,10 @@ def create_page_font_tab(self):
218217
self.fontlist.config(yscrollcommand=scroll_font.set)
219218
font_size_title = Label(frame_font_param, text='Size :')
220219
self.opt_menu_font_size = DynOptionMenu(
221-
frame_font_param, self.font_size, None, command=self.set_font_sample)
222-
check_font_bold = Checkbutton(
220+
frame_font_param, self.font_size, None, command=self.set_samples)
221+
bold_toggle = Checkbutton(
223222
frame_font_param, variable=self.font_bold, onvalue=1,
224-
offvalue=0, text='Bold', command=self.set_font_sample)
223+
offvalue=0, text='Bold', command=self.set_samples)
225224
frame_font_sample = Frame(frame_font, relief=SOLID, borderwidth=1)
226225
self.font_sample = Label(
227226
frame_font_sample, justify=LEFT, font=self.edit_font,
@@ -247,7 +246,7 @@ def create_page_font_tab(self):
247246
scroll_font.pack(side=LEFT, fill=Y)
248247
font_size_title.pack(side=LEFT, anchor=W)
249248
self.opt_menu_font_size.pack(side=LEFT, anchor=W)
250-
check_font_bold.pack(side=LEFT, anchor=W, padx=20)
249+
bold_toggle.pack(side=LEFT, anchor=W, padx=20)
251250
frame_font_sample.pack(side=TOP, padx=5, pady=5, expand=TRUE, fill=BOTH)
252251
self.font_sample.pack(expand=TRUE, fill=BOTH)
253252
# frame_indent
@@ -292,7 +291,7 @@ def create_page_highlight(self):
292291
Widget Structure: (*) widgets bound to self
293292
frame
294293
frame_custom: LabelFrame
295-
(*)text_highlight_sample: Text
294+
(*)highlight_sample: Text
296295
(*)frame_color_set: Frame
297296
button_set_color: Button
298297
(*)opt_menu_highlight_target: DynOptionMenu - highlight_target
@@ -342,11 +341,11 @@ def create_page_highlight(self):
342341
frame_theme = LabelFrame(frame, borderwidth=2, relief=GROOVE,
343342
text=' Highlighting Theme ')
344343
#frame_custom
345-
self.text_highlight_sample=Text(
344+
self.highlight_sample=Text(
346345
frame_custom, relief=SOLID, borderwidth=1,
347346
font=('courier', 12, ''), cursor='hand2', width=21, height=11,
348347
takefocus=FALSE, highlightthickness=0, wrap=NONE)
349-
text=self.text_highlight_sample
348+
text=self.highlight_sample
350349
text.bind('<Double-Button-1>', lambda e: 'break')
351350
text.bind('<B1-Motion>', lambda e: 'break')
352351
text_and_tags=(
@@ -416,7 +415,7 @@ def tem(event, elem=element):
416415
#frame_custom
417416
self.frame_color_set.pack(side=TOP, padx=5, pady=5, expand=TRUE, fill=X)
418417
frame_fg_bg_toggle.pack(side=TOP, padx=5, pady=0)
419-
self.text_highlight_sample.pack(
418+
self.highlight_sample.pack(
420419
side=TOP, padx=5, pady=5, expand=TRUE, fill=BOTH)
421420
button_set_color.pack(side=TOP, expand=TRUE, fill=X, padx=8, pady=4)
422421
self.opt_menu_highlight_target.pack(
@@ -1142,7 +1141,7 @@ def on_new_color_set(self):
11421141
self.frame_color_set.config(bg=new_color) # Set sample.
11431142
plane ='foreground' if self.fg_bg_toggle.get() else 'background'
11441143
sample_element = self.theme_elements[self.highlight_target.get()][0]
1145-
self.text_highlight_sample.tag_config(sample_element, **{plane:new_color})
1144+
self.highlight_sample.tag_config(sample_element, **{plane:new_color})
11461145
theme = self.custom_theme.get()
11471146
theme_element = sample_element + '-' + plane
11481147
changes.add_option('highlight', theme, theme_element, new_color)
@@ -1210,41 +1209,25 @@ def on_fontlist_select(self, event):
12101209
"""Handle selecting a font from the list.
12111210
12121211
Event can result from either mouse click or Up or Down key.
1213-
Set font_name and example display to selection.
1214-
1215-
Attributes updated:
1216-
font_name: Set to name selected from fontlist.
1217-
1218-
Methods:
1219-
set_font_sample
1212+
Set font_name and example displays to selection.
12201213
"""
12211214
font = self.fontlist.get(
12221215
ACTIVE if event.type.name == 'KeyRelease' else ANCHOR)
12231216
self.font_name.set(font.lower())
1224-
self.set_font_sample()
1225-
1226-
def set_font_sample(self, event=None):
1227-
"""Update the screen samples with the font settings from the dialog.
1217+
self.set_samples()
12281218

1229-
Attributes accessed:
1230-
font_name
1231-
font_bold
1232-
font_size
1219+
def set_samples(self, event=None):
1220+
"""Update update both screen samples with the font settings.
12331221
1234-
Attributes updated:
1235-
font_sample: Set to selected font name, size, and weight.
1236-
text_highlight_sample: Set to selected font name, size, and weight.
1237-
1238-
Called from:
1239-
handler for opt_menu_font_size and check_font_bold
1240-
on_fontlist_select
1241-
load_font_cfg
1222+
Called on font initialization and change events.
1223+
Accesses font_name, font_size, and font_bold Variables.
1224+
Updates font_sample and hightlight page highlight_sample.
12421225
"""
12431226
font_name = self.font_name.get()
12441227
font_weight = tkFont.BOLD if self.font_bold.get() else tkFont.NORMAL
12451228
new_font = (font_name, self.font_size.get(), font_weight)
1246-
self.font_sample.config(font=new_font)
1247-
self.text_highlight_sample.configure(font=new_font)
1229+
self.font_sample['font'] = new_font
1230+
self.highlight_sample['font'] = new_font
12481231

12491232
def set_highlight_target(self):
12501233
"""Set fg/bg toggle and color based on highlight tag target.
@@ -1289,15 +1272,15 @@ def set_color_sample(self):
12891272
theme_elements
12901273
highlight_target
12911274
fg_bg_toggle
1292-
text_highlight_sample
1275+
highlight_sample
12931276
12941277
Attributes updated:
12951278
frame_color_set
12961279
"""
12971280
# Set the color sample area.
12981281
tag = self.theme_elements[self.highlight_target.get()][0]
12991282
plane = 'foreground' if self.fg_bg_toggle.get() else 'background'
1300-
color = self.text_highlight_sample.tag_cget(tag, plane)
1283+
color = self.highlight_sample.tag_cget(tag, plane)
13011284
self.frame_color_set.config(bg=color)
13021285

13031286
def paint_theme_sample(self):
@@ -1310,7 +1293,7 @@ def paint_theme_sample(self):
13101293
custom_theme
13111294
13121295
Attributes updated:
1313-
text_highlight_sample: Set the tag elements to the theme.
1296+
highlight_sample: Set the tag elements to the theme.
13141297
13151298
Methods:
13161299
set_color_sample
@@ -1337,7 +1320,7 @@ def paint_theme_sample(self):
13371320
colors['foreground'] = theme_dict[element + '-foreground']
13381321
if element + '-background' in theme_dict:
13391322
colors['background'] = theme_dict[element + '-background']
1340-
self.text_highlight_sample.tag_config(element, **colors)
1323+
self.highlight_sample.tag_config(element, **colors)
13411324
self.set_color_sample()
13421325

13431326
def help_source_selected(self, event):
@@ -1424,7 +1407,7 @@ def load_font_cfg(self):
14241407
font_bold: Set to current font weight.
14251408
14261409
Methods:
1427-
set_font_sample
1410+
set_samples
14281411
"""
14291412
# Set base editor font selection list.
14301413
fonts = list(tkFont.families(self))
@@ -1452,7 +1435,7 @@ def load_font_cfg(self):
14521435
# Set font weight.
14531436
self.font_bold.set(font_bold)
14541437
# Set font sample.
1455-
self.set_font_sample()
1438+
self.set_samples()
14561439

14571440
def load_tab_cfg(self):
14581441
"""Load current configuration settings for the tab options.

Lib/idlelib/idle_test/test_configdialog.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,12 @@
2828
highpage = changes['highlight']
2929
keyspage = changes['keys']
3030

31-
32-
class TestDialog(ConfigDialog):
33-
pass # Delete?
34-
35-
3631
def setUpModule():
3732
global root, dialog
3833
idleConf.userCfg = testcfg
3934
root = Tk()
4035
# root.withdraw() # Comment out, see issue 30870
41-
dialog = TestDialog(root, 'Test', _utest=True)
42-
36+
dialog = ConfigDialog(root, 'Test', _utest=True)
4337

4438
def tearDownModule():
4539
global root, dialog
@@ -56,7 +50,7 @@ class FontTabTest(unittest.TestCase):
5650
def setUp(self):
5751
changes.clear()
5852

59-
def test_font(self):
53+
def test_font_set(self):
6054
# Set values guaranteed not to be defaults.
6155
default_font = idleConf.GetFont(root, 'main', 'EditorWindow')
6256
default_size = str(default_font[1])
@@ -79,9 +73,22 @@ def test_font(self):
7973
'font-bold': str(not default_bold)}}
8074
self.assertEqual(mainpage, expected)
8175

82-
def test_set_sample(self):
83-
# Set_font_sample also sets highlight_sample.
84-
pass
76+
def test_bold_toggle(self):
77+
d = dialog
78+
d.set_samples = Func()
79+
d.bold_toggle.toggle()
80+
self.assertEqual(d.set_samples.called, 1)
81+
82+
def test_set_samples(self):
83+
d = dialog
84+
d.font_sample, d.highlight_sample = {}, {}
85+
d.font_name.set('test')
86+
d.font_size.set('5')
87+
d.font_bold.set(1)
88+
d.set_samples()
89+
expected = {'font': ('test', '5', 'bold')}
90+
self.assertTrue(d.font_sample == d.highlight_sample == expected)
91+
del d.font_sample, d.highlight_sample
8592

8693
def test_tabspace(self):
8794
dialog.space_num.set(6)
@@ -90,22 +97,22 @@ def test_tabspace(self):
9097

9198
class FontSelectTest(unittest.TestCase):
9299
# These two functions test that selecting a new font in the
93-
# list of fonts changes font_name and calls set_font_sample.
100+
# list of fonts changes font_name and calls set_samples.
94101
# The fontlist widget and on_fontlist_select event handler
95102
# are tested here together.
96103

97104
@classmethod
98105
def setUpClass(cls):
99106
if dialog.fontlist.size() < 2:
100107
cls.skipTest('need at least 2 fonts')
101-
dialog.set_font_sample = Func() # Mask instance method.
108+
dialog.set_samples = Func() # Mask instance method.
102109

103110
@classmethod
104111
def tearDownClass(cls):
105-
del dialog.set_font_sample # Unmask instance method.
112+
del dialog.set_samples # Unmask instance method.
106113

107114
def setUp(self):
108-
dialog.set_font_sample.called = 0
115+
dialog.set_samples.called = 0
109116
changes.clear()
110117

111118
def test_select_font_key(self):
@@ -124,7 +131,7 @@ def test_select_font_key(self):
124131
down_font = fontlist.get('active')
125132
self.assertNotEqual(down_font, font)
126133
self.assertIn(dialog.font_name.get(), down_font.lower())
127-
self.assertEqual(dialog.set_font_sample.called, 1)
134+
self.assertEqual(dialog.set_samples.called, 1)
128135

129136
# Test Up key.
130137
fontlist.focus_force()
@@ -135,7 +142,7 @@ def test_select_font_key(self):
135142
up_font = fontlist.get('active')
136143
self.assertEqual(up_font, font)
137144
self.assertIn(dialog.font_name.get(), up_font.lower())
138-
self.assertEqual(dialog.set_font_sample.called, 2)
145+
self.assertEqual(dialog.set_samples.called, 2)
139146

140147
def test_select_font_mouse(self):
141148
# Click on item should select that item.
@@ -157,7 +164,7 @@ def test_select_font_mouse(self):
157164
select_font = fontlist.get('anchor')
158165
self.assertEqual(select_font, font1)
159166
self.assertIn(dialog.font_name.get(), font1.lower())
160-
self.assertEqual(dialog.set_font_sample.called, 1)
167+
self.assertEqual(dialog.set_samples.called, 1)
161168

162169

163170
class HighlightTest(unittest.TestCase):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
IDLE -- Add more configdialog font page tests.

0 commit comments

Comments
 (0)