Fix for ConfigurationTests and ActivityProcessingTests suits 14/141414/3
authorLukasz Wlazly <l.wlazly@partner.samsung.com>
Fri, 28 Jul 2017 10:19:11 +0000 (12:19 +0200)
committerLukasz Wlazly <l.wlazly@partner.samsung.com>
Mon, 31 Jul 2017 13:02:37 +0000 (15:02 +0200)
Architecture changes required modifications of way the integration tests are executed.

This patch:
    1. changes default values of properties in SwitchInteractionManager
    2. overrides 'detach' method of Subject in CompositeSwitchProvider
    3. adds some nullptr verifications
    4. introduces invokation of ecore main loop to tests what enables using ecore timers during them

Change-Id: Id2fbbba163117bf9c415639d96223c5f758182a5

src/CompositeSwitchProvider.cpp
src/CompositeSwitchProvider.hpp
src/SQLiteConfiguration.cpp
src/Subject.hpp
src/SwitchInteractionManager.cpp
src/SwitchInteractionManager.hpp
src/SwitchManager.cpp
tests/ActivityProcessingTests.cpp
tests/ActivityProcessingTests.hpp
tests/ConfigurationTests.cpp
tests/ConfigurationTests.hpp

index 44016158b45c4fabfcbdc00553f046ecf18f3b50..41eb48eda440da3ead45774cd2a61499cbc866a1 100644 (file)
@@ -54,6 +54,12 @@ void CompositeSwitchProvider::attach(std::weak_ptr<Observer<Switch>> observer, s
        }
 }
 
+void CompositeSwitchProvider::detach(const Observer<Switch> *observer, const Switch *item)
+{
+       for (auto &p : providers)
+               p->detach(observer, item);
+}
+
 std::shared_ptr<SwitchProvider> CompositeSwitchProvider::getProviderById(const std::string &id) const
 {
        for (auto &provider : providers) {
index 072a78287a54fc4f51af79402258efe8c48e6ad9..e67ff7a4f7554164884b0f0ed4599268a6527a9e 100644 (file)
@@ -18,6 +18,7 @@ public:
        std::shared_ptr<SwitchProvider> getProviderById(const std::string &id) const;
 
        void attach(std::weak_ptr<Observer<Switch>> observer, std::shared_ptr<Condition<Switch>> cond) override;
+       void detach(const Observer<Switch> *observer, const Switch *item) override;
 
 private:
        std::vector<std::shared_ptr<SwitchProvider>> providers;
index 7daa8c42d034d9bd9120cfb43df040f9733e06be..e171b83415bfc055b05866e08fca53c3080f032b 100644 (file)
@@ -106,8 +106,12 @@ std::shared_ptr<SwitchConfigurationItem> SQLiteConfiguration::findBySwitchId(con
        }))
        return nullptr;
 
-       INFO("Found: %s, %s, %s", item->getSwitchId().c_str(), item->getUserName().c_str(), item->getActivityType().c_str());
+       if (!item) {
+               DEBUG("No item found");
+               return nullptr;
+       }
 
+       INFO("Found: %s, %s, %s", item->getSwitchId().c_str(), item->getUserName().c_str(), item->getActivityType().c_str());
        return item;
 }
 
@@ -176,7 +180,10 @@ void SQLiteConfiguration::createTable()
 
 std::shared_ptr<SwitchConfigurationItem> SQLiteConfiguration::constructItem(int argc, char **argv, char **azColName)
 {
-       ASSERT(argc == SwitchConfigurationItemFields::Count, "Fields number does not match the one in SwitchConfigurationItem");
+       if (argc != SwitchConfigurationItemFields::Count) {
+               ERROR("Fields number does not match the one in SwitchConfigurationItem");
+               return nullptr;
+       }
 
        auto item = std::make_shared<SwitchConfigurationItem>();
 
index 6861138ed969a7efa9efd4f75b272788a9bfe4c1..ea5e23bb47fe48507e4863a9271d60fdff2c8888 100644 (file)
@@ -15,7 +15,7 @@ public:
                observers.emplace_back(std::move(observer), std::move(cond));
        }
 
