add unittest for managing policy accepted/tizen/unified/20200422.032227 submit/tizen/20200421.092050
authorYoungjae Shin <yj99.shin@samsung.com>
Tue, 21 Apr 2020 09:19:08 +0000 (18:19 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Tue, 21 Apr 2020 09:20:11 +0000 (18:20 +0900)
It includes fixing bug of policy

plugin/TestPluginAction.cpp
plugin/TestPluginAction.h
supervisor/Action.cpp
supervisor/Action.h
supervisor/ModeXMLParser.h
supervisor/PluginMapper.cpp
supervisor/RuleManager.h
unittest/CMakeLists.txt
unittest/modes-gtest-run.sh
unittest/modes_test_policy.cpp [new file with mode: 0644]

index a7296cd81c6df6747fa76708ecdbce6dbcca9417..8cc008e8d0e8623e0eedda81155402413b2b05fa 100644 (file)
 
 MODES_NAMESPACE_USE;
 
+int TestPluginAction::printIntVal = 0;
+
 TestPluginAction::TestPluginAction(const std::string &actionKey)
        : PluginAction(actionKey), cb(nullptr), cbData(nullptr)
 {
 }
 
+bool TestPluginAction::IsCurrentValue(int val)
+{
+       if (val == printIntVal)
+               return true;
+       return false;
+}
+
 bool TestPluginAction::IsCurrentValue(const std::string &val)
 {
        return true;
@@ -43,6 +52,7 @@ int TestPluginAction::set(int val)
                DBG("set(%s, %d)", key.c_str(), val);
        } else if ("printInt" == key) {
                DBG("set(%s, %d)", key.c_str(), val);
+               printIntVal = val;
        } else if ("sleep" == key) {
                std::this_thread::sleep_for(std::chrono::seconds(val));
                DBG("set(%s, %d)", key.c_str(), val);
index c62cafbec40c7651b065f4d837b88ececd1c7ff6..c44e45cf1d9d2aaa988ae6fef2b6dd2e87a2504f 100644 (file)
@@ -27,6 +27,7 @@ public:
        TestPluginAction(const std::string &actionKey);
        ~TestPluginAction() override = default;
 
+       bool IsCurrentValue(int val) override;
        bool IsCurrentValue(const std::string &val) override;
 
        int set(int val) override;
@@ -42,6 +43,7 @@ public:
 private:
        static gboolean changeTimeout(gpointer data);
 
+       static int printIntVal;
        valueChangedCB cb;
        void *cbData;
 };
index d0192ba13bbaad09cf17e04db56604a0cf54fd9a..28f9d0779134166c17f93155faa773e435edf319 100644 (file)
@@ -153,8 +153,10 @@ void Action::subscribeChanges()
                WARN("piAction(%s) setChangedCallback() Fail(%d)", piAction->getKey().c_str(), ret);
        } else {
                auto result = subscribedActions.insert(std::pair<std::string, Action*>(ruleName, this));
-               if (false == result.second)
+               if (false == result.second) {
                        WARN("Action(%s), already exists", ruleName.c_str());
+                       return;
+               }
                subscribed = true;
        }
 }
@@ -171,7 +173,9 @@ void Action::unsubscribeChanges()
 
 void Action::invokeChangedCB()
 {
-       valueChangedCallback(subscribedActions[ruleName]);
+       auto found = subscribedActions.find(ruleName);
+       if (subscribedActions.end() != found)
+               valueChangedCallback(found->second);
 }
 
 void Action::valueChangedCallback(void *userData)
index c6369148395a05c632341dfbe392c68995689879..20242a8f0998d146c76d42fa54f4989eb8952756 100644 (file)
@@ -92,6 +92,9 @@ private:
        ActionRestrict restriction;
        std::list<ActionObserver*> observers;
        static std::map<std::string, Action*> subscribedActions;
+#ifdef MDS_TEST
+       friend class PolicyTest;
+#endif
 };
 
 MODES_NAMESPACE_END
index d04c45cc51a320954ecd292857757f4758f33d7e..1313710f9fc1c65cdfc0e4a0c488c729c2c8fba4 100644 (file)
@@ -43,7 +43,7 @@ private:
        void parseActionAttr(xmlNodePtr node, Action *action);
        void parseAction(xmlNodePtr node);
        void parseUndo(xmlNodePtr node);
