[gstreamermm] Gst::Bin: added more tests to GstBin wrapper.



commit 6c5a3d3da777f1208e57b21bb66372970c2cebca
Author: Marcin Kolny <marcin kolny gmail com>
Date:   Fri Aug 8 23:12:02 2014 +0200

    Gst::Bin: added more tests to GstBin wrapper.
    
        * .gitignore: added test-bin file to ignored list
        * tests/Makefile.am: previously Bin test file wasn't added to a
        makefile. I fixed it in this commit.
        * tests/test-bin.cc: renamed existing tests, added tests for most
        of functions, virtual methods, signals.

 .gitignore        |    1 +
 tests/Makefile.am |    3 +-
 tests/test-bin.cc |  211 ++++++++++++++++++++++++++++++++++++++---------------
 3 files changed, 155 insertions(+), 60 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index fba784a..870ae81 100644
--- a/.gitignore
+++ b/.gitignore
@@ -478,6 +478,7 @@ gstreamer/src/xvimagesink.hg
 #tests/
 tests/test-allocator
 tests/test-atomicqueue
+tests/test-bin
 tests/test-buffer
 tests/test-bus
 tests/test-caps
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 62b7d05..e1e7fdf 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -21,7 +21,7 @@ AM_CXXFLAGS = $(GSTREAMERMM_WXXFLAGS) -g
 LDADD = $(GSTREAMERMM_LIBS) $(local_libgstreamermm) -lgtest -lpthread
 
 check_PROGRAMS = test-caps test-buffer test-bus test-caps test-pad \
-                 test-allocator test-atomicqueue \
+                 test-allocator test-atomicqueue test-bin \
                  test-urihandler test-ghostpad test-init \
                  test-query test-structure test-taglist test-plugin-appsink \
                  test-plugin-appsrc test-plugin-register test-plugin-pushsrc \
@@ -36,6 +36,7 @@ TEST_REGRESSION_UTILS = regression/utils.cc
 
 test_allocator_SOURCES         = test-allocator.cc $(TEST_MAIN_SOURCE)
 test_atomicqueue_SOURCES       = test-atomicqueue.cc $(TEST_MAIN_SOURCE)
+test_bin_SOURCES                       = test-bin.cc $(TEST_MAIN_SOURCE)
 test_caps_SOURCES                      = test-caps.cc $(TEST_MAIN_SOURCE)
 test_buffer_SOURCES                    = test-buffer.cc $(TEST_MAIN_SOURCE)
 test_bus_SOURCES                       = test-bus.cc $(TEST_MAIN_SOURCE)
diff --git a/tests/test-bin.cc b/tests/test-bin.cc
index 7e0560f..c3dc203 100644
--- a/tests/test-bin.cc
+++ b/tests/test-bin.cc
@@ -15,96 +15,101 @@ using Glib::RefPtr;
 class BinTest : public ::testing::Test
 {
 protected:
-    RefPtr<Bin> bin;
-    static constexpr gchar* element_name = "source";
-    static constexpr gchar* default_element = "fakesrc";
-
-    virtual void SetUp()
-    {
-        bin = Bin::create("sample-bin");
-    }
-
-    RefPtr<Element> AddElementToBin(const gchar* element_name = default_element, const gchar* name = 
element_name)
-    {
-        RefPtr<Element> element = ElementFactory::create_element(element_name, name);
-
-        bin->add(element);
-
-        return element;
-    }
+  RefPtr<Bin> bin;
+  static std::string element_name;
+  static std::string default_element;
+
+  virtual void SetUp()
+  {
+    bin = Bin::create("sample-bin");
+  }
+
+  RefPtr<Element> AddElementToBin(const std::string& element_name = default_element, const std::string& name 
= element_name)
+  {
+    RefPtr<Element> element = ElementFactory::create_element(element_name, name);
+    bin->add(element);
+    return element;
+  }
 };
 
+std::string BinTest::default_element = "fakesrc";
+std::string BinTest::element_name = "source";
 
-TEST_F(BinTest, CreatingBin)
+TEST_F(BinTest, ShouldCreateBinWithGivenName)
 {
-    ASSERT_TRUE(bin);
+  ASSERT_TRUE(bin);
 }
 
