| 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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 #include "BaseJsTest.h" | 18 #include "BaseJsTest.h" |
| 19 #include "../src/Thread.h" | 19 #include "../src/Thread.h" |
| 20 | 20 |
| 21 namespace | 21 namespace |
| 22 { | 22 { |
| 23 class GlobalJsObjectTest : public ::testing::Test | 23 class GlobalJsObjectTest : public BaseJsTest |
| 24 { | 24 { |
| 25 protected: | 25 protected: |
| 26 std::unique_ptr<AdblockPlus::Platform> platform; | |
| 27 AdblockPlus::JsEnginePtr jsEngine; | |
| 28 | |
| 29 void SetUp() override | 26 void SetUp() override |
| 30 { | 27 { |
| 31 ThrowingPlatformCreationParameters params; | 28 ThrowingPlatformCreationParameters params; |
| 32 params.timer = AdblockPlus::CreateDefaultTimer(); | 29 params.timer = AdblockPlus::CreateDefaultTimer(); |
| 33 platform.reset(new AdblockPlus::Platform(std::move(params))); | 30 platform.reset(new AdblockPlus::Platform(std::move(params))); |
| 34 jsEngine = platform->GetJsEngine(); | |
| 35 } | 31 } |
| 36 }; | 32 }; |
| 37 } | 33 } |
| 38 | 34 |
| 39 TEST_F(GlobalJsObjectTest, SetTimeout) | 35 TEST_F(GlobalJsObjectTest, SetTimeout) |
| 40 { | 36 { |
| 41 jsEngine->Evaluate("setTimeout(function() {foo = 'bar';}, 100)"); | 37 GetJsEngine().Evaluate("setTimeout(function() {foo = 'bar';}, 100)"); |
| 42 ASSERT_TRUE(jsEngine->Evaluate("this.foo").IsUndefined()); | 38 ASSERT_TRUE(GetJsEngine().Evaluate("this.foo").IsUndefined()); |
| 43 AdblockPlus::Sleep(200); | 39 AdblockPlus::Sleep(200); |
| 44 ASSERT_EQ("bar", jsEngine->Evaluate("this.foo").AsString()); | 40 ASSERT_EQ("bar", GetJsEngine().Evaluate("this.foo").AsString()); |
| 45 } | 41 } |
| 46 | 42 |
| 47 TEST_F(GlobalJsObjectTest, SetTimeoutWithArgs) | 43 TEST_F(GlobalJsObjectTest, SetTimeoutWithArgs) |
| 48 { | 44 { |
| 49 jsEngine->Evaluate("setTimeout(function(s) {foo = s;}, 100, 'foobar')"); | 45 GetJsEngine().Evaluate("setTimeout(function(s) {foo = s;}, 100, 'foobar')"); |
| 50 ASSERT_TRUE(jsEngine->Evaluate("this.foo").IsUndefined()); | 46 ASSERT_TRUE(GetJsEngine().Evaluate("this.foo").IsUndefined()); |
| 51 AdblockPlus::Sleep(200); | 47 AdblockPlus::Sleep(200); |
| 52 ASSERT_EQ("foobar", jsEngine->Evaluate("this.foo").AsString()); | 48 ASSERT_EQ("foobar", GetJsEngine().Evaluate("this.foo").AsString()); |
| 53 } | 49 } |
| 54 | 50 |
| 55 TEST_F(GlobalJsObjectTest, SetTimeoutWithInvalidArgs) | 51 TEST_F(GlobalJsObjectTest, SetTimeoutWithInvalidArgs) |
| 56 { | 52 { |
| 57 ASSERT_ANY_THROW(jsEngine->Evaluate("setTimeout()")); | 53 ASSERT_ANY_THROW(GetJsEngine().Evaluate("setTimeout()")); |
| 58 ASSERT_ANY_THROW(jsEngine->Evaluate("setTimeout('', 1)")); | 54 ASSERT_ANY_THROW(GetJsEngine().Evaluate("setTimeout('', 1)")); |
| 59 } | 55 } |
| 60 | 56 |
| 61 TEST_F(GlobalJsObjectTest, SetMultipleTimeouts) | 57 TEST_F(GlobalJsObjectTest, SetMultipleTimeouts) |
| 62 { | 58 { |
| 63 jsEngine->Evaluate("foo = []"); | 59 GetJsEngine().Evaluate("foo = []"); |
| 64 jsEngine->Evaluate("setTimeout(function(s) {foo.push('1');}, 100)"); | 60 GetJsEngine().Evaluate("setTimeout(function(s) {foo.push('1');}, 100)"); |
| 65 jsEngine->Evaluate("setTimeout(function(s) {foo.push('2');}, 150)"); | 61 GetJsEngine().Evaluate("setTimeout(function(s) {foo.push('2');}, 150)"); |
| 66 AdblockPlus::Sleep(200); | 62 AdblockPlus::Sleep(200); |
| 67 ASSERT_EQ("1,2", jsEngine->Evaluate("this.foo").AsString()); | 63 ASSERT_EQ("1,2", GetJsEngine().Evaluate("this.foo").AsString()); |
| 68 } | 64 } |
| 69 | |
| 70 TEST_F(GlobalJsObjectTest, TimeoutDoesNotKeepJsEngine) | |
| 71 { | |
| 72 jsEngine->Evaluate("setTimeout(function() {}, 50000)"); | |
| 73 // 1 reference is held by platform and yet one by the test fixture class. | |
| 74 EXPECT_EQ(2u, jsEngine.use_count()); | |
| 75 AdblockPlus::Sleep(200); | |
| 76 std::weak_ptr<AdblockPlus::JsEngine> weakJsEngine = jsEngine; | |
| 77 jsEngine.reset(); | |
| 78 platform.reset(); | |
| 79 EXPECT_FALSE(weakJsEngine.lock()); | |
| 80 } | |
| OLD | NEW |