[Bf-blender-cvs] [4e9807a3bd2] sybren-usd: Blendfile-loading test class

Sybren A. Stüvel noreply at git.blender.org
Thu Nov 21 10:36:00 CET 2019


Commit: 4e9807a3bd23cc75db7894f279466727d5a103a6
Author: Sybren A. Stüvel
Date:   Wed Nov 13 16:39:11 2019 +0100
Branches: sybren-usd
https://developer.blender.org/rB4e9807a3bd23cc75db7894f279466727d5a103a6

Blendfile-loading test class

To test some functionality of the USD exporter, I want to be able to load a blend file and run parts of the exporter. Up to now this wasn't possible from a GTest test. Of course a Python test could run the USD exporter, but testing the result would require writing a parser for USD files, which I want to avoid.

This new test class minimally sets up Blender so that it can load blend files and construct a depsgraph without crashing. Note that I haven't tested this on very complex blend files, so it may still crash when the loaded blend file references/requires uninitialised data structures.

The test will certainly crash with Blend files created with Blender 2.80, as the versioning code requires space types to be registered. This is normally done by initialising the window manager, which is not done in this test. The WM requires Python to run, which in turn requires that Blender finds the release directory in the same directory that contains the running executable, which is not the case for GTest tests (they are written to `bin/tests/executablename`.

This patch requires the functionality from {D6236}.

Differential Revision: https://developer.blender.org/D6246

===================================================================

M	tests/gtests/CMakeLists.txt
A	tests/gtests/blenloader/CMakeLists.txt
A	tests/gtests/blenloader/blendfile_load_test.cc
M	tests/gtests/testing/CMakeLists.txt
A	tests/gtests/testing/blendfile_loading_test.cc
A	tests/gtests/testing/blendfile_loading_test.h

===================================================================

diff --git a/tests/gtests/CMakeLists.txt b/tests/gtests/CMakeLists.txt
index 285b414e997..54a1ee41198 100644
--- a/tests/gtests/CMakeLists.txt
+++ b/tests/gtests/CMakeLists.txt
@@ -13,6 +13,7 @@ if(WITH_GTESTS)
 
   add_subdirectory(testing)
   add_subdirectory(blenlib)
+  add_subdirectory(blenloader)
   add_subdirectory(guardedalloc)
   add_subdirectory(bmesh)
   if(WITH_ALEMBIC)
diff --git a/tests/gtests/blenloader/CMakeLists.txt b/tests/gtests/blenloader/CMakeLists.txt
new file mode 100644
index 00000000000..0930fc4501a
--- /dev/null
+++ b/tests/gtests/blenloader/CMakeLists.txt
@@ -0,0 +1,58 @@
+# ***** BEGIN GPL LICENSE BLOCK *****
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# The Original Code is Copyright (C) 2014, Blender Foundation
+# All rights reserved.
+# ***** END GPL LICENSE BLOCK *****
+
+set(INC
+    .
+    ..
+    ../../../source/blender/blenlib
+    ../../../source/blender/blenloader
+    ../../../source/blender/blenkernel
+    ../../../source/blender/makesdna
+    ../../../source/blender/makesrna
+    ../../../source/blender/depsgraph
+    ../../../intern/guardedalloc
+)
+
+set(LIB
+    bf_blenloader
+    bf_intern_opencolorio # Should not be needed but gives windows linker errors if the ocio libs are linked before this
+    bf_gpu # Should not be needed but gives windows linker errors if the ocio libs are linked before this
+)
+
+include_directories(${INC})
+
+setup_libdirs()
+get_property(BLENDER_SORTED_LIBS GLOBAL PROPERTY BLENDER_SORTED_LIBS_PROP)
+
+if(WITH_BUILDINFO)
+    set(_buildinfo_src "$<TARGET_OBJECTS:buildinfoobj>")
+else()
+    set(_buildinfo_src "")
+endif()
+
+BLENDER_SRC_GTEST_EX(
+  NAME blenloader
+  SRC "blendfile_load_test.cc;${_buildinfo_src}"
+  EXTRA_LIBS "${LIB}"
+  EXTRA_CLI --test-assets-dir "${CMAKE_SOURCE_DIR}/../lib/tests")
+
+unset(_buildinfo_src)
+
+setup_liblinks(blenloader_test)
diff --git a/tests/gtests/blenloader/blendfile_load_test.cc b/tests/gtests/blenloader/blendfile_load_test.cc
new file mode 100644
index 00000000000..69a74cf0dd1
--- /dev/null
+++ b/tests/gtests/blenloader/blendfile_load_test.cc
@@ -0,0 +1,14 @@
+#include "testing/blendfile_loading_test.h"
+
+class BlendfileLoadingTest : public BlendfileLoadingAbstractTest {
+};
+
+TEST_F(BlendfileLoadingTest, CanaryTest)
+{
+  /* Load the smallest blend file we have in the SVN lib/tests directory. */
+  if (!blendfile_load("./modifier_stack/array_test.blend")) {
+    return;
+  }
+  depsgraph_create(DAG_EVAL_RENDER);
+  EXPECT_NE(nullptr, this->depsgraph);
+}
diff --git a/tests/gtests/testing/CMakeLists.txt b/tests/gtests/testing/CMakeLists.txt
index e08ee486933..5115376b43c 100644
--- a/tests/gtests/testing/CMakeLists.txt
+++ b/tests/gtests/testing/CMakeLists.txt
@@ -21,6 +21,15 @@
 set(INC
   .
   ..
+  ../../../source/blender/blenkernel
+  ../../../source/blender/blenlib
+  ../../../source/blender/blenloader
+  ../../../source/blender/depsgraph
+  ../../../source/blender/imbuf
+  ../../../source/blender/makesdna
+  ../../../source/blender/makesrna
+  ../../../source/blender/windowmanager
+  ../../../intern/guardedalloc
   ${GLOG_INCLUDE_DIRS}
   ${GFLAGS_INCLUDE_DIRS}
   ../../../extern/gtest/include
@@ -30,8 +39,10 @@ set(INC_SYS
 )
 
 set(SRC
+  blendfile_loading_test.cc
   testing_main.cc
 
+  blendfile_loading_test.h
   testing.h
 )
 
diff --git a/tests/gtests/testing/blendfile_loading_test.cc b/tests/gtests/testing/blendfile_loading_test.cc
new file mode 100644
index 00000000000..fe7f7ac1256
--- /dev/null
+++ b/tests/gtests/testing/blendfile_loading_test.cc
@@ -0,0 +1,122 @@
+#include "blendfile_loading_test.h"
+
+extern "C" {
+#include "BKE_blender.h"
+#include "BKE_context.h"
+#include "BKE_global.h"
+#include "BKE_image.h"
+#include "BKE_main.h"
+#include "BKE_modifier.h"
+#include "BKE_node.h"
+#include "BKE_scene.h"
+
+#include "BLI_threads.h"
+#include "BLI_path_util.h"
+
+#include "BLO_readfile.h"
+
+#include "DEG_depsgraph_build.h"
+#include "DEG_depsgraph.h"
+
+#include "DNA_genfile.h" /* for DNA_sdna_current_init() */
+#include "DNA_windowmanager_types.h"
+
+#include "IMB_imbuf.h"
+
+#include "MEM_guardedalloc.h"
+
+#include "RNA_define.h"
+
+#include "WM_api.h"
+}
+
+DEFINE_string(test_assets_dir, "", "lib/tests directory from SVN containing the test assets.");
+
+BlendfileLoadingAbstractTest::BlendfileLoadingAbstractTest()
+    : testing::Test(), bfile(nullptr), depsgraph(nullptr)
+{
+}
+
+BlendfileLoadingAbstractTest::~BlendfileLoadingAbstractTest()
+{
+}
+
+void BlendfileLoadingAbstractTest::SetUpTestCase()
+{
+  testing::Test::SetUpTestCase();
+
+  /* Minimal code to make loading a blendfile and constructing a depsgraph not crash, copied from
+   * main() in creator.c. */
+  BLI_threadapi_init();
+
+  DNA_sdna_current_init();
+  BKE_blender_globals_init();
+  IMB_init();
+  BKE_images_init();
+  BKE_modifier_init();
+  DEG_register_node_types();
+  RNA_init();
+  init_nodesystem();
+
+  G.background = true;
+  G.factory_startup = true;
+
+  /* Allocate a dummy window manager. The real window manager will try and load Python scripts from
+   * the release directory, which it won't be able ot find. */
+  G.main->wm.first = MEM_callocN(sizeof(wmWindowManager), __func__);
+}
+
+void BlendfileLoadingAbstractTest::TearDown()
+{
+  depsgraph_free();
+  blendfile_free();
+
+  testing::Test::TearDown();
+}
+
+bool BlendfileLoadingAbstractTest::blendfile_load(const char *filepath)
+{
+  const char *test_assets_dir = FLAGS_test_assets_dir.c_str();
+  if (test_assets_dir == nullptr || test_assets_dir[0] == '\0') {
+    ADD_FAILURE()
+        << "Pass the flag --test-assets-dir and point to the lib/tests directory from SVN.";
+    return false;
+  }
+
+  char abspath[FILENAME_MAX];
+  BLI_path_join(abspath, sizeof(abspath), test_assets_dir, filepath, NULL);
+
+  bfile = BLO_read_from_file(abspath, BLO_READ_SKIP_NONE, NULL /* reports */);
+  if (bfile == nullptr) {
+    ADD_FAILURE();
+    return false;
+  }
+
+  return true;
+}
+
+void BlendfileLoadingAbstractTest::blendfile_free()
+{
+  if (bfile == nullptr) {
+    return;
+  }
+  BLO_blendfiledata_free(bfile);
+  bfile = nullptr;
+}
+
+void BlendfileLoadingAbstractTest::depsgraph_create(eEvaluationMode depsgraph_evaluation_mode)
+{
+  depsgraph = DEG_graph_new(
+      bfile->main, bfile->curscene, bfile->cur_view_layer, depsgraph_evaluation_mode);
+  DEG_graph_build_from_view_layer(depsgraph, bfile->main, bfile->curscene, bfile->cur_view_layer);
+  BKE_scene_graph_update_tagged(depsgraph, bfile->main);
+}
+
+void BlendfileLoadingAbstractTest::depsgraph_free()
+{
+  if (depsgraph == nullptr) {
+    return;
+  }
+  DEG_graph_free(depsgraph);
+  depsgraph = nullptr;
+}
diff --git a/tests/gtests/testing/blendfile_loading_test.h b/tests/gtests/testing/blendfile_loading_test.h
new file mode 100644
index 00000000000..e1f4bbbbdf7
--- /dev/null
+++ b/tests/gtests/testing/blendfile_loading_test.h
@@ -0,0 +1,47 @@
+#ifndef __BLENDFILE_LOADING_TEST_H__
+#define __BLENDFILE_LOADING_TEST_H__
+
+#include "testing/testing.h"
+#include <DEG_depsgraph.h>
+
+struct BlendFileData;
+struct Depsgraph;
+
+class BlendfileLoadingAbstractTest : public testing::Test {
+ protected:
+  struct BlendFileData *bfile;
+  struct Depsgraph *depsgraph;
+
+ public:
+  BlendfileLoadingAbstractTest();
+  virtual ~BlendfileLoadingAbstractTest();
+
+  /* Sets up Blender just enough to not crash on loading
+   * a blendfile and constructing a depsgraph. */
+  static void SetUpTestCase();
+
+ protected:
+  /* Frees the depsgraph & blendfile. */
+  virtual void TearDown();
+
+  /* Loads a blend file from the lib/tests directory from SVN.
+   * Returns 'ok' flag (true=good, false=bad) and sets this->bfile.
+   * Fails the test if the file cannot be loaded (still returns though).
+   * Requires the CLI argument --test-asset-dir to point to ../../lib/tests.
+   *
+   * WARNING: only files saved with Blender 2.80+ can be loaded. Since Blender
+   * is only partially initialised (most importantly, without window manager),
+   * the space types are not registered, so any versioning code that handles
+   * those will SEGFAULT.
+   */
+  bool blendfile_load(const char *filepath);
+  /* Free bfile if it is not nullptr */
+  void blendfile_free();
+
+  /* Create a depsgraph. Assumes a blend file has been loaded to this->bfile. */
+  void depsgraph_create(eEvaluationMode depsgraph_evaluation_mode);
+  /* Free the depsgraph if it's not nullptr. */
+  void depsgraph_free();
+};
+
+#endif /* __BLENDFILE_LOADING_TEST_H__ */



More information about the Bf-blender-cvs mailing list