gtest for modemanager
authorHyunjee Kim <hj0426.kim@samsung.com>
Fri, 3 May 2019 04:56:13 +0000 (13:56 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Wed, 18 Mar 2020 08:53:41 +0000 (17:53 +0900)
13 files changed:
CMakeLists.txt
packaging/modes.spec
supervisor/CMakeLists.txt
supervisor/ClientConnection.h
supervisor/ModeManager.h
supervisor/PluginManager.h
unittest/CMakeLists.txt [new file with mode: 0644]
unittest/modes_test_client.cpp [new file with mode: 0644]
unittest/modes_test_main.cpp [new file with mode: 0644]
unittest/modes_test_modemgr.cpp [new file with mode: 0644]
unittest/modes_test_parser.cpp [new file with mode: 0644]
unittest/modes_test_plugin.cpp [new file with mode: 0644]
unittest/tizen_mode.xsd [new file with mode: 0644]

index 6ea7991..fa09d6a 100644 (file)
@@ -33,3 +33,4 @@ ADD_SUBDIRECTORY(client)
 ADD_SUBDIRECTORY(supervisor)
 ADD_SUBDIRECTORY(plugin)
 ADD_SUBDIRECTORY(test)
+ADD_SUBDIRECTORY(unittest)
index 9a4270d..9e31f4e 100644 (file)
@@ -15,6 +15,7 @@ BuildRequires: pkgconfig(dlog)
 BuildRequires: pkgconfig(capi-base-common)
 BuildRequires: pkgconfig(libxml-2.0)
 BuildRequires: pkgconfig(vconf)
+BuildRequires: pkgconfig(gmock)
 
 %description
 The modes(Mode Supervisor) manages configurations which is described at a mode.
@@ -91,6 +92,11 @@ pushd ./example
 xmllint --noout --schema tizen_mode_actions.xsd tizen_mode_actions.xml
 xmllint --noout --schema tizen_mode.xsd tizen_*_mode.xml
 popd
+pushd ./unittest
+./gtest-modes-modemgr
+./gtest-modes-plugin
+./gtest-modes-parser
+popd
 
 %post -p /sbin/ldconfig
 
@@ -126,3 +132,4 @@ popd
 %license LICENSE.APLv2
 %{_datadir}/%{name}/*.xml
 %{_datadir}/%{name}/*.xsd
+%{_bindir}/gtest-modes-*
index 356d5ec..5921df5 100644 (file)
@@ -1,5 +1,3 @@
-INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/common)
-
 FILE(GLOB DAEMON_SRCS *.cpp ${CMAKE_SOURCE_DIR}/common/*.c)
 SET(DAEMON_SRCS ${DAEMON_SRCS} ${CMAKE_SOURCE_DIR}/common/dbus.c)
 
index 9be5b1e..ab838c8 100644 (file)
@@ -16,7 +16,7 @@
 #pragma once
 
 #include <string>
-#include "dbus.h"
+#include "common/dbus.h"
 #include "mdss.h"
 
 MODES_NAMESPACE_BEGIN
index 88ed7cb..913bf80 100644 (file)
@@ -34,6 +34,10 @@ private:
        void addModeName(ModeParser *parser, std::string &path);
        ModeParser* getParser(const std::string &path);
        PluginManager pluginManager;
+
+#ifdef MDS_TEST
+       friend class TestModeManager;
+#endif
 };
 
 MODES_NAMESPACE_END
index e156940..b612757 100644 (file)
@@ -38,6 +38,10 @@ private:
        PluginPair loadClass(std::string libraryName);
        int unloadClass(PluginPair object);
 
+#ifdef MDS_TEST
+       friend class TestPluginManager;
+#endif
+
 public:
        PluginManager() {};
        ~PluginManager() {};
diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt
new file mode 100644 (file)
index 0000000..97f604f
--- /dev/null
@@ -0,0 +1,78 @@
+LINK_DIRECTORIES(${CMAKE_BINARY_DIR})
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE")
+
+SET_SOURCE_FILES_PROPERTIES(${CMAKE_SOURCE_DIR}/common/dbus.c
+    PROPERTIES GENERATED TRUE)
+
+pkg_check_modules(gtest_pkgs REQUIRED dlog glib-2.0 gio-2.0 gio-unix-2.0 libxml-2.0 gmock)
+
+INCLUDE_DIRECTORIES(${gtest_pkgs_INCLUDE_DIRS})
+LINK_DIRECTORIES(${gtest_pkgs_LIBRARY_DIRS})
+ADD_DEFINITIONS("-DMDS_TEST")
+
+SET(SUPERVISOR_DIR "${CMAKE_SOURCE_DIR}/supervisor/" )
+FILE(GLOB SRC "modes_test_main.cpp")
+FILE(GLOB GTEST_EXAMPLE
+               ${CMAKE_SOURCE_DIR}/example/tizen_A_mode.xml
+               ${CMAKE_SOURCE_DIR}/example/tizen_mode.xsd
+       )
+INSTALL(FILES ${GTEST_EXAMPLE} DESTINATION ${MODE_DESCRIPTION_DIR})
+#=======================================================================================#
+SET(GTEST_MODEMGR "gtest-modes-modemgr")
+FILE(GLOB GTEST_MODEMGR_SRCS
+               ${SUPERVISOR_DIR}/ModeManager.cpp
+               ${SUPERVISOR_DIR}/ModeXMLParser.cpp
+               ${SUPERVISOR_DIR}/Action.cpp
+               ${SUPERVISOR_DIR}/Mode.cpp
+               ${SUPERVISOR_DIR}/ModesEx.cpp
+               ${SUPERVISOR_DIR}/PluginManager.cpp
+               "modes_test_modemgr.cpp"
+       )
+ADD_EXECUTABLE(${GTEST_MODEMGR} ${SRC} ${GTEST_MODEMGR_SRCS})
+TARGET_LINK_LIBRARIES(${GTEST_MODEMGR} ${gtest_pkgs_LIBRARIES} dl)
+INSTALL(TARGETS ${GTEST_MODEMGR} DESTINATION ${BIN_INSTALL_DIR})
+#=======================================================================================#
+SET(GTEST_PLUGIN "gtest-modes-plugin")
+FILE(GLOB GTEST_PLUGIN_SRCS
+               ${SUPERVISOR_DIR}/PluginManager.cpp
+               "modes_test_plugin.cpp"
+       )
+ADD_EXECUTABLE(${GTEST_PLUGIN} ${SRC} ${GTEST_PLUGIN_SRCS})
+TARGET_LINK_LIBRARIES(${GTEST_PLUGIN} ${gtest_pkgs_LIBRARIES} dl)
+INSTALL(TARGETS ${GTEST_PLUGIN} DESTINATION ${BIN_INSTALL_DIR})
+#=======================================================================================#
+SET(GTEST_PARSER "gtest-modes-parser")
+FILE(GLOB GTEST_PARSER_SRCS
+               ${SUPERVISOR_DIR}/PluginManager.cpp
+               ${SUPERVISOR_DIR}/ModeXMLParser.cpp
+               ${SUPERVISOR_DIR}/ModesEx.cpp
+               ${SUPERVISOR_DIR}/Action.cpp
+               ${SUPERVISOR_DIR}/Mode.cpp
+               "modes_test_parser.cpp"
+       )
+ADD_EXECUTABLE(${GTEST_PARSER} ${SRC} ${GTEST_PARSER_SRCS})
+TARGET_LINK_LIBRARIES(${GTEST_PARSER} ${gtest_pkgs_LIBRARIES} dl)
+INSTALL(TARGETS ${GTEST_PARSER} DESTINATION ${BIN_INSTALL_DIR})
+#=======================================================================================#
+SET(GTEST_CLIENT "gtest-modes-client")
+SET(GTEST_CLIENT_SRCS
+               ${SUPERVISOR_DIR}/ModeManager.cpp
+               ${SUPERVISOR_DIR}/ModeXMLParser.cpp
+               ${SUPERVISOR_DIR}/Action.cpp
+               ${SUPERVISOR_DIR}/Mode.cpp
+               ${SUPERVISOR_DIR}/ModesEx.cpp
+               ${SUPERVISOR_DIR}/PluginManager.cpp
+               ${SUPERVISOR_DIR}/Supervisor.cpp
+               ${SUPERVISOR_DIR}/RequestHandler.cpp
+               ${SUPERVISOR_DIR}/ClientConnection.cpp
+               ${CMAKE_SOURCE_DIR}/client/mdsc.c
+               ${CMAKE_SOURCE_DIR}/common/dbus.c
+               "modes_test_client.cpp"
+       )
+ADD_EXECUTABLE(${GTEST_CLIENT} ${SRC} ${GTEST_CLIENT_SRCS})
+ADD_DEPENDENCIES(${GTEST_CLIENT} GENERATED_DBUS_CODE)
+TARGET_LINK_LIBRARIES(${GTEST_CLIENT} ${gtest_pkgs_LIBRARIES} dl)
+INSTALL(TARGETS ${GTEST_CLIENT} DESTINATION ${BIN_INSTALL_DIR})
+#=======================================================================================#
diff --git a/unittest/modes_test_client.cpp b/unittest/modes_test_client.cpp
new file mode 100644 (file)
index 0000000..4b6560a
--- /dev/null
@@ -0,0 +1,30 @@
+#include <iostream>
+#include <glib.h>
+#include <gtest/gtest.h>
+#include "modes.h"
+#include "supervisor/Supervisor.h"
+
+MODES_NAMESPACE_USE;
+
+void *client_test(void *data)
+{
+       int ret = 0;
+       GMainLoop *main_loop = (GMainLoop *)data;
+       ret = modes_apply_mode("A");
+       EXPECT_EQ(0, ret);
+       g_main_loop_quit(main_loop);
+
+       return 0;
+}
+
+TEST(client, applyMode) {
+       Supervisor supervisor("/usr/share/modes");
+       supervisor.registerHandler();
+
+       GMainLoop *main_loop = g_main_loop_new(NULL, FALSE);
+       GThread *my_thread = g_thread_new(NULL, client_test, main_loop);
+       g_main_loop_run(main_loop);
+
+       g_thread_join(my_thread);
+       g_main_loop_unref(main_loop);
+}
diff --git a/unittest/modes_test_main.cpp b/unittest/modes_test_main.cpp
new file mode 100644 (file)
index 0000000..8d52f71
--- /dev/null
@@ -0,0 +1,6 @@
+#include <gtest/gtest.h>
+
+int main(int argc, char **argv) {
+       testing::InitGoogleTest(&argc, argv);
+       return RUN_ALL_TESTS();
+}
diff --git a/unittest/modes_test_modemgr.cpp b/unittest/modes_test_modemgr.cpp
new file mode 100644 (file)
index 0000000..a643e8d
--- /dev/null
@@ -0,0 +1,51 @@
+#include <iostream>
+#include <gtest/gtest.h>
+#include "supervisor/ModeManager.h"
+
+MODES_NAMESPACE_USE;
+using namespace std;
+
+MODES_NAMESPACE_BEGIN
+class TestModeManager {
+public:
+       string getDir(ModeManager& m);
+       map<string, string> getModeMap(ModeManager& m);
+};
+MODES_NAMESPACE_END
+
+string TestModeManager::getDir(ModeManager& m)
+{
+       return m.directoryOfMode;
+}
+
+map<string, string> TestModeManager::getModeMap(ModeManager& m)
+{
+       return m.modeMap;
+}
+
+TEST(modemanager, getdir) {
+       string dir = "../example";
+
+       ModeManager md_mgr(dir);
+       TestModeManager mdmgrT;
+
+       EXPECT_EQ(dir, mdmgrT.getDir(md_mgr));
+}
+
+TEST(modemanager, getmodemap) {
+       string dir = "../example";
+
+       ModeManager md_mgr(dir);
+       TestModeManager mdmgrT;
+
+       map<string, string> map = mdmgrT.getModeMap(md_mgr);
+       EXPECT_FALSE(map.empty());
+
+       if (!map.empty()) {
+               std::map<string, string>::iterator it;
+               printf("| %-15.15s | %s\n", "Mode Name", "ModeXML file Path");
+
+               for(it = map.begin(); it != map.end(); it++)
+                       printf("| %-15.15s | %s\n", it->first.c_str(), it->second.c_str());
+       }
+}
diff --git a/unittest/modes_test_parser.cpp b/unittest/modes_test_parser.cpp
new file mode 100644 (file)
index 0000000..e3ab865
--- /dev/null
@@ -0,0 +1,68 @@
+#include <iostream>
+#include <gtest/gtest.h>
+#include "supervisor/Mode.h"
+#include "supervisor/ModeXMLParser.h"
+
+MODES_NAMESPACE_USE;
+using namespace std;
+
+MODES_NAMESPACE_BEGIN
+class TestParser {
+       public:
+               void parserTest(const string &fileName);
+               list<Action> getActionList(Mode& m);
+       private:
+};
+MODES_NAMESPACE_END
+
+list<Action> TestParser::getActionList(Mode& m)
+{
+       return m.actionList;
+}
+
+TEST(XMLParser, getModeName) {
+       PluginManager pluginManager;
+       ModeXMLParser modeparser("../example/tizen_A_mode.xml", pluginManager);
+       string modename = modeparser.getModeName();
+
+       EXPECT_EQ("A", modeparser.getModeName());
+}
+
+TEST(XMLParser, getMode) {
+       PluginManager pluginManager;
+       ModeXMLParser modeparser("../example/tizen_A_mode.xml", pluginManager);
+       Mode mode = modeparser.getMode();
+
+       //EXPECT_TRUE(mode.empty());
+}
+
+TEST(Mode, getModeName) {
+       PluginManager pluginManager;
+       ModeXMLParser modeparser("../example/tizen_A_mode.xml", pluginManager);
+       Mode mode = modeparser.getMode();
+       EXPECT_EQ("A", mode.getName());
+}
+
+TEST(Mode, getTransparent) {
+       PluginManager pluginManager;
+       ModeXMLParser modeparser("../example/tizen_A_mode.xml", pluginManager);
+       Mode mode = modeparser.getMode();
+       EXPECT_EQ("No", mode.getTransparent());
+}
+
+TEST(Mode, printAction) {
+       PluginManager pluginManager;
+       ModeXMLParser modeparser("../example/tizen_A_mode.xml", pluginManager);
+       Mode mode = modeparser.getMode();
+       TestParser parserT;
+
+       list<Action> actList = parserT.getActionList(mode);
+       EXPECT_FALSE(actList.empty());
+
+       for (list<Action>::iterator it = actList.begin(); it != actList.end(); ++it) {
+               cout << "* Action : " << it->getName() << endl;
+               cout << "\t- Achieve : " << it->getAchieve() << endl;
+               cout << "\t- Changeable : " << it->getChangeable() << endl;
+               cout << "\t- Content : " << it->getValue() << endl;
+       }
+}
diff --git a/unittest/modes_test_plugin.cpp b/unittest/modes_test_plugin.cpp
new file mode 100644 (file)
index 0000000..197635b
--- /dev/null
@@ -0,0 +1,105 @@
+#include <iostream>
+#include <gtest/gtest.h>
+#include "supervisor/PluginManager.h"
+
+MODES_NAMESPACE_USE;
+using namespace std;
+
+MODES_NAMESPACE_BEGIN
+class TestPluginManager {
+public:
+       PluginMap getPluginMap(PluginManager& p);
+};
+MODES_NAMESPACE_END
+
+
+PluginMap TestPluginManager::getPluginMap(PluginManager& p){
+       return p.pluginMap;
+}
+
+TEST(pluginmanager, readLibraryList) {
+       PluginManager pluginManager;
+
+       string path = "../plugin/";
+       pluginManager.setPluginDir(path);
+
+       list<string> list = pluginManager.readLibraryList();
+       EXPECT_FALSE(list.empty());
+
+       if (!list.empty()) {
+               for (auto it = list.begin(); it != list.end(); ++it)
+                       printf("library : %s\n", it->c_str());
+       }
+}
+
+TEST(PluginManager, setPluginDir) {
+       PluginManager pluginManager;
+
+       string path = "../plugin/";
+       pluginManager.setPluginDir(path);
+
+       string getPath = pluginManager.getPluginDir();
+
+       EXPECT_EQ(path, getPath);
+}
+
+TEST(PluginManager, loadClassMap) {
+       PluginManager pluginManager;
+       std::string path = "../plugin";
+       pluginManager.setPluginDir(path);
+       pluginManager.readLibraryList();
+
+       int ret = pluginManager.loadClassMap();
+       EXPECT_EQ(ret, MODES_ERROR_NONE);
+}
+
+TEST(PluginManager, unloadClassMap) {
+       PluginManager pluginManager;
+       int ret = pluginManager.unloadClassMap();
+       EXPECT_EQ(ret, MODES_ERROR_NONE);
+
+       TestPluginManager pmgrT;
+       PluginMap pm = pmgrT.getPluginMap(pluginManager);
+       EXPECT_TRUE(pm.empty());
+}
+
+TEST(PluginManager, getClassList) {
+       PluginManager pluginManager;
+       std::string path = "../plugin";
+       pluginManager.setPluginDir(path);
+       pluginManager.readLibraryList();
+
+       pluginManager.loadClassMap();
+
+       list<string> list = pluginManager.getClassList();
+       EXPECT_FALSE(list.empty());
+
+       for (auto it = list.begin(); it != list.end(); ++it) {
+               printf("Class : %s\n", it->c_str());
+       }
+}
+
+TEST(PluginManager, getPlugin) {
+       PluginManager pluginManager;
+       pluginManager.loadClassMap();
+
+       list<string> list = pluginManager.getClassList();
+
+       for (auto it = list.begin(); it != list.end(); ++it) {
+               string key1 = "Happy";
+               string key2 = "1004";
+               string key3 = "true";
+               bool b = (key3 == "true");
+
+               /* Call plugin get */
+               string value = pluginManager.getPlugin(*it)->getString(key1);
+               EXPECT_EQ("Happy", value);
+
+               int valueInt = pluginManager.getPlugin(*it)->getInt(key2);
+               EXPECT_EQ(stoi(key2), valueInt);
+
+               bool valueBool = pluginManager.getPlugin(*it)->getBool(key3);
+               EXPECT_EQ(b, valueBool);
+       }
+}
+
diff --git a/unittest/tizen_mode.xsd b/unittest/tizen_mode.xsd
new file mode 100644 (file)
index 0000000..de90e07
--- /dev/null
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:o="http://www.tizen.org" targetNamespace="http://www.tizen.org">
+       <xs:element name="tizen">
+               <xs:complexType>
+                       <xs:sequence>
+                               <xs:element maxOccurs="1" minOccurs="1" ref="o:mode"/>
+                       </xs:sequence>
+                       <xs:attribute name="version" use="required" type="o:versionT"/>
+               </xs:complexType>
+       </xs:element>
+       <xs:element name="mode">
+               <xs:complexType>
+                       <xs:sequence>
+                               <xs:element maxOccurs="unbounded" ref="o:action"/>
+                       </xs:sequence>
+                       <xs:attribute name="name" use="required" type="xs:string"/>
+                       <xs:attribute name="Transparent" use="required" type="o:yesnoT"/>
+               </xs:complexType>
+       </xs:element>
+       <xs:element name="action">
+               <xs:complexType>
+                       <xs:simpleContent>
+                               <xs:extension base="xs:string">
+                                       <xs:attribute name="name" use="required" type="xs:string"/>
+                                       <xs:attribute name="Achieve" use="optional" type="xs:string"/>
+                                       <xs:attribute name="Changeable" use="required" type="xs:string"/>
+                               </xs:extension>
+                       </xs:simpleContent>
+               </xs:complexType>
+       </xs:element>
+       <xs:simpleType name="versionT">
+               <xs:restriction base="xs:string">
+                       <xs:enumeration value="5.5"/>
+                       <xs:enumeration value="6.0"/>
+               </xs:restriction>
+       </xs:simpleType>
+       <xs:simpleType name="yesnoT">
+               <xs:restriction base="xs:string">
+                       <xs:enumeration value="Yes"/>
+                       <xs:enumeration value="No"/>
+               </xs:restriction>
+       </xs:simpleType>
+</xs:schema>