-       Action *parseActionInfo(xmlNodePtr node);
+       ActionparseActionInfo(xmlNodePtr node);
        void parseModeAttrOnly();
        void validateAction(xmlNodePtr node);
        void validateElement(xmlNodePtr node);
index 0e4001bb8338d32f9e1e89c4a9ea0141964f05e4..993c831db24d2c4a031987b48711181dd0664847 100644 (file)
@@ -142,7 +142,7 @@ std::list<std::string> PluginMapper::getPluginList()
        return list;
 }
 
-Plugin *PluginMapper::getPlugin(std::string name)
+PluginPluginMapper::getPlugin(std::string name)
 {
        return pluginMap[name].second;
 }
index b95040d56bffcc3e99674ae6622f160b1611f8a2..dc7026c10e4d17dcd5bcad588768b643578caf3a 100644 (file)
@@ -53,7 +53,7 @@ private:
        void freeRuleMap();
        ActionRule* makeRule(xmlNodePtr node);
        void parseRule(xmlNodePtr node);
-       void addBoolAlias(ActionRule * actionRule);
+       void addBoolAlias(ActionRule *actionRule);
        void parseActionRule(const std::string &xmlFile);
        void parseElement(xmlNodePtr node, ActionRule *actionRule);
 
index 335073b6867d0385c8addcd36ffb3cc7ea62e441..5151dc01b68d15bd1f354d9cacadad5887ad2f66 100644 (file)
@@ -159,3 +159,16 @@ ADD_EXECUTABLE(${GTEST_RULEMGR} ${SRC} ${GTEST_RULE_SRCS})
 ADD_DEPENDENCIES(${GTEST_RULEMGR} GENERATED_DBUS_CODE)
 TARGET_LINK_LIBRARIES(${GTEST_RULEMGR} ${gtest_pkgs_LIBRARIES} dl)
 INSTALL(TARGETS ${GTEST_RULEMGR} DESTINATION ${TEST_INSTALL_DIR})
