From ae88141fa0c425c4a34dcd133cbc0688512c9330 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 22 Oct 2017 17:07:18 +0200 Subject: [PATCH] Don't define unnecessary constants in public webview_ie.h There is no need to check that calling MSWSetModernEmulationLevel() changed the registry key in the test, this is just an implementation detail of this function. This makes it unnecessary to define wxIE_EMULATION_LEVEL and wxREGISTRY_IE_PATH (which are both badly named) in the public header. Finally, improve the error message in MSWSetModernEmulationLevel() and add another one for failing to reset the emulation level too. --- include/wx/msw/webview_ie.h | 8 ++---- src/msw/webview_ie.cpp | 50 ++++++++++++++++++++++++------------- tests/controls/webtest.cpp | 41 +++++++++++++++++++++--------- 3 files changed, 63 insertions(+), 36 deletions(-) diff --git a/include/wx/msw/webview_ie.h b/include/wx/msw/webview_ie.h index edd2926f8f..1c9d154bae 100644 --- a/include/wx/msw/webview_ie.h +++ b/include/wx/msw/webview_ie.h @@ -36,11 +36,6 @@ class DocHostUIHandler; class wxFindPointers; class wxIInternetProtocol; -#define wxIE_EMULATION_LEVEL 8000 - -//Registry key where emulation level for programs are set -#define wxREGISTRY_IE_PATH wxT("SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION") - class WXDLLIMPEXP_WEBVIEW wxWebViewIE : public wxWebView { public: @@ -149,7 +144,8 @@ public: void onActiveXEvent(wxActiveXEvent& evt); void onEraseBg(wxEraseEvent&) {} - //Establish EmulationLevel for RunScript IE + // Establish sufficiently modern emulation level for the browser control to + // allow RunScript() to return any kind of values. static bool MSWSetModernEmulationLevel(bool modernLevel = true); wxDECLARE_EVENT_TABLE(); diff --git a/src/msw/webview_ie.cpp b/src/msw/webview_ie.cpp index 2234f51012..46c26c3d21 100644 --- a/src/msw/webview_ie.cpp +++ b/src/msw/webview_ie.cpp @@ -857,26 +857,40 @@ wxString wxWebViewIE::GetPageText() const bool wxWebViewIE::MSWSetModernEmulationLevel(bool modernLevel) { - wxRegKey key(wxRegKey::HKCU, wxREGISTRY_IE_PATH); - if ( key.Exists() ) + // Registry key where emulation level for programs are set + static const wxChar* IE_EMULATION_KEY = + wxT("SOFTWARE\\Microsoft\\Internet Explorer\\Main") + wxT("\\FeatureControl\\FEATURE_BROWSER_EMULATION"); + + wxRegKey key(wxRegKey::HKCU, IE_EMULATION_KEY); + if ( !key.Exists() ) { - wxString programName = wxGetFullModuleName().AfterLast('\\'); - if ( modernLevel ) - { - if ( !key.SetValue(programName, wxIE_EMULATION_LEVEL) ) - { - wxLogWarning(_("Failed to set the current browser control emulation level")); - return false; - } - } - else - { - key.DeleteValue(programName); - } - return true; + wxLogWarning(_("Failed to find web view emulation level in the registry")); + return false; } - wxLogWarning(_("Failed to find current browser control emulation level")); - return false; + + const wxString programName = wxGetFullModuleName().AfterLast('\\'); + if ( modernLevel ) + { + // IE8 (8000) is sufficiently modern for our needs, see + // https://msdn.microsoft.com/library/ee330730.aspx#browser_emulation + // for other values that could be used here. + if ( !key.SetValue(programName, 8000) ) + { + wxLogWarning(_("Failed to set web view to modern emulation level")); + return false; + } + } + else + { + if ( !key.DeleteValue(programName) ) + { + wxLogWarning(_("Failed to reset web view to standard emulation level")); + return false; + } + } + + return true; } static diff --git a/tests/controls/webtest.cpp b/tests/controls/webtest.cpp index e492ad0170..eeecb2ec08 100644 --- a/tests/controls/webtest.cpp +++ b/tests/controls/webtest.cpp @@ -23,8 +23,7 @@ #include "wx/webview.h" #include "asserthelper.h" #if wxUSE_WEBVIEW_IE -#include "wx/msw/registry.h" -#include "wx/msw/webview_ie.h" + #include "wx/msw/webview_ie.h" #endif class WebTestCase : public CppUnit::TestCase @@ -276,11 +275,32 @@ void WebTestCase::RunScript() wxString result; #if wxUSE_WEBVIEW_IE CPPUNIT_ASSERT(wxWebViewIE::MSWSetModernEmulationLevel()); - wxRegKey key(wxRegKey::HKCU, wxREGISTRY_IE_PATH); - long val = 0; - wxString programName = wxGetFullModuleName().AfterLast('\\'); - key.QueryValue(programName, &val); - CPPUNIT_ASSERT_EQUAL(val, wxIE_EMULATION_LEVEL); + + // Define a specialized scope guard ensuring that we reset the emulation + // level to its default value even if any asserts below fail. + class ResetEmulationLevel + { + public: + ResetEmulationLevel() + { + m_reset = true; + } + + bool DoReset() + { + m_reset = false; + return wxWebViewIE::MSWSetModernEmulationLevel(false); + } + + ~ResetEmulationLevel() + { + if ( m_reset ) + DoReset(); + } + + private: + bool m_reset; + } resetEmulationLevel; CPPUNIT_ASSERT(m_browser->RunScript("function f(){var person = new Object();person.name = 'Bar'; \ person.lastName = 'Foo';return person;}f();", &result)); @@ -294,11 +314,8 @@ void WebTestCase::RunScript() &result)); CPPUNIT_ASSERT_EQUAL("\"2017-10-08T21:30:40.000Z\"", result); - CPPUNIT_ASSERT(wxWebViewIE::MSWSetModernEmulationLevel(false)); - val = 0; - key.QueryValue(programName, &val); - CPPUNIT_ASSERT_EQUAL(val, 0); -#endif + CPPUNIT_ASSERT(resetEmulationLevel.DoReset()); +#endif // wxUSE_WEBVIEW_IE CPPUNIT_ASSERT(m_browser->RunScript("document.write(\"Hello World!\");")); CPPUNIT_ASSERT_EQUAL("Hello World!", m_browser->GetPageText());