TestFileSystemBackgroundRequestHandler.cpp (2505B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "FileSystemBackgroundRequestHandler.h" 8 #include "FileSystemMocks.h" 9 #include "gtest/gtest.h" 10 #include "mozilla/SpinEventLoopUntil.h" 11 #include "mozilla/dom/FileSystemManager.h" 12 #include "mozilla/dom/FileSystemManagerChild.h" 13 #include "mozilla/dom/PFileSystemManager.h" 14 15 namespace mozilla::dom::fs::test { 16 17 class TestFileSystemBackgroundRequestHandler : public ::testing::Test { 18 protected: 19 void SetUp() override { 20 // TODO: Fix the test to not depend on CreateFileSystemManagerParent 21 // failure because of the pref set to false. 22 nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID); 23 prefs->SetBoolPref("dom.fs.enabled", false); 24 25 mFileSystemManagerChild = MakeAndAddRef<TestFileSystemManagerChild>(); 26 } 27 28 void TearDown() override { 29 nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID); 30 prefs->SetBoolPref("dom.fs.enabled", true); 31 } 32 33 RefPtr<FileSystemBackgroundRequestHandler> 34 GetFileSystemBackgroundRequestHandler() { 35 return MakeRefPtr<FileSystemBackgroundRequestHandler>( 36 new TestFileSystemChildFactory(mFileSystemManagerChild)); 37 } 38 39 mozilla::ipc::PrincipalInfo mPrincipalInfo = GetPrincipalInfo(); 40 RefPtr<TestFileSystemManagerChild> mFileSystemManagerChild; 41 }; 42 43 TEST_F(TestFileSystemBackgroundRequestHandler, 44 isCreateFileSystemManagerChildSuccessful) { 45 EXPECT_CALL(*mFileSystemManagerChild, Shutdown()) 46 .WillOnce([fileSystemManagerChild = 47 static_cast<void*>(mFileSystemManagerChild.get())]() { 48 static_cast<TestFileSystemManagerChild*>(fileSystemManagerChild) 49 ->FileSystemManagerChild::Shutdown(); 50 }); 51 52 bool done = false; 53 auto testable = GetFileSystemBackgroundRequestHandler(); 54 testable->CreateFileSystemManagerChild(mPrincipalInfo) 55 ->Then(GetCurrentSerialEventTarget(), __func__, 56 [&done](const FileSystemManagerChild::ActorPromise:: 57 ResolveOrRejectValue&) { done = true; }); 58 // MozPromise should be rejected 59 SpinEventLoopUntil("Promise is fulfilled or timeout"_ns, 60 [&done]() { return done; }); 61 } 62 63 } // namespace mozilla::dom::fs::test