+#=======================================================================================#
+SET(GTEST_POLICY "modes-gtest-policy")
+FILE(GLOB GTEST_POLICY_SRCS
+               ${SUPERVISOR_DIR}/ModesEx.cpp
+               ${SUPERVISOR_DIR}/Action.cpp
+               ${SUPERVISOR_DIR}/ValueChecker.cpp
+               ${CMAKE_SOURCE_DIR}/plugin/*.cpp
+               modes_test_policy.cpp
+       )
+ADD_EXECUTABLE(${GTEST_POLICY} ${SRC} ${GTEST_POLICY_SRCS})
+ADD_DEPENDENCIES(${GTEST_POLICY} GENERATED_DBUS_CODE)
+TARGET_LINK_LIBRARIES(${GTEST_POLICY} ${gtest_pkgs_LIBRARIES})
+INSTALL(TARGETS ${GTEST_POLICY} DESTINATION ${TEST_INSTALL_DIR})
index e85baba92371333fbdb0f1dba2bc1ddf53accec4..540d09ac1c5a05f96537f00cd97b575656004e43 100755 (executable)
@@ -41,6 +41,7 @@ fi
 ./modes-gtest-generator
 ./modes-gtest-conflict
 ./modes-gtest-plugin
+./modes-gtest-policy
 
 rm -rf *.xsd extra
 
diff --git a/unittest/modes_test_policy.cpp b/unittest/modes_test_policy.cpp
new file mode 100644 (file)
index 0000000..d69e5e5
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <functional>
+#include <gtest/gtest.h>
+#include "Plugin.h"
+#include "supervisor/ModesEx.h"
+#include "supervisor/TAction.h"
+
+using namespace std;
+MODES_NAMESPACE_USE;
+
+extern "C" Plugin* objectCreate(void);
+extern "C" void objectDelete(Plugin *plugin);
+
+MODES_NAMESPACE_BEGIN
+class PolicyTest : public ::testing::Test {
+protected:
+       void SetUp() override
+       {
+               plugin = objectCreate();
+       }
+
+       void TearDown() override
+       {
+               objectDelete(plugin);
+       }
+
+       bool isSubscribed(Action *action)
+       {
+               return action->subscribed;
+       }
+
+       Plugin *plugin;
+};
+MODES_NAMESPACE_END
+
+TEST_F(PolicyTest, OnlyOneSubscribe)
+{
+       int ret;
+       string ruleName = "test.printInt";
+
+       int pos = ruleName.find_first_of(".");
+       std::string actionKey = ruleName.substr(pos + 1);
+       std::shared_ptr<PluginAction> piAction1(plugin->newAction(actionKey),
+               std::bind(&Plugin::deleteAction, plugin, std::placeholders::_1));
+
+       Action *action1 = nullptr;
+       ASSERT_NO_THROW(action1 = new TAction<int>(ruleName, piAction1));
+
+       ret = action1->setValue("4");
+       EXPECT_EQ(ret, MODES_ERROR_NONE);
+       ret = action1->apply();
+       EXPECT_EQ(ret, MODES_ERROR_NONE);
+       EXPECT_EQ(isSubscribed(action1), true);
+
+       std::shared_ptr<PluginAction> piAction2(plugin->newAction(actionKey),
+               std::bind(&Plugin::deleteAction, plugin, std::placeholders::_1));
+       Action *action2 = nullptr;
+       ASSERT_NO_THROW(action2 = new TAction<int>(ruleName, piAction2));
+
+       ret = action2->setValue("2");
+       EXPECT_EQ(ret, MODES_ERROR_NONE);
+       ret = action2->apply();
+       EXPECT_EQ(ret, MODES_ERROR_NONE);
+       EXPECT_EQ(isSubscribed(action2), true);
+       EXPECT_EQ(isSubscribed(action1), false);
+}
+
+TEST_F(PolicyTest, ignoreSameValue)
+{
+       int ret;
+       string ruleName = "test.printInt";
+
+       int pos = ruleName.find_first_of(".");
+       std::string actionKey = ruleName.substr(pos + 1);
+       std::shared_ptr<PluginAction> piAction1(plugin->newAction(actionKey),
+               std::bind(&Plugin::deleteAction, plugin, std::placeholders::_1));
+
+       Action *action1 = nullptr;
+       ASSERT_NO_THROW(action1 = new TAction<int>(ruleName, piAction1));
+
+       ret = action1->setValue("4");
+       EXPECT_EQ(ret, MODES_ERROR_NONE);
+       ret = action1->apply();
+       EXPECT_EQ(ret, MODES_ERROR_NONE);
+       EXPECT_EQ(isSubscribed(action1), true);
+
+       std::shared_ptr<PluginAction> piAction2(plugin->newAction(actionKey),
+               std::bind(&Plugin::deleteAction, plugin, std::placeholders::_1));
+       Action *action2 = nullptr;
+       ASSERT_NO_THROW(action2 = new TAction<int>(ruleName, piAction2));
+
+       ret = action2->setValue("4");
+       EXPECT_EQ(ret, MODES_ERROR_NONE);
+       ret = action2->apply();
+       EXPECT_EQ(ret, MODES_ERROR_NONE);
+       EXPECT_EQ(isSubscribed(action2), false);
+       EXPECT_EQ(isSubscribed(action1), true);
+}
+
+TEST_F(PolicyTest, oneShotNoSubscribe)
+{
+       int ret;
+       string ruleName = "test.printInt";
+
+       int pos = ruleName.find_first_of(".");
+       std::string actionKey = ruleName.substr(pos + 1);
+       std::shared_ptr<PluginAction> piAction1(plugin->newAction(actionKey),
+               std::bind(&Plugin::deleteAction, plugin, std::placeholders::_1));
+
+       Action *action1 = nullptr;
+       ASSERT_NO_THROW(action1 = new TAction<int>(ruleName, piAction1));
+
+       ret = action1->setValue("4");
+       EXPECT_EQ(ret, MODES_ERROR_NONE);
+       ret = action1->applyOneShot();
+       EXPECT_EQ(ret, MODES_ERROR_NONE);
+       EXPECT_EQ(isSubscribed(action1), false);
+}