--- /dev/null
+/*
+ * 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);
+}