KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_editor_tabs_panel.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#define BOOST_TEST_NO_MAIN
21#include <boost/test/unit_test.hpp>
22
25
26
27BOOST_AUTO_TEST_SUITE( EditorTabsPanel )
28
29
30BOOST_AUTO_TEST_CASE( TabVisualState )
31{
32 BOOST_CHECK( ResolveTabVisualState( /*preview*/ true, false ).italic );
33
34 TAB_VISUAL_STATE mod = ResolveTabVisualState( false, /*modified*/ true );
35 BOOST_CHECK( mod.bold && mod.showAsterisk && !mod.italic );
36
37 TAB_VISUAL_STATE plain = ResolveTabVisualState( false, false );
38 BOOST_CHECK( !plain.italic && !plain.bold && !plain.showAsterisk );
39}
40
41
42BOOST_AUTO_TEST_CASE( TabVisualStateModifiedPreviewIsModified )
43{
44 // A modified document is never shown as a preview, so modified wins over italic.
45 TAB_VISUAL_STATE st = ResolveTabVisualState( /*preview*/ true, /*modified*/ true );
46
47 BOOST_CHECK( st.bold );
48 BOOST_CHECK( st.showAsterisk );
49 BOOST_CHECK( !st.italic );
50}
51
52
53BOOST_AUTO_TEST_CASE( PreviewReuse )
54{
56 int a = m.OpenDocument( "lib:A", /*asPreview*/ true );
57 int b = m.OpenDocument( "lib:B", /*asPreview*/ true );
58
59 BOOST_CHECK_EQUAL( a, b );
60 BOOST_CHECK_EQUAL( m.Entries().size(), 1u );
61 BOOST_CHECK_EQUAL( m.Entries()[0].key, "lib:B" );
62 BOOST_CHECK( m.Entries()[0].preview );
63}
64
65
66BOOST_AUTO_TEST_CASE( EditPromotesPreview )
67{
69 m.OpenDocument( "lib:A", true );
70 m.MarkModified( "lib:A", true );
71
72 int c = m.OpenDocument( "lib:B", true ); // must not replace the now-modified A
73
74 BOOST_CHECK_EQUAL( m.Entries().size(), 2u );
75 BOOST_CHECK_EQUAL( c, 1 );
76 BOOST_CHECK( !m.Entries()[0].preview );
77}
78
79
80BOOST_AUTO_TEST_CASE( PromoteClearsPreview )
81{
83 m.OpenDocument( "lib:A", true );
84
85 BOOST_CHECK( m.Entries()[0].preview );
87
88 m.Promote( "lib:A" );
89
90 BOOST_CHECK( !m.Entries()[0].preview );
92
93 // A promoted tab is no longer reused, so the next preview opens its own tab.
94 m.OpenDocument( "lib:B", true );
95 BOOST_CHECK_EQUAL( m.Entries().size(), 2u );
96}
97
98
99BOOST_AUTO_TEST_CASE( ModifiedNeedsPrompt )
100{
102 m.OpenDocument( "lib:A", false );
103 BOOST_CHECK( m.CanCloseWithoutPrompt( "lib:A" ) );
104
105 m.MarkModified( "lib:A", true );
106 BOOST_CHECK( !m.CanCloseWithoutPrompt( "lib:A" ) );
107}
108
109
110BOOST_AUTO_TEST_CASE( ReopenExistingKeyDoesNotDuplicate )
111{
113 int a = m.OpenDocument( "lib:A", false );
114 int again = m.OpenDocument( "lib:A", false );
115
116 BOOST_CHECK_EQUAL( a, again );
117 BOOST_CHECK_EQUAL( m.Entries().size(), 1u );
118}
119
120
121BOOST_AUTO_TEST_CASE( PermanentOpenDoesNotReusePreview )
122{
124 m.OpenDocument( "lib:A", /*asPreview*/ true );
125 int b = m.OpenDocument( "lib:B", /*asPreview*/ false );
126
127 BOOST_CHECK_EQUAL( m.Entries().size(), 2u );
128 BOOST_CHECK_EQUAL( b, 1 );
129 BOOST_CHECK( !m.Entries()[1].preview );
130}
131
132
133BOOST_AUTO_TEST_CASE( ModifiedPreviewBecomesPermanentForReuse )
134{
136 m.OpenDocument( "lib:A", true );
137 m.MarkModified( "lib:A", true );
138 m.MarkModified( "lib:A", false );
139
140 // The tab stays permanent once promoted, even after the dirty flag clears.
141 int b = m.OpenDocument( "lib:B", true );
142 BOOST_CHECK_EQUAL( m.Entries().size(), 2u );
143 BOOST_CHECK_EQUAL( b, 1 );
144}
145
146
147BOOST_AUTO_TEST_CASE( CloseRemovesEntryAndClearsPreviewIndex )
148{
150 m.OpenDocument( "lib:A", true );
151 m.CloseDocument( "lib:A" );
152
153 BOOST_CHECK_EQUAL( m.Entries().size(), 0u );
154
155 // The preview slot is freed, so the next preview opens fresh.
156 int b = m.OpenDocument( "lib:B", true );
157 BOOST_CHECK_EQUAL( b, 0 );
158 BOOST_CHECK_EQUAL( m.Entries().size(), 1u );
159}
160
161
GUI-free preview/dirty/close state machine.
const std::vector< ENTRY > & Entries() const
int PreviewIndex() const
Index of the current reusable preview tab, or -1 if none.
bool CanCloseWithoutPrompt(const wxString &aKey) const
True when the document has no unsaved edits and can be closed silently.
void Promote(const wxString &aKey)
Clear the preview flag so the tab becomes permanent and is no longer reused.
void MarkModified(const wxString &aKey, bool aModified)
Update the modified flag.
int OpenDocument(const wxString &aKey, bool aAsPreview)
Return the index to display the document at, reusing a preview slot or appending a new tab.
void CloseDocument(const wxString &aKey)
Drop the document with aKey, freeing the preview slot if it held it.
TAB_VISUAL_STATE ResolveTabVisualState(bool aPreview, bool aModified)
Resolve a tab's decorations from its document state flags.
Visual decorations derived from document state: preview is italic, modified is bold with a leading as...
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(TabVisualState)
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")