OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 27 matching lines...) Expand all Loading... |
38 ASSERT_THROW(jsEngine->Evaluate("doesnotexist()"), std::runtime_error); | 38 ASSERT_THROW(jsEngine->Evaluate("doesnotexist()"), std::runtime_error); |
39 } | 39 } |
40 | 40 |
41 TEST_F(JsEngineTest, CompileTimeExceptionIsThrown) | 41 TEST_F(JsEngineTest, CompileTimeExceptionIsThrown) |
42 { | 42 { |
43 ASSERT_THROW(jsEngine->Evaluate("'foo'bar'"), std::runtime_error); | 43 ASSERT_THROW(jsEngine->Evaluate("'foo'bar'"), std::runtime_error); |
44 } | 44 } |
45 | 45 |
46 TEST_F(JsEngineTest, ValueCreation) | 46 TEST_F(JsEngineTest, ValueCreation) |
47 { | 47 { |
48 AdblockPlus::JsValuePtr value; | 48 auto value = jsEngine->NewValue("foo"); |
49 | 49 ASSERT_TRUE(value.IsString()); |
50 value = jsEngine->NewValue("foo"); | 50 ASSERT_EQ("foo", value.AsString()); |
51 ASSERT_TRUE(value->IsString()); | |
52 ASSERT_EQ("foo", value->AsString()); | |
53 | 51 |
54 value = jsEngine->NewValue(12345678901234); | 52 value = jsEngine->NewValue(12345678901234); |
55 ASSERT_TRUE(value->IsNumber()); | 53 ASSERT_TRUE(value.IsNumber()); |
56 ASSERT_EQ(12345678901234, value->AsInt()); | 54 ASSERT_EQ(12345678901234, value.AsInt()); |
57 | 55 |
58 value = jsEngine->NewValue(true); | 56 value = jsEngine->NewValue(true); |
59 ASSERT_TRUE(value->IsBool()); | 57 ASSERT_TRUE(value.IsBool()); |
60 ASSERT_TRUE(value->AsBool()); | 58 ASSERT_TRUE(value.AsBool()); |
61 | 59 |
62 value = jsEngine->NewObject(); | 60 value = jsEngine->NewObject(); |
63 ASSERT_TRUE(value->IsObject()); | 61 ASSERT_TRUE(value.IsObject()); |
64 ASSERT_EQ(0u, value->GetOwnPropertyNames().size()); | 62 ASSERT_EQ(0u, value.GetOwnPropertyNames().size()); |
| 63 } |
| 64 |
| 65 TEST_F(JsEngineTest, ValueCopy) |
| 66 { |
| 67 { |
| 68 auto value = jsEngine->NewValue("foo"); |
| 69 ASSERT_TRUE(value.IsString()); |
| 70 ASSERT_EQ("foo", value.AsString()); |
| 71 |
| 72 AdblockPlus::JsValue value2(value); |
| 73 ASSERT_TRUE(value2.IsString()); |
| 74 ASSERT_EQ("foo", value2.AsString()); |
| 75 } |
| 76 { |
| 77 auto value = jsEngine->NewValue(12345678901234); |
| 78 ASSERT_TRUE(value.IsNumber()); |
| 79 ASSERT_EQ(12345678901234, value.AsInt()); |
| 80 |
| 81 AdblockPlus::JsValue value2(value); |
| 82 ASSERT_TRUE(value2.IsNumber()); |
| 83 ASSERT_EQ(12345678901234, value2.AsInt()); |
| 84 } |
| 85 { |
| 86 auto value = jsEngine->NewValue(true); |
| 87 ASSERT_TRUE(value.IsBool()); |
| 88 ASSERT_TRUE(value.AsBool()); |
| 89 |
| 90 AdblockPlus::JsValue value2(value); |
| 91 ASSERT_TRUE(value2.IsBool()); |
| 92 ASSERT_TRUE(value2.AsBool()); |
| 93 } |
| 94 { |
| 95 auto value = jsEngine->NewObject(); |
| 96 ASSERT_TRUE(value.IsObject()); |
| 97 ASSERT_EQ(0u, value.GetOwnPropertyNames().size()); |
| 98 |
| 99 AdblockPlus::JsValue value2(value); |
| 100 ASSERT_TRUE(value2.IsObject()); |
| 101 ASSERT_EQ(0u, value2.GetOwnPropertyNames().size()); |
| 102 } |
65 } | 103 } |
66 | 104 |
67 TEST_F(JsEngineTest, EventCallbacks) | 105 TEST_F(JsEngineTest, EventCallbacks) |
68 { | 106 { |
69 bool callbackCalled = false; | 107 bool callbackCalled = false; |
70 AdblockPlus::JsConstValueList callbackParams; | 108 AdblockPlus::JsValueList callbackParams; |
71 auto Callback = [&callbackCalled, & callbackParams]( | 109 auto Callback = [&callbackCalled, & callbackParams]( |
72 const AdblockPlus::JsConstValueList& params) | 110 const AdblockPlus::JsValueList& params) |
73 { | 111 { |
74 callbackCalled = true; | 112 callbackCalled = true; |
75 callbackParams = params; | 113 callbackParams = params; |
76 }; | 114 }; |
77 | 115 |
78 // Trigger event without a callback | 116 // Trigger event without a callback |
79 callbackCalled = false; | 117 callbackCalled = false; |
80 jsEngine->Evaluate("_triggerEvent('foobar')"); | 118 jsEngine->Evaluate("_triggerEvent('foobar')"); |
81 ASSERT_FALSE(callbackCalled); | 119 ASSERT_FALSE(callbackCalled); |
82 | 120 |
83 // Set callback | 121 // Set callback |
84 jsEngine->SetEventCallback("foobar", Callback); | 122 jsEngine->SetEventCallback("foobar", Callback); |
85 callbackCalled = false; | 123 callbackCalled = false; |
86 jsEngine->Evaluate("_triggerEvent('foobar', 1, 'x', true)"); | 124 jsEngine->Evaluate("_triggerEvent('foobar', 1, 'x', true)"); |
87 ASSERT_TRUE(callbackCalled); | 125 ASSERT_TRUE(callbackCalled); |
88 ASSERT_EQ(callbackParams.size(), 3u); | 126 ASSERT_EQ(callbackParams.size(), 3u); |
89 ASSERT_EQ(callbackParams[0]->AsInt(), 1); | 127 ASSERT_EQ(callbackParams[0].AsInt(), 1); |
90 ASSERT_EQ(callbackParams[1]->AsString(), "x"); | 128 ASSERT_EQ(callbackParams[1].AsString(), "x"); |
91 ASSERT_TRUE(callbackParams[2]->AsBool()); | 129 ASSERT_TRUE(callbackParams[2].AsBool()); |
92 | 130 |
93 // Trigger a different event | 131 // Trigger a different event |
94 callbackCalled = false; | 132 callbackCalled = false; |
95 jsEngine->Evaluate("_triggerEvent('barfoo')"); | 133 jsEngine->Evaluate("_triggerEvent('barfoo')"); |
96 ASSERT_FALSE(callbackCalled); | 134 ASSERT_FALSE(callbackCalled); |
97 | 135 |
98 // Remove callback | 136 // Remove callback |
99 jsEngine->RemoveEventCallback("foobar"); | 137 jsEngine->RemoveEventCallback("foobar"); |
100 callbackCalled = false; | 138 callbackCalled = false; |
101 jsEngine->Evaluate("_triggerEvent('foobar')"); | 139 jsEngine->Evaluate("_triggerEvent('foobar')"); |
(...skipping 19 matching lines...) Expand all Loading... |
121 ASSERT_TRUE(jsEngine->GetWebRequest()); | 159 ASSERT_TRUE(jsEngine->GetWebRequest()); |
122 ASSERT_ANY_THROW(jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr())); | 160 ASSERT_ANY_THROW(jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr())); |
123 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest()); | 161 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest()); |
124 jsEngine->SetWebRequest(webRequest); | 162 jsEngine->SetWebRequest(webRequest); |
125 ASSERT_EQ(webRequest, jsEngine->GetWebRequest()); | 163 ASSERT_EQ(webRequest, jsEngine->GetWebRequest()); |
126 } | 164 } |
127 | 165 |
128 TEST(NewJsEngineTest, GlobalPropertyTest) | 166 TEST(NewJsEngineTest, GlobalPropertyTest) |
129 { | 167 { |
130 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | 168 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); |
131 jsEngine->SetGlobalProperty("foo", *jsEngine->NewValue("bar")); | 169 jsEngine->SetGlobalProperty("foo", jsEngine->NewValue("bar")); |
132 AdblockPlus::JsValuePtr foo = jsEngine->Evaluate("foo"); | 170 AdblockPlus::JsValuePtr foo = jsEngine->Evaluate("foo"); |
133 ASSERT_TRUE(foo->IsString()); | 171 ASSERT_TRUE(foo->IsString()); |
134 ASSERT_EQ(foo->AsString(), "bar"); | 172 ASSERT_EQ(foo->AsString(), "bar"); |
135 } | 173 } |
136 | 174 |
OLD | NEW |