Index: test/JsEngine.cpp |
=================================================================== |
--- a/test/JsEngine.cpp |
+++ b/test/JsEngine.cpp |
@@ -17,16 +17,22 @@ |
#include "BaseJsTest.h" |
namespace |
{ |
class JsEngineTest : public BaseJsTest |
{ |
}; |
+ |
+ bool callbackCalled = false; |
+ void Callback() |
+ { |
+ callbackCalled = true; |
+ } |
} |
TEST_F(JsEngineTest, Evaluate) |
{ |
jsEngine->Evaluate("function hello() { return 'Hello'; }"); |
AdblockPlus::JsValuePtr result = jsEngine->Evaluate("hello()"); |
ASSERT_TRUE(result->IsString()); |
ASSERT_EQ("Hello", result->AsString()); |
@@ -58,16 +64,41 @@ TEST_F(JsEngineTest, ValueCreation) |
ASSERT_TRUE(value->IsBool()); |
ASSERT_TRUE(value->AsBool()); |
value = jsEngine->NewObject(); |
ASSERT_TRUE(value->IsObject()); |
ASSERT_EQ(0u, value->GetOwnPropertyNames().size()); |
} |
+TEST_F(JsEngineTest, EventCallbacks) |
+{ |
+ // Trigger event without a callback |
+ callbackCalled = false; |
+ jsEngine->Evaluate("_triggerEvent('foobar')"); |
+ ASSERT_FALSE(callbackCalled); |
+ |
+ // Set callback |
+ jsEngine->SetEventCallback("foobar", Callback); |
+ callbackCalled = false; |
+ jsEngine->Evaluate("_triggerEvent('foobar')"); |
+ ASSERT_TRUE(callbackCalled); |
+ |
+ // Trigger a different event |
+ callbackCalled = false; |
+ jsEngine->Evaluate("_triggerEvent('barfoo')"); |
+ ASSERT_FALSE(callbackCalled); |
+ |
+ // Remove callback |
+ jsEngine->RemoveEventCallback("foobar"); |
+ callbackCalled = false; |
+ jsEngine->Evaluate("_triggerEvent('foobar')"); |
+ ASSERT_FALSE(callbackCalled); |
+} |
+ |
TEST(NewJsEngineTest, CallbackGetSet) |
{ |
AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); |
ASSERT_TRUE(jsEngine->GetLogSystem()); |
ASSERT_ANY_THROW(jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr())); |
AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem()); |
jsEngine->SetLogSystem(logSystem); |