MockAssetDatabaseRequestsHandler.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <QDir>
  10. #include <QTemporaryDir>
  11. #include <AzToolsFramework/API/AssetDatabaseBus.h>
  12. #include <native/utilities/assetUtils.h>
  13. namespace AssetProcessor
  14. {
  15. // Mock the AssetDatabaseRequests handler for retrieving the asset database location in unit tests
  16. class MockAssetDatabaseRequestsHandler
  17. : public AzToolsFramework::AssetDatabase::AssetDatabaseRequests::Bus::Handler
  18. {
  19. public:
  20. MockAssetDatabaseRequestsHandler()
  21. {
  22. // the canonicalization of the path here is to get around the fact that on some platforms
  23. // the "temporary" folder location could be junctioned into some other folder and getting "QDir::current()"
  24. // and other similar functions may actually return a different string but still be referring to the same folder
  25. QString canonicalTempDirPath = AssetUtilities::NormalizeDirectoryPath(QDir(m_temporaryDir.path()).canonicalPath());
  26. m_assetDatabasePath = QDir(canonicalTempDirPath).absoluteFilePath("test_database.sqlite").toUtf8().data();
  27. BusConnect();
  28. }
  29. ~MockAssetDatabaseRequestsHandler()
  30. {
  31. BusDisconnect();
  32. }
  33. bool GetAssetDatabaseLocation(AZStd::string& location) override
  34. {
  35. location = m_assetDatabasePath;
  36. return true;
  37. }
  38. AZStd::string GetAssetRootDir()
  39. {
  40. return QFileInfo(m_assetDatabasePath.c_str()).dir().path().toUtf8().data();
  41. }
  42. AZStd::string m_assetDatabasePath;
  43. private:
  44. QTemporaryDir m_temporaryDir;
  45. };
  46. }