-       void detach(const Observer<T> *observer, const T *item)
+       virtual void detach(const Observer<T> *observer, const T *item)
        {
                for (auto it = observers.begin(); it != observers.end(); ++it) {
                        auto tempObserver = it->first.lock();
index 320d51032e491cc21ed08afe2207377a303a7a64..886edcbc82958dc4b4510383c2b52b2dff66801a 100644 (file)
@@ -29,13 +29,15 @@ void SwitchInteractionManager::startInteraction(const NotifyMethod &method, cons
        currentSwitch = sw;
        notifyMethod = method;
        minimalSwitchInteractionTimer = ecore_timer_add(minimalSwitchInteractionTime, minimalSwitchInteractionTimeCb, this);
+       isActiveInteraction = true;
 
        DEBUG("switch: %s", currentSwitch->getId()->getGlobalId().c_str());
+       DEBUG("minimalSwitchInteractionTimer %s for %lf s", minimalSwitchInteractionTimer ? "set" : "unset", minimalSwitchInteractionTime);
 }
 
 void SwitchInteractionManager::stopInteraction(const std::shared_ptr<Switch> &sw)
 {
-       if (!isActiveInteraction())
+       if (!isActiveInteraction)
                return;
 
        if (!currentSwitch || !sw || currentSwitch->getId()->getGlobalId() != sw->getId()->getGlobalId())
@@ -63,15 +65,15 @@ SwitchInteractionManager::SwitchInteractionManager()
        : minimalSwitchInteractionTime(0.0), switchInteractionInterval(0.0), notificationRepetitionInterval(0.0),
          minimalSwitchInteractionTimer(nullptr), switchInteractionIntervalTimer(nullptr), notificationRepetitionIntervalTimer(nullptr)
 {
-       minimalSwitchInteractionTimeHandler = VConfSingleton::instance().registerAndGet<double>(VCONF_PREFIX "TAP_DURATION_VALUE", 1.0, [this](int x) {
+       minimalSwitchInteractionTimeHandler = VConfSingleton::instance().registerAndGet<double>(VCONF_PREFIX "TAP_DURATION_VALUE", 0.0, [this](int x) {
                minimalSwitchInteractionTime = x;
        });
 
-       switchInteractionIntervalHandler = VConfSingleton::instance().registerAndGet<double>(VCONF_PREFIX "SGL_IACTION_INT_VALUE", 3.0, [this](int x) {
+       switchInteractionIntervalHandler = VConfSingleton::instance().registerAndGet<double>(VCONF_PREFIX "SGL_IACTION_INT_VALUE", 0.0, [this](int x) {
                switchInteractionInterval = x;
        });
 
-       notificationRepetitionIntervalHandler = VConfSingleton::instance().registerAndGet<double>(VCONF_PREFIX "AUTO_MOVE_INT_VALUE", 2.0, [this](int x) {
+       notificationRepetitionIntervalHandler = VConfSingleton::instance().registerAndGet<double>(VCONF_PREFIX "AUTO_MOVE_INT_VALUE", 0.0, [this](int x) {
                notificationRepetitionInterval = x;
        });
 }
@@ -85,16 +87,12 @@ SwitchInteractionManager::~SwitchInteractionManager()
 
 void SwitchInteractionManager::uncoditionallyStopInteraction()
 {
-       deleteAndClearTimer(minimalSwitchInteractionTimer);
-       deleteAndClearTimer(notificationRepetitionIntervalTimer);
+       isActiveInteraction = false;
 
        if (!switchInteractionIntervalTimer) {
                DEBUG("interval timer set");
                switchInteractionIntervalTimer = ecore_timer_add(switchInteractionInterval, switchInteractionIntervalCb, this);
        }
-
-       currentSwitch = nullptr;
-       notifyMethod = nullptr;
 }
 
 void SwitchInteractionManager::deleteAndClearTimer(Ecore_Timer *&timer)
@@ -106,11 +104,6 @@ void SwitchInteractionManager::deleteAndClearTimer(Ecore_Timer *&timer)
        timer = nullptr;
 }
 
-bool SwitchInteractionManager::isActiveInteraction()
-{
-       return minimalSwitchInteractionTimer || notificationRepetitionIntervalTimer;
-}
-
 Eina_Bool SwitchInteractionManager::minimalSwitchInteractionTimeCb(void *data)
 {
        auto im = static_cast<SwitchInteractionManager *>(data);
@@ -137,15 +130,16 @@ Eina_Bool SwitchInteractionManager::notificationRepetitionIntervalCb(void *data)
 
 void SwitchInteractionManager::afterMinimalInteractionTime()
 {
-       DEBUG("invoked: %s", currentSwitch->getId()->getGlobalId().c_str());
+       DEBUG("invoked, switch: %s", currentSwitch->getId()->getGlobalId().c_str());
 
        minimalSwitchInteractionTimer = nullptr;
        notifyMethod();
 
        if (notificationRepetitionInterval != 0.0)
                notificationRepetitionIntervalTimer = ecore_timer_add(notificationRepetitionInterval, notificationRepetitionIntervalCb, this);
-       else
-               ecore_timer_add(switchInteractionInterval, switchInteractionIntervalCb, this);
+
+       currentSwitch = nullptr;
+       notifyMethod = nullptr;
 }
 
 void SwitchInteractionManager::afterInteractionInterval()
index bb2259fccff6a6014de6e0492e277c9173847746..46d6aade7fd53b5064681389733d2e726d6134f3 100644 (file)
@@ -31,7 +31,7 @@ private:
        void uncoditionallyStopInteraction();
 
        void deleteAndClearTimer(Ecore_Timer *&timer);
-       bool isActiveInteraction();
+       bool isActiveInteraction = false;
 
        static Eina_Bool minimalSwitchInteractionTimeCb(void *data);
        static Eina_Bool switchInteractionIntervalCb(void *data);
index a3c2fb221c89bcc68b41ea8ab28435882b1ed61d..8b19edc2c9307d29463cd9089f68f7b6ba1cfcda 100644 (file)
@@ -31,6 +31,8 @@ void SwitchManager::initialize()
 
 void SwitchManager::update(const std::shared_ptr<SwitchConfigurationItem> &item)
 {
+       DEBUG("notified with item: %s", item->getSwitchId().c_str());
+
        switch (item->getChangeType()) {
        case ChangeType::ADD: {
                auto sw = switchProvider->findSwitchById(item->getSwitchId());
@@ -63,6 +65,9 @@ void SwitchManager::update(const std::shared_ptr<Switch> &sw)
        DEBUG("notified with: %s", sw->getId()->getGlobalId().c_str());
 
        auto confItem = configuration->findBySwitchId(sw->getId()->getGlobalId());
+       if (!confItem)
+               return;
+
        auto activityType = confItem->getActivityType();
        auto request = std::make_shared<ActivityChangeRequest>(activityType, nullptr);
        changeActivity(request);
index 419c2dcc20fa9a3ad7772cf0652c1b08708c2d0d..2a4b527099ecc71c5dbabd9cf7c4c60910a0fb5c 100644 (file)
@@ -35,7 +35,8 @@ TEST_F(ActivityProcessingFixture, ActivityReady)
        ASSERT_NE(activity, nullptr);
        EXPECT_EQ(activity->getType(), "TWO_STEP_PROCESS_ACTIVITY");
 
-       simulateKeyDown(keyMappedToTwoStepProcessActivity_);
+       simulateKeyPress(keyMappedToTwoStepProcessActivity_);
+       runMainLoop();
        auto isEmpty = switchManager_->getActivities().empty();
        EXPECT_EQ(isEmpty, true);
 }
@@ -51,19 +52,23 @@ TEST_F(ActivityProcessingFixture, ActivityAbandoned)
        ASSERT_NE(activity, nullptr);
        EXPECT_EQ(activity->getType(), "TWO_STEP_PROCESS_ACTIVITY");
 
-       simulateKeyDown(keyMappedToThreeStepProcessActivity_);
+       simulateKeyPress(keyMappedToThreeStepProcessActivity_);
+       runMainLoop();
        EXPECT_EQ(switchManager_->getActivities().size(), 2u);
        activity = switchManager_->getActivities().top();
        ASSERT_NE(activity, nullptr);
        EXPECT_EQ(activity->getType(), "THREE_STEP_PROCESS_ACTIVITY");
 
-       simulateKeyDown(keyMappedToThreeStepProcessActivity_);
+       simulateKeyPress(keyMappedToThreeStepProcessActivity_);
+       runMainLoop();
        EXPECT_EQ(switchManager_->getActivities().size(), 2u);
        activity = switchManager_->getActivities().top();
+
        ASSERT_NE(activity, nullptr);
        EXPECT_EQ(activity->getType(), "THREE_STEP_PROCESS_ACTIVITY");
 
-       simulateKeyDown(keyMappedToThreeStepProcessActivity_);
+       simulateKeyPress(keyMappedToThreeStepProcessActivity_);
+       runMainLoop();
        EXPECT_EQ(switchManager_->getActivities().size(), 1u);
        activity = switchManager_->getActivities().top();
        ASSERT_NE(activity, nullptr);
index 20dcd47fbb1734bf00f184141af28b352e070a00..6bab73e300217cea2d9e2c8c579ef94e13d8c98c 100644 (file)
@@ -23,7 +23,8 @@ public:
 
        void initialEnvironmentExpect()
        {
-               simulateKeyDown(keyMappedToTwoStepProcessActivity_);
+               simulateKeyPress(keyMappedToTwoStepProcessActivity_);
+               runMainLoop();
                EXPECT_EQ(switchManager_->updatedWithSwitch, 1);
        }
 
index df23e750ef4fed6a2e704d41e6abc54d20a7bea2..a4846e87a92df282bfa8f6a5d5c6decfc454d34e 100644 (file)
@@ -9,10 +9,13 @@ TEST_F(ConfigurationTestsFixture, ConfigurationNotEmpty_BeUpdatedOnEvent)
        configuration_->add(item_);
 
        auto switchManager = SwitchManager::create<SwitchManager_ConfigurationTests>(switchProvider_, configuration_, activityFactory_);
-       simulateKeyDown(key_);
+       simulateKeyPress(key_);
+       runMainLoop();
 
        EXPECT_EQ(switchManager->updatedWithSwitch, 1);
-       EXPECT_EQ(switchManager->receivedItem->getId()->getGlobalId(), switchId_);
+
+       if (switchManager->receivedItem)
+               EXPECT_EQ(switchManager->receivedItem->getId()->getGlobalId(), switchId_);
 }
 
 /*
@@ -24,10 +27,13 @@ TEST_F(ConfigurationTestsFixture, ConfigurationEmpty_MappingAdded_BeUpdatedOnEve
 {
        auto switchManager = SwitchManager::create<SwitchManager_ConfigurationTests>(switchProvider_, configuration_, activityFactory_);
        configuration_->add(item_);
-       simulateKeyDown(key_);
+       simulateKeyPress(key_);
+       runMainLoop();
 
        EXPECT_EQ(switchManager->updatedWithSwitch, 1);
-       EXPECT_EQ(switchManager->receivedItem->getId()->getGlobalId(), switchId_);
+
+       if (switchManager->receivedItem)
+               EXPECT_EQ(switchManager->receivedItem->getId()->getGlobalId(), switchId_);
 }
 
 /*
@@ -40,7 +46,8 @@ TEST_F(ConfigurationTestsFixture, MappingAddedAndRemoved_BeNotUpdatedOnEvent)
        configuration_->add(item_);
        auto switchManager = SwitchManager::create<SwitchManager_ConfigurationTests>(switchProvider_, configuration_, activityFactory_);
        configuration_->remove(switchId_);
-       simulateKeyDown(key_);
+       simulateKeyPress(key_);
+       runMainLoop();
 
        EXPECT_EQ(switchManager->updatedWithSwitch, 0);
 }
@@ -57,10 +64,13 @@ TEST_F(ConfigurationTestsFixture, MappingAddedAndUpdated_BeUpdatedWithNewValue)
        auto newActivityType = "SCAN_OFF";
        auto newItem = std::make_shared<SwitchConfigurationItem>(switch_->getId()->getGlobalId(), newActivityType);
        configuration_->update(newItem);
-       simulateKeyDown(key_);
+       simulateKeyPress(key_);
+       runMainLoop();
 
        EXPECT_EQ(switchManager->updatedWithSwitch, 1);
-       EXPECT_EQ(switchManager->receivedItem->getId()->getGlobalId(), switchId_);
+
+       if (switchManager->receivedItem)
+               EXPECT_EQ(switchManager->receivedItem->getId()->getGlobalId(), switchId_);
 }
 
 int main(int argc, char **argv)
@@ -72,4 +82,4 @@ int main(int argc, char **argv)
        ecore_event_shutdown();
        ecore_shutdown();
        return r;
-}
\ No newline at end of file
+}
index 0c54fe17592f29d8a8bb93fc811906c05b77c80e..7b66fa044d0d983481ee04a8a00db14a682d75e6 100644 (file)
@@ -61,18 +61,33 @@ public:
                item_ = std::make_shared<SwitchConfigurationItem>(switch_->getId()->getGlobalId(), activityType_);
        }
 
-       void simulateKeyDown(const std::string &key)
+       void simulateKeyPress(const std::string &key)
        {
-               Ecore_Event_Key *event = (Ecore_Event_Key *)malloc(sizeof(Ecore_Event_Key));
+               Ecore_Event_Key *eventDown = (Ecore_Event_Key *)malloc(sizeof(Ecore_Event_Key));
+               Ecore_Event_Key *eventUp = (Ecore_Event_Key *)malloc(sizeof(Ecore_Event_Key));
 
-               if (!event) {
+               if (!eventDown || !eventUp) {
                        ERROR("Memory allocation error");
                        return;
                }
 
-               event->key = key.c_str();
-               EXPECT_TRUE(ecore_event_add(ECORE_EVENT_KEY_DOWN, event, NULL, NULL));
-               ecore_main_loop_iterate();
+               eventDown->key = key.c_str();
+               eventUp->key = key.c_str();
+
+               EXPECT_TRUE(ecore_event_add(ECORE_EVENT_KEY_DOWN, eventDown, NULL, NULL));
+               EXPECT_TRUE(ecore_event_add(ECORE_EVENT_KEY_UP, eventUp, NULL, NULL));
+       }
+
+       void runMainLoop()
+       {
+               EXPECT_TRUE(ecore_timer_add(estimatedTimeRequiredToSafelyPassingThroughTests_, mainLoopQuitCb, nullptr));
+               ecore_main_loop_begin();
+       }
+
+       static Eina_Bool mainLoopQuitCb(void *)
+       {
+               ecore_main_loop_quit();
+               return ECORE_CALLBACK_CANCEL;
        }
 
 protected:
@@ -84,6 +99,7 @@ protected:
        std::string key_ = "Down";
        std::string switchId_ = "AccessoriesSwitchProvider_Down";
        std::string activityType_ = "TWO_STEP_PROCESS_ACTIVITY";
+       double estimatedTimeRequiredToSafelyPassingThroughTests_ = 1.0;
 };
 
 class SwitchManager_ConfigurationTests : public SwitchManager