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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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" | |
20 | 19 |
21 namespace | 20 namespace |
22 { | 21 { |
23 class GlobalJsObjectTest : public BaseJsTest | 22 class GlobalJsObjectTest : public BaseJsTest |
24 { | 23 { |
25 protected: | 24 protected: |
26 void SetUp() override | 25 void SetUp() override |
27 { | 26 { |
28 ThrowingPlatformCreationParameters params; | 27 ThrowingPlatformCreationParameters params; |
29 params.timer = AdblockPlus::CreateDefaultTimer(); | 28 params.timer = AdblockPlus::CreateDefaultTimer(); |
30 platform.reset(new AdblockPlus::Platform(std::move(params))); | 29 platform.reset(new AdblockPlus::Platform(std::move(params))); |
31 } | 30 } |
32 }; | 31 }; |
33 } | 32 } |
34 | 33 |
35 TEST_F(GlobalJsObjectTest, SetTimeout) | 34 TEST_F(GlobalJsObjectTest, SetTimeout) |
36 { | 35 { |
37 GetJsEngine().Evaluate("let foo; setTimeout(function() {foo = 'bar';}, 100)"); | 36 GetJsEngine().Evaluate("let foo; setTimeout(function() {foo = 'bar';}, 100)"); |
38 ASSERT_TRUE(GetJsEngine().Evaluate("foo").IsUndefined()); | 37 ASSERT_TRUE(GetJsEngine().Evaluate("foo").IsUndefined()); |
39 AdblockPlus::Sleep(200); | 38 std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
40 ASSERT_EQ("bar", GetJsEngine().Evaluate("foo").AsString()); | 39 ASSERT_EQ("bar", GetJsEngine().Evaluate("foo").AsString()); |
41 } | 40 } |
42 | 41 |
43 TEST_F(GlobalJsObjectTest, SetTimeoutWithArgs) | 42 TEST_F(GlobalJsObjectTest, SetTimeoutWithArgs) |
44 { | 43 { |
45 GetJsEngine().Evaluate("let foo; setTimeout(function(s) {foo = s;}, 100, 'foob
ar')"); | 44 GetJsEngine().Evaluate("let foo; setTimeout(function(s) {foo = s;}, 100, 'foob
ar')"); |
46 ASSERT_TRUE(GetJsEngine().Evaluate("foo").IsUndefined()); | 45 ASSERT_TRUE(GetJsEngine().Evaluate("foo").IsUndefined()); |
47 AdblockPlus::Sleep(200); | 46 std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
48 ASSERT_EQ("foobar", GetJsEngine().Evaluate("foo").AsString()); | 47 ASSERT_EQ("foobar", GetJsEngine().Evaluate("foo").AsString()); |
49 } | 48 } |
50 | 49 |
51 TEST_F(GlobalJsObjectTest, SetTimeoutWithInvalidArgs) | 50 TEST_F(GlobalJsObjectTest, SetTimeoutWithInvalidArgs) |
52 { | 51 { |
53 ASSERT_ANY_THROW(GetJsEngine().Evaluate("setTimeout()")); | 52 ASSERT_ANY_THROW(GetJsEngine().Evaluate("setTimeout()")); |
54 ASSERT_ANY_THROW(GetJsEngine().Evaluate("setTimeout('', 1)")); | 53 ASSERT_ANY_THROW(GetJsEngine().Evaluate("setTimeout('', 1)")); |
55 } | 54 } |
56 | 55 |
57 TEST_F(GlobalJsObjectTest, SetMultipleTimeouts) | 56 TEST_F(GlobalJsObjectTest, SetMultipleTimeouts) |
58 { | 57 { |
59 GetJsEngine().Evaluate("let foo = []"); | 58 GetJsEngine().Evaluate("let foo = []"); |
60 GetJsEngine().Evaluate("setTimeout(function(s) {foo.push('1');}, 100)"); | 59 GetJsEngine().Evaluate("setTimeout(function(s) {foo.push('1');}, 100)"); |
61 GetJsEngine().Evaluate("setTimeout(function(s) {foo.push('2');}, 150)"); | 60 GetJsEngine().Evaluate("setTimeout(function(s) {foo.push('2');}, 150)"); |
62 AdblockPlus::Sleep(200); | 61 std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
63 ASSERT_EQ("1,2", GetJsEngine().Evaluate("foo").AsString()); | 62 ASSERT_EQ("1,2", GetJsEngine().Evaluate("foo").AsString()); |
64 } | 63 } |
OLD | NEW |