| 1 | /* |
| 2 | * test-structure.cc |
| 3 | * |
| 4 | * Created on: 1 sie 2013 |
| 5 | * Author: loganek |
| 6 | */ |
| 7 | |
| 8 | |
| 9 | #include "mmtest.h" |
| 10 | #include <gstreamermm.h> |
| 11 | |
| 12 | using namespace Gst; |
| 13 | |
| 14 | template<typename T> |
| 15 | void CheckEq(const T& expected, const T& output) |
| 16 | { |
| 17 | EXPECT_EQ(expected, output); |
| 18 | } |
| 19 | |
| 20 | template<> |
| 21 | void CheckEq(const Fraction& expected, const Fraction& output) |
| 22 | { |
| 23 | EXPECT_EQ(expected.denom, output.denom); |
| 24 | EXPECT_EQ(expected.num, output.num); |
| 25 | } |
| 26 | |
| 27 | template<> |
| 28 | void CheckEq(const Range<Fraction>& expected, const Range<Fraction>& output) |
| 29 | { |
| 30 | CheckEq(expected.min, output.min); |
| 31 | CheckEq(expected.max, output.max); |
| 32 | } |
| 33 | |
| 34 | template<> |
| 35 | void CheckEq(const Glib::DateTime& expected, const Glib::DateTime& output) |
| 36 | { |
| 37 | CheckEq(expected.get_year(), output.get_year()); |
| 38 | CheckEq(expected.get_second(), output.get_second()); |
| 39 | } |
| 40 | |
| 41 | class StructureTest : public ::testing::Test |
| 42 | { |
| 43 | protected: |
| 44 | Structure structure; |
| 45 | |
| 46 | virtual void SetUp() |
| 47 | { |
| 48 | structure = Structure("test-struct"); |
| 49 | } |
| 50 | |
| 51 | template<typename T> |
| 52 | void CheckGetField(const T& expected, const Glib::ustring& field_name) |
| 53 | { |
| 54 | T output; |
| 55 | structure.get_field(field_name, output); |
| 56 | |
| 57 | CheckEq(expected, output); |
| 58 | } |
| 59 | |
| 60 | template<typename T> |
| 61 | void CheckGetSetField(const T& expected, const Glib::ustring& field_name) |
| 62 | { |
| 63 | structure.set_field(field_name, expected); |
| 64 | |
| 65 | CheckGetField(expected, field_name); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | TEST_F(StructureTest, GetSetStringVariable) |
| 70 | { |
| 71 | CheckGetSetField<std::string>("this is simple test", "string"); |
| 72 | } |
| 73 | |
| 74 | TEST_F(StructureTest, GetSetInt64Variable) |
| 75 | { |
| 76 | structure.set_field<gint64>("int64", 123); |
| 77 | guint64 val; |
| 78 | MM_ASSERT_FALSE(structure.get_field<guint64>("int64", val)); |
| 79 | } |
| 80 | |
| 81 | TEST_F(StructureTest, GetSetIntegerVariable) |
| 82 | { |
| 83 | CheckGetSetField(1234, "integer"); |
| 84 | } |
| 85 | |
| 86 | TEST_F(StructureTest, GetSetFractionVariable) |
| 87 | { |
| 88 | CheckGetSetField(Fraction(5,6), "fraction"); |
| 89 | } |
| 90 | |
| 91 | TEST_F(StructureTest, GetSetFractionRangeVariable) |
| 92 | { |
| 93 | CheckGetSetField(Gst::Range<Gst::Fraction>(Fraction(5,6), Fraction(13,14)), "fraction_range"); |
| 94 | } |
| 95 | |
| 96 | TEST_F(StructureTest, GetSetDateVariable) |
| 97 | { |
| 98 | CheckGetSetField(Glib::Date(10, Glib::Date::DECEMBER, 1991), "date"); |
| 99 | } |
| 100 | |
| 101 | TEST_F(StructureTest, GetSetEnumVariable) |
| 102 | { |
| 103 | State input_state = STATE_PAUSED; |
| 104 | structure.set_field("state", Glib::Value<State>::value_type(), input_state); |
| 105 | |
| 106 | int output_state; |
| 107 | structure.get_field("state", Glib::Value<State>::value_type(), output_state); |
| 108 | |
| 109 | EXPECT_EQ(input_state, (State)output_state); |
| 110 | } |
| 111 | |
| 112 | TEST_F(StructureTest, CreateStructureFromFieldsList) |
| 113 | { |
| 114 | structure = Structure("first", "field1", "sample string", "field2", 12); |
| 115 | CheckGetField<std::string>("sample string", "field1"); |
| 116 | CheckGetField<Glib::ustring>("sample string", "field1"); |
| 117 | CheckGetField(12, "field2"); |
| 118 | } |
| 119 | |
| 120 | struct StructureTestStruct |
| 121 | { |
| 122 | int x = 0; |
| 123 | StructureTestStruct(int x) : x(x) {} |
| 124 | StructureTestStruct() {} |
| 125 | }; |
| 126 | |
| 127 | TEST_F(StructureTest, CreateStructureWithCustomFields) |
| 128 | { |
| 129 | structure = Structure("first", "field1", StructureTestStruct(14), "field2", StructureTestStruct(85)); |
| 130 | StructureTestStruct d1; |
| 131 | Glib::Value<StructureTestStruct> d2; |
| 132 | structure.get_field("field1", d1); |
| 133 | structure.get_field("field2", d2); |
| 134 | ASSERT_EQ(14, d1.x); |
| 135 | ASSERT_EQ(85, d2.get().x); |
| 136 | } |
| 137 | |
| 138 | TEST_F(StructureTest, UseStructureWithGlibValue) |
| 139 | { |
| 140 | structure = Structure("dummy"); |
| 141 | Glib::Value<int> value; |
| 142 | value.init(Glib::Value<int>::value_type()); |
| 143 | value.set(123); |
| 144 | structure.set_field("field1", value); |
| 145 | |
| 146 | Glib::Value<int> read_value; |
| 147 | structure.get_field("field1", read_value); |
| 148 | ASSERT_EQ(123, read_value.get()); |
| 149 | } |