]> git.stg.codes - stg.git/blob - tests/test_reconnect_on_tariff_change.cpp
a5f1ce7387d0e96b6043b156eace2c4fbcbd7997
[stg.git] / tests / test_reconnect_on_tariff_change.cpp
1 #include "tut/tut.hpp"
2
3 #include "stg/user_property.h"
4 #include "user_impl.h"
5
6 #include "testsettings.h"
7 #include "testtariffs.h"
8 #include "testadmin.h"
9 #include "teststore.h"
10
11 class AFTER_CONNECTED_NOTIFIER : public PROPERTY_NOTIFIER_BASE<bool>,
12                                  private NONCOPYABLE {
13 public:
14     AFTER_CONNECTED_NOTIFIER()
15         : connects(0),
16           disconnects(0)
17     {}
18     void Notify(const bool & oldValue, const bool & newValue);
19
20     size_t GetConnects() const { return connects; }
21     size_t GetDisconnects() const { return disconnects; }
22
23 private:
24     size_t connects;
25     size_t disconnects;
26 };
27
28 namespace tut
29 {
30     struct reconnect_on_tariff_change_data {
31     };
32
33     typedef test_group<reconnect_on_tariff_change_data> tg;
34     tg reconnect_on_tariff_change_test_group("Reconnect on tariff change tests group");
35
36     typedef tg::object testobject;
37
38     class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
39         public:
40             TEST_SETTINGS_LOCAL(bool _reconnectOnTariffChange)
41                 : reconnectOnTariffChange(_reconnectOnTariffChange)
42             {}
43
44             bool GetReconnectOnTariffChange() const { return reconnectOnTariffChange; }
45
46         private:
47             bool reconnectOnTariffChange;
48     };
49
50     template<>
51     template<>
52     void testobject::test<1>()
53     {
54         set_test_name("Check normal behaviour");
55
56         TEST_SETTINGS_LOCAL settings(false);
57         TEST_TARIFFS tariffs;
58         TEST_ADMIN admin;
59         TEST_STORE store;
60         USER_IMPL user(&settings, &store, &tariffs, &admin, NULL);
61
62         AFTER_CONNECTED_NOTIFIER connectionNotifier;
63
64         user.AddConnectedAfterNotifier(&connectionNotifier);
65
66         USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
67
68         ensure_equals("connects = 0", connectionNotifier.GetConnects(), 0);
69         ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), 0);
70
71         ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperty().tariffName.ConstData(), NO_TARIFF_NAME);
72         tariffName = "test";
73         ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
74     }
75 }
76
77 void AFTER_CONNECTED_NOTIFIER::Notify(const bool & oldValue, const bool & newValue)
78 {
79     if (!oldValue && newValue)
80         ++connects;
81     if (oldValue && !newValue)
82         ++disconnects;
83 }