>From 95960722b42b92cc11cfa940b2b0ab544cda79bf Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 12 Mar 2015 17:28:34 +0100 Subject: [PATCH 1/3] Move wx_test_new_document_base into a separate header. Also rename this class to just wx_test_document_base as it will be used for the existing, and not only new, documents now as well. --- wx_test_document.hpp | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++ wx_test_new.hpp | 114 ++---------------------------------------- 2 files changed, 142 insertions(+), 111 deletions(-) create mode 100644 wx_test_document.hpp diff --git a/wx_test_document.hpp b/wx_test_document.hpp new file mode 100644 index 0000000..3fb75fb --- /dev/null +++ b/wx_test_document.hpp @@ -0,0 +1,139 @@ +// Helper for creating documents in unattended GUI tests. +// +// Copyright (C) 2014, 2015 Gregory W. Chicares. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA +// +// http://savannah.nongnu.org/projects/lmi +// email: +// snail: Chicares, 186 Belle Woods Drive, Glastonbury CT 06033, USA + +// $Id$ + +#ifndef wx_test_document_hpp +#define wx_test_document_hpp + +#include "config.hpp" + +#include "mvc_controller.hpp" +#include "uncopyable_lmi.hpp" + +#include +#include +#include + +#include + +/// Helper function for finding and focusing a control with the specified name +/// inside MvcController (actually it could be any top level window containing +/// a book control). +/// +/// Returns the never null pointer to the window. +/// +/// Throws if the control couldn't be found. +wxWindow* wx_test_focus_controller_child(MvcController& dialog, char const* name); + +/// Helper base class for classes creating or opening documents. +/// +/// This class provides methods for closing the current document, optionally +/// discarding the changes done to it. +/// +/// Unfortunately it is impossible to close the document automatically from +/// this class dtor as doing this may result in an exception and throwing from +/// dtors is too dangerous, generally speaking (and not allowed at all by +/// default since C++11), to prefer it to an approach involving explicit calls +/// to close(). + +class wx_test_document_base + :private lmi::uncopyable +{ + public: + wx_test_document_base() + :opened_(false) + { + } + + ~wx_test_document_base() + { + // Normally either close() or close_discard_changes() should be called, + // so complain about forgetting to do this if neither was. Except that + // we shouldn't do this if we're unwinding due to an exception from a + // test failure, as this is not a bug in the test code then. + if(opened_) + { + if(std::uncaught_exception()) + { + // Moreover, in case of exception, try to close the window to + // avoid showing message boxes asking the user if it should be + // saved: this is undesirable in an unattended test. + do_close(); + + wxTEST_DIALOG + (wxYield() + ,wxExpectModal(wxNO).Optional() + ); + } + else + { + wxSafeShowMessage + ("Programming error" + ,"A document created during unattended test hasn't been closed, " + "please report this." + ); + } + } + } + + // Close the document window, the document must not be modified. + void close() + { + do_close(); + + wxYield(); + } + + // Close the document window, the document must have been modified and the + // changes to it will be discarded. + void close_discard_changes() + { + do_close(); + + wxTEST_DIALOG(wxYield() + ,wxExpectModal(wxNO). + Describe("message box confirming closing modified file") + ); + } + + protected: + // This method should be called by the derived classes when the document + // window is really opened. + void set_opened() { opened_ = true; } + + private: + // Common part of different close() methods. + void do_close() + { + // If we started closing the document, we should reset the flag: even + // if closing it fails, we shouldn't complain about forgetting to close + // it as we clearly didn't forget to do it. + opened_ = false; + + wxUIActionSimulator ui; + ui.Char('l', wxMOD_CONTROL); // "File|Close" + } + + bool opened_; +}; + +#endif // wx_test_document_hpp diff --git a/wx_test_new.hpp b/wx_test_new.hpp index 5bb21f9..5646169 100644 --- a/wx_test_new.hpp +++ b/wx_test_new.hpp @@ -26,115 +26,7 @@ #include "config.hpp" -#include "mvc_controller.hpp" -#include "uncopyable_lmi.hpp" - -#include -#include -#include - -#include - -/// Helper function for finding and focusing a control with the specified name -/// inside MvcController (actually it could be any top level window containing -/// a book control). -/// -/// Returns the never null pointer to the window. -/// -/// Throws if the control couldn't be found. -wxWindow* wx_test_focus_controller_child(MvcController& dialog, char const* name); - -/// Helper base class for classes testing creation of specific new documents. -/// -/// This class provides methods for closing the current document, optionally -/// discarding the changes done to it. -/// -/// Unfortunately it is impossible to close the document automatically from -/// this class dtor as doing this may result in an exception and throwing from -/// dtors is too dangerous, generally speaking (and not allowed at all by -/// default since C++11), to prefer it to an approach involving explicit calls -/// to close(). - -class wx_test_new_document_base - :private lmi::uncopyable -{ - public: - wx_test_new_document_base() - :opened_(false) - { - } - - ~wx_test_new_document_base() - { - // Normally either close() or close_discard_changes() should be called, - // so complain about forgetting to do this if neither was. Except that - // we shouldn't do this if we're unwinding due to an exception from a - // test failure, as this is not a bug in the test code then. - if(opened_) - { - if(std::uncaught_exception()) - { - // Moreover, in case of exception, try to close the window to - // avoid showing message boxes asking the user if it should be - // saved: this is undesirable in an unattended test. - do_close(); - - wxTEST_DIALOG - (wxYield() - ,wxExpectModal(wxNO).Optional() - ); - } - else - { - wxSafeShowMessage - ("Programming error" - ,"A document created during unattended test hasn't been closed, " - "please report this." - ); - } - } - } - - // Close the document window, the document must not be modified. - void close() - { - do_close(); - - wxYield(); - } - - // Close the document window, the document must have been modified and the - // changes to it will be discarded. - void close_discard_changes() - { - do_close(); - - wxTEST_DIALOG(wxYield() - ,wxExpectModal(wxNO). - Describe("message box confirming closing modified file") - ); - } - - protected: - // This method should be called by the derived classes when the document - // window is really opened. - void set_opened() { opened_ = true; } - - private: - // Common part of different close() methods. - void do_close() - { - // If we started closing the document, we should reset the flag: even - // if closing it fails, we shouldn't complain about forgetting to close - // it as we clearly didn't forget to do it. - opened_ = false; - - wxUIActionSimulator ui; - ui.Char('l', wxMOD_CONTROL); // "File|Close" - } - - bool opened_; -}; +#include "wx_test_document.hpp" /// Represents a new illustration document. /// @@ -143,7 +35,7 @@ class wx_test_new_document_base /// to ensure that it doesn't stay open. class wx_test_new_illustration - :public wx_test_new_document_base + :public wx_test_document_base { public: // Default constructor creates an illustration with the default parameters. @@ -182,7 +74,7 @@ class wx_test_new_illustration /// destroying it. class wx_test_new_census - :public wx_test_new_document_base + :public wx_test_document_base { public: wx_test_new_census() -- 2.1.0