-
-TEST_F(BinTest, GetElementFromBin)
+TEST_F(BinTest, ShouldCreateBinWithAutogeneratedName)
 {
-    AddElementToBin();
+  RefPtr<Bin> test_bin = Bin::create();
 
-    RefPtr<Element> source_copy = bin->get_element(element_name);
-
-    ASSERT_TRUE(source_copy);
+  ASSERT_TRUE(test_bin);
 }
 
-TEST_F(BinTest, GetNonExistingElement)
+TEST_F(BinTest, ShouldThrowExceptionOnNullElementAdded)
 {
-    AddElementToBin();
-
-    RefPtr<Element> source_copy = bin->get_element("element-with-this-name-cannot-exists-in-bin");
+  RefPtr<Element> empty;
 
-    ASSERT_FALSE(source_copy);
+  ASSERT_THROW(bin->add(empty), std::runtime_error);
 }
 
-TEST_F(BinTest, RemoveElementFromBin)
+TEST_F(BinTest, ShouldFindElementInBin)
 {
-    AddElementToBin();
+  AddElementToBin();
+  RefPtr<Element> source_copy = bin->get_element(element_name);
 
-    RefPtr<Element> source = bin->get_element(element_name);
-
-    bin->remove(source);
+  ASSERT_TRUE(source_copy);
+}
 
-    RefPtr<Element> source_copy = bin->get_element(element_name);
+TEST_F(BinTest, ShouldntFindNonexistingElementInBin)
+{
+  AddElementToBin();
+  RefPtr<Element> source_copy = bin->get_element("element-with-this-name-cannot-exists-in-bin");
 
-    ASSERT_FALSE(source_copy);
+  ASSERT_FALSE(source_copy);
 }
 
-TEST_F(BinTest, FindElementInParentBin)
+TEST_F(BinTest, ShouldRemoveElementFromBin)
 {
-    RefPtr<Bin> bin_child = Bin::create();
-
-    AddElementToBin();
+  AddElementToBin();
+  RefPtr<Element> source = bin->get_element(element_name);
+  bin->remove(source);
+  RefPtr<Element> source_copy = bin->get_element(element_name);
 
-    bin->add(bin_child);
+  ASSERT_FALSE(source_copy);
+}
 
-    RefPtr<Element> source = bin_child->get_element_recurse_up(element_name);
+TEST_F(BinTest, ShouldFindElementRecursive)
+{
+  RefPtr<Bin> bin_child = Bin::create();
+  AddElementToBin();
+  bin->add(bin_child);
+  RefPtr<Element> source = bin_child->get_element_recurse_up(element_name);
 
-    ASSERT_TRUE(source);
+  ASSERT_TRUE(source);
 }
 
 TEST_F(BinTest, AddTwoElementsWithTheSameName)
 {
-    AddElementToBin();
+  AddElementToBin();
 
-    EXPECT_ANY_THROW(AddElementToBin());
+  EXPECT_THROW(AddElementToBin(), std::runtime_error);
 }
 
-TEST_F(BinTest, FindUnlinkedPad)
+TEST_F(BinTest, ShouldFindUnlinkedPads)
 {
-    AddElementToBin();
-    AddElementToBin("fakesink", "sink");
+  AddElementToBin();
+  AddElementToBin("fakesink", "sink");
 
-    RefPtr<Pad> pad_sink = bin->find_unlinked_pad(PAD_SINK);
-    RefPtr<Pad> pad_src = bin->find_unlinked_pad(PAD_SRC);
+  RefPtr<Pad> pad_sink = bin->find_unlinked_pad(PAD_SINK);
+  RefPtr<Pad> pad_src = bin->find_unlinked_pad(PAD_SRC);
 
-    ASSERT_TRUE(pad_sink);
-    ASSERT_TRUE(pad_src);
+  ASSERT_TRUE(pad_sink);
+  ASSERT_TRUE(pad_src);
 }
 
