~aleteoryx/muditaos

ref: 25a5d90f4e12ee5d33f4202170f18b59e16a9564 muditaos/module-services/service-cellular/tests/unittest_connection-manager.cpp -rw-r--r-- 5.0 KiB
25a5d90f — rrandomsky [CP-2156] Fixed no response when editing a contact to have the same number as another 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include <service-cellular/connection-manager/ConnectionManager.hpp>
#include <service-cellular/connection-manager/ConnectionManagerCellularCommandsInterface.hpp>

#include <optional>

using namespace testing;

class MockCellular : public ConnectionManagerCellularCommandsInterface
{
  public:
    MOCK_METHOD(bool, disconnectFromNetwork, (), (override));
    MOCK_METHOD(bool, connectToNetwork, (), (override));
    MOCK_METHOD(std::optional<bool>, isConnectedToNetwork, (), (override));
    MOCK_METHOD(bool, clearNetworkIndicator, (), (override));
    MOCK_METHOD(void, hangUpOngoingCall, (), (override));
    MOCK_METHOD(bool, isConnectionTimerActive, (), (override));
    MOCK_METHOD(void, startConnectionTimer, (), (override));
    MOCK_METHOD(void, stopConnectionTimer, (), (override));
    MOCK_METHOD(void, holdMinimumCpuFrequency, (), (override));
    MOCK_METHOD(void, retryPhoneModeChange, (), (override));
};

TEST(ConnectionManager, onPhoneModeChange)
{
    std::shared_ptr<MockCellular> mock = std::make_shared<MockCellular>();
    ConnectionManager tested(true, std::chrono::minutes{5}, mock);

    // Connected / Do not disturb to Offline
    EXPECT_CALL(*mock, isConnectedToNetwork()).Times(1).WillOnce(Return(true));
    EXPECT_CALL(*mock, hangUpOngoingCall()).Times(1).WillOnce(Return());
    EXPECT_CALL(*mock, disconnectFromNetwork()).Times(1).WillOnce(Return(true));
    EXPECT_CALL(*mock, clearNetworkIndicator()).Times(1).WillOnce(Return(true));
    EXPECT_CALL(*mock, isConnectionTimerActive()).Times(1).WillOnce(Return(true));
    EXPECT_CALL(*mock, stopConnectionTimer()).Times(1);

    EXPECT_CALL(*mock, startConnectionTimer()).Times(0);
    EXPECT_CALL(*mock, connectToNetwork()).Times(0);
    tested.onPhoneModeChange(sys::phone_modes::PhoneMode::Offline);

    // Connected / Do not disturb to Messages only
    EXPECT_CALL(*mock, isConnectedToNetwork()).Times(1).WillOnce(Return(true));
    EXPECT_CALL(*mock, hangUpOngoingCall()).Times(1).WillOnce(Return());
    EXPECT_CALL(*mock, disconnectFromNetwork()).Times(1).WillOnce(Return(true));
    EXPECT_CALL(*mock, clearNetworkIndicator()).Times(1).WillOnce(Return(true));
    EXPECT_CALL(*mock, isConnectionTimerActive()).Times(1).WillOnce(Return(false));
    EXPECT_CALL(*mock, startConnectionTimer()).Times(1);

    EXPECT_CALL(*mock, stopConnectionTimer()).Times(0);
    EXPECT_CALL(*mock, connectToNetwork()).Times(0);
    tested.setFlightMode(false);
    tested.onPhoneModeChange(sys::phone_modes::PhoneMode::Offline);

    // Offline to Connected / Do not disturb
    EXPECT_CALL(*mock, isConnectionTimerActive()).Times(1).WillOnce(Return(false));
    EXPECT_CALL(*mock, isConnectedToNetwork()).Times(1).WillOnce(Return(false));
    EXPECT_CALL(*mock, connectToNetwork()).Times(1).WillOnce(Return(true));

    EXPECT_CALL(*mock, disconnectFromNetwork()).Times(0);
    tested.setFlightMode(true);
    tested.onPhoneModeChange(sys::phone_modes::PhoneMode::Connected);

    // Messages only to Connected / Do not disturb
    EXPECT_CALL(*mock, isConnectionTimerActive()).Times(1).WillOnce(Return(true));
    EXPECT_CALL(*mock, stopConnectionTimer()).Times(1);
    EXPECT_CALL(*mock, isConnectedToNetwork()).Times(1).WillOnce(Return(false));
    EXPECT_CALL(*mock, connectToNetwork()).Times(1).WillOnce(Return(true));

    EXPECT_CALL(*mock, disconnectFromNetwork()).Times(0);
    tested.onPhoneModeChange(sys::phone_modes::PhoneMode::Connected);
}

TEST(ConnectionManager, onTimerTick_Messages_only)
{
    std::shared_ptr<MockCellular> mock = std::make_shared<MockCellular>();
    ConnectionManager tested(false, std::chrono::minutes{15}, mock);

    // Messages only, interval 15 min
    EXPECT_CALL(*mock, isConnectedToNetwork()).Times(1).WillOnce(Return(false));
    EXPECT_CALL(*mock, isConnectionTimerActive()).Times(1).WillOnce(Return(false));
    EXPECT_CALL(*mock, startConnectionTimer()).Times(1);
    tested.onPhoneModeChange(sys::phone_modes::PhoneMode::Offline);

    EXPECT_CALL(*mock, connectToNetwork()).Times(1).WillOnce(Return(true));
    for (auto i = 0; i < 15; i++) {
        tested.onTimerTick();
    }

    EXPECT_CALL(*mock, disconnectFromNetwork()).Times(1).WillOnce(Return(true));
    for (auto i = 0; i < 5; i++) {
        tested.onTimerTick();
    }
}

TEST(ConnectionManager, onTimerTick_Offline)
{
    std::shared_ptr<MockCellular> mock = std::make_shared<MockCellular>();
    ConnectionManager tested(true, std::chrono::minutes{15}, mock);

    // Offline, timer was active
    EXPECT_CALL(*mock, isConnectedToNetwork()).Times(1).WillOnce(Return(false));
    EXPECT_CALL(*mock, isConnectionTimerActive()).Times(1).WillOnce(Return(true));
    EXPECT_CALL(*mock, stopConnectionTimer()).Times(1);

    tested.onPhoneModeChange(sys::phone_modes::PhoneMode::Offline);

    EXPECT_CALL(*mock, connectToNetwork()).Times(0);
    EXPECT_CALL(*mock, disconnectFromNetwork()).Times(0);
    for (auto i = 0; i < 20; i++) {
        tested.onTimerTick();
    }
}