Improve translated mnemonics unit test

Don't switch locale to Japanese, which might not be supported, but just use
wxTranslations directly because we only need translations, not full locale
support.

Also rename the test method to a more descriptive name.
This commit is contained in:
Vadim Zeitlin 2016-06-24 23:56:49 +02:00
parent 13068d3603
commit 68b2e06f70

View File

@ -21,6 +21,7 @@
#endif // WX_PRECOMP #endif // WX_PRECOMP
#include "wx/menu.h" #include "wx/menu.h"
#include "wx/translation.h"
#include "wx/uiaction.h" #include "wx/uiaction.h"
#include <stdarg.h> #include <stdarg.h>
@ -86,7 +87,9 @@ private:
CPPUNIT_TEST( EnableTop ); CPPUNIT_TEST( EnableTop );
CPPUNIT_TEST( Count ); CPPUNIT_TEST( Count );
CPPUNIT_TEST( Labels ); CPPUNIT_TEST( Labels );
CPPUNIT_TEST( i18n ); #if wxUSE_INTL
CPPUNIT_TEST( TranslatedMnemonics );
#endif // wxUSE_INTL
CPPUNIT_TEST( RadioItems ); CPPUNIT_TEST( RadioItems );
CPPUNIT_TEST( RemoveAdd ); CPPUNIT_TEST( RemoveAdd );
CPPUNIT_TEST( ChangeBitmap ); CPPUNIT_TEST( ChangeBitmap );
@ -100,7 +103,9 @@ private:
void EnableTop(); void EnableTop();
void Count(); void Count();
void Labels(); void Labels();
void i18n(); #if wxUSE_INTL
void TranslatedMnemonics();
#endif // wxUSE_INTL
void RadioItems(); void RadioItems();
void RemoveAdd(); void RemoveAdd();
void ChangeBitmap(); void ChangeBitmap();
@ -336,35 +341,57 @@ void MenuTestCase::Labels()
CPPUNIT_ASSERT_EQUAL( "Foo", wxMenuItem::GetLabelText("&Foo\tCtrl-F") ); CPPUNIT_ASSERT_EQUAL( "Foo", wxMenuItem::GetLabelText("&Foo\tCtrl-F") );
} }
void MenuTestCase::i18n() #if wxUSE_INTL
static wxString
GetTranslatedString(const wxTranslations& trans, const wxString& s)
{
const wxString* t = trans.GetTranslatedString(s);
return t ? *t : s;
}
void MenuTestCase::TranslatedMnemonics()
{ {
// Check that appended mnemonics are correctly stripped; // Check that appended mnemonics are correctly stripped;
// see http://trac.wxwidgets.org/ticket/16736 // see http://trac.wxwidgets.org/ticket/16736
wxLocale::AddCatalogLookupPathPrefix("./intl"); wxTranslations trans;
CPPUNIT_ASSERT( wxLocale::IsAvailable(wxLANGUAGE_JAPANESE) ); trans.SetLanguage(wxLANGUAGE_JAPANESE);
wxFileTranslationsLoader::AddCatalogLookupPathPrefix("./intl");
wxLocale locale(wxLANGUAGE_JAPANESE, wxLOCALE_DONT_LOAD_DEFAULT); CPPUNIT_ASSERT( trans.AddCatalog("internat") );
locale.AddCatalog("internat");
// Check the translation is being used: // Check the translation is being used:
CPPUNIT_ASSERT( wxString("&File") != wxString(_("&File")) ); CPPUNIT_ASSERT( wxString("&File") != GetTranslatedString(trans, "&File") );
wxString filemenu = m_frame->GetMenuBar()->GetMenuLabel(0); wxString filemenu = m_frame->GetMenuBar()->GetMenuLabel(0);
CPPUNIT_ASSERT_EQUAL CPPUNIT_ASSERT_EQUAL
( wxStripMenuCodes(_("&File")), (
wxStripMenuCodes(wxGetTranslation(filemenu)) wxStripMenuCodes(GetTranslatedString(trans, "&File")),
); wxStripMenuCodes(GetTranslatedString(trans, filemenu))
);
// Test strings that have shortcuts. Duplicate non-mnemonic translations // Test strings that have shortcuts. Duplicate non-mnemonic translations
// exist for both "Edit" and "View", for ease of comparison // exist for both "Edit" and "View", for ease of comparison
CPPUNIT_ASSERT_EQUAL( _("Edit"), wxStripMenuCodes(_("E&dit\tCtrl+E")) ); CPPUNIT_ASSERT_EQUAL
(
GetTranslatedString(trans, "Edit"),
wxStripMenuCodes(GetTranslatedString(trans, "E&dit\tCtrl+E"))
);
//"Vie&w" also has a space before the (&W) // "Vie&w" also has a space before the (&W)
CPPUNIT_ASSERT_EQUAL( _("View"), wxStripMenuCodes(_("Vie&w\tCtrl+V")) ); CPPUNIT_ASSERT_EQUAL
(
GetTranslatedString(trans, "View"),
wxStripMenuCodes(GetTranslatedString(trans, "Vie&w\tCtrl+V"))
);
// Test a 'normal' mnemonic too: the translation is "Preten&d" // Test a 'normal' mnemonic too: the translation is "Preten&d"
CPPUNIT_ASSERT_EQUAL( "Pretend", wxStripMenuCodes(_("B&ogus")) ); CPPUNIT_ASSERT_EQUAL
(
"Pretend",
wxStripMenuCodes(GetTranslatedString(trans, "B&ogus"))
);
} }
#endif // wxUSE_INTL
void MenuTestCase::RadioItems() void MenuTestCase::RadioItems()
{ {