-TEST_F(BinTest, FindUnlinkedPadWhenElementsAreLinked)
+TEST_F(BinTest, ShouldNotFindUnlinkedPads)
 {
     RefPtr<Element> src = AddElementToBin();
     RefPtr<Element> sink = AddElementToBin("fakesink", "sink");
@@ -118,12 +123,100 @@ TEST_F(BinTest, FindUnlinkedPadWhenElementsAreLinked)
     ASSERT_FALSE(pad_src);
 }
 
-TEST_F(BinTest, GetCorrectChildNumber)
+TEST_F(BinTest, ShouldReturnCorrectQuantityOfChildren)
+{
+  int element_count = 5;
+
+  for (int i =0; i < element_count; i++)
+    AddElementToBin("fakesrc", std::string(std::string("element") + std::to_string(i)).c_str());
+
+  EXPECT_EQ(element_count, bin->get_num_children());
+}
+
+TEST_F(BinTest, ShouldCorrectIterateElements)
+{
+  AddElementToBin("fakesrc", "element1");
+  AddElementToBin("fakesrc", "element2");
+  std::vector<std::string> elements = {"element1", "element2"};
+
+  Gst::Iterator<Gst::Element> iterator = bin->iterate_elements();
+
+  while (iterator.next())
+  {
+    auto pos = std::find(elements.begin(), elements.end(), iterator->get_name());
+    ASSERT_NE(elements.end(), pos);
+    elements.erase(pos);
+  }
+
+  ASSERT_EQ(0, elements.size());
+}
+
+TEST_F(BinTest, ShouldContainsAddedElements)
+{
+  AddElementToBin("fakesrc", "fakesrc0");
+  AddElementToBin("tee", "tee0");
+  AddElementToBin("queue", "queue0");
+  std::vector<std::string> elements = {"fakesrc0", "tee0", "queue0"};
+
+  Glib::ListHandle<RefPtr<Element> > children = bin->get_children();
+  for (auto element : children)
+  {
+    auto pos = std::find(elements.begin(), elements.end(), element->get_name());
+    ASSERT_NE(elements.end(), pos);
+    elements.erase(pos);
+  }
+  ASSERT_EQ(0, elements.size());
+}
+
+TEST_F(BinTest, ShouldCallOnElementAddedHandler)
 {
-    int element_count = 5;
+  bool call = false;
+  bin->signal_element_added().connect([&call](const RefPtr<Element>& e){
+    call = true;
+  });
+  AddElementToBin();
+  ASSERT_TRUE(call);
+}
 
-    for (int i =0; i < element_count; i++)
-        AddElementToBin("fakesrc", std::string(std::string("element") + std::to_string(i)).c_str());
+TEST_F(BinTest, ShouldCallOnElementRemovedHandler)
+{
+  bool call = false;
+  RefPtr<Element> e = AddElementToBin();
+  bin->signal_element_removed().connect([&call](const RefPtr<Element>& e){
+    call = true;
+  });
+  bin->remove(e);
+
+  ASSERT_TRUE(call);
+}
 
-    EXPECT_EQ(element_count, bin->get_num_children());
+class OverrideBin : public Bin
+{
+  bool add_element_flag = false;
+  bool remove_element_flag = false;
+public:
+  OverrideBin() : Bin() {}
+  bool add_element_vfunc(const Glib::RefPtr<Gst::Element>& element) override
+  {
+    add_element_flag = true;
+    return Bin::add_element_vfunc(element);
+  }
+  bool remove_element_vfunc(const Glib::RefPtr<Gst::Element>& element) override
+  {
+    remove_element_flag = true;
+    return Bin::remove_element_vfunc(element);
+  }
+
+  bool get_add_flag() const { return add_element_flag; }
+  bool get_remove_flag() const { return remove_element_flag; }
+};
+
+TEST_F(BinTest, ShouldCorrectOverrideAddAndRemoveElementFunction)
+{
+  OverrideBin bin;
+  RefPtr<Element> element = ElementFactory::create_element("tee");
+  bin.add(element);
+  ASSERT_TRUE(bin.get_add_flag());
+  bin.remove(element);
+  ASSERT_TRUE(bin.get_remove_flag());
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]