1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2010,2012 Licq Developers <licq-dev@googlegroups.com>
4  *
5  * Licq is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Licq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "../logdistributor.h"
21 #include "mocklogsink.h"
22 
23 #include <gtest/gtest.h>
24 
25 using ::testing::_;
26 using ::testing::Return;
27 using ::testing::StrictMock;
28 
29 using Licq::Log;
30 using Licq::LogSink;
31 using namespace LicqDaemon;
32 
33 namespace LicqTest {
34 
35 class LogDistributorFixture : public ::testing::Test
36 {
37 public:
38   StrictMock<MockLogSink> myMockSink1;
39   StrictMock<MockLogSink> myMockSink2;
40 
41   LogSink::Ptr mySink1;
42   LogSink::Ptr mySink2;
43 
44   LogDistributor distributor;
45 
LogDistributorFixture()46   LogDistributorFixture() :
47     mySink1(&myMockSink1, &nullDeleter),
48     mySink2(&myMockSink2, &nullDeleter)
49   {
50     // Empty
51   }
52 
nullDeleter(void *)53   static void nullDeleter(void*)
54   {
55     // Empty
56   }
57 };
58 
TEST(LogDistributor,shouldNotLogWhenEmpty)59 TEST(LogDistributor, shouldNotLogWhenEmpty)
60 {
61   LogDistributor distributor;
62   EXPECT_FALSE(distributor.isLogging(Log::Unknown));
63   EXPECT_FALSE(distributor.isLogging(Log::Info));
64   EXPECT_FALSE(distributor.isLogging(Log::Warning));
65   EXPECT_FALSE(distributor.isLogging(Log::Error));
66   EXPECT_FALSE(distributor.isLogging(Log::Debug));
67   EXPECT_FALSE(distributor.isLoggingPackets());
68 }
69 
TEST_F(LogDistributorFixture,registerSinkWorks)70 TEST_F(LogDistributorFixture, registerSinkWorks)
71 {
72   EXPECT_CALL(myMockSink1, isLogging(_))
73       .WillOnce(Return(false));
74 
75   distributor.registerSink(mySink1);
76 
77   LogSink::Message::Ptr msg(new LogSink::Message());
78   distributor.log(msg);
79 }
80 
TEST_F(LogDistributorFixture,sameSinkIsOnlyRegisteredOnce)81 TEST_F(LogDistributorFixture, sameSinkIsOnlyRegisteredOnce)
82 {
83   EXPECT_CALL(myMockSink1, isLogging(_))
84       .WillOnce(Return(false));
85 
86   distributor.registerSink(mySink1);
87   distributor.registerSink(mySink1);
88 
89   LogSink::Message::Ptr msg(new LogSink::Message());
90   distributor.log(msg);
91 }
92 
TEST_F(LogDistributorFixture,unregisterSinkWorks)93 TEST_F(LogDistributorFixture, unregisterSinkWorks)
94 {
95   EXPECT_CALL(myMockSink2, isLogging(_))
96       .WillOnce(Return(false));
97 
98   distributor.registerSink(mySink1);
99   distributor.registerSink(mySink2);
100   distributor.unregisterSink(mySink1);
101 
102   LogSink::Message::Ptr msg(new LogSink::Message());
103   distributor.log(msg);
104 }
105 
TEST_F(LogDistributorFixture,logsAreSentToAllActiveSinks)106 TEST_F(LogDistributorFixture, logsAreSentToAllActiveSinks)
107 {
108   EXPECT_CALL(myMockSink1, isLogging(Log::Info))
109       .WillOnce(Return(true));
110   EXPECT_CALL(myMockSink1, isLogging(Log::Debug))
111       .WillOnce(Return(true));
112   EXPECT_CALL(myMockSink1, log(_))
113       .Times(2);
114 
115   EXPECT_CALL(myMockSink2, isLogging(Log::Info))
116       .WillOnce(Return(true));
117   EXPECT_CALL(myMockSink2, isLogging(Log::Debug))
118       .WillOnce(Return(false));
119   EXPECT_CALL(myMockSink2, log(_));
120 
121   distributor.registerSink(mySink1);
122   distributor.registerSink(mySink2);
123 
124   LogSink::Message* msg1 = new LogSink::Message();
125   msg1->level = Log::Info;
126   distributor.log(LogSink::Message::Ptr(msg1));
127   msg1 = NULL;
128 
129   LogSink::Message* msg2 = new LogSink::Message();
130   msg2->level = Log::Debug;
131   distributor.log(LogSink::Message::Ptr(msg2));
132   msg2 = NULL;
133 }
134 
TEST_F(LogDistributorFixture,logPacketWorks)135 TEST_F(LogDistributorFixture, logPacketWorks)
136 {
137   EXPECT_CALL(myMockSink1, isLoggingPackets())
138       .WillRepeatedly(Return(true));
139 
140   EXPECT_CALL(myMockSink2, isLoggingPackets())
141       .WillRepeatedly(Return(false));
142 
143   distributor.registerSink(mySink2);
144   distributor.registerSink(mySink1);
145 
146   EXPECT_TRUE(distributor.isLoggingPackets());
147 }
148 
149 } // namespace LicqTest
150