| 1 |
# (C) Copyright 2007-2025 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 |
"""Tests for the PreferencesPane.""" |
| 12 |
|
| 13 |
import unittest |
| 14 |
|
| 15 |
from apptools.preferences.api import ( |
| 16 |
IPreferences, |
| 17 |
Preferences, |
| 18 |
PreferencesHelper, |
| 19 |
ScopedPreferences, |
| 20 |
) |
| 21 |
from traits.api import Instance, Str |
| 22 |
from traitsui.api import Item, View |
| 23 |
|
| 24 |
from envisage.ui.tasks.api import PreferencesPane |
| 25 |
|
| 26 |
|
| 27 |
class MyPreferencesHelper(PreferencesHelper): |
| 28 |
#: Redeclare preferences to force trait copying order. |
| 29 |
preferences = Instance(IPreferences) |
| 30 |
|
| 31 |
#: The node that contains the preferences. |
| 32 |
preferences_path = Str("app") |
| 33 |
|
| 34 |
#: The user's favourite colour |
| 35 |
color = Str() |
| 36 |
|
| 37 |
|
| 38 |
class MyPreferencesPane(PreferencesPane): |
| 39 |
model_factory = MyPreferencesHelper |
| 40 |
|
| 41 |
view = View(Item("color")) |
| 42 |
|
| 43 |
|
| 44 |
class TestPreferencesPane(unittest.TestCase): |
| 45 |
def test_no_preference_changes_without_apply(self): |
| 46 |
# Regression test for https://github.com/enthought/envisage/issues/582 |
| 47 |
|
| 48 |
# Given scoped preferences where the default preferences layer has |
| 49 |
# a value for app.color ... |
| 50 |
default_preferences = Preferences(name="default") |
| 51 |
application_preferences = Preferences(name="application") |
| 52 |
preferences = ScopedPreferences( |
| 53 |
scopes=[application_preferences, default_preferences] |
| 54 |
) |
| 55 |
default_preferences.set("app.color", "red") |
| 56 |
|
| 57 |
# When we create the preferences helper and pane, and trigger the |
| 58 |
# trait_context method (which will usually be called as part of |
| 59 |
# creating the TraitsUI UI). |
| 60 |
helper = MyPreferencesHelper(preferences=preferences) |
| 61 |
self.assertIsNone(application_preferences.get("app.color")) |
| 62 |
pane = MyPreferencesPane(model=helper) |
| 63 |
pane.trait_context()["object"] |
| 64 |
|
| 65 |
# Then the application preferences should not have been changed. |
| 66 |
self.assertIsNone(application_preferences.get("app.color")) |