| 1 | /* |
| 2 | * test-bin.cc |
| 3 | * |
| 4 | * Created on: Jul 31, 2013 |
| 5 | * Author: m.kolny |
| 6 | */ |
| 7 | |
| 8 | #include "mmtest.h" |
| 9 | #include <gstreamermm.h> |
| 10 | #include <string> |
| 11 | |
| 12 | using namespace Gst; |
| 13 | using Glib::RefPtr; |
| 14 | |
| 15 | class BinTest : public ::testing::Test |
| 16 | { |
| 17 | protected: |
| 18 | RefPtr<Bin> bin; |
| 19 | static std::string element_name; |
| 20 | static std::string default_element; |
| 21 | |
| 22 | virtual void SetUp() |
| 23 | { |
| 24 | bin = Bin::create("sample-bin"); |
| 25 | } |
| 26 | |
| 27 | RefPtr<Element> AddElementToBin(const std::string& element_name = default_element, const std::string& name = element_name) |
| 28 | { |
| 29 | RefPtr<Element> element = ElementFactory::create_element(element_name, name); |
| 30 | bin->add(element); |
| 31 | return element; |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | std::string BinTest::default_element = "fakesrc"; |
| 36 | std::string BinTest::element_name = "source"; |
| 37 | |
| 38 | TEST_F(BinTest, ShouldCreateBinWithGivenName) |
| 39 | { |
| 40 | MM_ASSERT_TRUE(bin); |
| 41 | } |
| 42 | |
| 43 | TEST_F(BinTest, ShouldCreateBinWithAutogeneratedName) |
| 44 | { |
| 45 | RefPtr<Bin> test_bin = Bin::create(); |
| 46 | |
| 47 | MM_ASSERT_TRUE(test_bin); |
| 48 | } |
| 49 | |
| 50 | TEST_F(BinTest, ShouldThrowExceptionOnNullElementAdded) |
| 51 | { |
| 52 | RefPtr<Element> empty; |
| 53 | |
| 54 | ASSERT_THROW(bin->add(empty), std::runtime_error); |
| 55 | } |
| 56 | |
| 57 | TEST_F(BinTest, ShouldFindElementInBin) |
| 58 | { |
| 59 | AddElementToBin(); |
| 60 | RefPtr<Element> source_copy = bin->get_element(element_name); |
| 61 | |
| 62 | MM_ASSERT_TRUE(source_copy); |
| 63 | } |
| 64 | |
| 65 | TEST_F(BinTest, ShouldntFindNonexistingElementInBin) |
| 66 | { |
| 67 | AddElementToBin(); |
| 68 | RefPtr<Element> source_copy = bin->get_element("element-with-this-name-cannot-exists-in-bin"); |
| 69 | |
| 70 | MM_ASSERT_FALSE(source_copy); |
| 71 | } |
| 72 | |
| 73 | TEST_F(BinTest, ShouldRemoveElementFromBin) |
| 74 | { |
| 75 | AddElementToBin(); |
| 76 | RefPtr<Element> source = bin->get_element(element_name); |
| 77 | bin->remove(source); |
| 78 | RefPtr<Element> source_copy = bin->get_element(element_name); |
| 79 | |
| 80 | MM_ASSERT_FALSE(source_copy); |
| 81 | } |
| 82 | |
| 83 | TEST_F(BinTest, ShouldFindElementRecursive) |
| 84 | { |
| 85 | RefPtr<Bin> bin_child = Bin::create(); |
| 86 | AddElementToBin(); |
| 87 | bin->add(bin_child); |
| 88 | RefPtr<Element> source = bin_child->get_element_recurse_up(element_name); |
| 89 | |
| 90 | MM_ASSERT_TRUE(source); |
| 91 | } |
| 92 | |
| 93 | TEST_F(BinTest, AddTwoElementsWithTheSameName) |
| 94 | { |
| 95 | AddElementToBin(); |
| 96 | |
| 97 | EXPECT_THROW(AddElementToBin(), std::runtime_error); |
| 98 | } |
| 99 | |
| 100 | TEST_F(BinTest, ShouldFindUnlinkedPads) |
| 101 | { |
| 102 | AddElementToBin(); |
| 103 | AddElementToBin("fakesink", "sink"); |
| 104 | |
| 105 | RefPtr<Pad> pad_sink = bin->find_unlinked_pad(PAD_SINK); |
| 106 | RefPtr<Pad> pad_src = bin->find_unlinked_pad(PAD_SRC); |
| 107 | |
| 108 | MM_ASSERT_TRUE(pad_sink); |
| 109 | MM_ASSERT_TRUE(pad_src); |
| 110 | } |
| 111 | |
| 112 | TEST_F(BinTest, ShouldNotFindUnlinkedPads) |
| 113 | { |
| 114 | RefPtr<Element> src = AddElementToBin(); |
| 115 | RefPtr<Element> sink = AddElementToBin("fakesink", "sink"); |
| 116 | |
| 117 | src->link(sink); |
| 118 | |
| 119 | RefPtr<Pad> pad_sink = bin->find_unlinked_pad(PAD_SINK); |
| 120 | RefPtr<Pad> pad_src = bin->find_unlinked_pad(PAD_SRC); |
| 121 | |
| 122 | MM_ASSERT_FALSE(pad_sink); |
| 123 | MM_ASSERT_FALSE(pad_src); |
| 124 | } |
| 125 | |
| 126 | TEST_F(BinTest, ShouldReturnCorrectQuantityOfChildren) |
| 127 | { |
| 128 | int element_count = 5; |
| 129 | |
| 130 | for (int i =0; i < element_count; i++) |
| 131 | AddElementToBin("fakesrc", std::string(std::string("element") + std::to_string(i)).c_str()); |
| 132 | |
| 133 | EXPECT_EQ(element_count, bin->get_num_children()); |
| 134 | } |
| 135 | |
| 136 | TEST_F(BinTest, ShouldCorrectIterateElements) |
| 137 | { |
| 138 | AddElementToBin("fakesrc", "element1"); |
| 139 | AddElementToBin("fakesrc", "element2"); |
| 140 | std::vector<std::string> elements = {"element1", "element2"}; |
| 141 | |
| 142 | Gst::Iterator<Gst::Element> iterator = bin->iterate_elements(); |
| 143 | |
| 144 | while (iterator.next()) |
| 145 | { |
| 146 | auto pos = std::find(elements.begin(), elements.end(), iterator->get_name()); |
| 147 | ASSERT_NE(elements.end(), pos); |
| 148 | elements.erase(pos); |
| 149 | } |
| 150 | |
| 151 | ASSERT_EQ(0u, elements.size()); |
| 152 | } |
| 153 | |
| 154 | TEST_F(BinTest, ShouldContainsAddedElements) |
| 155 | { |
| 156 | AddElementToBin("fakesrc", "fakesrc0"); |
| 157 | AddElementToBin("tee", "tee0"); |
| 158 | AddElementToBin("queue", "queue0"); |
| 159 | std::vector<std::string> elements = {"fakesrc0", "tee0", "queue0"}; |
| 160 | |
| 161 | Glib::ListHandle<RefPtr<Element> > children = bin->get_children(); |
| 162 | for (auto element : children) |
| 163 | { |
| 164 | auto pos = std::find(elements.begin(), elements.end(), element->get_name()); |
| 165 | ASSERT_NE(elements.end(), pos); |
| 166 | elements.erase(pos); |
| 167 | } |
| 168 | ASSERT_EQ(0u, elements.size()); |
| 169 | } |
| 170 | |
| 171 | TEST_F(BinTest, ShouldCallOnElementAddedHandler) |
| 172 | { |
| 173 | bool call = false; |
| 174 | bin->signal_element_added().connect([&call](const RefPtr<Element>&){ |
| 175 | call = true; |
| 176 | }); |
| 177 | AddElementToBin(); |
| 178 | MM_ASSERT_TRUE(call); |
| 179 | } |
| 180 | |
| 181 | TEST_F(BinTest, ShouldCallOnElementRemovedHandler) |
| 182 | { |
| 183 | bool call = false; |
| 184 | RefPtr<Element> e = AddElementToBin(); |
| 185 | bin->signal_element_removed().connect([&call](const RefPtr<Element>&){ |
| 186 | call = true; |
| 187 | }); |
| 188 | bin->remove(e); |
| 189 | |
| 190 | MM_ASSERT_TRUE(call); |
| 191 | } |
| 192 | |
| 193 | class OverrideBin : public Bin |
| 194 | { |
| 195 | bool add_element_flag = false; |
| 196 | bool remove_element_flag = false; |
| 197 | public: |
| 198 | OverrideBin() : Bin() {} |
| 199 | bool add_element_vfunc(const Glib::RefPtr<Gst::Element>& element) override |
| 200 | { |
| 201 | add_element_flag = true; |
| 202 | return Bin::add_element_vfunc(element); |
| 203 | } |
| 204 | bool remove_element_vfunc(const Glib::RefPtr<Gst::Element>& element) override |
| 205 | { |
| 206 | remove_element_flag = true; |
| 207 | return Bin::remove_element_vfunc(element); |
| 208 | } |
| 209 | |
| 210 | bool get_add_flag() const { return add_element_flag; } |
| 211 | bool get_remove_flag() const { return remove_element_flag; } |
| 212 | }; |
| 213 | |
| 214 | TEST_F(BinTest, ShouldCorrectOverrideAddAndRemoveElementFunction) |
| 215 | { |
| 216 | OverrideBin bin; |
| 217 | RefPtr<Element> element = ElementFactory::create_element("tee"); |
| 218 | bin.add(element); |
| 219 | MM_ASSERT_TRUE(bin.get_add_flag()); |
| 220 | bin.remove(element); |
| 221 | MM_ASSERT_TRUE(bin.get_remove_flag()); |
| 222 | } |