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.
This commit is contained in:
Vadim Zeitlin
2017-10-22 17:07:18 +02:00
parent 732cb97af3
commit ae88141fa0
3 changed files with 63 additions and 36 deletions

View File

@@ -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();

View File

@@ -857,27 +857,41 @@ 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('\\');
wxLogWarning(_("Failed to find web view emulation level in the registry"));
return false;
}
const wxString programName = wxGetFullModuleName().AfterLast('\\');
if ( modernLevel )
{
if ( !key.SetValue(programName, wxIE_EMULATION_LEVEL) )
// 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 the current browser control emulation level"));
wxLogWarning(_("Failed to set web view to modern emulation level"));
return false;
}
}
else
{
key.DeleteValue(programName);
}
return true;
}
wxLogWarning(_("Failed to find current browser control emulation level"));
if ( !key.DeleteValue(programName) )
{
wxLogWarning(_("Failed to reset web view to standard emulation level"));
return false;
}
}
return true;
}
static
bool CallEval(const wxString& code,

View File

@@ -23,7 +23,6 @@
#include "wx/webview.h"
#include "asserthelper.h"
#if wxUSE_WEBVIEW_IE
#include "wx/msw/registry.h"
#include "wx/msw/webview_ie.h"
#endif
@@ -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());