Index: test/FileSystemJsObject.cpp
===================================================================
--- a/test/FileSystemJsObject.cpp
+++ b/test/FileSystemJsObject.cpp
@@ -17,6 +17,8 @@
 
 #include <sstream>
 #include "BaseJsTest.h"
+#include "JsLatch.h"
+#include "../src/JsEngineTransition.h"
 
 namespace
 {
@@ -100,8 +102,10 @@
   void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content,
                 std::string& error)
   {
-    jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})");
-    std::this_thread::sleep_for(std::chrono::milliseconds(50));
+    auto engine = ToInternal(jsEngine);
+    JsTestingLatch latch(engine, "latch");
+    jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r; latch.Arrive();})");
+    latch.Wait();
     content = jsEngine->Evaluate("result.content")->AsString();
     error = jsEngine->Evaluate("result.error")->AsString();
   }
@@ -156,8 +160,9 @@
 
 TEST_F(FileSystemJsObjectTest, Write)
 {
-  jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})");
-  std::this_thread::sleep_for(std::chrono::milliseconds(50));
+  JsTestingLatch latch(engine, "latch");
+  jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e; latch.Arrive();})");
+  latch.Wait();
   ASSERT_EQ("foo", mockFileSystem->lastWrittenPath);
   ASSERT_EQ("bar", mockFileSystem->lastWrittenContent);
   ASSERT_EQ("", jsEngine->Evaluate("error")->AsString());
@@ -172,15 +177,17 @@
 TEST_F(FileSystemJsObjectTest, WriteError)
 {
   mockFileSystem->success = false;
-  jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})");
-  std::this_thread::sleep_for(std::chrono::milliseconds(50));
+  JsTestingLatch latch(engine, "latch");
+  jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e; latch.Arrive();})");
+  latch.Wait();
   ASSERT_NE("", jsEngine->Evaluate("error")->AsString());
 }
 
 TEST_F(FileSystemJsObjectTest, Move)
 {
-  jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})");
-  std::this_thread::sleep_for(std::chrono::milliseconds(50));
+  JsTestingLatch latch(engine, "latch");
+  jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e; latch.Arrive();})");
+  latch.Wait();
   ASSERT_EQ("foo", mockFileSystem->movedFrom);
   ASSERT_EQ("bar", mockFileSystem->movedTo);
   ASSERT_EQ("", jsEngine->Evaluate("error")->AsString());
@@ -195,15 +202,17 @@
 TEST_F(FileSystemJsObjectTest, MoveError)
 {
   mockFileSystem->success = false;
-  jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})");
-  std::this_thread::sleep_for(std::chrono::milliseconds(50));
+  JsTestingLatch latch(engine, "latch");
+  jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e; latch.Arrive();})");
+  latch.Wait();
   ASSERT_NE("", jsEngine->Evaluate("error")->AsString());
 }
 
 TEST_F(FileSystemJsObjectTest, Remove)
 {
-  jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})");
-  std::this_thread::sleep_for(std::chrono::milliseconds(50));
+  JsTestingLatch latch(engine, "latch");
+  jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e; latch.Arrive();})");
+  latch.Wait();
   ASSERT_EQ("foo", mockFileSystem->removedPath);
   ASSERT_EQ("", jsEngine->Evaluate("error")->AsString());
 }
@@ -217,8 +226,9 @@
 TEST_F(FileSystemJsObjectTest, RemoveError)
 {
   mockFileSystem->success = false;
-  jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})");
-  std::this_thread::sleep_for(std::chrono::milliseconds(50));
+  JsTestingLatch latch(engine, "latch");
+  jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e; latch.Arrive();})");
+  latch.Wait();
   ASSERT_NE("", jsEngine->Evaluate("error")->AsString());
 }
 
@@ -228,8 +238,9 @@
   mockFileSystem->statIsDirectory= false;
   mockFileSystem->statIsFile = true;
   mockFileSystem->statLastModified = 1337;
-  jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
-  std::this_thread::sleep_for(std::chrono::milliseconds(50));
+  JsTestingLatch latch(engine, "latch");
+  jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r; latch.Arrive();})");
+  latch.Wait();
   ASSERT_EQ("foo", mockFileSystem->statPath);
   ASSERT_EQ("", jsEngine->Evaluate("result.error")->AsString());
   ASSERT_TRUE(jsEngine->Evaluate("result.exists")->AsBool());
@@ -247,7 +258,8 @@
 TEST_F(FileSystemJsObjectTest, StatError)
 {
   mockFileSystem->success = false;
-  jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
-  std::this_thread::sleep_for(std::chrono::milliseconds(50));
+  JsTestingLatch latch(engine, "latch");
+  jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r; latch.Arrive();})");
+  latch.Wait();
   ASSERT_NE("", jsEngine->Evaluate("result.error")->AsString());
 }
