Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: test/GlobalJsObject.cpp

Issue 10173031: Don`t use references to JsEngine to avoid use-after-free errors,switch to shared_ptr instead (Closed)
Patch Set: Created April 18, 2013, 4:15 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/GlobalJsObject.cpp
===================================================================
--- a/test/GlobalJsObject.cpp
+++ b/test/GlobalJsObject.cpp
@@ -1,39 +1,39 @@
#include <AdblockPlus.h>
#include <gtest/gtest.h>
#include "../src/Thread.h"
TEST(GlobalJsObjectTest, SetTimeout)
{
- AdblockPlus::JsEngine jsEngine;
- jsEngine.Evaluate("setTimeout(function() {foo = 'bar';}, 100)");
- ASSERT_TRUE(jsEngine.Evaluate("this.foo")->IsUndefined());
+ AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
+ jsEngine->Evaluate("setTimeout(function() {foo = 'bar';}, 100)");
+ ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined());
AdblockPlus::Sleep(200);
- ASSERT_EQ("bar", jsEngine.Evaluate("this.foo")->AsString());
+ ASSERT_EQ("bar", jsEngine->Evaluate("this.foo")->AsString());
}
TEST(GlobalJsObjectTest, SetTimeoutWithArgs)
{
- AdblockPlus::JsEngine jsEngine;
- jsEngine.Evaluate("setTimeout(function(s) {foo = s;}, 100, 'foobar')");
- ASSERT_TRUE(jsEngine.Evaluate("this.foo")->IsUndefined());
+ AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
+ jsEngine->Evaluate("setTimeout(function(s) {foo = s;}, 100, 'foobar')");
+ ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined());
AdblockPlus::Sleep(200);
- ASSERT_EQ("foobar", jsEngine.Evaluate("this.foo")->AsString());
+ ASSERT_EQ("foobar", jsEngine->Evaluate("this.foo")->AsString());
}
TEST(GlobalJsObjectTest, SetTimeoutWithInvalidArgs)
{
- AdblockPlus::JsEngine jsEngine;
- ASSERT_ANY_THROW(jsEngine.Evaluate("setTimeout()"));
- ASSERT_ANY_THROW(jsEngine.Evaluate("setTimeout('', 1)"));
+ AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
+ ASSERT_ANY_THROW(jsEngine->Evaluate("setTimeout()"));
+ ASSERT_ANY_THROW(jsEngine->Evaluate("setTimeout('', 1)"));
}
TEST(GlobalJsObjectTest, SetMultipleTimeouts)
{
- AdblockPlus::JsEngine jsEngine;
- jsEngine.Evaluate("foo = []");
- jsEngine.Evaluate("setTimeout(function(s) {foo.push('1');}, 100)");
- jsEngine.Evaluate("setTimeout(function(s) {foo.push('2');}, 150)");
+ AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
+ jsEngine->Evaluate("foo = []");
+ jsEngine->Evaluate("setTimeout(function(s) {foo.push('1');}, 100)");
+ jsEngine->Evaluate("setTimeout(function(s) {foo.push('2');}, 150)");
AdblockPlus::Sleep(200);
- ASSERT_EQ("1,2", jsEngine.Evaluate("this.foo")->AsString());
+ ASSERT_EQ("1,2", jsEngine->Evaluate("this.foo")->AsString());
}
« src/JsEngine.cpp ('K') | « test/FilterEngineStubs.cpp ('k') | test/JsEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld