>From ebfd0f5477f477c4790d0516ad32a8a0dc7513db Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 8 Mar 2015 01:55:22 +0100 Subject: [PATCH] Implement the revised calculation summary unit test specification. Don't paste the output for inspection any more, just check that the preferences dialog UI behaves correctly and results in correct columns in the illustrations. --- wx_test_calculation_summary.cpp | 414 ++++++++++++++++++++-------------------- 1 file changed, 205 insertions(+), 209 deletions(-) diff --git a/wx_test_calculation_summary.cpp b/wx_test_calculation_summary.cpp index 47528ff..64c2ed4 100644 --- a/wx_test_calculation_summary.cpp +++ b/wx_test_calculation_summary.cpp @@ -33,6 +33,8 @@ #include "wx_test_new.hpp" #include "wx_utility.hpp" +#include +#include #include #include #include @@ -66,104 +68,115 @@ struct name_and_title // summary. name_and_title const custom_columns_info[] = {{ "PolicyYear" , "Policy Year" } - ,{ "AttainedAge" , "Attained Age" } - ,{ "Outlay" , "Net Outlay" } - ,{ "CSVNet_Current" , "Curr Net Cash Surr Value" } - ,{ "AcctVal_Current" , "Curr Account Value" } - ,{ "CSVNet_Guaranteed" , "Guar Net Cash Surr Value" } - ,{ "AcctVal_Guaranteed" , "Guar Account Value" } - ,{ "EOYDeathBft_Current" , "Curr EOY Death Benefit" } - ,{ "EOYDeathBft_Guaranteed" , "Guar EOY Death Benefit" } - ,{ "NetWD" , "Withdrawal" } ,{ "NewCashLoan" , "Annual Loan" } - ,{ "LoanIntAccrued_Current" , "Curr Loan Int Accrued" } }; std::size_t const number_of_custom_columns = sizeof custom_columns_info / sizeof(custom_columns_info[0]); -// Special name used when the column is not used at all. +// Special name used when the column is not used at all. This is the same +// string used in preferences_model.cpp, but we duplicate it here as we don't +// have access to it. char const* const magic_null_column_name = "[none]"; -// Change the calculation summary settings in the preferences dialog to use, or -// not use, the built-in defaults. -void use_builtin_calculation_summary(bool b) +// Total number of configurable summary columns. This, again, duplicates the +// number [implicitly] used in preferences_model.cpp. +std::size_t const total_number_of_columns = 12; + +// Base class for all the tests working with the preferences dialog. It +// defines both a simpler interface for the derived classes to define the +// tests with the preferences dialog, and also provides a helper run() method +// which shows the preferences dialog and performs these checks. +class expect_preferences_dialog_base + :public wxExpectModalBase { - wxUIActionSimulator ui; - ui.Char('f', wxMOD_CONTROL); // "File|Preferences" + public: + expect_preferences_dialog_base() + :dialog_(NULL) + ,use_checkbox_(NULL) + { + } - class ChangeCalculationSummaryInPreferencesDialog - :public wxExpectModalBase - { - public: - ChangeCalculationSummaryInPreferencesDialog - (bool use_builtin_summary) - :use_builtin_summary_(use_builtin_summary) - { - } + void run() const + { + wxUIActionSimulator ui; + ui.Char('f', wxMOD_CONTROL); // "File|Preferences" - virtual int OnInvoked(MvcController* dialog) const - { - dialog->Show(); - wxYield(); + wxTEST_DIALOG(wxYield(), *this); + } - wxUIActionSimulator ui; + virtual int OnInvoked(MvcController* dialog) const + { + // OnInvoked() is const but it doesn't make much sense for + // OnPreferencesInvoked() to be const as it is going to modify the + // dialog, so cast away this constness once and for all. + expect_preferences_dialog_base* const + self = const_cast(this); - // Go to the "Use built-in calculation summary" checkbox. - ui.Char(WXK_TAB); - wxYield(); + self->dialog_ = dialog; - // Disable the checkbox initially as we need it to be disabled to - // change the values of the column controls. - ui.Char('-'); - wxYield(); + dialog->Show(); + wxYield(); - // Update the columns controls when using them. - for(std::size_t n = 0; n < number_of_custom_columns; ++n) - { - ui.Char(WXK_TAB); - wxYield(); + wxWindow* const use_window = wx_test_focus_controller_child + (*dialog + ,"UseBuiltinCalculationSummary" + ); - wxString const column_name - (use_builtin_summary_ - ? magic_null_column_name - : custom_columns_info[n].name - ); + self->use_checkbox_ = dynamic_cast(use_window); + LMI_ASSERT(use_checkbox_); - LMI_ASSERT(ui.Select(column_name)); + return self->OnPreferencesInvoked(); + } - wxYield(); - } + virtual wxString GetDefaultDescription() const + { + return "preferences dialog"; + } - // Finally return to the initial checkbox. - for(std::size_t n = 0; n < number_of_custom_columns; ++n) - { - ui.Char(WXK_TAB, wxMOD_SHIFT); - } + protected: + virtual int OnPreferencesInvoked() = 0; - wxYield(); + // Helpers for the derived classes OnPreferencesInvoked(). + void set_use_builtin_summary(bool use) + { + // Under MSW we could use "+" and "-" keys to set the checkbox value + // unconditionally, but these keys don't work under the other + // platforms, so it's simpler to use the space key which can be used on + // all platforms to toggle the checkbox -- but then we only must do it + // if really needed. + if(use_checkbox_->GetValue() != use) + { + use_checkbox_->SetFocus(); - // And set it to the desired value. - ui.Char(use_builtin_summary_ ? '+' : '-'); + wxUIActionSimulator ui; + ui.Char(WXK_SPACE); wxYield(); - - return wxID_OK; - } - - virtual wxString GetDefaultDescription() const - { - return "preferences dialog"; } + } - private: - bool const use_builtin_summary_; - }; + wxComboBox* focus_column_combobox(unsigned n) + { + wxWindow* const column_window = wx_test_focus_controller_child + (*dialog_ + ,wxString::Format("CalculationSummaryColumn%02u", n).c_str() + ); + + wxComboBox* const + column_combobox = dynamic_cast(column_window); + LMI_ASSERT_WITH_MSG + (column_combobox + ,"control for column #" << n << "is not a wxComboBox" + ); + + return column_combobox; + } - wxTEST_DIALOG - (wxYield() - ,ChangeCalculationSummaryInPreferencesDialog(b) - ); -} + // These variables are only valid inside the overridden + // OnPreferencesInvoked() method. + MvcController* dialog_; + wxCheckBox* use_checkbox_; +}; void check_calculation_summary_columns (std::size_t number_of_columns @@ -225,109 +238,8 @@ void use_builtin_calculation_summary(bool b) LMI_ASSERT_EQUAL(wxString(html, pos, 5), ""); } -// Save the current clipboard contents to a file with the given name, -// overwriting it if necessary. -void save_clipboard(wxString const& filename) -{ - wxFFile f(filename, "w"); - LMI_ASSERT(f.IsOpened()); - LMI_ASSERT(f.Write(wxString(ClipboardEx::GetText()))); - LMI_ASSERT(f.Close()); -} - -// Save the illustration calculation summary and full data to files with the -// given prefix. -void save_illustration_data(wxString const& prefix) -{ - wxUIActionSimulator ui; - ui.Char('c', wxMOD_CONTROL); // "Illustration|Copy calculation summary" - wxYield(); - save_clipboard(prefix + "IllSummary.txt"); - - ui.Char('d', wxMOD_CONTROL); // "Illustration|Copy full illustration data" - wxYield(); - save_clipboard(prefix + "IllFull.txt"); -} - } // Unnamed namespace. -/* - Start of the calculation summary unit test. - - This partially implements the first half of the item 8 of the - testing specification: - - 8. Validate Calculation summary and a few of its features. - - A. File | Preferences | check 'Use built-in calculation summary' - File | New | Illustration | Ok - Expected results: - These columns display in the calculation summary view: - Policy Year - Net Outlay - Curr Account Value - Curr Net Cash Surr Value - Curr EOY Death Benefit - Inspect 'configurable_settings.xml' for: - - - B. File | Preferences | uncheck 'Use built-in calculation summary' - select twelve different columns | inspect display for those columns - Expected results: - These columns display in the calculation summary view: - Policy Year - Attained Age - Net Outlay - Curr Net Cash Surr Value - Curr Account Value - Guar Net Cash Surr Value - Guar Account Value - Curr EOY Death Benefit - Guar EOY Death Benefit - Withdrawal - Annual Loan - Curr Loan Int Accrued - Inspect 'configurable_settings.xml' for: - AttainedAge PolicyYear Outlay \ - CSVNet_Current AcctVal_Current CSVNet_Guaranteed \ - AcctVal_Guaranteed EOYDeathBft_Current EOYDeathBft_Guaranteed \ - NetWD NewCashLoan LoanIntAccrued_Current - - C. File | New | Illustration - Illustration | Copy calculation summary - paste the output for inspection, then - Illustration | Copy full illustration data - paste the output for inspection - - D. File | New | Census - Census | Run case - Illustration | Copy calculation summary - paste the output for inspection, then - Illustration | Copy full illustration data - paste the output for inspection - [note--apparently this creates a file automatically] - - The output is pasted into 4 files called {New,Calc}Ill{Summary,Full}.txt in - the current working directory. - [note--we'd rather write them to the gui-test directory; but, as - noted above, (D) already writes its own file...and, as noted below, - it may be better to remove (C) and (D) from this file altogether.] - */ - -// ERASE THIS BLOCK COMMENT WHEN IMPLEMENTATION COMPLETE. The block -// comment below changes the original specification, and does not -// yet describe the present code. Desired changes: -// - Make sure default columns are used with '--distribution'. -// - Remove comment block (A) above: it's incorporated below. -// - Remove comment block (B) above: it's incorporated below, with -// only one column selected (twelve is overkill). -// - Remove comment blocks (C) and (D) above and their associated -// code: cf. 'wx_test_validate_output.cpp'. -// - Code the additional tests specified below. -// - Don't bother reading 'configurable_settings' directly: it cannot -// be wrong if all columns on the screen are correct. -// ERASABLE BLOCK COMMENT ENDS. - // Deferred ideas: // // Someday, test supplemental-report column selections similarly. @@ -349,7 +261,7 @@ void save_illustration_data(wxString const& prefix) /// File | Preferences /// make sure that "Use built-in calculation summary" is checked, and /// that the saved selections (those that would become active if the -/// checkbox were unchecked) exactly match the default selctions given +/// checkbox were unchecked) exactly match the default selections given /// by default_calculation_summary_columns(). /// /// Display an illustration, to see calculation-summary effects: @@ -358,8 +270,6 @@ void save_illustration_data(wxString const& prefix) /// File | Preferences /// uncheck "Use built-in calculation summary" /// set all "Column" controls to "[none]" -/// do this by simulating {Tab Home} repeatedly because that is -/// much faster and is what a smart user does /// in "Column 2" (two, not zero), select "NewCashLoan" /// OK /// Verify that the columns shown in the open illustration are exactly @@ -387,53 +297,139 @@ void save_illustration_data(wxString const& prefix) LMI_WX_TEST_CASE(calculation_summary) { - configurable_settings const& settings = configurable_settings::instance(); + if(is_distribution_test()) + { + // Not only is this the expected value in the GUI, but we also want to be + // sure that effective_calculation_summary_columns() returns the default + // columns in the code below -- and this is only the case when we are using + // the built-in calculation summary. + LMI_ASSERT + (configurable_settings::instance().use_builtin_calculation_summary() + ); + + struct verify_builtin_calculation_summary : expect_preferences_dialog_base + { + virtual int OnPreferencesInvoked() + { + LMI_ASSERT_EQUAL(use_checkbox_->GetValue(), true); + + std::vector const& + summary_columns = effective_calculation_summary_columns(); + + for(unsigned n = 0; n < number_of_custom_columns; ++n) + { + wxString const& column = focus_column_combobox(n)->GetValue(); + if(n < summary_columns.size()) + { + LMI_ASSERT_EQUAL(column, summary_columns[n]); + } + else + { + LMI_ASSERT_EQUAL(column, magic_null_column_name); + } + } + + return wxID_CANCEL; + } + }; - use_builtin_calculation_summary(true); + verify_builtin_calculation_summary().run(); + } - LMI_ASSERT(settings.calculation_summary_columns().empty()); - LMI_ASSERT(settings.use_builtin_calculation_summary()); + wx_test_new_illustration ill; - check_calculation_summary_columns - (number_of_default_columns - ,default_columns_info - ); + // Use a single "NewCashLoan" custom column in third position. + struct set_custom_columns_in_preferences_dialog : expect_preferences_dialog_base + { + virtual int OnPreferencesInvoked() + { + set_use_builtin_summary(false); - use_builtin_calculation_summary(false); + wxUIActionSimulator ui; + for(unsigned n = 0; n < total_number_of_columns; ++n) + { + focus_column_combobox(n); + ui.Select(n == 2 ? "NewCashLoan" : magic_null_column_name); + } - // Concatenate all the custom column names together. Notice that the - // trailing space is intentional as it is present in the configurable - // settings file too. - std::string all_custom_columns; - for(std::size_t n = 0; n < number_of_custom_columns; ++n) - { - all_custom_columns += custom_columns_info[n].name; - all_custom_columns += ' '; - } + return wxID_OK; + } + }; - LMI_ASSERT_EQUAL(settings.calculation_summary_columns(), all_custom_columns); - LMI_ASSERT(!settings.use_builtin_calculation_summary()); + set_custom_columns_in_preferences_dialog().run(); check_calculation_summary_columns (number_of_custom_columns ,custom_columns_info ); - wxUIActionSimulator ui; + // Now switch to using the default columns. + struct use_builtin_calculation_summary : expect_preferences_dialog_base + { + virtual int OnPreferencesInvoked() + { + // Before returning to the built-in summary, check that our custom + // value for the column #2 moved into the position #0 (because the + // first two columns were left unspecified). + LMI_ASSERT_EQUAL + (focus_column_combobox(0)->GetValue() + ,"NewCashLoan" + ); + + // And all the rest of the columns are (still) empty. + for(unsigned n = 1; n < total_number_of_columns; ++n) + { + LMI_ASSERT_EQUAL + (focus_column_combobox(n)->GetValue() + ,magic_null_column_name + ); + } - wx_test_new_illustration ill; - save_illustration_data("New"); - ill.close(); + set_use_builtin_summary(true); + + return wxID_OK; + } + }; + + use_builtin_calculation_summary().run(); + + check_calculation_summary_columns + (number_of_default_columns + ,default_columns_info + ); + + // Finally, switch back to the previously configured custom columns. + struct use_custom_calculation_summary : expect_preferences_dialog_base + { + public: + virtual int OnPreferencesInvoked() + { + set_use_builtin_summary(false); - wx_test_new_census census; - ui.Char('r', wxMOD_CONTROL | wxMOD_SHIFT); // "Census|Run case" - wxYield(); + // The custom columns shouldn't have changed. + LMI_ASSERT_EQUAL + (focus_column_combobox(0)->GetValue() + ,"NewCashLoan" + ); - save_illustration_data("Calc"); + for(unsigned n = 1; n < total_number_of_columns; ++n) + { + LMI_ASSERT_EQUAL + (focus_column_combobox(n)->GetValue() + ,magic_null_column_name + ); + } + + return wxID_OK; + } + }; - // Close the illustration opened by "Run case". - ui.Char('l', wxMOD_CONTROL); // "File|Close" - wxYield(); + use_custom_calculation_summary().run(); - census.close(); + check_calculation_summary_columns + (number_of_custom_columns + ,custom_columns_info + ); + + ill.close(); } -- 2.1.0