OSDN Git Service

test: move vaInitialize/vaTerminate to a global test environment
authorU. Artie Eoff <ullysses.a.eoff@intel.com>
Wed, 5 Oct 2016 17:54:24 +0000 (10:54 -0700)
committerSean V Kelley <seanvk@posteo.de>
Thu, 6 Oct 2016 21:02:27 +0000 (14:02 -0700)
Move the VADisplay, vaInitialize and vaTerminate responsibility
out of the I965TestFixture class and into a global
I965TestEnvironment (::testing::Environment) singleton.

The I965TestEnvironment singleton instance is registered with
the gtest framework at startup and it's SetUp and TearDown routines
are executed before and after all tests are executed.  This
allows all tests to obtain access to the VADisplay et. al.
outside of an I965TestFixture instance.

Essentially, this results in only one VADisplay being shared
between all executed test cases and one init/term sequence
for the entire test program execution.  This more closely
resembles how several real-world programs would use the
driver (i.e. init driver once, encode/decode multiple streams
and terminate driver once).

Prior to this, each test case had it's own VADisplay instance
and init/term sequence.  That behavior can still be achieved by
executing one test case at a time via the --gtest_filter option.

Signed-off-by: U. Artie Eoff <ullysses.a.eoff@intel.com>
Reviewed-by: Sean V Kelley <seanvk@posteo.de>
test/Makefile.am
test/i965_test_environment.cpp [new file with mode: 0644]
test/i965_test_environment.h [new file with mode: 0644]
test/i965_test_fixture.cpp
test/i965_test_fixture.h
test/test_main.cpp

index 7d1ba43..6448ad5 100644 (file)
@@ -46,6 +46,7 @@ noinst_PROGRAMS = test_i965_drv_video
 noinst_HEADERS =                                                       \
        i965_internal_decl.h                                            \
        i965_jpeg_test_data.h                                           \
+       i965_test_environment.h                                         \
        i965_test_fixture.h                                             \
        test.h                                                          \
        test_utils.h                                                    \
@@ -55,10 +56,11 @@ test_i965_drv_video_SOURCES =                                               \
        i965_chipset_test.cpp                                           \
        i965_initialize_test.cpp                                        \
        i965_jpeg_test_data.cpp                                         \
-       i965_test_fixture.cpp                                           \
        i965_jpeg_decode_test.cpp                                       \
        i965_jpeg_encode_test.cpp                                       \
        i965_surface_test.cpp                                           \
+       i965_test_environment.cpp                                       \
+       i965_test_fixture.cpp                                           \
        object_heap_test.cpp                                            \
        test_main.cpp                                                   \
        $(NULL)
diff --git a/test/i965_test_environment.cpp b/test/i965_test_environment.cpp
new file mode 100644 (file)
index 0000000..0049c14
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2016 Intel Corporation. All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include "i965_test_environment.h"
+
+#include <fcntl.h> // for O_RDWR
+#include <unistd.h> // for close()
+#include <va/va_drm.h>
+
+I965TestEnvironment* I965TestEnvironment::instance()
+{
+    static I965TestEnvironment* e = new I965TestEnvironment;
+    return e;
+}
+
+I965TestEnvironment::I965TestEnvironment()
+    : ::testing::Environment()
+    , m_handle(-1)
+    , m_vaDisplay(NULL)
+{
+    return;
+}
+
+void I965TestEnvironment::SetUp()
+{
+    ASSERT_EQ(-1, m_handle);
+    ASSERT_PTR_NULL(m_vaDisplay);
+
+    m_handle = open("/dev/dri/renderD128", O_RDWR);
+    if (m_handle < 0)
+        m_handle = open("/dev/dri/card0", O_RDWR);
+
+    m_vaDisplay = vaGetDisplayDRM(m_handle);
+
+    ASSERT_PTR(m_vaDisplay);
+
+    setenv("LIBVA_DRIVERS_PATH", TEST_VA_DRIVERS_PATH, 1);
+    setenv("LIBVA_DRIVER_NAME", "i965", 1);
+
+    int major, minor;
+    ASSERT_STATUS(vaInitialize(*this, &major, &minor));
+
+    EXPECT_EQ(VA_MAJOR_VERSION, major);
+    EXPECT_EQ(VA_MINOR_VERSION, minor);
+
+    VADriverContextP context(*this);
+    ASSERT_PTR(context);
+
+    const std::string vendor(context->str_vendor);
+
+    ::testing::Test::RecordProperty("driver_vendor", vendor);
+    ::testing::Test::RecordProperty("vaapi_version", VA_VERSION_S);
+}
+
+void I965TestEnvironment::TearDown()
+{
+    if (m_vaDisplay)
+        EXPECT_STATUS(vaTerminate(m_vaDisplay));
+
+    if (m_handle >= 0)
+        close(m_handle);
+
+    m_handle = -1;
+    m_vaDisplay = NULL;
+}
diff --git a/test/i965_test_environment.h b/test/i965_test_environment.h
new file mode 100644 (file)
index 0000000..b80c360
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2016 Intel Corporation. All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef I965_TEST_ENVIRONMENT_H
+#define I965_TEST_ENVIRONMENT_H
+
+#include "test.h"
+#include "i965_internal_decl.h"
+
+/**
+ * This test environment handles initialization and termination of the i965
+ * driver and display. It defines various operators to make it implicitly
+ * convertible to a VADriverContextP, VADisplay, VADisplayContextP, and
+ * i965_driver_data*.  Other operators may be defined, too.  These operators
+ * allow an instance of the test environment to be passed to various driver
+ * functions that take one of those parameter types.
+ *
+ * See the "Global Set-Up and Tear-Down" section in gtest/docs/AdvancedGuide.md
+ * for more details on how a ::testing::Environment operates.
+ */
+class I965TestEnvironment
+    : public ::testing::Environment
+{
+protected:
+    /**
+     * This is invoked by gtest before any tests are executed.  Gtest will not
+     * run any tests if this method generates a fatal test assertion failure.
+     */
+    virtual void SetUp();
+
+    /**
+     * This is invoked by gtest after all the tests are executed. If SetUp()
+     * generates a fatal test assertion, this is also invoked by gtest
+     * afterwards.
+     */
+    virtual void TearDown();
+
+private:
+    I965TestEnvironment();
+
+    int m_handle; /* current native display handle */
+    VADisplay m_vaDisplay; /* current VADisplay handle */
+
+public:
+    static I965TestEnvironment* instance();
+
+    /**
+     * VADisplay implicit and explicit conversion operator.
+     */
+    inline operator VADisplay() { return m_vaDisplay; }
+
+    /**
+     * VADisplayContextP implict and explicit conversion operator.
+     */
+    inline operator VADisplayContextP()
+    {
+        return (VADisplayContextP)((VADisplay)*this);
+    }
+
+    /**
+     * VADriverContextP implict and explicit conversion operator.
+     */
+    inline operator VADriverContextP()
+    {
+        VADisplayContextP dctx(*this);
+        return dctx ? dctx->pDriverContext : NULL;
+    }
+
+    /**
+     * i965_driver_data * implict and explicit conversion operator.
+     */
+    inline operator struct i965_driver_data *()
+    {
+        VADriverContextP ctx(*this);
+        return ctx ? i965_driver_data(ctx) : NULL;
+    }
+};
+
+#endif
index 073a24e..8fd9142 100644 (file)
 
 #include "i965_test_fixture.h"
 
-#include <fcntl.h> // for O_RDWR
-#include <unistd.h> // for close()
-#include <va/va_drm.h>
-
-I965TestFixture::I965TestFixture()
-    : ::testing::Test::Test()
-    , m_handle(-1)
-    , m_vaDisplay(NULL)
-{
-    setenv("LIBVA_DRIVERS_PATH", TEST_VA_DRIVERS_PATH, 1);
-    setenv("LIBVA_DRIVER_NAME", "i965", 1);
-}
-
-I965TestFixture::~I965TestFixture()
-{
-    if (m_handle >= 0)
-        close(m_handle);
-    m_handle = -1;
-    m_vaDisplay = NULL;
-}
-
 const std::string I965TestFixture::getFullTestName() const
 {
     const ::testing::TestInfo * const info =
@@ -54,49 +33,6 @@ const std::string I965TestFixture::getFullTestName() const
         + std::string(info->name());
 }
 
-void I965TestFixture::initialize()
-{
-    ASSERT_FALSE(NULL == (VADisplay)*this);
-
-    int major, minor;
-    ASSERT_STATUS(vaInitialize(*this, &major, &minor));
-
-    EXPECT_EQ(VA_MAJOR_VERSION, major);
-    EXPECT_EQ(VA_MINOR_VERSION, minor);
-
-    VADriverContextP context(*this);
-    ASSERT_PTR(context);
-
-    const std::string vendor(context->str_vendor);
-
-    ::testing::Test::RecordProperty("driver_vendor", vendor);
-    ::testing::Test::RecordProperty("vaapi_version", VA_VERSION_S);
-}
-
-void I965TestFixture::terminate()
-{
-    if (m_vaDisplay)
-        EXPECT_STATUS(vaTerminate(m_vaDisplay));
-}
-
-I965TestFixture::operator VADisplay()
-{
-    if (m_vaDisplay)
-        return m_vaDisplay;
-
-    m_handle = open("/dev/dri/renderD128", O_RDWR);
-    if (m_handle < 0)
-        m_handle = open("/dev/dri/card0", O_RDWR);
-
-    m_vaDisplay = vaGetDisplayDRM(m_handle);
-    if (!m_vaDisplay && m_handle >= 0) {
-        close(m_handle);
-        m_handle = -1;
-    }
-
-    return m_vaDisplay;
-}
-
 Surfaces I965TestFixture::createSurfaces(int w, int h, int format, size_t count,
     const SurfaceAttribs& attributes)
 {
@@ -217,33 +153,3 @@ void I965TestFixture::syncSurface(VASurfaceID surface)
     EXPECT_STATUS(
         i965_SyncSurface(*this, surface));
 }
-
-class I965TestFixtureTest
-    : public I965TestFixture
-{
-protected:
-    virtual void SetUp() { } // override I965TestFixture::SetUp
-    virtual void TearDown() { } // override I965TestFixture::TearDown
-};
-
-TEST_F(I965TestFixtureTest, Logic)
-{
-    VADisplayContextP dispCtx(*this);
-    VADriverContextP drvCtx(*this);
-    struct i965_driver_data* i965(*this);
-    VADisplay display(*this);
-
-    EXPECT_PTR(display);
-    EXPECT_PTR(dispCtx);
-    EXPECT_PTR(drvCtx);
-    EXPECT_TRUE(NULL == i965);
-    EXPECT_TRUE(NULL == drvCtx->handle);
-
-    ASSERT_NO_FATAL_FAILURE(initialize());
-
-    i965 = *this;
-    EXPECT_PTR(i965);
-    EXPECT_PTR(drvCtx->handle);
-
-    terminate();
-}
index dc6f6c4..9122d84 100644 (file)
@@ -25,8 +25,7 @@
 #ifndef I965_TEST_FIXTURE_H
 #define I965_TEST_FIXTURE_H
 
-#include "test.h"
-#include "i965_internal_decl.h"
+#include "i965_test_environment.h"
 
 #include <string>
 #include <vector>
@@ -37,8 +36,7 @@ typedef std::vector<VAConfigAttrib> ConfigAttribs;
 typedef std::vector<VABufferID> Buffers;
 
 /**
- * This test fixture handles initialization and termination of the i965 driver
- * and display. It defines various operators to make it implicitly convertible
+ * This test fixture defines various operators to make it implicitly convertible
  * to a VADriverContextP, VADisplay, VADisplayContextP, and i965_driver_data*.
  * Other operators may be defined, too.  These operators allow an instance of
  * the test fixture to be passed to various driver functions that take one of
@@ -46,12 +44,7 @@ typedef std::vector<VABufferID> Buffers;
  * test fixture to simplify writing test cases.
  *
  * Test cases that wish to use this fixture should define their own test
- * fixture class that derives from this one.  The derived test fixture may
- * override the SetUp() and TearDown() methods.  These two methods are invoked
- * by gtest before and after a TEST_F test body is executed, respectively.  The
- * derived test fixture should be sure to call the I965TestFixture::SetUp() and
- * I965TestFixture::TearDown() methods when they are overridden to ensure
- * proper initialization and termination of the driver and display.
+ * fixture class that derives from this one.
  *
  * See the "Test Fixtures" section in gtest/docs/Primer.md for more details
  * on how test fixtures are used.
@@ -60,46 +53,10 @@ class I965TestFixture
     : public ::testing::Test
 {
 public:
-    I965TestFixture();
-    virtual ~I965TestFixture();
-
-protected:
-    /**
-     * This is invoked by gtest before the test body is executed.  Gtest will
-     * not run the test body if this method generates a fatal test assertion
-     * failure.
-     */
-    virtual void SetUp()
-    {
-        ASSERT_NO_FATAL_FAILURE(initialize());
-    }
-
-    /**
-     * This is invoked by gtest after the test body is executed... even if the
-     * test body generates a fatal or non-fatal test assertion failure.  If
-     * SetUp() generates a fatal test assertion, this is also invoked by gtest
-     * afterwards.
-     */
-    virtual void TearDown()
-    {
-        terminate();
-    }
+    virtual ~I965TestFixture() { }
 
     const std::string getFullTestName() const;
 
-public:
-    /**
-     * Initializes the i965 driver and display.  May generate a fatal or
-     * non-fatal test assertion failure.
-     */
-    void initialize();
-
-    /**
-     * Terminates the i965 driver and display.  May generate a non-fatal
-     * test assertion failure.
-     */
-    void terminate();
-
     /**
      * Convenience wrapper for i965_CreateSurfaces or i965_CreateSurfaces2.
      * If SurfaceAttribs are specified then i965_CreateSurfaces2 is used,
@@ -215,37 +172,26 @@ public:
     /**
      * VADisplay implicit and explicit conversion operator.
      */
-    operator VADisplay();
+    inline operator VADisplay()
+    { return *I965TestEnvironment::instance(); }
 
     /**
      * VADisplayContextP implict and explicit conversion operator.
      */
     inline operator VADisplayContextP()
-    {
-        return (VADisplayContextP)((VADisplay)*this);
-    }
+    { return *I965TestEnvironment::instance(); }
 
     /**
      * VADriverContextP implict and explicit conversion operator.
      */
     inline operator VADriverContextP()
-    {
-        VADisplayContextP dctx(*this);
-        return dctx ? dctx->pDriverContext : NULL;
-    }
+    { return *I965TestEnvironment::instance(); }
 
     /**
      * i965_driver_data * implict and explicit conversion operator.
      */
     inline operator struct i965_driver_data *()
-    {
-        VADriverContextP ctx(*this);
-        return ctx ? i965_driver_data(ctx) : NULL;
-    }
-
-private:
-    int m_handle; /* current native display handle */
-    VADisplay m_vaDisplay; /* current VADisplay handle */
+    { return *I965TestEnvironment::instance(); }
 };
 
 #endif
index 55a5b09..0f87856 100644 (file)
  */
 
 #include "test.h"
+#include "i965_test_environment.h"
 
 int main(int argc, char **argv)
 {
     ::testing::InitGoogleTest(&argc, argv);
 
+    /** NOTE: gtest takes ownership of the I965TestEnvironment instance **/
+    ::testing::AddGlobalTestEnvironment(I965TestEnvironment::instance());
+
     return RUN_ALL_TESTS();
 }