Merge pull request #26362 from ksooo/estuary-rework-pvr-info-dialog
[xbmc.git] / xbmc / utils / test / TestComponentContainer.cpp
blobd7246be7c5125970270350a620b34fbb90177ccf
1 /*
2 * Copyright (C) 2015-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "utils/ComponentContainer.h"
11 #include <utility>
13 #include <gtest/gtest.h>
15 class BaseTestType
17 public:
18 virtual ~BaseTestType() = default;
21 struct DerivedType1 : public BaseTestType
23 int a = 1;
26 struct DerivedType2 : public BaseTestType
28 int a = 2;
31 struct DerivedType3 : public BaseTestType
33 int a = 3;
36 class TestContainer : public CComponentContainer<BaseTestType>
38 FRIEND_TEST(TestComponentContainer, Generic);
41 TEST(TestComponentContainer, Generic)
43 TestContainer container;
45 // check that we can register types
46 container.RegisterComponent(std::make_shared<DerivedType1>());
47 EXPECT_EQ(container.size(), 1u);
48 container.RegisterComponent(std::make_shared<DerivedType2>());
49 EXPECT_EQ(container.size(), 2u);
51 // check that trying to register a component twice does nothing
52 container.RegisterComponent(std::make_shared<DerivedType2>());
53 EXPECT_EQ(container.size(), 2u);
55 // check that first component is valid
56 const auto t1 = container.GetComponent<DerivedType1>();
57 EXPECT_TRUE(t1 != nullptr);
58 EXPECT_EQ(t1->a, 1);
60 // check that second component is valid
61 const auto t2 = container.GetComponent<DerivedType2>();
62 EXPECT_TRUE(t2 != nullptr);
63 EXPECT_EQ(t2->a, 2);
65 // check that third component is not there
66 EXPECT_THROW(container.GetComponent<DerivedType3>(), std::logic_error);
68 // check that component instance is constant
69 const auto t4 = container.GetComponent<DerivedType1>();
70 EXPECT_EQ(t1.get(), t4.get());
72 // check we can call the const overload for GetComponent
73 // and that the returned type is const
74 const auto t5 = const_cast<const TestContainer&>(container).GetComponent<DerivedType1>();
75 EXPECT_TRUE(std::is_const_v<typename decltype(t5)::element_type>);