blender/tests/python/ui_simulate/test_search_in_editors.py
Guillermo Venegas c40a5cd00f Refactor: UI: Activate area filters from hidden regions
This adds a more generalized way to operators to activate text buttons in
regions; this first tries to activate the text button in the region, and
if it fails it tags to rebuild the layout so there is a second chance to
the button to become available for activation.

This refactors how some operators activate text buttons, most of them
assumed the button were visible, and others immediately redrew the
region so the buttons can be made available. This instead makes the
button activation part of the block layout build.

This will affect Preferences, Properties, Space Text and Outliner.

Pull Request: https://projects.blender.org/blender/blender/pulls/161477
2026-09-03 16:07:51 +02:00

301 lines
9.2 KiB
Python

# SPDX-FileCopyrightText: 2026 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later
"""
This file does not run anything; its methods are accessed by run_blender_setup.py.
Tests for search/filter functionality in:
- Properties editor
- Outliner
- Dope Sheet
- Graph Editor
- File Browser
Requires: tests/files/ui_tests/test_search_in_editors.blend
Objects expected in that file:
__search_test_cube__ - mesh with a Subdivision Surface modifier
"""
import os
import modules.ui_test_utils as ui
_BLEND_FILE = os.path.join(
os.path.dirname(__file__),
"..", "..", "files", "ui_tests", "test_search_in_editors.blend",
)
def _set_area_type(area, area_type):
"""Switch *area* to *area_type*."""
import bpy
with bpy.context.temp_override(area=area):
area.type = area_type
def _load_blend():
"""Load the test blend file; re-acquire and return (e, t, window, area)."""
import bpy
assert os.path.isfile(_BLEND_FILE), f"Test blend file not found: {_BLEND_FILE}"
bpy.ops.wm.open_mainfile(filepath=_BLEND_FILE)
yield # wait for file load to complete
e, t, window = ui.test_window()
area = ui.largest_area(window.screen)
return e, t, window, area
def _op_hide_region_header():
from bpy.types import Operator
class TEST_PT_hide_region_header(Operator):
bl_label = "Test Hide Region Header Operator"
bl_idname = "test.hide_region_header"
def execute(self, context):
context.area.spaces.active.show_region_header = False
return {'FINISHED'}
return TEST_PT_hide_region_header
def _space_hide_region_header(space, e, t):
"""Hides the Header region in the space, search filter invocation (`Ctrl+F`) should un-hide this region to properly activate the filter button."""
yield from ui.call_menu(e, "Test Hide Region Header Operator")
t.assertFalse(space.show_region_header, "Header Region did not get hidden")
def _op_hide_region_channels():
from bpy.types import Operator
class TEST_PT_hide_region_channels(Operator):
bl_label = "Test Hide Region Channels Operator"
bl_idname = "test.hide_region_channels"
def execute(self, context):
context.area.spaces.active.show_region_channels = False
return {'FINISHED'}
return TEST_PT_hide_region_channels
def _space_hide_region_channels(space, e, t):
"""Hides the Channels region in the space, search filter invocation (`Ctrl+F`) should un-hide this region to properly activate the filter button."""
yield from ui.call_menu(e, "Test Hide Region Channels Operator")
t.assertFalse(space.show_region_channels, "Channels Region did not get hidden")
def _op_hide_region_ui():
from bpy.types import Operator
class TEST_PT_hide_region_ui(Operator):
bl_label = "Test Hide Region UI Operator"
bl_idname = "test.hide_region_ui"
def execute(self, context):
context.area.spaces.active.show_region_ui = False
return {'FINISHED'}
return TEST_PT_hide_region_ui
def _space_hide_region_ui(space, e, t):
"""Hides the UI region in the space, search filter invocation (`Ctrl+F`) should un-hide this region to properly activate the filter button."""
yield from ui.call_menu(e, "Test Hide Region UI Operator")
t.assertFalse(space.show_region_ui, "UI Region did not get hidden")
def test_properties_search():
"""
Properties editor - Ctrl+F -> 'subdivision'.
Verifies the search filter matches the Subdivision Surface modifier on
__search_test_cube__ and that clearing the field resets it.
"""
import bpy
e, t, window, area = yield from _load_blend()
op_hide_region_header = _op_hide_region_header()
bpy.utils.register_class(op_hide_region_header)
_set_area_type(area, 'PROPERTIES')
space = area.spaces.active
t.assertIsInstance(space, bpy.types.SpaceProperties, "Area did not switch to Properties editor")
t.assertIn("__search_test_cube__", bpy.data.objects, "Blend file is missing __search_test_cube__")
# Activate the object so its modifier panels are visible.
bpy.context.view_layer.objects.active = bpy.data.objects["__search_test_cube__"]
yield
e.cursor_position_set(*ui.get_area_center(area), move=True)
yield from _space_hide_region_header(space, e, t)
yield e.ctrl.f()
yield e.text("subdivision")
yield e.ret()
t.assertEqual(space.search_filter, "subdivision", "Properties: search_filter was not set by Ctrl+F")
yield from _space_hide_region_header(space, e, t)
yield e.ctrl.f()
yield e.back_space()
yield e.ret()
t.assertEqual(space.search_filter, "", "Properties: search_filter should be empty after clearing")
bpy.utils.unregister_class(op_hide_region_header)
def test_outliner_search():
"""
Outliner - Ctrl+F -> '__search_test_cube__'.
Verifies filter_text is set and cleared correctly.
"""
import bpy
e, t, window, area = yield from _load_blend()
op_hide_region_header = _op_hide_region_header()
bpy.utils.register_class(op_hide_region_header)
_set_area_type(area, 'OUTLINER')
space = area.spaces.active
t.assertIsInstance(space, bpy.types.SpaceOutliner, "Area did not switch to Outliner")
t.assertIn("__search_test_cube__", bpy.data.objects, "Blend file is missing __search_test_cube__")
e.cursor_position_set(*ui.get_area_center(area), move=True)
yield from _space_hide_region_header(space, e, t)
yield e.ctrl.f()
yield e.text("__search_test_cube__")
yield e.ret()
t.assertEqual(space.filter_text, "__search_test_cube__", "Outliner: filter_text was not set by Ctrl+F")
yield from _space_hide_region_header(space, e, t)
yield e.ctrl.f()
yield e.back_space()
yield e.ret()
t.assertEqual(space.filter_text, "", "Outliner: filter_text was not cleared")
bpy.utils.unregister_class(op_hide_region_header)
def test_dopesheet_search():
"""
Dope Sheet - Ctrl+F -> 'location'.
"""
import bpy
e, t, window, area = yield from _load_blend()
op_hide_region_channels = _op_hide_region_channels()
bpy.utils.register_class(op_hide_region_channels)
_set_area_type(area, 'DOPESHEET_EDITOR')
with bpy.context.temp_override(area=area):
area.spaces.active.ui_mode = 'DOPESHEET'
yield # wait for mode switch
space = area.spaces.active
t.assertIsInstance(space, bpy.types.SpaceDopeSheetEditor, "Area did not switch to Dope Sheet")
e.cursor_position_set(*ui.get_area_center(area), move=True)
yield from _space_hide_region_channels(space, e, t)
yield e.ctrl.f()
yield e.text("location")
yield e.ret()
t.assertEqual(space.dopesheet.filter_text, "location",
"Dope Sheet: filter_text was not set by Ctrl+F")
yield from _space_hide_region_channels(space, e, t)
yield e.ctrl.f()
yield e.back_space()
yield e.ret()
t.assertEqual(space.dopesheet.filter_text, "",
"Dope Sheet: filter_text was not cleared")
def test_graph_editor_search():
"""
Graph Editor - Ctrl+F -> 'location'.
"""
import bpy
e, t, window, area = yield from _load_blend()
op_hide_region_channels = _op_hide_region_channels()
bpy.utils.register_class(op_hide_region_channels)
_set_area_type(area, 'GRAPH_EDITOR')
space = area.spaces.active
t.assertIsInstance(space, bpy.types.SpaceGraphEditor, "Area did not switch to Graph Editor")
e.cursor_position_set(*ui.get_area_center(area), move=True)
yield from _space_hide_region_channels(space, e, t)
yield e.ctrl.f()
yield e.text("location")
yield e.ret()
t.assertEqual(space.dopesheet.filter_text, "location",
"Graph Editor: filter_text was not set by Ctrl+F")
yield from _space_hide_region_channels(space, e, t)
yield e.ctrl.f()
yield e.back_space()
yield e.ret()
t.assertEqual(space.dopesheet.filter_text, "",
"Graph Editor: filter_text was not cleared")
bpy.utils.unregister_class(op_hide_region_channels)
def test_file_browser_search():
"""
File Browser - Ctrl+F -> 'search_target'.
"""
import bpy
e, t, window, area = yield from _load_blend()
op_hide_region_ui = _op_hide_region_ui()
bpy.utils.register_class(op_hide_region_ui)
_set_area_type(area, 'FILE_BROWSER')
space = area.spaces.active
t.assertIsInstance(space, bpy.types.SpaceFileBrowser, "Area did not switch to File Browser")
blend_dir = os.path.dirname(_BLEND_FILE)
with bpy.context.temp_override(area=area):
bpy.ops.file.select_bookmark(dir=blend_dir)
yield # wait for navigation to complete
params = space.params
t.assertIsNotNone(params, "File Browser: params is None after navigation")
e.cursor_position_set(*ui.get_area_center(area), move=True)
yield from _space_hide_region_ui(space, e, t)
yield e.ctrl.f()
yield e.text("search_target")
yield e.ret()
t.assertEqual(params.filter_search, "search_target",
"File Browser: filter_search was not set by Ctrl+F")
yield from _space_hide_region_ui(space, e, t)
yield e.ctrl.f()
yield e.back_space()
yield e.ret()
t.assertEqual(params.filter_search, "", "File Browser: filter_search was not cleared")
bpy.utils.unregister_class(op_hide_region_ui)