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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 "JsLatch.h" |
19 | 20 |
20 namespace | 21 namespace |
21 { | 22 { |
22 class GlobalJsObjectTest : public BaseJsTest | 23 class GlobalJsObjectTest : public BaseJsTest |
23 { | 24 { |
24 }; | 25 }; |
25 } | 26 } |
26 | 27 |
27 TEST_F(GlobalJsObjectTest, SetTimeout) | 28 TEST_F(GlobalJsObjectTest, SetTimeout) |
28 { | 29 { |
29 jsEngine->Evaluate("setTimeout(function() {foo = 'bar';}, 100)"); | 30 JsTestingLatch latch(engine, "latch"); |
30 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); | 31 jsEngine->Evaluate("setTimeout(function() {foo = 'bar'; latch.Arrive();}, 100)
"); |
31 std::this_thread::sleep_for(std::chrono::milliseconds(200)); | 32 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); // RACE |
| 33 latch.Wait(); |
32 ASSERT_EQ("bar", jsEngine->Evaluate("this.foo")->AsString()); | 34 ASSERT_EQ("bar", jsEngine->Evaluate("this.foo")->AsString()); |
33 } | 35 } |
34 | 36 |
35 TEST_F(GlobalJsObjectTest, SetTimeoutWithArgs) | 37 TEST_F(GlobalJsObjectTest, SetTimeoutWithArgs) |
36 { | 38 { |
37 jsEngine->Evaluate("setTimeout(function(s) {foo = s;}, 100, 'foobar')"); | 39 JsTestingLatch latch(engine, "latch"); |
38 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); | 40 jsEngine->Evaluate("setTimeout(function(s) {foo = s; latch.Arrive();}, 100, 'f
oobar')"); |
39 std::this_thread::sleep_for(std::chrono::milliseconds(200)); | 41 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); // RACE |
| 42 latch.Wait(); |
40 ASSERT_EQ("foobar", jsEngine->Evaluate("this.foo")->AsString()); | 43 ASSERT_EQ("foobar", jsEngine->Evaluate("this.foo")->AsString()); |
41 } | 44 } |
42 | 45 |
43 TEST_F(GlobalJsObjectTest, SetTimeoutWithInvalidArgs) | 46 TEST_F(GlobalJsObjectTest, SetTimeoutWithInvalidArgs) |
44 { | 47 { |
45 ASSERT_ANY_THROW(jsEngine->Evaluate("setTimeout()")); | 48 ASSERT_ANY_THROW(jsEngine->Evaluate("setTimeout()")); |
46 ASSERT_ANY_THROW(jsEngine->Evaluate("setTimeout('', 1)")); | 49 ASSERT_ANY_THROW(jsEngine->Evaluate("setTimeout('', 1)")); |
47 } | 50 } |
48 | 51 |
49 TEST_F(GlobalJsObjectTest, SetMultipleTimeouts) | 52 TEST_F(GlobalJsObjectTest, SetMultipleTimeouts) |
50 { | 53 { |
| 54 JsTestingLatch latch(engine, "latch", 2); // two arrivals |
51 jsEngine->Evaluate("foo = []"); | 55 jsEngine->Evaluate("foo = []"); |
52 jsEngine->Evaluate("setTimeout(function(s) {foo.push('1');}, 100)"); | 56 jsEngine->Evaluate("setTimeout(function(s) {foo.push('1'); latch.Arrive();}, 1
00)"); |
53 jsEngine->Evaluate("setTimeout(function(s) {foo.push('2');}, 150)"); | 57 jsEngine->Evaluate("setTimeout(function(s) {foo.push('2'); latch.Arrive();}, 1
50)"); |
54 std::this_thread::sleep_for(std::chrono::milliseconds(200)); | 58 latch.Wait(); |
55 ASSERT_EQ("1,2", jsEngine->Evaluate("this.foo")->AsString()); | 59 ASSERT_EQ("1,2", jsEngine->Evaluate("this.foo")->AsString()); |
56 } | 60 } |
OLD | NEW |