From 3ab2081060ee22189ac9f583dd83ed1a41311803 Mon Sep 17 00:00:00 2001 From: Lukasz Wlazly Date: Fri, 12 May 2017 16:17:28 +0200 Subject: [PATCH 01/16] ConfigurationItem refactor Renaming ConfigurationItem class with associated refactor of SQLiteConfiguration and QueryBuilder Change-Id: Idfcc407784db957b9b76bda29750436c95f571ad --- src/AccessoriesSwitchProvider.cpp | 2 +- src/CMakeLists.txt | 4 +- src/Configuration.cpp | 1 - src/Configuration.hpp | 18 +++--- src/ConfigurationItem.cpp | 100 -------------------------------- src/ConfigurationItem.hpp | 48 --------------- src/ConfigurationItemObserver.hpp | 12 ---- src/QueryBuilder.cpp | 64 ++++++++++++++++---- src/QueryBuilder.hpp | 11 +++- src/SQLiteConfiguration.cpp | 74 ++++++++++++----------- src/SQLiteConfiguration.hpp | 19 +++--- src/Subject.hpp | 1 - src/Switch.cpp | 14 +++-- src/Switch.hpp | 5 +- src/SwitchConfigurationItem.cpp | 83 ++++++++++++++++++++++++++ src/SwitchConfigurationItem.hpp | 54 +++++++++++++++++ src/SwitchConfigurationItemObserver.hpp | 12 ++++ src/SwitchId.cpp | 25 ++++++++ src/SwitchId.hpp | 22 +++++++ src/SwitchManager.cpp | 8 +-- src/SwitchManager.hpp | 6 +- tests/main.cpp | 4 +- 22 files changed, 338 insertions(+), 249 deletions(-) delete mode 100644 src/Configuration.cpp delete mode 100644 src/ConfigurationItem.cpp delete mode 100644 src/ConfigurationItem.hpp delete mode 100644 src/ConfigurationItemObserver.hpp create mode 100644 src/SwitchConfigurationItem.cpp create mode 100644 src/SwitchConfigurationItem.hpp create mode 100644 src/SwitchConfigurationItemObserver.hpp create mode 100644 src/SwitchId.cpp create mode 100644 src/SwitchId.hpp diff --git a/src/AccessoriesSwitchProvider.cpp b/src/AccessoriesSwitchProvider.cpp index 986342a..0d8bb15 100644 --- a/src/AccessoriesSwitchProvider.cpp +++ b/src/AccessoriesSwitchProvider.cpp @@ -689,7 +689,7 @@ SwitchProvider::SwitchCollection AccessoriesSwitchProvider::findAllSwitches() std::shared_ptr AccessoriesSwitchProvider::findSwitchById(const std::string &id) { for (auto &sw : switches) { - if (sw->getId() == id) + if (sw->getId()->getGlobalId() == id) return sw; } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d82ec1f..41a6072 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,16 +3,16 @@ SET(SRCS ActivityTypes.cpp AdHocActivityDispatcher.cpp CachedActivityBuilderFactory.cpp - Configuration.cpp QueryBuilder.cpp Switch.cpp SwitchProvider.cpp universalswitch.cpp Activity.cpp CompositeSwitchProvider.cpp - ConfigurationItem.cpp + SwitchConfigurationItem.cpp SQLiteConfiguration.cpp SwitchManager.cpp + SwitchId.cpp ) ADD_LIBRARY(universal-switch-obj OBJECT ${SRCS}) diff --git a/src/Configuration.cpp b/src/Configuration.cpp deleted file mode 100644 index bb73132..0000000 --- a/src/Configuration.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "Configuration.hpp" diff --git a/src/Configuration.hpp b/src/Configuration.hpp index ddfb011..1b2a3fa 100644 --- a/src/Configuration.hpp +++ b/src/Configuration.hpp @@ -2,21 +2,23 @@ #define CONFIGURATION_HPP_ #include "Subject.hpp" -#include "ConfigurationItem.hpp" +#include "SwitchConfigurationItem.hpp" #include "Condition.hpp" #include #include -class Configuration : public Subject +class Configuration : public Subject { public: - virtual ~Configuration() {}; - virtual void add(const std::shared_ptr &item) = 0; - virtual void update(const std::shared_ptr &item) = 0; - virtual void remove(const std::string &key, const std::string &group) = 0; - virtual std::shared_ptr find(const std::string &name, const std::string &group) const = 0; - virtual std::vector> findAll() const = 0; + virtual ~Configuration() = default; + + virtual void add(const std::shared_ptr &item) = 0; + virtual void update(const std::shared_ptr &item) = 0; + virtual void remove(const std::string &switchId) = 0; + virtual std::shared_ptr findBySwitchId(const std::string &switchId) const = 0; + // virtual std::vector> findByProviderId(const std::string &providerId) const = 0; + virtual std::vector> findAll() const = 0; }; #endif diff --git a/src/ConfigurationItem.cpp b/src/ConfigurationItem.cpp deleted file mode 100644 index a260fe3..0000000 --- a/src/ConfigurationItem.cpp +++ /dev/null @@ -1,100 +0,0 @@ -#include "ConfigurationItem.hpp" - -#include - -ConfigurationItem::ConfigurationItem(ChangeType type) - : changeType(type) -{} - -ConfigurationItem::ConfigurationItem(std::string discoveryName, std::string groupName, std::string value, ChangeType type) - : key(std::move(discoveryName)), group(std::move(groupName)), value(std::move(value)), changeType(type) -{} - -ConfigurationItem::ConfigurationItem(std::string discoveryName, std::string displayName, std::string val) - : key(std::move(discoveryName)), name(std::move(displayName)), value(std::move(val)) -{} - -bool ConfigurationItem::operator==(const ConfigurationItem &item) const -{ - return key == item.getKey() && group == item.getGroup() && name == item.getName() - && description == item.getDescription() && value == item.getValue() && changeType == item.getChangeType(); -} - -std::string ConfigurationItem::getKey() const -{ - return key; -} - -void ConfigurationItem::setKey(const std::string &discoveryName) -{ - key = discoveryName; -} - -std::string ConfigurationItem::getGroup() const -{ - return group; -} - -void ConfigurationItem::setGroup(const std::string &groupName) -{ - group = groupName; -} - -std::string ConfigurationItem::getName() const -{ - return name; -} - -void ConfigurationItem::setName(const std::string &displayName) -{ - name = displayName; -} - -std::string ConfigurationItem::getDescription() const -{ - return description; -} - -void ConfigurationItem::setDescription(const std::string &description) -{ - this->description = description; -} - -std::string ConfigurationItem::getValue() const -{ - return value; -} - -void ConfigurationItem::setValue(const std::string &val) -{ - value = val; -} - -ChangeType ConfigurationItem::getChangeType() const -{ - return changeType; -} - -void ConfigurationItem::setChangeType(ChangeType type) -{ - changeType = type; -} - -void ConfigurationItem::construct(int argc, char **argv, char **azColName) -{ - dlog_print(DLOG_INFO, "universal_switch", ">>> buildItemCb <<<"); - - if (argc < 5) - return; - - dlog_print(DLOG_INFO, "universal_switch", "buildItemCb: %s, %s", argv[0], argv[2]); - - setKey(argv[0]); - setGroup(argv[1]); - setName(argv[2]); - setDescription(argv[3]); - setValue(argv[4]); - - dlog_print(DLOG_INFO, "universal_switch", "buildItemCb: %s, %s", getKey().c_str(), getValue().c_str()); - dlog_print(DLOG_INFO, "universal_switch", ">>> buildItemCb done <<<"); -} diff --git a/src/ConfigurationItem.hpp b/src/ConfigurationItem.hpp deleted file mode 100644 index 918c530..0000000 --- a/src/ConfigurationItem.hpp +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef CONFIGURATION_ITEM -#define CONFIGURATION_ITEM - -#include - -enum class ChangeType { - ADD, - UPDATE, - REMOVE, - NONE -}; - -class ConfigurationItem -{ -public: - ConfigurationItem() = default; - ConfigurationItem(ChangeType type); - ConfigurationItem(std::string discoveryName, std::string groupName, std::string value, ChangeType type); - ConfigurationItem(std::string discoveryName, std::string displayName, std::string value); - - bool operator==(const ConfigurationItem &item) const; - - std::string getKey() const; - void setKey(const std::string &discoveryName); - std::string getGroup() const; - void setGroup(const std::string &groupName); - std::string getName() const; - void setName(const std::string &displayName); - std::string getDescription() const; - void setDescription(const std::string &description); - std::string getValue() const; - void setValue(const std::string &val); - ChangeType getChangeType() const; - void setChangeType(ChangeType type); - - void construct(int argc, char **argv, char **azColName); - -private: - std::string key; - std::string group; - std::string name; - std::string description; - std::string value; - - ChangeType changeType = ChangeType::NONE; -}; - -#endif diff --git a/src/ConfigurationItemObserver.hpp b/src/ConfigurationItemObserver.hpp deleted file mode 100644 index b6aadba..0000000 --- a/src/ConfigurationItemObserver.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef CONFIGURATION_ITEM_OBSERVER_HPP -#define CONFIGURATION_ITEM_OBSERVER_HPP - -#include "Observer.hpp" -#include "ConfigurationItem.hpp" - -class ConfigurationItemObserver : public Observer -{ - -}; - -#endif diff --git a/src/QueryBuilder.cpp b/src/QueryBuilder.cpp index fe46294..196499c 100644 --- a/src/QueryBuilder.cpp +++ b/src/QueryBuilder.cpp @@ -1,69 +1,113 @@ #include "QueryBuilder.hpp" -//TODO(arguments passed to query should checked in security context) +#include "universalswitch.hpp" -QueryBuilder &QueryBuilder::select(const std::string &s) { - query << "select " << s << " "; +QueryBuilder &QueryBuilder::select(const std::initializer_list &list) { + query << "select "; + + bool first = true; + for (auto &elem : list) { + if (first) + first = false; + else + query << ", "; + query << elem; + } + + printDebug(); return *this; } QueryBuilder &QueryBuilder::from(const std::string &s) { query << "from " << s << " "; + + printDebug(); return *this; } QueryBuilder &QueryBuilder::where(const std::string &lv, const std::string &op, const std::string &rv) { query << "where " << lv << op << "'" << rv << "' "; + + printDebug(); return *this; } -QueryBuilder &QueryBuilder::set(const std::string &lv, const std::string &rv) { - query << "set " << lv << "='" << rv << "' "; +QueryBuilder &QueryBuilder::set(const std::initializer_list> &pairs) { + query << "set "; + + bool first = true; + for (auto &elem : pairs) { + if (first) + first = false; + else + query << ", "; + query << elem.first << "='" << elem.second <<"'"; + } + + printDebug(); return *this; } QueryBuilder &QueryBuilder::insertInto(const std::string &s) { query << "insert into " << s << " "; + + printDebug(); return *this; } -QueryBuilder &QueryBuilder::values(std::initializer_list list) { +QueryBuilder &QueryBuilder::values(const std::initializer_list &list) { query << "values ("; - for (auto elem = list.begin(); elem != list.end(); ++elem) { - query << "'" << *elem << "'"; - - if (elem + 1 != list.end()) + bool first = true; + for (auto &elem : list) { + if (first) + first = false; + else query << ", "; + query << "'" << elem <<"'"; } query << ") "; + printDebug(); return *this; } QueryBuilder &QueryBuilder::update(const std::string &s) { query << "update " << s << " "; + + printDebug(); return *this; } QueryBuilder &QueryBuilder::deleteFrom(const std::string &s) { query << "delete from " << s << " "; + + printDebug(); return *this; } QueryBuilder &QueryBuilder::qAnd(const std::string &lv, const std::string &op, const std::string &rv) { query << "and " << lv << op << "'" << rv << "' "; + + printDebug(); return *this; } QueryBuilder &QueryBuilder::qOr(const std::string &lv, const std::string &op, const std::string &rv) { query << "or " << lv << op << "'" << rv << "' "; + + printDebug(); return *this; } std::string QueryBuilder::build() { query << ";"; + printDebug(); return query.str(); } + +void QueryBuilder::printDebug() { + DEBUG("query: %s", query.str().c_str()); +} diff --git a/src/QueryBuilder.hpp b/src/QueryBuilder.hpp index e681c71..51da803 100644 --- a/src/QueryBuilder.hpp +++ b/src/QueryBuilder.hpp @@ -4,15 +4,18 @@ #include #include +// This function is temporary solution. +// Better one will be provided in the future. + class QueryBuilder { public: - QueryBuilder &select(const std::string &s); + QueryBuilder &select(const std::initializer_list &list); QueryBuilder &from(const std::string &s); QueryBuilder &where(const std::string &lv, const std::string &op, const std::string &rv); - QueryBuilder &set(const std::string &lv, const std::string &rv); + QueryBuilder &set(const std::initializer_list> &pairs); QueryBuilder &insertInto(const std::string &s); - QueryBuilder &values(std::initializer_list list); + QueryBuilder &values(const std::initializer_list &list); QueryBuilder &update(const std::string &s); QueryBuilder &deleteFrom(const std::string &s); QueryBuilder &qAnd(const std::string &lv, const std::string &op, const std::string &rv); @@ -21,6 +24,8 @@ public: std::string build(); private: + void printDebug(); + std::ostringstream query; }; diff --git a/src/SQLiteConfiguration.cpp b/src/SQLiteConfiguration.cpp index 0cb3190..c9ca0a2 100644 --- a/src/SQLiteConfiguration.cpp +++ b/src/SQLiteConfiguration.cpp @@ -12,8 +12,8 @@ SQLiteConfiguration &SQLiteConfiguration::instance() SQLiteConfiguration::SQLiteConfiguration() : password("UniversalSwitchPassword"), dbName("UniversalSwitch.db"), tableName("`UNIVERSAL_SWITCH`"), - discoveryCol("`DISCOVERY_NAME`"), groupCol("`GROUP_NAME`"), displayCol("`DISPLAY_NAME`"), - descriptionCol("`DESCRIPTION`"), valueCol("`VALUE`") + switchIdCol("`SWITCH_ID`"), /*providerIdCol("`PROVIDER_ID`"),*/ userNameCol("`USER_NAME`"), + activityTypeCol("`ACTIVITY_TYPE`") { initialize(); } @@ -45,19 +45,26 @@ void SQLiteConfiguration::initialize() createTable(); } -void SQLiteConfiguration::add(std::shared_ptr const &item) +void SQLiteConfiguration::add(const std::shared_ptr &item) { - QueryBuilder builder; - auto query = builder.insertInto(tableName).values({item.get()->getKey(), item.get()->getGroup(), item.get()->getName(), item.get()->getDescription(), item.get()->getValue()}).build(); + auto query = QueryBuilder().insertInto(tableName).values({item->getSwitchId(), + /*item.get()->getProviderId(),*/ + item->getUserName(), + item->getActivityType() + }).build(); if (executeQuery(query)) notify(item); } -void SQLiteConfiguration::update(std::shared_ptr const &item) +void SQLiteConfiguration::update(const std::shared_ptr &item) { - QueryBuilder builder; - auto query = builder.update(tableName).set(valueCol, item.get()->getValue()).where(discoveryCol, "=", item.get()->getKey()).qAnd(groupCol, "=", item.get()->getGroup()).build(); + std::initializer_list> setCollection { + {userNameCol, item->getUserName()}, + {activityTypeCol, item->getActivityType()} + }; + + auto query = QueryBuilder().update(tableName).set(setCollection).where(switchIdCol, "=", item->getSwitchId()).build(); INFO("update query: %s", query.c_str()); @@ -65,48 +72,46 @@ void SQLiteConfiguration::update(std::shared_ptr const &item) notify(item); } -void SQLiteConfiguration::remove(const std::string &key, const std::string &group) +void SQLiteConfiguration::remove(const std::string &switchId) { - auto item = find(key, group); + auto item = findBySwitchId(switchId); if (!item) { INFO("empty item"); return; } - INFO("item to remove: %s, %s", item.get()->getKey().c_str(), item.get()->getValue().c_str()); + INFO("item to remove: %s, %s", item->getSwitchId().c_str(), item->getActivityType().c_str()); - QueryBuilder builder; - auto query = builder.deleteFrom(tableName).where(discoveryCol, "=", key).qAnd(groupCol, "=", group).build(); + auto query = QueryBuilder().deleteFrom(tableName).where(switchIdCol, "=", switchId).build(); INFO("remove query: %s", query.c_str()); if (executeQuery(query)) notify(item); } -std::shared_ptr SQLiteConfiguration::find(const std::string &name, const std::string &group) const +std::shared_ptr SQLiteConfiguration::findBySwitchId(const std::string &switchId) const { - QueryBuilder builder; - auto query = builder.select("*").from(tableName).where(discoveryCol, "=", name).qAnd(groupCol, "=", group).build(); - dlog_print(DLOG_INFO, "universal_switch", "find: %s", query.c_str()); + auto query = QueryBuilder().select({"*"}).from(tableName).where(switchIdCol, "=", switchId).build(); + INFO("query: %s", query.c_str()); - auto item = std::make_shared(); + auto item = std::make_shared(); if (!executeQuery(query, [=](int argc, char **argv, char **azColName) { item->construct(argc, argv, azColName); })) return nullptr; - dlog_print(DLOG_INFO, "universal_switch", "find: %s, %s", item->getKey().c_str(), item->getValue().c_str()); + INFO("Found: %s, %s, %s", item->getSwitchId().c_str(), item->getUserName().c_str(), item->getActivityType().c_str()); + return item; } -std::vector> SQLiteConfiguration::findAll() const { - QueryBuilder builder; - auto query = builder.select("*").from(tableName).build(); +std::vector> SQLiteConfiguration::findAll() const { + auto query = QueryBuilder().select({"*"}).from(tableName).build(); - auto collection = std::vector>(); + auto collection = std::vector>(); if (!executeQuery(query, [&](int argc, char **argv, char **azColName) { - auto item = std::make_shared(); + auto item = std::make_shared(); item->construct(argc, argv, azColName); collection.emplace_back(item); })) return {}; @@ -137,14 +142,13 @@ bool SQLiteConfiguration::executeQuery(const std::string &query, const execCallb void SQLiteConfiguration::printRecords() const { - QueryBuilder builder; - auto query = builder.select("*").from(tableName).build(); + auto query = QueryBuilder().select({"*"}).from(tableName).build(); executeQuery(query, [](int argc, char **argv, char **colName) { - dlog_print(DLOG_INFO, "universal_switch", "Table"); + INFO("Data base content:"); - for (int i = 0; i < argc; i++) - dlog_print(DLOG_INFO, "universal_switch", "%s = %s | ", colName[i], argv[i] ? argv[i] : "NULL"); + for (int i = 0; i < argc; ++i) + INFO("%s: %s", colName[i], argv[i] ? argv[i] : ""); }); } @@ -156,13 +160,13 @@ bool SQLiteConfiguration::executeQuery(const std::string &query) const void SQLiteConfiguration::createTable() { auto query = "CREATE TABLE IF NOT EXISTS " + tableName + "(" - + discoveryCol + " TEXT NOT NULL, " - + groupCol + " TEXT NOT NULL, " - + displayCol + " TEXT, " - + descriptionCol + " TEXT, " - + valueCol + " TEXT NOT NULL, " - + "PRIMARY KEY (" + discoveryCol + ", " + groupCol + "));"; + + switchIdCol + " TEXT NOT NULL, " + // + providerIdCol + " TEXT NOT NULL, " + + userNameCol + " TEXT, " + + activityTypeCol + " TEXT NOT NULL, " + + "PRIMARY KEY (" + switchIdCol + "));"; + INFO("query: %s", query.c_str()); executeQuery(query); } diff --git a/src/SQLiteConfiguration.hpp b/src/SQLiteConfiguration.hpp index f930964..8c17fbf 100644 --- a/src/SQLiteConfiguration.hpp +++ b/src/SQLiteConfiguration.hpp @@ -17,11 +17,11 @@ class SQLiteConfiguration : public Configuration public: static SQLiteConfiguration &instance(); - void add(const std::shared_ptr &item) override; - void update(const std::shared_ptr &item) override; - void remove(const std::string &key, const std::string &group) override; - std::shared_ptr find(const std::string &name, const std::string &group) const override; - std::vector> findAll() const override; + void add(const std::shared_ptr &item) override; + void update(const std::shared_ptr &item) override; + void remove(const std::string &switchId) override; + std::shared_ptr findBySwitchId(const std::string &switchId) const override; + std::vector> findAll() const override; void printRecords() const; void clearTable(); @@ -43,11 +43,10 @@ private: const std::string dbName; const std::string tableName; - const std::string discoveryCol; - const std::string groupCol; - const std::string displayCol; - const std::string descriptionCol; - const std::string valueCol; + const std::string switchIdCol; + // const std::string providerIdCol; + const std::string userNameCol; + const std::string activityTypeCol; sqlite3 *dbHandler; }; diff --git a/src/Subject.hpp b/src/Subject.hpp index 1e7196b..7f766e9 100644 --- a/src/Subject.hpp +++ b/src/Subject.hpp @@ -2,7 +2,6 @@ #define SUBJECT_HPP_ #include "Condition.hpp" -#include "ConfigurationItem.hpp" #include "Observer.hpp" #include #include diff --git a/src/Switch.cpp b/src/Switch.cpp index dc8db16..a80ed27 100644 --- a/src/Switch.cpp +++ b/src/Switch.cpp @@ -1,8 +1,11 @@ #include "Switch.hpp" Switch::Switch(std::string id, std::weak_ptr provider) - : id(std::move(id)), provider(std::move(provider)) -{} + : provider(std::move(provider)) +{ + auto prov = provider.lock(); + switchId = std::make_shared(id, prov->getId()); +} std::weak_ptr Switch::getProvider() const { @@ -11,13 +14,12 @@ std::weak_ptr Switch::getProvider() const bool Switch::operator==(const Switch &sw) const { - return id == sw.getId() && name == getName() && description == getDescription(); + return getId() == sw.getId(); } -std::string Switch::getId() const +std::shared_ptr Switch::getId() const { - auto p = provider.lock(); - return p->getId() + "_" + id; + return switchId; } std::string Switch::getName() const diff --git a/src/Switch.hpp b/src/Switch.hpp index 07dbb7c..08bd5d5 100644 --- a/src/Switch.hpp +++ b/src/Switch.hpp @@ -2,6 +2,7 @@ #define SWITCH_HPP #include "SwitchProvider.hpp" +#include "SwitchId.hpp" #include #include @@ -16,7 +17,7 @@ public: std::weak_ptr getProvider() const; bool operator==(const Switch &sw) const; - std::string getId() const; + std::shared_ptr getId() const; std::string getName() const; std::string getDescription() const; @@ -24,7 +25,7 @@ public: void setDescription(const std::string &desc); private: - const std::string id; + std::shared_ptr switchId; std::string name; std::string description; std::weak_ptr provider; diff --git a/src/SwitchConfigurationItem.cpp b/src/SwitchConfigurationItem.cpp new file mode 100644 index 0000000..d16253c --- /dev/null +++ b/src/SwitchConfigurationItem.cpp @@ -0,0 +1,83 @@ +#include "SwitchConfigurationItem.hpp" + +#include + +SwitchConfigurationItem::SwitchConfigurationItem(ChangeType type) + : changeType(type) +{} + +SwitchConfigurationItem::SwitchConfigurationItem(const std::string &switchId, /*const std::string &providerId,*/ const std::string &activityType, ChangeType type) + : switchId(switchId), /*providerId(providerId),*/ activityType(activityType), changeType(type) +{} + +bool SwitchConfigurationItem::operator==(const SwitchConfigurationItem &item) const +{ + return switchId == item.getSwitchId() /*&& providerId == item.getProviderId()*/ + && activityType == item.getActivityType() && changeType == item.getChangeType(); +} + +std::string SwitchConfigurationItem::getSwitchId() const +{ + return switchId; +} + +void SwitchConfigurationItem::setSwitchId(const std::string &switchId) +{ + this->switchId = switchId; +} + +// std::string SwitchConfigurationItem::getProviderId() const +// { +// return providerId; +// } +// +// void SwitchConfigurationItem::setProviderId(const std::string &providerId) +// { +// this->providerId = providerId; +// } + +std::string SwitchConfigurationItem::getUserName() const { + return userName; +} + +void SwitchConfigurationItem::setUserName(const std::string &userName) { + this->userName = userName; +} + +std::string SwitchConfigurationItem::getActivityType() const +{ + return activityType; +} + +void SwitchConfigurationItem::setActivityType(const std::string &activityType) +{ + this->activityType = activityType; +} + +ChangeType SwitchConfigurationItem::getChangeType() const +{ + return changeType; +} + +void SwitchConfigurationItem::setChangeType(ChangeType type) +{ + changeType = type; +} + +void SwitchConfigurationItem::construct(int argc, char **argv, char **azColName) +{ + INFO("Function invokation"); + + if (argc != static_cast(SwitchConfigurationItemFields::Count)) { + ERROR("Fields number does not match the one in SwitchConfigurationItem"); + return; + } + + setSwitchId(argv[static_cast(SwitchConfigurationItemFields::SWITCH_ID)]); + // setProviderId(argv[SwitchConfigurationItemFields::PROVIDER_ID]); + setUserName(argv[static_cast(SwitchConfigurationItemFields::USER_NAME)]); + setActivityType(argv[static_cast(SwitchConfigurationItemFields::ACTIVITY_TYPE)]); + + // INFO("switchId: %s, providerId: %s, value: %s", switchId.c_str(), /*providerId.c_str(),*/ activityType.c_str()); + INFO("switchId: %s, userName: %s, activityType: %s", switchId.c_str(), userName.c_str(), activityType.c_str()); +} diff --git a/src/SwitchConfigurationItem.hpp b/src/SwitchConfigurationItem.hpp new file mode 100644 index 0000000..4b55aa4 --- /dev/null +++ b/src/SwitchConfigurationItem.hpp @@ -0,0 +1,54 @@ +#ifndef SWITCH_CONFIGURATION_ITEM_HPP +#define SWITCH_CONFIGURATION_ITEM_HPP + +#include + +#include "universalswitch.hpp" + +enum class ChangeType { + ADD, + UPDATE, + REMOVE, + NONE +}; + +enum class SwitchConfigurationItemFields { + SWITCH_ID, + // PROVIDER_ID, + USER_NAME, + ACTIVITY_TYPE, + Count +}; + +class SwitchConfigurationItem +{ +public: + SwitchConfigurationItem() = default; + SwitchConfigurationItem(ChangeType type); + SwitchConfigurationItem(const std::string &switchId, /*const std::string &providerId,*/ const std::string &activityType, ChangeType type); + + bool operator==(const SwitchConfigurationItem &item) const; + + std::string getSwitchId() const; + void setSwitchId(const std::string &switchId); + // std::string getProviderId() const; + // void setProviderId(const std::string &providerId); + std::string getUserName() const; + void setUserName(const std::string &userName); + std::string getActivityType() const; + void setActivityType(const std::string &activityType); + ChangeType getChangeType() const; + void setChangeType(ChangeType type); + + void construct(int argc, char **argv, char **azColName); + +private: + std::string switchId; + // std::string providerId; + std::string userName; + std::string activityType; + + ChangeType changeType = ChangeType::NONE; +}; + +#endif // SWITCH_CONFIGURATION_ITEM_HPP diff --git a/src/SwitchConfigurationItemObserver.hpp b/src/SwitchConfigurationItemObserver.hpp new file mode 100644 index 0000000..5e86ebb --- /dev/null +++ b/src/SwitchConfigurationItemObserver.hpp @@ -0,0 +1,12 @@ +#ifndef SWITCH_CONFIGURATION_ITEM_OBSERVER_HPP +#define SWITCH_CONFIGURATION_ITEM_OBSERVER_HPP + +#include "Observer.hpp" +#include "SwitchConfigurationItem.hpp" + +class SwitchConfigurationItemObserver : public Observer +{ + +}; + +#endif // SWITCH_CONFIGURATION_ITEM_OBSERVER_HPP_ diff --git a/src/SwitchId.cpp b/src/SwitchId.cpp new file mode 100644 index 0000000..cfcce3c --- /dev/null +++ b/src/SwitchId.cpp @@ -0,0 +1,25 @@ +#include "SwitchId.hpp" + +SwitchId::SwitchId(const std::string &localId, const std::string providerId) + : localId(localId), providerId(providerId) +{} + +bool SwitchId::operator==(const SwitchId &switchId) const +{ + return getGlobalId() == switchId.getGlobalId(); +} + +std::string SwitchId::getGlobalId() const +{ + return providerId + "_" + localId; +} + +std::string SwitchId::getLocalId() const +{ + return localId; +} + +std::string SwitchId::getProviderId() const +{ + return providerId; +} diff --git a/src/SwitchId.hpp b/src/SwitchId.hpp new file mode 100644 index 0000000..09e5514 --- /dev/null +++ b/src/SwitchId.hpp @@ -0,0 +1,22 @@ +#ifndef SWITCH_ID_HPP +#define SWITCH_ID_HPP + +#include + +class SwitchId +{ +public: + SwitchId(const std::string &localId, const std::string providerid); + + bool operator==(const SwitchId &switchId) const; + + std::string getGlobalId() const; + std::string getLocalId() const; + std::string getProviderId() const; + +private: + const std::string localId; + const std::string providerId; +}; + +#endif diff --git a/src/SwitchManager.cpp b/src/SwitchManager.cpp index 3bae117..bb2108e 100644 --- a/src/SwitchManager.cpp +++ b/src/SwitchManager.cpp @@ -11,16 +11,16 @@ SwitchManager::SwitchManager(std::shared_ptr provider, configuration(std::move(configuration)), activityBuilderFactory(std::move(factory)) { - asyncActivityConstrutor = std::make_shared(); + asyncActivityConstructor = std::make_shared(); activityDispatcher = std::make_shared(); - auto cond = std::unique_ptr>(new ConditionAll); + auto cond = std::unique_ptr>(new ConditionAll); configuration->attach(shared_from_this(), std::move(cond)); auto confItems = configuration->findAll(); - for (auto &i : confItems) { - auto sw = provider->findSwitchById(i->getKey()); + for (auto &item : confItems) { + auto sw = provider->findSwitchById(item->getSwitchId()); if (!sw) continue; diff --git a/src/SwitchManager.hpp b/src/SwitchManager.hpp index 24f986b..fa64d16 100644 --- a/src/SwitchManager.hpp +++ b/src/SwitchManager.hpp @@ -1,7 +1,7 @@ #ifndef SWITCH_MANAGER_HPP #define SWITCH_MANAGER_HPP -#include "ConfigurationItemObserver.hpp" +#include "SwitchConfigurationItemObserver.hpp" #include "SwitchObserver.hpp" #include "SwitchProvider.hpp" #include "AsyncActivityConstructor.hpp" @@ -12,7 +12,7 @@ #include -class SwitchManager : public ConfigurationItemObserver, public SwitchObserver, public ActivityObserver, std::enable_shared_from_this +class SwitchManager : public SwitchConfigurationItemObserver, public SwitchObserver, public ActivityObserver, std::enable_shared_from_this { public: SwitchManager(std::shared_ptr provider, @@ -23,7 +23,7 @@ private: std::shared_ptr switchProvider; std::shared_ptr configuration; std::shared_ptr activityBuilderFactory; - std::shared_ptr asyncActivityConstrutor; + std::shared_ptr asyncActivityConstructor; std::shared_ptr activityDispatcher; }; diff --git a/tests/main.cpp b/tests/main.cpp index a8e5d14..fbaa64c 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,9 +1,7 @@ #include -#include "Activity.hpp" - using namespace ::testing; -TEST(FooTest, Example) { +TEST(FooTest, SQLiteUsage) { EXPECT_TRUE(true); } -- 2.7.4 From 3c3b6522f8198a72bd2aab6b48bfe423cf97698a Mon Sep 17 00:00:00 2001 From: Lukasz Wlazly Date: Tue, 16 May 2017 15:28:19 +0200 Subject: [PATCH 02/16] Integration tests for Configuration and SwitchManager Change-Id: Ic52cb42766bec9b2cbc12cc83aab74c12b0e7782 --- src/AccessoriesSwitchProvider.cpp | 15 +++-- src/AccessoriesSwitchProvider.hpp | 6 +- src/CompositeSwitchProvider.cpp | 4 +- src/CompositeSwitchProvider.hpp | 4 +- src/Condition.hpp | 9 ++- src/ConditionAll.hpp | 4 ++ src/ConditionAny.hpp | 25 -------- src/ConditionEqual.hpp | 10 ++- src/Configuration.hpp | 4 +- src/Observer.hpp | 10 ++- src/QueryBuilder.cpp | 4 +- src/SQLiteConfiguration.cpp | 53 +++++++--------- src/SQLiteConfiguration.hpp | 10 +-- src/Subject.hpp | 13 ++-- src/Switch.cpp | 14 +---- src/Switch.hpp | 7 +-- src/SwitchConfigurationItem.cpp | 23 +++---- src/SwitchConfigurationItem.hpp | 9 +-- src/SwitchConfigurationItemObserver.hpp | 2 +- src/SwitchId.hpp | 2 +- src/SwitchManager.cpp | 58 ++++++++++++++--- src/SwitchManager.hpp | 26 ++++++-- src/SwitchProvider.cpp | 6 +- src/SwitchProvider.hpp | 11 ++-- ...{universalswitch.hpp => UniversalSwitchLog.hpp} | 8 ++- src/universalswitch.cpp | 2 +- tests/CMakeLists.txt | 3 +- tests/ConfigurationTests.cpp | 41 ++++++++++++ tests/ConfigurationTests.hpp | 73 ++++++++++++++++++++++ tests/main.cpp | 7 --- 30 files changed, 290 insertions(+), 173 deletions(-) delete mode 100644 src/ConditionAny.hpp rename src/{universalswitch.hpp => UniversalSwitchLog.hpp} (93%) create mode 100644 tests/ConfigurationTests.cpp create mode 100644 tests/ConfigurationTests.hpp delete mode 100644 tests/main.cpp diff --git a/src/AccessoriesSwitchProvider.cpp b/src/AccessoriesSwitchProvider.cpp index 0d8bb15..64c4540 100644 --- a/src/AccessoriesSwitchProvider.cpp +++ b/src/AccessoriesSwitchProvider.cpp @@ -1,5 +1,7 @@ #include "AccessoriesSwitchProvider.hpp" +#include + AccessoriesSwitchProvider::AccessoriesSwitchProvider() : SwitchProvider("AccessoriesSwitchProvider"), keys( {"XF86AddFavorite", @@ -676,20 +678,20 @@ AccessoriesSwitchProvider::AccessoriesSwitchProvider() }) { for (auto &key : keys) - switches.emplace_back(std::make_shared(key, shared_from_this())); + switches.emplace_back(std::make_shared(key, switchProviderId)); ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, onEcoreEventKeyDownCb, this); } -SwitchProvider::SwitchCollection AccessoriesSwitchProvider::findAllSwitches() +SwitchProvider::SwitchCollection AccessoriesSwitchProvider::findAllSwitches() const { return switches; } -std::shared_ptr AccessoriesSwitchProvider::findSwitchById(const std::string &id) +std::shared_ptr AccessoriesSwitchProvider::findSwitchById(const std::string &switchId) const { for (auto &sw : switches) { - if (sw->getId()->getGlobalId() == id) + if (sw->getId()->getGlobalId() == switchId) return sw; } @@ -699,10 +701,11 @@ std::shared_ptr AccessoriesSwitchProvider::findSwitchById(const std::str Eina_Bool AccessoriesSwitchProvider::onEcoreEventKeyDownCb(void *data, int type, void *ev) { auto event = static_cast(ev); - std::string id(event->key); + std::string keyId(event->key); auto accessoriesSwitchProvider = reinterpret_cast(data); - auto sw = accessoriesSwitchProvider->findSwitchById(id); + auto switchId = SwitchId(keyId, accessoriesSwitchProvider->getId()); + auto sw = accessoriesSwitchProvider->findSwitchById(switchId.getGlobalId()); if (sw) accessoriesSwitchProvider->notify(sw); diff --git a/src/AccessoriesSwitchProvider.hpp b/src/AccessoriesSwitchProvider.hpp index ecacf4b..2dd0011 100644 --- a/src/AccessoriesSwitchProvider.hpp +++ b/src/AccessoriesSwitchProvider.hpp @@ -9,13 +9,13 @@ #include #include -class AccessoriesSwitchProvider : public SwitchProvider, std::enable_shared_from_this +class AccessoriesSwitchProvider : public SwitchProvider { public: AccessoriesSwitchProvider(); - SwitchCollection findAllSwitches() override; - std::shared_ptr findSwitchById(const std::string &id) override; + SwitchCollection findAllSwitches() const override; + std::shared_ptr findSwitchById(const std::string &switchId) const override; private: static Eina_Bool onEcoreEventKeyDownCb(void *data, int type, void *ev); diff --git a/src/CompositeSwitchProvider.cpp b/src/CompositeSwitchProvider.cpp index d7a686d..91c5e86 100644 --- a/src/CompositeSwitchProvider.cpp +++ b/src/CompositeSwitchProvider.cpp @@ -4,7 +4,7 @@ CompositeSwitchProvider::CompositeSwitchProvider() : SwitchProvider("CompositeSwitchProvider") {} -CompositeSwitchProvider::SwitchCollection CompositeSwitchProvider::findAllSwitches() +CompositeSwitchProvider::SwitchCollection CompositeSwitchProvider::findAllSwitches() const { SwitchCollection result; @@ -16,7 +16,7 @@ CompositeSwitchProvider::SwitchCollection CompositeSwitchProvider::findAllSwitch return result; } -std::shared_ptr CompositeSwitchProvider::findSwitchById(const std::string &id) +std::shared_ptr CompositeSwitchProvider::findSwitchById(const std::string &id) const { for (auto &provider : providers) { auto sw = provider->findSwitchById(id); diff --git a/src/CompositeSwitchProvider.hpp b/src/CompositeSwitchProvider.hpp index 540a7b6..9f96903 100644 --- a/src/CompositeSwitchProvider.hpp +++ b/src/CompositeSwitchProvider.hpp @@ -10,8 +10,8 @@ class CompositeSwitchProvider : public SwitchProvider public: CompositeSwitchProvider(); - SwitchCollection findAllSwitches() override; - std::shared_ptr findSwitchById(const std::string &id) override; + SwitchCollection findAllSwitches() const override; + std::shared_ptr findSwitchById(const std::string &id) const override; void add(std::shared_ptr provider); std::vector> getProviders(); diff --git a/src/Condition.hpp b/src/Condition.hpp index d777f71..f917ddd 100644 --- a/src/Condition.hpp +++ b/src/Condition.hpp @@ -1,11 +1,14 @@ -#ifndef CONDITION_HPP_ -#define CONDITION_HPP_ +#ifndef CONDITION_HPP +#define CONDITION_HPP + +#include template class Condition { public: - virtual ~Condition(){}; + virtual ~Condition() = default; virtual bool matching(const T *item) const = 0; + virtual std::shared_ptr getItem() const = 0; }; #endif diff --git a/src/ConditionAll.hpp b/src/ConditionAll.hpp index 7dde971..8eb5886 100644 --- a/src/ConditionAll.hpp +++ b/src/ConditionAll.hpp @@ -10,6 +10,10 @@ public: bool matching(const T *item) const override { return true; } + + std::shared_ptr getItem() const override { + return nullptr; + } }; #endif diff --git a/src/ConditionAny.hpp b/src/ConditionAny.hpp deleted file mode 100644 index 43406bc..0000000 --- a/src/ConditionAny.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef CONDITION_ANY_HPP -#define CONDITION_ANY_HPP - -#include "Condition.hpp" - -template -class ConditionAny : public Condition -{ -public: - ConditionAny(std::vector> items) - : items(std::move(items)) - {} - - bool matching(const T* item) const override { - for (auto &it : items) - if (*item == *it) - return true; - return false; - } - -private: - std::vector> items; -}; - -#endif diff --git a/src/ConditionEqual.hpp b/src/ConditionEqual.hpp index 54e62b2..f3c9d9a 100644 --- a/src/ConditionEqual.hpp +++ b/src/ConditionEqual.hpp @@ -7,16 +7,20 @@ template class ConditionEqual : public Condition { public: - ConditionEqual(std::shared_ptr item) - :item(std::move(item)) + ConditionEqual(const std::shared_ptr &item) + :item(item) {} bool matching(const T *item) const override { return *item == *(this->item); } + std::shared_ptr getItem() const override { + return item; + } + private: - std::shared_ptr item; + const std::shared_ptr item; }; #endif diff --git a/src/Configuration.hpp b/src/Configuration.hpp index 1b2a3fa..f989728 100644 --- a/src/Configuration.hpp +++ b/src/Configuration.hpp @@ -1,5 +1,5 @@ -#ifndef CONFIGURATION_HPP_ -#define CONFIGURATION_HPP_ +#ifndef CONFIGURATION_HPP +#define CONFIGURATION_HPP #include "Subject.hpp" #include "SwitchConfigurationItem.hpp" diff --git a/src/Observer.hpp b/src/Observer.hpp index 9e2532f..082db7d 100644 --- a/src/Observer.hpp +++ b/src/Observer.hpp @@ -1,16 +1,14 @@ -#ifndef OBSERVER_HPP_ -#define OBSERVER_HPP_ +#ifndef OBSERVER_HPP +#define OBSERVER_HPP -#include "universalswitch.hpp" +#include "UniversalSwitchLog.hpp" #include template class Observer { public: - void update(std::shared_ptr item) { - INFO("updated: %p", item.get()); - } + virtual void update(const std::shared_ptr &item) = 0; }; #endif diff --git a/src/QueryBuilder.cpp b/src/QueryBuilder.cpp index 196499c..aa4308b 100644 --- a/src/QueryBuilder.cpp +++ b/src/QueryBuilder.cpp @@ -1,6 +1,6 @@ #include "QueryBuilder.hpp" -#include "universalswitch.hpp" +#include "UniversalSwitchLog.hpp" QueryBuilder &QueryBuilder::select(const std::initializer_list &list) { query << "select "; @@ -19,7 +19,7 @@ QueryBuilder &QueryBuilder::select(const std::initializer_list &lis } QueryBuilder &QueryBuilder::from(const std::string &s) { - query << "from " << s << " "; + query << " from " << s << " "; printDebug(); return *this; diff --git a/src/SQLiteConfiguration.cpp b/src/SQLiteConfiguration.cpp index c9ca0a2..f39123b 100644 --- a/src/SQLiteConfiguration.cpp +++ b/src/SQLiteConfiguration.cpp @@ -4,55 +4,50 @@ #include "QueryBuilder.hpp" -SQLiteConfiguration &SQLiteConfiguration::instance() -{ - static SQLiteConfiguration instance; - return instance; -} - -SQLiteConfiguration::SQLiteConfiguration() - : password("UniversalSwitchPassword"), dbName("UniversalSwitch.db"), tableName("`UNIVERSAL_SWITCH`"), - switchIdCol("`SWITCH_ID`"), /*providerIdCol("`PROVIDER_ID`"),*/ userNameCol("`USER_NAME`"), +SQLiteConfiguration::SQLiteConfiguration(bool createInMemory) + : password("UniversalSwitchPassword"), dbName(createInMemory ? ":memory:" : "UniversalSwitch.db"), tableName("`UNIVERSAL_SWITCH`"), + switchIdCol("`SWITCH_ID`"), userNameCol("`USER_NAME`"), activityTypeCol("`ACTIVITY_TYPE`") { - initialize(); -} - -SQLiteConfiguration::~SQLiteConfiguration() -{ - sqlite3_close(dbHandler); - sqlite3_shutdown(); -} - -void SQLiteConfiguration::initialize() -{ sqlite3_shutdown(); sqlite3_config(SQLITE_CONFIG_URI, 1); sqlite3_initialize(); - auto resource = app_get_data_path(); - if (!resource) - ERROR("app_get_data_path() failed"); + std::string path; + if (createInMemory) { + auto resource = app_get_data_path(); + if (!resource) + ERROR("app_get_data_path() failed"); - auto path = std::string(resource ? resource : "") + dbName; - free(resource); + path = (resource ? resource : "") + dbName; + free(resource); + } else + path = dbName; - if (sqlite3_open(path.c_str(), &dbHandler) != SQLITE_OK) + if (sqlite3_open(path.c_str(), &dbHandler) != SQLITE_OK) { ERROR("sqlite3_open() failed"); + throw std::runtime_error("SQLite connection failed"); + } sqlite3_update_hook(dbHandler, update_callback, nullptr); createTable(); } +SQLiteConfiguration::~SQLiteConfiguration() +{ + sqlite3_close(dbHandler); + sqlite3_shutdown(); +} + void SQLiteConfiguration::add(const std::shared_ptr &item) { auto query = QueryBuilder().insertInto(tableName).values({item->getSwitchId(), - /*item.get()->getProviderId(),*/ item->getUserName(), item->getActivityType() }).build(); + item->setChangeType(ChangeType::ADD); if (executeQuery(query)) notify(item); } @@ -68,6 +63,7 @@ void SQLiteConfiguration::update(const std::shared_ptr INFO("update query: %s", query.c_str()); + item->setChangeType(ChangeType::UPDATE); if (executeQuery(query)) notify(item); } @@ -86,6 +82,7 @@ void SQLiteConfiguration::remove(const std::string &switchId) auto query = QueryBuilder().deleteFrom(tableName).where(switchIdCol, "=", switchId).build(); INFO("remove query: %s", query.c_str()); + item->setChangeType(ChangeType::REMOVE); if (executeQuery(query)) notify(item); } @@ -117,7 +114,6 @@ std::vector> SQLiteConfiguration::findA return {}; return collection; - } static int call_functor(void *data, int argc, char **argv, char **colName) @@ -161,7 +157,6 @@ void SQLiteConfiguration::createTable() { auto query = "CREATE TABLE IF NOT EXISTS " + tableName + "(" + switchIdCol + " TEXT NOT NULL, " - // + providerIdCol + " TEXT NOT NULL, " + userNameCol + " TEXT, " + activityTypeCol + " TEXT NOT NULL, " + "PRIMARY KEY (" + switchIdCol + "));"; diff --git a/src/SQLiteConfiguration.hpp b/src/SQLiteConfiguration.hpp index 8c17fbf..2cb0ce1 100644 --- a/src/SQLiteConfiguration.hpp +++ b/src/SQLiteConfiguration.hpp @@ -11,11 +11,13 @@ #include #include +#include class SQLiteConfiguration : public Configuration { public: - static SQLiteConfiguration &instance(); + SQLiteConfiguration(bool createInMemory = false); + ~SQLiteConfiguration(); void add(const std::shared_ptr &item) override; void update(const std::shared_ptr &item) override; @@ -28,11 +30,6 @@ public: using execCallback_t = std::function; private: - SQLiteConfiguration(); - ~SQLiteConfiguration(); - - void initialize(); - bool executeQuery(const std::string &query, const execCallback_t &cb) const; bool executeQuery(const std::string &query) const; void createTable(); @@ -44,7 +41,6 @@ private: const std::string tableName; const std::string switchIdCol; - // const std::string providerIdCol; const std::string userNameCol; const std::string activityTypeCol; diff --git a/src/Subject.hpp b/src/Subject.hpp index 7f766e9..2d5ed02 100644 --- a/src/Subject.hpp +++ b/src/Subject.hpp @@ -1,5 +1,5 @@ -#ifndef SUBJECT_HPP_ -#define SUBJECT_HPP_ +#ifndef SUBJECT_HPP +#define SUBJECT_HPP #include "Condition.hpp" #include "Observer.hpp" @@ -15,14 +15,17 @@ public: observers.emplace_back(std::move(observer), std::move(cond)); } - void detach(const Observer *observer, const Condition *cond) + void detach(const Observer *observer, const T *item) { - for (auto it = observers.begin(); it != observers.end(); ++it) - if (it->first.get() == observer && it->second.get() == cond) { + for (auto it = observers.begin(); it != observers.end(); ++it) { + auto tempObserver = it->first.lock(); + + if (tempObserver.get() == observer && it->second->getItem().get() == item) { *it = std::move(observers.back()); observers.pop_back(); break; } + } } protected: diff --git a/src/Switch.cpp b/src/Switch.cpp index a80ed27..9e0a96c 100644 --- a/src/Switch.cpp +++ b/src/Switch.cpp @@ -1,16 +1,8 @@ #include "Switch.hpp" -Switch::Switch(std::string id, std::weak_ptr provider) - : provider(std::move(provider)) -{ - auto prov = provider.lock(); - switchId = std::make_shared(id, prov->getId()); -} - -std::weak_ptr Switch::getProvider() const -{ - return provider; -} +Switch::Switch(const std::string &switchId, const std::string &providerId) + : switchId(std::make_shared(switchId, providerId)) +{} bool Switch::operator==(const Switch &sw) const { diff --git a/src/Switch.hpp b/src/Switch.hpp index 08bd5d5..336f8d4 100644 --- a/src/Switch.hpp +++ b/src/Switch.hpp @@ -1,20 +1,16 @@ #ifndef SWITCH_HPP #define SWITCH_HPP -#include "SwitchProvider.hpp" #include "SwitchId.hpp" #include #include -class SwitchProvider; - class Switch { public: - Switch(std::string id, std::weak_ptr provider); + Switch(const std::string &switchId, const std::string &providerId); - std::weak_ptr getProvider() const; bool operator==(const Switch &sw) const; std::shared_ptr getId() const; @@ -28,7 +24,6 @@ private: std::shared_ptr switchId; std::string name; std::string description; - std::weak_ptr provider; }; #endif diff --git a/src/SwitchConfigurationItem.cpp b/src/SwitchConfigurationItem.cpp index d16253c..00a41df 100644 --- a/src/SwitchConfigurationItem.cpp +++ b/src/SwitchConfigurationItem.cpp @@ -6,14 +6,17 @@ SwitchConfigurationItem::SwitchConfigurationItem(ChangeType type) : changeType(type) {} -SwitchConfigurationItem::SwitchConfigurationItem(const std::string &switchId, /*const std::string &providerId,*/ const std::string &activityType, ChangeType type) - : switchId(switchId), /*providerId(providerId),*/ activityType(activityType), changeType(type) +SwitchConfigurationItem::SwitchConfigurationItem(const std::string &switchId, const std::string &activityType) + : switchId(switchId), activityType(activityType) +{} + +SwitchConfigurationItem::SwitchConfigurationItem(const std::string &switchId, const std::string &activityType, ChangeType type) + : switchId(switchId), activityType(activityType), changeType(type) {} bool SwitchConfigurationItem::operator==(const SwitchConfigurationItem &item) const { - return switchId == item.getSwitchId() /*&& providerId == item.getProviderId()*/ - && activityType == item.getActivityType() && changeType == item.getChangeType(); + return switchId == item.getSwitchId() && activityType == item.getActivityType() && changeType == item.getChangeType(); } std::string SwitchConfigurationItem::getSwitchId() const @@ -26,16 +29,6 @@ void SwitchConfigurationItem::setSwitchId(const std::string &switchId) this->switchId = switchId; } -// std::string SwitchConfigurationItem::getProviderId() const -// { -// return providerId; -// } -// -// void SwitchConfigurationItem::setProviderId(const std::string &providerId) -// { -// this->providerId = providerId; -// } - std::string SwitchConfigurationItem::getUserName() const { return userName; } @@ -74,10 +67,8 @@ void SwitchConfigurationItem::construct(int argc, char **argv, char **azColName) } setSwitchId(argv[static_cast(SwitchConfigurationItemFields::SWITCH_ID)]); - // setProviderId(argv[SwitchConfigurationItemFields::PROVIDER_ID]); setUserName(argv[static_cast(SwitchConfigurationItemFields::USER_NAME)]); setActivityType(argv[static_cast(SwitchConfigurationItemFields::ACTIVITY_TYPE)]); - // INFO("switchId: %s, providerId: %s, value: %s", switchId.c_str(), /*providerId.c_str(),*/ activityType.c_str()); INFO("switchId: %s, userName: %s, activityType: %s", switchId.c_str(), userName.c_str(), activityType.c_str()); } diff --git a/src/SwitchConfigurationItem.hpp b/src/SwitchConfigurationItem.hpp index 4b55aa4..1796bd1 100644 --- a/src/SwitchConfigurationItem.hpp +++ b/src/SwitchConfigurationItem.hpp @@ -3,7 +3,7 @@ #include -#include "universalswitch.hpp" +#include "UniversalSwitchLog.hpp" enum class ChangeType { ADD, @@ -14,7 +14,6 @@ enum class ChangeType { enum class SwitchConfigurationItemFields { SWITCH_ID, - // PROVIDER_ID, USER_NAME, ACTIVITY_TYPE, Count @@ -25,14 +24,13 @@ class SwitchConfigurationItem public: SwitchConfigurationItem() = default; SwitchConfigurationItem(ChangeType type); - SwitchConfigurationItem(const std::string &switchId, /*const std::string &providerId,*/ const std::string &activityType, ChangeType type); + SwitchConfigurationItem(const std::string &switchId, const std::string &activityType); + SwitchConfigurationItem(const std::string &switchId, const std::string &activityType, ChangeType type); bool operator==(const SwitchConfigurationItem &item) const; std::string getSwitchId() const; void setSwitchId(const std::string &switchId); - // std::string getProviderId() const; - // void setProviderId(const std::string &providerId); std::string getUserName() const; void setUserName(const std::string &userName); std::string getActivityType() const; @@ -44,7 +42,6 @@ public: private: std::string switchId; - // std::string providerId; std::string userName; std::string activityType; diff --git a/src/SwitchConfigurationItemObserver.hpp b/src/SwitchConfigurationItemObserver.hpp index 5e86ebb..b3410d6 100644 --- a/src/SwitchConfigurationItemObserver.hpp +++ b/src/SwitchConfigurationItemObserver.hpp @@ -9,4 +9,4 @@ class SwitchConfigurationItemObserver : public Observer }; -#endif // SWITCH_CONFIGURATION_ITEM_OBSERVER_HPP_ +#endif // SWITCH_CONFIGURATION_ITEM_OBSERVER_HPP diff --git a/src/SwitchId.hpp b/src/SwitchId.hpp index 09e5514..c5ae5dd 100644 --- a/src/SwitchId.hpp +++ b/src/SwitchId.hpp @@ -6,7 +6,7 @@ class SwitchId { public: - SwitchId(const std::string &localId, const std::string providerid); + SwitchId(const std::string &localId, const std::string providerId); bool operator==(const SwitchId &switchId) const; diff --git a/src/SwitchManager.cpp b/src/SwitchManager.cpp index bb2108e..34cf8ee 100644 --- a/src/SwitchManager.cpp +++ b/src/SwitchManager.cpp @@ -4,27 +4,67 @@ #include "ConditionEqual.hpp" #include "AdHocActivityDispatcher.hpp" -SwitchManager::SwitchManager(std::shared_ptr provider, - std::shared_ptr configuration, - std::shared_ptr factory) - : switchProvider(std::move(provider)), - configuration(std::move(configuration)), - activityBuilderFactory(std::move(factory)) +SwitchManager::SwitchManager(const std::shared_ptr &provider, + const std::shared_ptr &configuration, + const std::shared_ptr &factory) + : switchProvider(provider), + configuration(configuration), + activityBuilderFactory(factory) { asyncActivityConstructor = std::make_shared(); activityDispatcher = std::make_shared(); +} - auto cond = std::unique_ptr>(new ConditionAll); +void SwitchManager::initialize() +{ + auto cond = std::unique_ptr>(new ConditionAll); configuration->attach(shared_from_this(), std::move(cond)); auto confItems = configuration->findAll(); for (auto &item : confItems) { - auto sw = provider->findSwitchById(item->getSwitchId()); + auto sw = switchProvider->findSwitchById(item->getSwitchId()); if (!sw) continue; auto cond = std::unique_ptr>(new ConditionEqual(sw)); - provider->attach(shared_from_this(), std::move(cond)); + switchProvider->attach(shared_from_this(), std::move(cond)); } } + +void SwitchManager::update(const std::shared_ptr &item) { + switch (item->getChangeType()) + { + case ChangeType::ADD: { + auto sw = switchProvider->findSwitchById(item->getSwitchId()); + if (!sw) + return; + + auto cond = std::unique_ptr>(new ConditionEqual(sw)); + switchProvider->attach(shared_from_this(), std::move(cond)); + + break; + } + + case ChangeType::UPDATE: + + break; + + case ChangeType::REMOVE: { + auto sw = switchProvider->findSwitchById(item->getSwitchId()); + switchProvider->detach(this, sw.get()); + break; + } + + case ChangeType::NONE: + WARNING("Invalid item type"); + } +} + +void SwitchManager::update(const std::shared_ptr &item) { + +} + +void SwitchManager::update(const std::shared_ptr &item) { + +} diff --git a/src/SwitchManager.hpp b/src/SwitchManager.hpp index fa64d16..4af0f5a 100644 --- a/src/SwitchManager.hpp +++ b/src/SwitchManager.hpp @@ -9,17 +9,33 @@ #include "Configuration.hpp" #include "Condition.hpp" #include "ActivityBuilderFactory.hpp" +#include "Activity.hpp" #include -class SwitchManager : public SwitchConfigurationItemObserver, public SwitchObserver, public ActivityObserver, std::enable_shared_from_this +class SwitchManager : public SwitchConfigurationItemObserver, public SwitchObserver, public ActivityObserver, public std::enable_shared_from_this { public: - SwitchManager(std::shared_ptr provider, - std::shared_ptr configuration, - std::shared_ptr factory); + void update(const std::shared_ptr &item) override; + void update(const std::shared_ptr &item) override; + void update(const std::shared_ptr &item) override; + + template + static std::shared_ptr create(const std::shared_ptr &provider, + const std::shared_ptr &configuration, + const std::shared_ptr &factory) { + auto ptr = std::shared_ptr(new A(provider, configuration, factory)); + ptr->initialize(); + return ptr; + } + +protected: + SwitchManager(const std::shared_ptr &provider, + const std::shared_ptr &configuration, + const std::shared_ptr &factory); + + void initialize(); -private: std::shared_ptr switchProvider; std::shared_ptr configuration; std::shared_ptr activityBuilderFactory; diff --git a/src/SwitchProvider.cpp b/src/SwitchProvider.cpp index 1f37436..e4d4e54 100644 --- a/src/SwitchProvider.cpp +++ b/src/SwitchProvider.cpp @@ -1,10 +1,10 @@ #include "SwitchProvider.hpp" -SwitchProvider::SwitchProvider(std::string id) - : id(std::move(id)) +SwitchProvider::SwitchProvider(const std::string &switchProviderId) + : switchProviderId(switchProviderId) {} std::string SwitchProvider::getId() const { - return id; + return switchProviderId; } diff --git a/src/SwitchProvider.hpp b/src/SwitchProvider.hpp index ce2803f..5c204ef 100644 --- a/src/SwitchProvider.hpp +++ b/src/SwitchProvider.hpp @@ -8,23 +8,20 @@ #include #include -class Switch; - class SwitchProvider : public Subject { public: using SwitchCollection = std::vector>; - using SwitchPtr = std::shared_ptr; virtual ~SwitchProvider() {}; - virtual SwitchCollection findAllSwitches() = 0; - virtual std::shared_ptr findSwitchById(const std::string &id) = 0; + virtual SwitchCollection findAllSwitches() const = 0; + virtual std::shared_ptr findSwitchById(const std::string &switchId) const = 0; std::string getId() const; protected: - SwitchProvider(std::string id); - const std::string id; + SwitchProvider(const std::string &id); + const std::string switchProviderId; }; #endif diff --git a/src/universalswitch.hpp b/src/UniversalSwitchLog.hpp similarity index 93% rename from src/universalswitch.hpp rename to src/UniversalSwitchLog.hpp index b65206f..ff18eec 100644 --- a/src/universalswitch.hpp +++ b/src/UniversalSwitchLog.hpp @@ -1,10 +1,12 @@ -#ifndef UNIVERSAL_SWITCH_HPP_ -#define UNIVERSAL_SWITCH_HPP_ +#ifndef UNIVERSAL_SWITCH_HPP +#define UNIVERSAL_SWITCH_HPP #include #include #include +#include + #ifdef LOG_TAG #undef LOG_TAG #endif @@ -69,4 +71,4 @@ #define _(str) (gettext(str)) -#endif /* UNIVERSAL_SWITCH_HPP_ */ +#endif /* UNIVERSAL_SWITCH_HPP */ diff --git a/src/universalswitch.cpp b/src/universalswitch.cpp index 10b1a6c..4ba8dcf 100644 --- a/src/universalswitch.cpp +++ b/src/universalswitch.cpp @@ -1,4 +1,4 @@ -#include "universalswitch.hpp" +#include "UniversalSwitchLog.hpp" #include "SQLiteConfiguration.hpp" #include diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e39f439..06c91cc 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,4 +1,3 @@ - ADD_SUBDIRECTORY(googletest) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/) @@ -18,4 +17,4 @@ FUNCTION(CREATE_TEST_FROM_SOURCE TEST_SRC_FILE) ADD_TEST(test_${TESTNAME} test_${TESTNAME}) ENDFUNCTION(CREATE_TEST_FROM_SOURCE) -CREATE_TEST_FROM_SOURCE(main.cpp) +CREATE_TEST_FROM_SOURCE(ConfigurationTests.cpp) diff --git a/tests/ConfigurationTests.cpp b/tests/ConfigurationTests.cpp new file mode 100644 index 0000000..802702d --- /dev/null +++ b/tests/ConfigurationTests.cpp @@ -0,0 +1,41 @@ +#include "ConfigurationTests.hpp" + +TEST_F(ConfigurationFixture, ConfigurationNotEmpty_BeUpdatedOnEvent) { + configuration_->add(item_); + + auto switchManager = SwitchManager::create(switchProvider_, configuration_, nullptr); + simulateKeyDown(); + + EXPECT_EQ(switchManager->updatedWithSwitch, 1); + EXPECT_EQ(switchManager->receivedItem->getId()->getGlobalId(), switchId_); +} + +TEST_F(ConfigurationFixture, ConfigurationEmpty_MappingAdded_BeUpdatedOnEvent) { + auto switchManager = SwitchManager::create(switchProvider_, configuration_, nullptr); + configuration_->add(item_); + simulateKeyDown(); + + EXPECT_EQ(switchManager->updatedWithSwitch, 1); + EXPECT_EQ(switchManager->receivedItem->getId()->getGlobalId(), switchId_); +} + +TEST_F(ConfigurationFixture, MappingAddedAndRemoved_BeNotUpdatedOnEvent) { + configuration_->add(item_); + auto switchManager = SwitchManager::create(switchProvider_, configuration_, nullptr); + configuration_->remove(switchId_); + simulateKeyDown(); + + EXPECT_EQ(switchManager->updatedWithSwitch, 0); +} + +TEST_F(ConfigurationFixture, MappingAddedAndUpdated_BeUpdatedWithNewValue) { + configuration_->add(item_); + auto switchManager = SwitchManager::create(switchProvider_, configuration_, nullptr); + auto newActivityType = "SCAN_OFF"; + auto newItem = std::make_shared(switch_->getId()->getGlobalId(), newActivityType); + configuration_->update(newItem); + simulateKeyDown(); + + EXPECT_EQ(switchManager->updatedWithSwitch, 1); + EXPECT_EQ(switchManager->receivedItem->getId()->getGlobalId(), switchId_); +} diff --git a/tests/ConfigurationTests.hpp b/tests/ConfigurationTests.hpp new file mode 100644 index 0000000..2d6de5c --- /dev/null +++ b/tests/ConfigurationTests.hpp @@ -0,0 +1,73 @@ +#ifndef CONFIGURATION_TESTS_HPP +#define CONFIGURATION_TESTS_HPP + +#include + +#include "SQLiteConfiguration.hpp" +#include "AccessoriesSwitchProvider.hpp" +#include "SwitchConfigurationItem.hpp" +#include "SwitchManager.hpp" + +#include + +using ::testing::StrictMock; +using ::testing::DoAll; +using ::testing::Invoke; +using ::testing::Return; +using ::testing::SetArgPointee; +using ::testing::StrEq; +using ::testing::_; + +class ConfigurationFixture : public ::testing::Test { +public: + void SetUp() { + EXPECT_TRUE(ecore_init()); + EXPECT_TRUE(ecore_event_init()); + configuration_ = std::make_shared(true); + switchProvider_ = std::make_shared(); + switch_ = switchProvider_->findSwitchById(switchId_); + item_ = std::make_shared(switch_->getId()->getGlobalId(), activityType_); + } + + void TearDown() { + ecore_event_shutdown(); + ecore_shutdown(); + } + + void simulateKeyDown() { + Ecore_Event_Key *event = (Ecore_Event_Key*)malloc(sizeof(Ecore_Event_Key)); + event->key = key.c_str(); + EXPECT_TRUE(ecore_event_add(ECORE_EVENT_KEY_DOWN, event, NULL, NULL)); + ecore_main_loop_iterate(); + } + +protected: + std::shared_ptr configuration_; + std::shared_ptr switchProvider_; + std::shared_ptr switch_; + std::shared_ptr item_; + std::string key = "Down"; + std::string switchId_ = "AccessoriesSwitchProvider_Down"; + std::string activityType_ = "SELECT"; +}; + +class SwitchManagerMocked : public SwitchManager { +public: + + SwitchManagerMocked(const std::shared_ptr &provider, + const std::shared_ptr &configuration, + const std::shared_ptr &factory) + : SwitchManager(provider, configuration, factory) + {} + + void update(const std::shared_ptr &item) override { + SwitchManager::update(item); + receivedItem = item; + ++updatedWithSwitch; + } + + int updatedWithSwitch = 0; + std::shared_ptr receivedItem; +}; + +#endif // CONFIGURATION_TESTS_HPP diff --git a/tests/main.cpp b/tests/main.cpp deleted file mode 100644 index fbaa64c..0000000 --- a/tests/main.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include - -using namespace ::testing; - -TEST(FooTest, SQLiteUsage) { - EXPECT_TRUE(true); -} -- 2.7.4 From c5fc0973e8a85b7ac2bd01923c251af4bb79b407 Mon Sep 17 00:00:00 2001 From: Lukasz Wlazly Date: Mon, 22 May 2017 15:03:55 +0200 Subject: [PATCH 03/16] Changing the way that activities are being built Provided tests currently fails. Patch https://review.tizen.org/gerrit/#/c/131357/ fix this problem. Change-Id: I265113e244fc2db397ead0e1d4e8d6db6c0b2fa2 --- src/AccessoriesSwitchProvider.cpp | 2 +- src/Activity.cpp | 11 ++++--- src/Activity.hpp | 14 ++++----- src/ActivityBuilder.hpp | 15 ---------- src/ActivityBuilderFactory.hpp | 15 ---------- src/ActivityDispatcher.hpp | 2 +- src/ActivityFactory.cpp | 33 ++++++++++++++++++++ src/ActivityFactory.hpp | 45 ++++++++++++++++++++++++++++ src/ActivityTypes.cpp | 58 ------------------------------------ src/ActivityTypes.hpp | 19 ------------ src/AdHocActivityDispatcher.cpp | 3 +- src/AsyncActivityConstructor.hpp | 13 -------- src/CMakeLists.txt | 6 ++-- src/CachedActivityBuilderFactory.cpp | 4 --- src/CachedActivityBuilderFactory.hpp | 12 -------- src/Condition.hpp | 3 +- src/ConditionAll.hpp | 8 +++-- src/ConditionEqual.hpp | 8 +++-- src/Configuration.hpp | 2 +- src/Observer.hpp | 3 +- src/QueryBuilder.cpp | 40 ++++++++++++++++--------- src/SQLiteConfiguration.cpp | 36 ++++++++++++---------- src/ScanningControlRequest.hpp | 9 ------ src/ScreenScanner.hpp | 10 ++++--- src/ScreenScannerManager.cpp | 48 +++++++++++++++++++++++++++++ src/ScreenScannerManager.hpp | 34 +++++++++++++++++++++ src/SelectActivity.cpp | 13 ++++++++ src/Subject.hpp | 3 +- src/SwitchConfigurationItem.cpp | 6 ++-- src/SwitchManager.cpp | 57 +++++++++++++++++++++++++++-------- src/SwitchManager.hpp | 29 +++++++++++------- src/UIActivity.cpp | 28 +++++++++++++++++ src/UIActivity.hpp | 23 ++++++++++++++ src/UIElement.cpp | 9 ++++-- src/UIElementObserver.hpp | 2 +- src/universalswitch.cpp | 14 ++++----- tests/ActivityDispatchingTests.cpp | 34 +++++++++++++++++++++ tests/ActivityDispatchingTests.hpp | 51 +++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 1 + tests/ConfigurationTests.cpp | 28 +++++++++-------- tests/ConfigurationTests.hpp | 33 ++++++++++++-------- 41 files changed, 516 insertions(+), 268 deletions(-) delete mode 100644 src/ActivityBuilder.hpp delete mode 100644 src/ActivityBuilderFactory.hpp create mode 100644 src/ActivityFactory.cpp create mode 100644 src/ActivityFactory.hpp delete mode 100644 src/ActivityTypes.cpp delete mode 100644 src/ActivityTypes.hpp delete mode 100644 src/AsyncActivityConstructor.hpp delete mode 100644 src/CachedActivityBuilderFactory.cpp delete mode 100644 src/CachedActivityBuilderFactory.hpp delete mode 100644 src/ScanningControlRequest.hpp create mode 100644 src/ScreenScannerManager.cpp create mode 100644 src/ScreenScannerManager.hpp create mode 100644 src/SelectActivity.cpp create mode 100644 src/UIActivity.cpp create mode 100644 src/UIActivity.hpp create mode 100644 tests/ActivityDispatchingTests.cpp create mode 100644 tests/ActivityDispatchingTests.hpp diff --git a/src/AccessoriesSwitchProvider.cpp b/src/AccessoriesSwitchProvider.cpp index 64c4540..a17784c 100644 --- a/src/AccessoriesSwitchProvider.cpp +++ b/src/AccessoriesSwitchProvider.cpp @@ -703,7 +703,7 @@ Eina_Bool AccessoriesSwitchProvider::onEcoreEventKeyDownCb(void *data, int type, auto event = static_cast(ev); std::string keyId(event->key); - auto accessoriesSwitchProvider = reinterpret_cast(data); + auto accessoriesSwitchProvider = reinterpret_cast(data); auto switchId = SwitchId(keyId, accessoriesSwitchProvider->getId()); auto sw = accessoriesSwitchProvider->findSwitchById(switchId.getGlobalId()); diff --git a/src/Activity.cpp b/src/Activity.cpp index f75a251..b0b2467 100644 --- a/src/Activity.cpp +++ b/src/Activity.cpp @@ -1,11 +1,10 @@ #include "Activity.hpp" -/* -Activity::Activity(const std::shared_ptr &element) -:uiElement(element) +Activity::Activity(const std::string &activityType) + : type(activityType) {} -Activity::setUIElement(const std::shared_ptr &element) { - UIElement = element; +std::string Activity::getType() +{ + return type; } -*/ diff --git a/src/Activity.hpp b/src/Activity.hpp index a711980..6f6065e 100644 --- a/src/Activity.hpp +++ b/src/Activity.hpp @@ -1,21 +1,21 @@ #ifndef ACTIVITY_HPP #define ACTIVITY_HPP -#include "UIElement.hpp" - #include class Activity { public: - Activity(){} - Activity(const std::shared_ptr &element); - virtual ~Activity(); - void setUIElement(const std::shared_ptr &element); + Activity(const std::string &activityType); + virtual ~Activity() = default; + + std::string getType(); + virtual bool prepare() = 0; virtual void execute() = 0; + private: - std::shared_ptr uiElement; + std::string type; }; #endif diff --git a/src/ActivityBuilder.hpp b/src/ActivityBuilder.hpp deleted file mode 100644 index bda5d87..0000000 --- a/src/ActivityBuilder.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef ACTIVITY_BUILDER_HPP -#define ACTIVITY_BUILDER_HPP - -#include "Activity.hpp" -#include "Subject.hpp" -#include "UIElement.hpp" - -class ActivityBuilder : public Subject -{ -public: - virtual void createNewActivity() = 0; - virtual void buildUIElement() = 0; -}; - -#endif diff --git a/src/ActivityBuilderFactory.hpp b/src/ActivityBuilderFactory.hpp deleted file mode 100644 index 39f02ed..0000000 --- a/src/ActivityBuilderFactory.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef ACTIVITY_BUILDER_FACTORY_HPP -#define ACTIVITY_BUILDER_FACTORY_HPP - -#include "ActivityBuilder.hpp" - -#include - -class ActivityBuilderFactory -{ -public: - virtual ~ActivityBuilderFactory(){}; - virtual std::shared_ptr getActivityBuilder(const std::string &activityType) = 0; -}; - -#endif diff --git a/src/ActivityDispatcher.hpp b/src/ActivityDispatcher.hpp index ae9815f..fc4cba7 100644 --- a/src/ActivityDispatcher.hpp +++ b/src/ActivityDispatcher.hpp @@ -8,7 +8,7 @@ class ActivityDispatcher { public: - virtual ~ActivityDispatcher(){}; + virtual ~ActivityDispatcher() {}; virtual void dispatch(std::shared_ptr activity) = 0; }; diff --git a/src/ActivityFactory.cpp b/src/ActivityFactory.cpp new file mode 100644 index 0000000..e4d0692 --- /dev/null +++ b/src/ActivityFactory.cpp @@ -0,0 +1,33 @@ +#include "ActivityFactory.hpp" + +std::shared_ptr ActivityFactory::getActivity(const std::string &activityType) +{ + auto it = constructorMap.find(activityType); + + if (it == constructorMap.end()) + return nullptr; + + ActivityConstructor constructor = it->second; + return constructor(); +} + +void ActivityFactory::registerActivityConstructor(const char *activityType, ActivityConstructor c) +{ + constructorMap.emplace(activityType, c); +} + +std::vector ActivityFactory::getAllActivityTypes() +{ + std::vector result; + + for (auto &pair : constructorMap) + result.emplace_back(pair.first); + + return result; +} + +std::shared_ptr ActivityFactory::getInstance() +{ + static auto instance = std::make_shared(); + return instance; +} diff --git a/src/ActivityFactory.hpp b/src/ActivityFactory.hpp new file mode 100644 index 0000000..15d0389 --- /dev/null +++ b/src/ActivityFactory.hpp @@ -0,0 +1,45 @@ +#ifndef ACTIVITY_FACTORY_HPP +#define ACTIVITY_FACTORY_HPP + +#include "Activity.hpp" + +#include +#include +#include + +class ActivityFactory +{ +public: + using ActivityConstructor = std::function()>; + + virtual ~ActivityFactory() = default; + static std::shared_ptr getInstance(); + + std::shared_ptr getActivity(const std::string &activityType); + void registerActivityConstructor(const char *activityType, ActivityConstructor c); + std::vector getAllActivityTypes(); + +private: + std::unordered_map constructorMap; +}; + + + +template +class RegisterActivity +{ +public: + static bool reg; + static bool init() + { + ActivityFactory::getInstance()->registerActivityConstructor(T::activityType, [](void) { + return std::make_shared(); + }); + return true; + } +}; + +template +bool RegisterActivity::reg = RegisterActivity::init(); + +#endif diff --git a/src/ActivityTypes.cpp b/src/ActivityTypes.cpp deleted file mode 100644 index 4576e53..0000000 --- a/src/ActivityTypes.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "ActivityTypes.hpp" - -std::set ActivityTypes::getActivityCollection() { - static ActivityTypes activityTypes; - return activityTypes.activities; -} - -ActivityTypes::ActivityTypes() { - for (auto name : {"ACTIVITY_SELECT", - "ACTIVITY_TOUCH", - "ACTIVITY_MORE_OPTIONS", - "ACTIVITY_MOVE_NEXT", - "ACTIVITY_MORE_PREVIOUS", - "ACTIVITY_HOME_SCREEN", - "ACTIVITY_BACK", - "ACTIVITY_RINGTONE_VOLUME_UP", - "ACTIVITY_RINGTONE_VOLUME_DOWN", - "ACTIVITY_NOTIFICATIONS", - "ACTIVITY_RECENTLY_USED_APPS", - "ACTIVITY_AUTO_SCAN_OFF", - "ACTIVITY_AUTO_SCAN_ON", - - "ACTIVITY_GESTURES", - "ACTIVITY_FLICK_LEFT", - "ACTIVITY_FLICK_RIGHT", - "ACTIVITY_FLICK_TOP", - "ACTIVITY_FLICK_DOWN", - "ACTIVITY_AUTOMATICALLY", - "ACTIVITY_TOUCH_AND_HOLD", - "ACTIVITY_FLICK", - "ACTIVITY_TOUCH_AND_FLICK", - - "ACTIVITY_ACTIONS", - "ACTIVITY_ZOOM_IN", - "ACTIVITY_ZOOM_OUT", - "ACTIVITY_SCREEN_ROTATION", - "ACTIVITY_VIBRATION", - "ACTIVITY_LOCK", - "ACTIVITY_SCREEN_SHOT", - - "ACTIVITY_SETTINGS", - "ACTIVITY_FASTER", - "ACTIVITY_SLOWER", - "ACTIVITY_POINT_SCANNING", - "ACTIVITY_BOTTOM_UP", - "ACTIVITY_UNMUTE", - "ACTIVITY_MUTE", - "ACTIVITY_MORE_SETTINGS", - - "ACTIVITY_KEYS", - "ACTIVITY_MEDIA_VOLUME_UP", - "ACTIVITY_MEDIA_VOLUME_DOWN", - "ACTIVITY_DOUBLE_TAP_HOME_BTN", - "ACTIVITY_TRIPLE_TAP_HOME_BTN", - "ACTIVITY_DEVICE_OPTIONS", - "ACTIVITY_MORE_OPTIONS"}) - activities.insert(name); -} diff --git a/src/ActivityTypes.hpp b/src/ActivityTypes.hpp deleted file mode 100644 index 751c0c4..0000000 --- a/src/ActivityTypes.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef ACTIVITY_TYPES_HPP -#define ACTIVITY_TYPES_HPP - -#include -#include - -class ActivityTypes -{ -public: - static std::set getActivityCollection(); - -private: - ActivityTypes(); - ~ActivityTypes(){}; - - std::set activities; -}; - -#endif diff --git a/src/AdHocActivityDispatcher.cpp b/src/AdHocActivityDispatcher.cpp index 2deb598..a2ec274 100644 --- a/src/AdHocActivityDispatcher.cpp +++ b/src/AdHocActivityDispatcher.cpp @@ -1,6 +1,7 @@ #include "AdHocActivityDispatcher.hpp" -void AdHocActivityDispatcher::dispatch(std::shared_ptr activity) { +void AdHocActivityDispatcher::dispatch(std::shared_ptr activity) +{ if (activity) activity->execute(); } diff --git a/src/AsyncActivityConstructor.hpp b/src/AsyncActivityConstructor.hpp deleted file mode 100644 index cee233d..0000000 --- a/src/AsyncActivityConstructor.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef ASYNC_ACTIVITY_CONSTRUCTOR_HPP -#define ASYNC_ACTIVITY_CONSTRUCTOR_HPP - -#include "ActivityBuilder.hpp" -#include "ActivityObserver.hpp" - -class AsyncActivityConstructor -{ -public: - void construct(const std::shared_ptr &builder, const std::shared_ptr &observer); -}; - -#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 41a6072..2383e17 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,8 +1,6 @@ SET(SRCS AccessoriesSwitchProvider.cpp - ActivityTypes.cpp AdHocActivityDispatcher.cpp - CachedActivityBuilderFactory.cpp QueryBuilder.cpp Switch.cpp SwitchProvider.cpp @@ -13,6 +11,10 @@ SET(SRCS SQLiteConfiguration.cpp SwitchManager.cpp SwitchId.cpp + ScreenScannerManager.cpp + SelectActivity.cpp + ActivityFactory.cpp + UIActivity.cpp ) ADD_LIBRARY(universal-switch-obj OBJECT ${SRCS}) diff --git a/src/CachedActivityBuilderFactory.cpp b/src/CachedActivityBuilderFactory.cpp deleted file mode 100644 index 6d91340..0000000 --- a/src/CachedActivityBuilderFactory.cpp +++ /dev/null @@ -1,4 +0,0 @@ -#include "CachedActivityBuilderFactory.hpp" - -std::shared_ptr CachedActivityBuilderFactory::getActivityBuilder(const std::string &activityType) { -} diff --git a/src/CachedActivityBuilderFactory.hpp b/src/CachedActivityBuilderFactory.hpp deleted file mode 100644 index 13a14b1..0000000 --- a/src/CachedActivityBuilderFactory.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef CACHED_ACTIVITY_BUILDER_FACTORY_HPP -#define CACHED_ACTIVITY_BUILDER_FACTORY_HPP - -#include "ActivityBuilderFactory.hpp" - -class CachedActivityBuilderFactory : public ActivityBuilderFactory -{ -public: - std::shared_ptr getActivityBuilder(const std::string &activityType) override; -}; - -#endif diff --git a/src/Condition.hpp b/src/Condition.hpp index f917ddd..5a9553a 100644 --- a/src/Condition.hpp +++ b/src/Condition.hpp @@ -4,7 +4,8 @@ #include template -class Condition { +class Condition +{ public: virtual ~Condition() = default; virtual bool matching(const T *item) const = 0; diff --git a/src/ConditionAll.hpp b/src/ConditionAll.hpp index 8eb5886..e8c3cb9 100644 --- a/src/ConditionAll.hpp +++ b/src/ConditionAll.hpp @@ -7,11 +7,13 @@ template class ConditionAll : public Condition { public: - bool matching(const T *item) const override { + bool matching(const T *item) const override + { return true; } - - std::shared_ptr getItem() const override { + + std::shared_ptr getItem() const override + { return nullptr; } }; diff --git a/src/ConditionEqual.hpp b/src/ConditionEqual.hpp index f3c9d9a..fa57d16 100644 --- a/src/ConditionEqual.hpp +++ b/src/ConditionEqual.hpp @@ -8,14 +8,16 @@ class ConditionEqual : public Condition { public: ConditionEqual(const std::shared_ptr &item) - :item(item) + : item(item) {} - bool matching(const T *item) const override { + bool matching(const T *item) const override + { return *item == *(this->item); } - std::shared_ptr getItem() const override { + std::shared_ptr getItem() const override + { return item; } diff --git a/src/Configuration.hpp b/src/Configuration.hpp index f989728..a7d375e 100644 --- a/src/Configuration.hpp +++ b/src/Configuration.hpp @@ -12,7 +12,7 @@ class Configuration : public Subject { public: virtual ~Configuration() = default; - + virtual void add(const std::shared_ptr &item) = 0; virtual void update(const std::shared_ptr &item) = 0; virtual void remove(const std::string &switchId) = 0; diff --git a/src/Observer.hpp b/src/Observer.hpp index 082db7d..ed94d05 100644 --- a/src/Observer.hpp +++ b/src/Observer.hpp @@ -6,7 +6,8 @@ #include template -class Observer { +class Observer +{ public: virtual void update(const std::shared_ptr &item) = 0; }; diff --git a/src/QueryBuilder.cpp b/src/QueryBuilder.cpp index aa4308b..3dd9c3e 100644 --- a/src/QueryBuilder.cpp +++ b/src/QueryBuilder.cpp @@ -2,7 +2,8 @@ #include "UniversalSwitchLog.hpp" -QueryBuilder &QueryBuilder::select(const std::initializer_list &list) { +QueryBuilder &QueryBuilder::select(const std::initializer_list &list) +{ query << "select "; bool first = true; @@ -18,21 +19,24 @@ QueryBuilder &QueryBuilder::select(const std::initializer_list &lis return *this; } -QueryBuilder &QueryBuilder::from(const std::string &s) { +QueryBuilder &QueryBuilder::from(const std::string &s) +{ query << " from " << s << " "; printDebug(); return *this; } -QueryBuilder &QueryBuilder::where(const std::string &lv, const std::string &op, const std::string &rv) { +QueryBuilder &QueryBuilder::where(const std::string &lv, const std::string &op, const std::string &rv) +{ query << "where " << lv << op << "'" << rv << "' "; printDebug(); return *this; } -QueryBuilder &QueryBuilder::set(const std::initializer_list> &pairs) { +QueryBuilder &QueryBuilder::set(const std::initializer_list> &pairs) +{ query << "set "; bool first = true; @@ -41,21 +45,23 @@ QueryBuilder &QueryBuilder::set(const std::initializer_list &list) { +QueryBuilder &QueryBuilder::values(const std::initializer_list &list) +{ query << "values ("; bool first = true; @@ -64,7 +70,7 @@ QueryBuilder &QueryBuilder::values(const std::initializer_list &lis first = false; else query << ", "; - query << "'" << elem <<"'"; + query << "'" << elem << "'"; } query << ") "; @@ -73,41 +79,47 @@ QueryBuilder &QueryBuilder::values(const std::initializer_list &lis return *this; } -QueryBuilder &QueryBuilder::update(const std::string &s) { +QueryBuilder &QueryBuilder::update(const std::string &s) +{ query << "update " << s << " "; printDebug(); return *this; } -QueryBuilder &QueryBuilder::deleteFrom(const std::string &s) { +QueryBuilder &QueryBuilder::deleteFrom(const std::string &s) +{ query << "delete from " << s << " "; printDebug(); return *this; } -QueryBuilder &QueryBuilder::qAnd(const std::string &lv, const std::string &op, const std::string &rv) { +QueryBuilder &QueryBuilder::qAnd(const std::string &lv, const std::string &op, const std::string &rv) +{ query << "and " << lv << op << "'" << rv << "' "; printDebug(); return *this; } -QueryBuilder &QueryBuilder::qOr(const std::string &lv, const std::string &op, const std::string &rv) { +QueryBuilder &QueryBuilder::qOr(const std::string &lv, const std::string &op, const std::string &rv) +{ query << "or " << lv << op << "'" << rv << "' "; printDebug(); return *this; } -std::string QueryBuilder::build() { +std::string QueryBuilder::build() +{ query << ";"; printDebug(); return query.str(); } -void QueryBuilder::printDebug() { +void QueryBuilder::printDebug() +{ DEBUG("query: %s", query.str().c_str()); } diff --git a/src/SQLiteConfiguration.cpp b/src/SQLiteConfiguration.cpp index f39123b..07e1403 100644 --- a/src/SQLiteConfiguration.cpp +++ b/src/SQLiteConfiguration.cpp @@ -43,9 +43,9 @@ SQLiteConfiguration::~SQLiteConfiguration() void SQLiteConfiguration::add(const std::shared_ptr &item) { auto query = QueryBuilder().insertInto(tableName).values({item->getSwitchId(), - item->getUserName(), - item->getActivityType() - }).build(); + item->getUserName(), + item->getActivityType() + }).build(); item->setChangeType(ChangeType::ADD); if (executeQuery(query)) @@ -94,31 +94,35 @@ std::shared_ptr SQLiteConfiguration::findBySwitchId(con auto item = std::make_shared(); - if (!executeQuery(query, [=](int argc, char **argv, char **azColName) { item->construct(argc, argv, azColName); })) - return nullptr; + if (!executeQuery(query, [ = ](int argc, char **argv, char **azColName) { + item->construct(argc, argv, azColName); + })) + return nullptr; INFO("Found: %s, %s, %s", item->getSwitchId().c_str(), item->getUserName().c_str(), item->getActivityType().c_str()); return item; } -std::vector> SQLiteConfiguration::findAll() const { +std::vector> SQLiteConfiguration::findAll() const +{ auto query = QueryBuilder().select({"*"}).from(tableName).build(); auto collection = std::vector>(); if (!executeQuery(query, [&](int argc, char **argv, char **azColName) { - auto item = std::make_shared(); - item->construct(argc, argv, azColName); - collection.emplace_back(item); })) - return {}; + auto item = std::make_shared(); + item->construct(argc, argv, azColName); + collection.emplace_back(item); + })) + return {}; return collection; } static int call_functor(void *data, int argc, char **argv, char **colName) { - auto &callback = *(const SQLiteConfiguration::execCallback_t*)data; + auto &callback = *(const SQLiteConfiguration::execCallback_t *)data; callback(argc, argv, colName); return 0; } @@ -127,7 +131,7 @@ bool SQLiteConfiguration::executeQuery(const std::string &query, const execCallb { INFO("executeQuery: %s", query.c_str()); char *errorMessage = nullptr; - auto result = sqlite3_exec(dbHandler, query.c_str(), call_functor, const_cast(&cb), &errorMessage); + auto result = sqlite3_exec(dbHandler, query.c_str(), call_functor, const_cast(&cb), &errorMessage); if (result != SQLITE_OK) { ERROR("sqlite ERROR: %s", errorMessage); sqlite3_free(errorMessage); @@ -156,10 +160,10 @@ bool SQLiteConfiguration::executeQuery(const std::string &query) const void SQLiteConfiguration::createTable() { auto query = "CREATE TABLE IF NOT EXISTS " + tableName + "(" - + switchIdCol + " TEXT NOT NULL, " - + userNameCol + " TEXT, " - + activityTypeCol + " TEXT NOT NULL, " - + "PRIMARY KEY (" + switchIdCol + "));"; + + switchIdCol + " TEXT NOT NULL, " + + userNameCol + " TEXT, " + + activityTypeCol + " TEXT NOT NULL, " + + "PRIMARY KEY (" + switchIdCol + "));"; INFO("query: %s", query.c_str()); executeQuery(query); diff --git a/src/ScanningControlRequest.hpp b/src/ScanningControlRequest.hpp deleted file mode 100644 index cc99542..0000000 --- a/src/ScanningControlRequest.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef SCANNING_CONTROL_REQUEST_HPP -#define SCANNING_CONTROL_REQUEST_HPP - -class ScannigControlRequest -{ - -}; - -#endif diff --git a/src/ScreenScanner.hpp b/src/ScreenScanner.hpp index 20d3f41..84baf4d 100644 --- a/src/ScreenScanner.hpp +++ b/src/ScreenScanner.hpp @@ -1,17 +1,19 @@ #ifndef SCREEN_SCANNER_HPP #define SCREEN_SCANNER_HPP -#include "Observer.hpp" #include "UIElement.hpp" -#include "ScanningControlRequest.hpp" #include -class ScreenScanner : public Subject +class ScreenScanner { public: virtual ~ScreenScanner(); - virtual void handle(std::shared_ptr request) = 0; + + virtual std::shared_ptr acceptAutoscanning() = 0; + +protected: + unsigned int speed; }; #endif diff --git a/src/ScreenScannerManager.cpp b/src/ScreenScannerManager.cpp new file mode 100644 index 0000000..3dab35e --- /dev/null +++ b/src/ScreenScannerManager.cpp @@ -0,0 +1,48 @@ +#include "ScreenScannerManager.hpp" + +ScreenScannerManager::ScreenScannerManager() +{ + //TODO: register for scanning parameteres in vconf. + // vconf key are defined in universal-switch UI in settings information +} + +ScreenScannerManager &ScreenScannerManager::instance() +{ + static ScreenScannerManager *instance = new ScreenScannerManager; + return *instance; +} + +void ScreenScannerManager::changeScanningMethod() +{ + +} + +void ScreenScannerManager::changeScanningDirection() +{ + +} + +void ScreenScannerManager::changeScanningSpeed(unsigned speed) +{ + +} + +void ScreenScannerManager::changeScanningLoopLimit(unsigned limit) +{ + +} + +void ScreenScannerManager::startAutoscanning() +{ + +} + +void ScreenScannerManager::stopAutoscanning() +{ + +} + +void ScreenScannerManager::acceptAutoscanning() +{ + +} diff --git a/src/ScreenScannerManager.hpp b/src/ScreenScannerManager.hpp new file mode 100644 index 0000000..1a7c680 --- /dev/null +++ b/src/ScreenScannerManager.hpp @@ -0,0 +1,34 @@ +#ifndef SCREEN_SCANNER_MANAGER_HPP +#define SCREEN_SCANNER_MANAGER_HPP + +#include "Subject.hpp" +#include "UIElement.hpp" +#include "ScreenScanner.hpp" + +enum class ScanningMethod { + ROW, + POINT +}; + +class ScreenScannerManager : public Subject +{ +public: + static ScreenScannerManager &instance(); + + void changeScanningMethod(); + void changeScanningDirection(); + void changeScanningSpeed(unsigned int speed); + void changeScanningLoopLimit(unsigned int limit); + + void startAutoscanning(); + void stopAutoscanning(); + void acceptAutoscanning(); + +private: + ScreenScannerManager(); + + ScanningMethod scanningMethod; + std::shared_ptr screenScanner; +}; + +#endif diff --git a/src/SelectActivity.cpp b/src/SelectActivity.cpp new file mode 100644 index 0000000..dff8c35 --- /dev/null +++ b/src/SelectActivity.cpp @@ -0,0 +1,13 @@ +#include "UIActivity.hpp" +#include "ActivityFactory.hpp" + + + +class SelectActivity : public UIActivity, RegisterActivity +{ + static constexpr const char *activityType = "SELECT"; + void execute() + { + //TODO: build contextual menu + } +}; diff --git a/src/Subject.hpp b/src/Subject.hpp index 2d5ed02..e5be8b2 100644 --- a/src/Subject.hpp +++ b/src/Subject.hpp @@ -33,8 +33,9 @@ protected: { for (auto &it : observers) { auto observer = it.first.lock(); - if (it.second->matching(item.get())) + if (observer && it.second->matching(item.get())) observer->update(item); + } } diff --git a/src/SwitchConfigurationItem.cpp b/src/SwitchConfigurationItem.cpp index 00a41df..cb7a18c 100644 --- a/src/SwitchConfigurationItem.cpp +++ b/src/SwitchConfigurationItem.cpp @@ -29,11 +29,13 @@ void SwitchConfigurationItem::setSwitchId(const std::string &switchId) this->switchId = switchId; } -std::string SwitchConfigurationItem::getUserName() const { +std::string SwitchConfigurationItem::getUserName() const +{ return userName; } -void SwitchConfigurationItem::setUserName(const std::string &userName) { +void SwitchConfigurationItem::setUserName(const std::string &userName) +{ this->userName = userName; } diff --git a/src/SwitchManager.cpp b/src/SwitchManager.cpp index 34cf8ee..42937c2 100644 --- a/src/SwitchManager.cpp +++ b/src/SwitchManager.cpp @@ -5,15 +5,14 @@ #include "AdHocActivityDispatcher.hpp" SwitchManager::SwitchManager(const std::shared_ptr &provider, - const std::shared_ptr &configuration, - const std::shared_ptr &factory) - : switchProvider(provider), + const std::shared_ptr &configuration, + const std::shared_ptr &factory, + const std::shared_ptr &dispatcher) + : switchProvider(provider), configuration(configuration), - activityBuilderFactory(factory) -{ - asyncActivityConstructor = std::make_shared(); - activityDispatcher = std::make_shared(); -} + activityFactory(factory), + activityDispatcher(dispatcher) +{} void SwitchManager::initialize() { @@ -32,9 +31,9 @@ void SwitchManager::initialize() } } -void SwitchManager::update(const std::shared_ptr &item) { - switch (item->getChangeType()) - { +void SwitchManager::update(const std::shared_ptr &item) +{ + switch (item->getChangeType()) { case ChangeType::ADD: { auto sw = switchProvider->findSwitchById(item->getSwitchId()); if (!sw) @@ -61,10 +60,42 @@ void SwitchManager::update(const std::shared_ptr &item) } } -void SwitchManager::update(const std::shared_ptr &item) { +void SwitchManager::update(const std::shared_ptr &sw) +{ + auto confItem = configuration->findBySwitchId(sw->getId()->getGlobalId()); + auto activityType = confItem->getActivityType(); + + if (!activity || activity->getType() != activityType) + activity = activityFactory->getActivity(activityType); + + bool ready = false; + if (!activity) + ERROR("No activity provided."); + else + ready = activity->prepare(); + + if (ready) { + activityDispatcher->dispatch(activity); + activity.reset(); + } +} + +void SwitchManager::setActivityProvider(const std::shared_ptr &provider) +{ + switchProvider = provider; +} +void SwitchManager::setConfiguration(const std::shared_ptr &configuration) +{ + this->configuration = configuration; } -void SwitchManager::update(const std::shared_ptr &item) { +void SwitchManager::setActivityFactory(const std::shared_ptr &factory) +{ + activityFactory = factory; +} +void SwitchManager::setActivityDispatcher(const std::shared_ptr &dispatcher) +{ + activityDispatcher = dispatcher; } diff --git a/src/SwitchManager.hpp b/src/SwitchManager.hpp index 4af0f5a..e0cf83c 100644 --- a/src/SwitchManager.hpp +++ b/src/SwitchManager.hpp @@ -4,43 +4,50 @@ #include "SwitchConfigurationItemObserver.hpp" #include "SwitchObserver.hpp" #include "SwitchProvider.hpp" -#include "AsyncActivityConstructor.hpp" #include "ActivityDispatcher.hpp" #include "Configuration.hpp" #include "Condition.hpp" -#include "ActivityBuilderFactory.hpp" +#include "ActivityFactory.hpp" #include "Activity.hpp" #include -class SwitchManager : public SwitchConfigurationItemObserver, public SwitchObserver, public ActivityObserver, public std::enable_shared_from_this +class SwitchManager : public SwitchConfigurationItemObserver, public SwitchObserver, public std::enable_shared_from_this { public: void update(const std::shared_ptr &item) override; void update(const std::shared_ptr &item) override; - void update(const std::shared_ptr &item) override; template static std::shared_ptr create(const std::shared_ptr &provider, - const std::shared_ptr &configuration, - const std::shared_ptr &factory) { - auto ptr = std::shared_ptr(new A(provider, configuration, factory)); + const std::shared_ptr &configuration, + const std::shared_ptr &factory, + const std::shared_ptr &dispatcher) + { + auto ptr = std::shared_ptr(new A(provider, configuration, factory, dispatcher)); ptr->initialize(); return ptr; } + void setActivityProvider(const std::shared_ptr &provider); + void setConfiguration(const std::shared_ptr &configuration); + void setActivityFactory(const std::shared_ptr &factory); + void setActivityDispatcher(const std::shared_ptr &dispatcher); + protected: SwitchManager(const std::shared_ptr &provider, - const std::shared_ptr &configuration, - const std::shared_ptr &factory); + const std::shared_ptr &configuration, + const std::shared_ptr &factory, + const std::shared_ptr &dispatcher); void initialize(); std::shared_ptr switchProvider; std::shared_ptr configuration; - std::shared_ptr activityBuilderFactory; - std::shared_ptr asyncActivityConstructor; + std::shared_ptr activityFactory; std::shared_ptr activityDispatcher; + + std::shared_ptr activity; }; #endif diff --git a/src/UIActivity.cpp b/src/UIActivity.cpp new file mode 100644 index 0000000..27c93f1 --- /dev/null +++ b/src/UIActivity.cpp @@ -0,0 +1,28 @@ +#include "UIActivity.hpp" + +#include "ScreenScannerManager.hpp" +#include "ConditionAll.hpp" + +UIActivity::UIActivity(const std::string &type) + : Activity(type) +{ + auto cond = std::unique_ptr>(new ConditionAll); + ScreenScannerManager::instance().attach(shared_from_this(), std::move(cond)); +} + +UIActivity::~UIActivity() +{ + ScreenScannerManager::instance().detach(this, nullptr); +} + +bool UIActivity::prepare() +{ + ScreenScannerManager::instance().acceptAutoscanning(); + + return uiElement != nullptr; +} + +void UIActivity::update(const std::shared_ptr &elem) +{ + uiElement = elem; +} diff --git a/src/UIActivity.hpp b/src/UIActivity.hpp new file mode 100644 index 0000000..3c7b9f2 --- /dev/null +++ b/src/UIActivity.hpp @@ -0,0 +1,23 @@ +#ifndef UI_ACTIVITY_HPP +#define UI_ACTIVITY_HPP + +#include "Activity.hpp" +#include "UIElementObserver.hpp" +#include "UIElement.hpp" + +#include + +class UIActivity : public Activity, public UIElementObserver, public std::enable_shared_from_this +{ +public: + UIActivity(const std::string &type); + ~UIActivity(); + bool prepare() override; + + void update(const std::shared_ptr &elem) override; + +private: + std::shared_ptr uiElement; +}; + +#endif diff --git a/src/UIElement.cpp b/src/UIElement.cpp index afb6937..bd02c0b 100644 --- a/src/UIElement.cpp +++ b/src/UIElement.cpp @@ -1,13 +1,16 @@ #include "UIElement.hpp" -int32_t UIElement::getX() { +int32_t UIElement::getX() +{ return x; } -int32_t UIElement::getY() { +int32_t UIElement::getY() +{ return y; } -std::shared_ptr UIElement::getObject() { +std::shared_ptr UIElement::getObject() +{ return obj; } diff --git a/src/UIElementObserver.hpp b/src/UIElementObserver.hpp index 487a8f9..06f75b2 100644 --- a/src/UIElementObserver.hpp +++ b/src/UIElementObserver.hpp @@ -7,7 +7,7 @@ class UIElementObserver : public Observer { public: - virtual ~UIElementObserver(); + virtual ~UIElementObserver() = default; }; #endif diff --git a/src/universalswitch.cpp b/src/universalswitch.cpp index 4ba8dcf..0e607ff 100644 --- a/src/universalswitch.cpp +++ b/src/universalswitch.cpp @@ -8,20 +8,20 @@ bool service_app_create(void *data) { - // Todo: add your code here. - return true; + // Todo: add your code here. + return true; } void service_app_terminate(void *data) { - // Todo: add your code here. - return; + // Todo: add your code here. + return; } void service_app_control(app_control_h app_control, void *data) { - // Todo: add your code here. - return; + // Todo: add your code here. + return; } static void @@ -49,7 +49,7 @@ service_app_low_memory(app_event_info_h event_info, void *user_data) /*APP_EVENT_LOW_MEMORY*/ } -int main(int argc, char* argv[]) +int main(int argc, char *argv[]) { DEBUG("main function called"); char ad[50] = {0,}; diff --git a/tests/ActivityDispatchingTests.cpp b/tests/ActivityDispatchingTests.cpp new file mode 100644 index 0000000..b6db195 --- /dev/null +++ b/tests/ActivityDispatchingTests.cpp @@ -0,0 +1,34 @@ +#include "ActivityDispatchingTests.hpp" + +#include "ActivityFactory.hpp" + +#include + +TEST_F(ActivityDispatchingFixture, FactoryWorks) +{ + EXPECT_TRUE(ActivityFactory::getInstance()->getActivity(activityType_)); +} + +TEST_F(ActivityDispatchingFixture, ActivityNotPrepared) +{ + initialEnvironmentExpect(); + EXPECT_EQ(adHocActivityDispatcher_->updatedWithActivity, 0); +} + +TEST_F(ActivityDispatchingFixture, ActivityReady) +{ + initialEnvironmentExpect(); + EXPECT_EQ(adHocActivityDispatcher_->updatedWithActivity, 0); + simulateKeyDown(keyMappedToSelectActivity_); + EXPECT_EQ(adHocActivityDispatcher_->updatedWithActivity, 1); +} + +TEST_F(ActivityDispatchingFixture, ActivityAbandoned) +{ + initialEnvironmentExpect(); + EXPECT_EQ(adHocActivityDispatcher_->updatedWithActivity, 0); + simulateKeyDown(keyNotMappedToSelectActivity_); + EXPECT_EQ(adHocActivityDispatcher_->updatedWithActivity, 0); + simulateKeyDown(keyMappedToSelectActivity_); + EXPECT_EQ(adHocActivityDispatcher_->updatedWithActivity, 0); +} diff --git a/tests/ActivityDispatchingTests.hpp b/tests/ActivityDispatchingTests.hpp new file mode 100644 index 0000000..4878bf5 --- /dev/null +++ b/tests/ActivityDispatchingTests.hpp @@ -0,0 +1,51 @@ +#include "ConfigurationTests.hpp" + +#include "AdHocActivityDispatcher.hpp" + +class AdHocActivityDispatcher_ActivityDispatchingTest : public AdHocActivityDispatcher +{ +public: + void dispatch(std::shared_ptr activity) override + { + AdHocActivityDispatcher::dispatch(activity); + ++updatedWithActivity; + } + + int updatedWithActivity = 0; +}; + +class ActivityDispatchingFixture : public ConfigurationTestsFixture +{ +public: + void SetUp() + { + ConfigurationTestsFixture::SetUp(); + configuration_->add(item_); + + auto newSwitch = switchProvider_->findSwitchById(newSwitchId_); + auto newItem = std::make_shared(newSwitch->getId()->getGlobalId(), newActivityType_); + configuration_->add(newItem); + + adHocActivityDispatcher_ = std::make_shared(); + switchManager_ = SwitchManager::create(switchProvider_, configuration_, activityFactory_, adHocActivityDispatcher_); + } + + void TearDown() + { + ConfigurationTestsFixture::TearDown(); + } + + void initialEnvironmentExpect() + { + simulateKeyDown(keyMappedToSelectActivity_); + EXPECT_EQ(switchManager_->updatedWithSwitch, 1); + } + +protected: + std::shared_ptr adHocActivityDispatcher_; + std::shared_ptr switchManager_; + std::string keyMappedToSelectActivity_ = "Down"; + std::string keyNotMappedToSelectActivity_ = "Up"; + std::string newSwitchId_ = "AccessoriesSwitchProvider_Up"; + std::string newActivityType_ = "SCAN_OFF"; +}; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 06c91cc..809da3f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -18,3 +18,4 @@ FUNCTION(CREATE_TEST_FROM_SOURCE TEST_SRC_FILE) ENDFUNCTION(CREATE_TEST_FROM_SOURCE) CREATE_TEST_FROM_SOURCE(ConfigurationTests.cpp) +CREATE_TEST_FROM_SOURCE(ActivityDispatchingTests.cpp) diff --git a/tests/ConfigurationTests.cpp b/tests/ConfigurationTests.cpp index 802702d..c2a64f1 100644 --- a/tests/ConfigurationTests.cpp +++ b/tests/ConfigurationTests.cpp @@ -1,40 +1,44 @@ #include "ConfigurationTests.hpp" -TEST_F(ConfigurationFixture, ConfigurationNotEmpty_BeUpdatedOnEvent) { +TEST_F(ConfigurationTestsFixture, ConfigurationNotEmpty_BeUpdatedOnEvent) +{ configuration_->add(item_); - auto switchManager = SwitchManager::create(switchProvider_, configuration_, nullptr); - simulateKeyDown(); + auto switchManager = SwitchManager::create(switchProvider_, configuration_, activityFactory_, nullptr); + simulateKeyDown(key_); EXPECT_EQ(switchManager->updatedWithSwitch, 1); EXPECT_EQ(switchManager->receivedItem->getId()->getGlobalId(), switchId_); } -TEST_F(ConfigurationFixture, ConfigurationEmpty_MappingAdded_BeUpdatedOnEvent) { - auto switchManager = SwitchManager::create(switchProvider_, configuration_, nullptr); +TEST_F(ConfigurationTestsFixture, ConfigurationEmpty_MappingAdded_BeUpdatedOnEvent) +{ + auto switchManager = SwitchManager::create(switchProvider_, configuration_, activityFactory_, nullptr); configuration_->add(item_); - simulateKeyDown(); + simulateKeyDown(key_); EXPECT_EQ(switchManager->updatedWithSwitch, 1); EXPECT_EQ(switchManager->receivedItem->getId()->getGlobalId(), switchId_); } -TEST_F(ConfigurationFixture, MappingAddedAndRemoved_BeNotUpdatedOnEvent) { +TEST_F(ConfigurationTestsFixture, MappingAddedAndRemoved_BeNotUpdatedOnEvent) +{ configuration_->add(item_); - auto switchManager = SwitchManager::create(switchProvider_, configuration_, nullptr); + auto switchManager = SwitchManager::create(switchProvider_, configuration_, activityFactory_, nullptr); configuration_->remove(switchId_); - simulateKeyDown(); + simulateKeyDown(key_); EXPECT_EQ(switchManager->updatedWithSwitch, 0); } -TEST_F(ConfigurationFixture, MappingAddedAndUpdated_BeUpdatedWithNewValue) { +TEST_F(ConfigurationTestsFixture, MappingAddedAndUpdated_BeUpdatedWithNewValue) +{ configuration_->add(item_); - auto switchManager = SwitchManager::create(switchProvider_, configuration_, nullptr); + auto switchManager = SwitchManager::create(switchProvider_, configuration_, activityFactory_, nullptr); auto newActivityType = "SCAN_OFF"; auto newItem = std::make_shared(switch_->getId()->getGlobalId(), newActivityType); configuration_->update(newItem); - simulateKeyDown(); + simulateKeyDown(key_); EXPECT_EQ(switchManager->updatedWithSwitch, 1); EXPECT_EQ(switchManager->receivedItem->getId()->getGlobalId(), switchId_); diff --git a/tests/ConfigurationTests.hpp b/tests/ConfigurationTests.hpp index 2d6de5c..bcf6287 100644 --- a/tests/ConfigurationTests.hpp +++ b/tests/ConfigurationTests.hpp @@ -18,24 +18,29 @@ using ::testing::SetArgPointee; using ::testing::StrEq; using ::testing::_; -class ConfigurationFixture : public ::testing::Test { +class ConfigurationTestsFixture : public ::testing::Test +{ public: - void SetUp() { + void SetUp() + { EXPECT_TRUE(ecore_init()); EXPECT_TRUE(ecore_event_init()); configuration_ = std::make_shared(true); switchProvider_ = std::make_shared(); + activityFactory_ = ActivityFactory::getInstance(); switch_ = switchProvider_->findSwitchById(switchId_); item_ = std::make_shared(switch_->getId()->getGlobalId(), activityType_); } - void TearDown() { + void TearDown() + { ecore_event_shutdown(); ecore_shutdown(); } - void simulateKeyDown() { - Ecore_Event_Key *event = (Ecore_Event_Key*)malloc(sizeof(Ecore_Event_Key)); + void simulateKeyDown(const std::string &key) + { + Ecore_Event_Key *event = (Ecore_Event_Key *)malloc(sizeof(Ecore_Event_Key)); event->key = key.c_str(); EXPECT_TRUE(ecore_event_add(ECORE_EVENT_KEY_DOWN, event, NULL, NULL)); ecore_main_loop_iterate(); @@ -44,23 +49,27 @@ public: protected: std::shared_ptr configuration_; std::shared_ptr switchProvider_; + std::shared_ptr activityFactory_; std::shared_ptr switch_; std::shared_ptr item_; - std::string key = "Down"; + std::string key_ = "Down"; std::string switchId_ = "AccessoriesSwitchProvider_Down"; std::string activityType_ = "SELECT"; }; -class SwitchManagerMocked : public SwitchManager { +class SwitchManager_ConfigurationTests : public SwitchManager +{ public: - SwitchManagerMocked(const std::shared_ptr &provider, - const std::shared_ptr &configuration, - const std::shared_ptr &factory) - : SwitchManager(provider, configuration, factory) + SwitchManager_ConfigurationTests(const std::shared_ptr &provider, + const std::shared_ptr &configuration, + const std::shared_ptr &factory, + const std::shared_ptr &dispatcher) + : SwitchManager(provider, configuration, factory, dispatcher) {} - void update(const std::shared_ptr &item) override { + void update(const std::shared_ptr &item) override + { SwitchManager::update(item); receivedItem = item; ++updatedWithSwitch; -- 2.7.4 From 379dc177fb85a435ca5bc07280d76f0513a22aff Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Stawicki?= Date: Fri, 26 May 2017 15:00:31 +0200 Subject: [PATCH 04/16] static class contructor registration in ActivityFactory Change-Id: I961b570d8965a748ce0453ac8f77e0475550bbaf --- src/Activity.hpp | 1 + src/ActivityFactory.cpp | 4 +++- src/ActivityFactory.hpp | 2 +- src/SelectActivity.cpp | 2 ++ src/UIActivity.cpp | 5 +++++ src/UIActivity.hpp | 2 ++ 6 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Activity.hpp b/src/Activity.hpp index 6f6065e..14842f6 100644 --- a/src/Activity.hpp +++ b/src/Activity.hpp @@ -11,6 +11,7 @@ public: std::string getType(); + virtual void initialize() {}; virtual bool prepare() = 0; virtual void execute() = 0; diff --git a/src/ActivityFactory.cpp b/src/ActivityFactory.cpp index e4d0692..ebb6f67 100644 --- a/src/ActivityFactory.cpp +++ b/src/ActivityFactory.cpp @@ -8,7 +8,9 @@ std::shared_ptr ActivityFactory::getActivity(const std::string &activi return nullptr; ActivityConstructor constructor = it->second; - return constructor(); + std::shared_ptr activity = constructor(); + activity->initialize(); + return activity; } void ActivityFactory::registerActivityConstructor(const char *activityType, ActivityConstructor c) diff --git a/src/ActivityFactory.hpp b/src/ActivityFactory.hpp index 15d0389..4bf0062 100644 --- a/src/ActivityFactory.hpp +++ b/src/ActivityFactory.hpp @@ -29,7 +29,6 @@ template class RegisterActivity { public: - static bool reg; static bool init() { ActivityFactory::getInstance()->registerActivityConstructor(T::activityType, [](void) { @@ -37,6 +36,7 @@ public: }); return true; } + [[gnu::used]] static bool reg; }; template diff --git a/src/SelectActivity.cpp b/src/SelectActivity.cpp index dff8c35..fe330c1 100644 --- a/src/SelectActivity.cpp +++ b/src/SelectActivity.cpp @@ -5,7 +5,9 @@ class SelectActivity : public UIActivity, RegisterActivity { +public: static constexpr const char *activityType = "SELECT"; + SelectActivity(): UIActivity(activityType) {} void execute() { //TODO: build contextual menu diff --git a/src/UIActivity.cpp b/src/UIActivity.cpp index 27c93f1..6402f11 100644 --- a/src/UIActivity.cpp +++ b/src/UIActivity.cpp @@ -6,10 +6,15 @@ UIActivity::UIActivity(const std::string &type) : Activity(type) { +} + +void UIActivity::initialize() +{ auto cond = std::unique_ptr>(new ConditionAll); ScreenScannerManager::instance().attach(shared_from_this(), std::move(cond)); } + UIActivity::~UIActivity() { ScreenScannerManager::instance().detach(this, nullptr); diff --git a/src/UIActivity.hpp b/src/UIActivity.hpp index 3c7b9f2..28ccc1d 100644 --- a/src/UIActivity.hpp +++ b/src/UIActivity.hpp @@ -12,6 +12,8 @@ class UIActivity : public Activity, public UIElementObserver, public std::enable public: UIActivity(const std::string &type); ~UIActivity(); + + void initialize() override; bool prepare() override; void update(const std::shared_ptr &elem) override; -- 2.7.4 From 42d67aefe5376273d4461d7ccf4071156c72baec Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Stawicki?= Date: Tue, 13 Jun 2017 14:20:52 +0200 Subject: [PATCH 05/16] Move ecore_init,ecore_shutdown to main function Workaround for errors generated by multiple ecore_init calls, issue doesn't exist from efl 1.18. Change-Id: I6f6925b5c31bd149c5103c47d030bdb2ddd9b75c --- packaging/org.tizen.universal-switch.spec | 2 ++ tests/ActivityDispatchingTests.cpp | 12 ++++++++++++ tests/ConfigurationTests.cpp | 11 +++++++++++ tests/ConfigurationTests.hpp | 9 +-------- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/packaging/org.tizen.universal-switch.spec b/packaging/org.tizen.universal-switch.spec index 9ce4891..a0759cf 100644 --- a/packaging/org.tizen.universal-switch.spec +++ b/packaging/org.tizen.universal-switch.spec @@ -13,6 +13,8 @@ BuildRequires: pkgconfig(ecore) BuildRequires: pkgconfig(elementary) BuildRequires: pkgconfig(sqlite3) BuildRequires: pkgconfig(libtzplatform-config) +#Required for tests +BuildRequires: net-config %description An utility library for developers of the menu screen. diff --git a/tests/ActivityDispatchingTests.cpp b/tests/ActivityDispatchingTests.cpp index b6db195..f33a2c2 100644 --- a/tests/ActivityDispatchingTests.cpp +++ b/tests/ActivityDispatchingTests.cpp @@ -32,3 +32,15 @@ TEST_F(ActivityDispatchingFixture, ActivityAbandoned) simulateKeyDown(keyMappedToSelectActivity_); EXPECT_EQ(adHocActivityDispatcher_->updatedWithActivity, 0); } + + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + EXPECT_TRUE(ecore_init()); + EXPECT_TRUE(ecore_event_init()); + int r = RUN_ALL_TESTS(); + ecore_event_shutdown(); + ecore_shutdown(); + return r; +} \ No newline at end of file diff --git a/tests/ConfigurationTests.cpp b/tests/ConfigurationTests.cpp index c2a64f1..64a2e7a 100644 --- a/tests/ConfigurationTests.cpp +++ b/tests/ConfigurationTests.cpp @@ -43,3 +43,14 @@ TEST_F(ConfigurationTestsFixture, MappingAddedAndUpdated_BeUpdatedWithNewValue) EXPECT_EQ(switchManager->updatedWithSwitch, 1); EXPECT_EQ(switchManager->receivedItem->getId()->getGlobalId(), switchId_); } + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + EXPECT_TRUE(ecore_init()); + EXPECT_TRUE(ecore_event_init()); + int r = RUN_ALL_TESTS(); + ecore_event_shutdown(); + ecore_shutdown(); + return r; +} \ No newline at end of file diff --git a/tests/ConfigurationTests.hpp b/tests/ConfigurationTests.hpp index bcf6287..cc6f87f 100644 --- a/tests/ConfigurationTests.hpp +++ b/tests/ConfigurationTests.hpp @@ -23,8 +23,6 @@ class ConfigurationTestsFixture : public ::testing::Test public: void SetUp() { - EXPECT_TRUE(ecore_init()); - EXPECT_TRUE(ecore_event_init()); configuration_ = std::make_shared(true); switchProvider_ = std::make_shared(); activityFactory_ = ActivityFactory::getInstance(); @@ -32,18 +30,13 @@ public: item_ = std::make_shared(switch_->getId()->getGlobalId(), activityType_); } - void TearDown() - { - ecore_event_shutdown(); - ecore_shutdown(); - } - void simulateKeyDown(const std::string &key) { Ecore_Event_Key *event = (Ecore_Event_Key *)malloc(sizeof(Ecore_Event_Key)); event->key = key.c_str(); EXPECT_TRUE(ecore_event_add(ECORE_EVENT_KEY_DOWN, event, NULL, NULL)); ecore_main_loop_iterate(); + free(event); } protected: -- 2.7.4 From b4a0814af2854de0646317964b8dfcd35d045f90 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Stawicki?= Date: Tue, 13 Jun 2017 14:52:44 +0200 Subject: [PATCH 06/16] Unregister ecore event handler in ~AccessoriesSwitchProvider Change-Id: I5dcd1e8a1c0c97b59555dfa998cc00bb54ebf01e --- src/AccessoriesSwitchProvider.cpp | 10 +++++++--- src/AccessoriesSwitchProvider.hpp | 3 +++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/AccessoriesSwitchProvider.cpp b/src/AccessoriesSwitchProvider.cpp index a17784c..045fbee 100644 --- a/src/AccessoriesSwitchProvider.cpp +++ b/src/AccessoriesSwitchProvider.cpp @@ -1,7 +1,5 @@ #include "AccessoriesSwitchProvider.hpp" -#include - AccessoriesSwitchProvider::AccessoriesSwitchProvider() : SwitchProvider("AccessoriesSwitchProvider"), keys( {"XF86AddFavorite", @@ -680,9 +678,15 @@ AccessoriesSwitchProvider::AccessoriesSwitchProvider() for (auto &key : keys) switches.emplace_back(std::make_shared(key, switchProviderId)); - ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, onEcoreEventKeyDownCb, this); + ecoreHandler = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, onEcoreEventKeyDownCb, this); } +AccessoriesSwitchProvider::~AccessoriesSwitchProvider() +{ + ecore_event_handler_del(ecoreHandler); +} + + SwitchProvider::SwitchCollection AccessoriesSwitchProvider::findAllSwitches() const { return switches; diff --git a/src/AccessoriesSwitchProvider.hpp b/src/AccessoriesSwitchProvider.hpp index 2dd0011..23cd8a7 100644 --- a/src/AccessoriesSwitchProvider.hpp +++ b/src/AccessoriesSwitchProvider.hpp @@ -4,6 +4,7 @@ #include "SwitchProvider.hpp" #include +#include #include #include @@ -13,6 +14,7 @@ class AccessoriesSwitchProvider : public SwitchProvider { public: AccessoriesSwitchProvider(); + ~AccessoriesSwitchProvider() override; SwitchCollection findAllSwitches() const override; std::shared_ptr findSwitchById(const std::string &switchId) const override; @@ -22,6 +24,7 @@ private: const std::vector keys; SwitchCollection switches; + Ecore_Event_Handler *ecoreHandler; }; #endif -- 2.7.4 From 560ac91c376afa6bac33c5703916c483432280e4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Stawicki?= Date: Wed, 14 Jun 2017 11:19:04 +0200 Subject: [PATCH 07/16] bugfix: double memory free bugfix for commit: 42d67aefe5376273d4461d7ccf4071156c72baec "Move ecore_init,ecore_shutdown to main function" Change-Id: Ia272e0274b3a838b1eacadef0b17bd2dd7619a4a --- tests/ConfigurationTests.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/ConfigurationTests.hpp b/tests/ConfigurationTests.hpp index cc6f87f..72c842b 100644 --- a/tests/ConfigurationTests.hpp +++ b/tests/ConfigurationTests.hpp @@ -36,7 +36,6 @@ public: event->key = key.c_str(); EXPECT_TRUE(ecore_event_add(ECORE_EVENT_KEY_DOWN, event, NULL, NULL)); ecore_main_loop_iterate(); - free(event); } protected: -- 2.7.4 From 82a10878aad1b472eb28567057e2639b30afe087 Mon Sep 17 00:00:00 2001 From: Lukasz Wlazly Date: Fri, 16 Jun 2017 11:12:50 +0200 Subject: [PATCH 08/16] Unnecessary class removed After design change ActivityObserver class occured useless Change-Id: I0bb9457deeb48115cd8cc93beeb8e5b68d2269b8 --- src/ActivityDispatcher.hpp | 2 +- src/ActivityObserver.hpp | 11 ----------- 2 files changed, 1 insertion(+), 12 deletions(-) delete mode 100644 src/ActivityObserver.hpp diff --git a/src/ActivityDispatcher.hpp b/src/ActivityDispatcher.hpp index fc4cba7..4b93186 100644 --- a/src/ActivityDispatcher.hpp +++ b/src/ActivityDispatcher.hpp @@ -1,7 +1,7 @@ #ifndef ACTIVITY_DISPATCHER_HPP #define ACTIVITY_DISPATCHER_HPP -#include "ActivityObserver.hpp" +#include "Activity.hpp" #include diff --git a/src/ActivityObserver.hpp b/src/ActivityObserver.hpp deleted file mode 100644 index 2cce477..0000000 --- a/src/ActivityObserver.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef ACTIVITY_OBSERVER_HPP -#define ACTIVITY_OBSERVER_HPP - -#include "Observer.hpp" -#include "Activity.hpp" - -class ActivityObserver : public Observer -{ -}; - -#endif -- 2.7.4 From 9a74b45c9e2f0b149a9d55fd082ab9f5c12bdaff Mon Sep 17 00:00:00 2001 From: Lukasz Wlazly Date: Tue, 20 Jun 2017 12:39:35 +0200 Subject: [PATCH 09/16] Default scanning method This commit prevent from undefined state of scanningMethod Change-Id: Ib008b89c263d988d2913bd4654a8fcfcf15ac38c --- src/ScreenScannerManager.cpp | 1 + src/SwitchProvider.hpp | 1 + 2 files changed, 2 insertions(+) diff --git a/src/ScreenScannerManager.cpp b/src/ScreenScannerManager.cpp index 3dab35e..86d1d33 100644 --- a/src/ScreenScannerManager.cpp +++ b/src/ScreenScannerManager.cpp @@ -1,6 +1,7 @@ #include "ScreenScannerManager.hpp" ScreenScannerManager::ScreenScannerManager() + : scanningMethod(ScanningMethod::ROW) { //TODO: register for scanning parameteres in vconf. // vconf key are defined in universal-switch UI in settings information diff --git a/src/SwitchProvider.hpp b/src/SwitchProvider.hpp index 5c204ef..a1d1435 100644 --- a/src/SwitchProvider.hpp +++ b/src/SwitchProvider.hpp @@ -13,6 +13,7 @@ class SwitchProvider : public Subject public: using SwitchCollection = std::vector>; + SwitchProvider() = delete; virtual ~SwitchProvider() {}; virtual SwitchCollection findAllSwitches() const = 0; virtual std::shared_ptr findSwitchById(const std::string &switchId) const = 0; -- 2.7.4 From d3bd5e909858fef3baf971c58c8807e9933772db Mon Sep 17 00:00:00 2001 From: Lukasz Date: Tue, 20 Jun 2017 11:36:29 +0200 Subject: [PATCH 10/16] Test case documentation added. Change-Id: Ifb9902359085652310b078c502c6b8704cadf65c --- tests/ActivityDispatchingTests.cpp | 18 ++++++++++++++++++ tests/ConfigurationTests.cpp | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/tests/ActivityDispatchingTests.cpp b/tests/ActivityDispatchingTests.cpp index f33a2c2..16af746 100644 --- a/tests/ActivityDispatchingTests.cpp +++ b/tests/ActivityDispatchingTests.cpp @@ -4,17 +4,31 @@ #include +/* + This test verifies, if auto-registration of activity types in ActivityFactory was performed on application bootstrap. +*/ + TEST_F(ActivityDispatchingFixture, FactoryWorks) { EXPECT_TRUE(ActivityFactory::getInstance()->getActivity(activityType_)); } +/* + This test verifies, that single usage of switch mapped to "Select" activity is not enough to build SelectActivity object. + SelectActivity is an UIActivity which requires UIElement and both scanning methods (Point, Row) requires at least two switch activations to provide UIElement. +*/ + TEST_F(ActivityDispatchingFixture, ActivityNotPrepared) { initialEnvironmentExpect(); EXPECT_EQ(adHocActivityDispatcher_->updatedWithActivity, 0); } +/* + This test verifies, that two subsequent usages of switch mapped to "Select" activity is enough to build SelectActivity object. + SelectActivity is an UIActivity which requires UIElement and both scanning methods (Point, Row) requires at least two switch activations to provide UIElement. +*/ + TEST_F(ActivityDispatchingFixture, ActivityReady) { initialEnvironmentExpect(); @@ -23,6 +37,10 @@ TEST_F(ActivityDispatchingFixture, ActivityReady) EXPECT_EQ(adHocActivityDispatcher_->updatedWithActivity, 1); } +/* + This test verifies, that activation of switch mapped to activity A abandons activity building process being in progress for activity B. +*/ + TEST_F(ActivityDispatchingFixture, ActivityAbandoned) { initialEnvironmentExpect(); diff --git a/tests/ConfigurationTests.cpp b/tests/ConfigurationTests.cpp index 64a2e7a..13728b2 100644 --- a/tests/ConfigurationTests.cpp +++ b/tests/ConfigurationTests.cpp @@ -1,5 +1,9 @@ #include "ConfigurationTests.hpp" +/* + This test case verifies, if Universal Switch can load existing configuration of switch to activity mapping during application bootstrap + (no User interaction with Settings app) and registers relevant switch event listeners and detect event instance occurence on simulated switch activation. +*/ TEST_F(ConfigurationTestsFixture, ConfigurationNotEmpty_BeUpdatedOnEvent) { configuration_->add(item_); @@ -11,6 +15,11 @@ TEST_F(ConfigurationTestsFixture, ConfigurationNotEmpty_BeUpdatedOnEvent) EXPECT_EQ(switchManager->receivedItem->getId()->getGlobalId(), switchId_); } +/* + This test case verifies, if Universal Switch can detect simulated change in configuration of switch to activity mapping (new item added) + and registers relevant event listener and detect event instance occurence on simulated switch activation. +*/ + TEST_F(ConfigurationTestsFixture, ConfigurationEmpty_MappingAdded_BeUpdatedOnEvent) { auto switchManager = SwitchManager::create(switchProvider_, configuration_, activityFactory_, nullptr); @@ -21,6 +30,11 @@ TEST_F(ConfigurationTestsFixture, ConfigurationEmpty_MappingAdded_BeUpdatedOnEve EXPECT_EQ(switchManager->receivedItem->getId()->getGlobalId(), switchId_); } +/* + This test case verifies, if Universal Switch can detect simulated change in configuration of switch to activity mapping (removal of existing item) + and deregisters relevant event listener and do not detect event instance occurence on simulated switch activation. +*/ + TEST_F(ConfigurationTestsFixture, MappingAddedAndRemoved_BeNotUpdatedOnEvent) { configuration_->add(item_); @@ -31,6 +45,11 @@ TEST_F(ConfigurationTestsFixture, MappingAddedAndRemoved_BeNotUpdatedOnEvent) EXPECT_EQ(switchManager->updatedWithSwitch, 0); } +/* + This test case verifies, if Universal Switch can detect simulated change in configuration of switch to activity mapping (update of existing item, like activity change) + and do not deregisters relevant event listener (listener should remain registered) and detect event instance occurence on simulated switch activation. +*/ + TEST_F(ConfigurationTestsFixture, MappingAddedAndUpdated_BeUpdatedWithNewValue) { configuration_->add(item_); -- 2.7.4 From 1a6f8c89b02b24123e4d4a69aab5665d0d8ca1aa Mon Sep 17 00:00:00 2001 From: Lukasz Wlazly Date: Tue, 20 Jun 2017 10:43:48 +0200 Subject: [PATCH 11/16] Wrapper class for elm_window object Provided class is singleton with lazy initialization. It can catch all gestures or pass them to the application below Change-Id: Ic8bd7ca899b8f557a0d03db2ab0e7f1d7e1d5d09 --- CMakeLists.txt | 2 + org.tizen.universal-switch.xml | 14 ++++--- packaging/org.tizen.universal-switch.spec | 3 ++ src/CMakeLists.txt | 1 + src/Window.cpp | 56 ++++++++++++++++++++++++++++ src/Window.hpp | 29 +++++++++++++++ src/universalswitch.cpp | 62 +++++++++---------------------- 7 files changed, 117 insertions(+), 50 deletions(-) create mode 100644 src/Window.cpp create mode 100644 src/Window.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 21b48c9..cec670f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,8 @@ pkg_check_modules(pkgs REQUIRED ecore sqlite3 atspi-2 + capi-ui-efl-util + efl-extension ) SET(COMMON_FLAGS "-fdiagnostics-color=always -fPIC") diff --git a/org.tizen.universal-switch.xml b/org.tizen.universal-switch.xml index 2aa8dd0..9b02ae9 100644 --- a/org.tizen.universal-switch.xml +++ b/org.tizen.universal-switch.xml @@ -1,8 +1,12 @@ - + - - universalswitch.png - - + + universal-switch.png + + + + http://tizen.org/privilege/window.priority.set + + diff --git a/packaging/org.tizen.universal-switch.spec b/packaging/org.tizen.universal-switch.spec index a0759cf..d6c10be 100644 --- a/packaging/org.tizen.universal-switch.spec +++ b/packaging/org.tizen.universal-switch.spec @@ -13,6 +13,9 @@ BuildRequires: pkgconfig(ecore) BuildRequires: pkgconfig(elementary) BuildRequires: pkgconfig(sqlite3) BuildRequires: pkgconfig(libtzplatform-config) +BuildRequires: pkgconfig(efl-extension) +BuildRequires: pkgconfig(capi-ui-efl-util) + #Required for tests BuildRequires: net-config diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2383e17..2336046 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,6 +15,7 @@ SET(SRCS SelectActivity.cpp ActivityFactory.cpp UIActivity.cpp + Window.cpp ) ADD_LIBRARY(universal-switch-obj OBJECT ${SRCS}) diff --git a/src/Window.cpp b/src/Window.cpp new file mode 100644 index 0000000..f53149c --- /dev/null +++ b/src/Window.cpp @@ -0,0 +1,56 @@ +#include "Window.hpp" + +#include "Ecore_Wayland.h" +#include "UniversalSwitchLog.hpp" + +std::shared_ptr Window::instance() +{ + static auto instance = std::shared_ptr(new Window); + return instance; +} + +std::shared_ptr Window::getHandler() +{ + return Window::instance()->handler; +} + +void Window::inputRegionOn() +{ + ecore_wl_window_input_region_set(waylandWindow, 0, 0, screenWidth, screenHeight); +} + +void Window::inputRegionOff() +{ + ecore_wl_window_input_region_set(waylandWindow, 0, 0, 0, 0); +} + +int Window::getWidth() +{ + return screenWidth; +} + +int Window::getHeight() +{ + return screenHeight; +} + +Window::Window() + : screenWidth(0), screenHeight(0) +{ + auto windowHandler = elm_win_add(NULL, "universal-switch", ELM_WIN_NOTIFICATION); + if (!windowHandler) { + ERROR("Window cannot be created"); + exit(1); + } + + efl_util_set_notification_window_level(windowHandler, EFL_UTIL_NOTIFICATION_LEVEL_3); + elm_win_override_set(windowHandler, EINA_TRUE); + elm_win_alpha_set(windowHandler, EINA_TRUE); + + elm_win_screen_size_get(windowHandler, NULL, NULL, &screenWidth, &screenHeight); + evas_object_resize(windowHandler, screenWidth, screenHeight); + evas_object_show(windowHandler); + + waylandWindow = elm_win_wl_window_get(windowHandler); + handler = std::shared_ptr(windowHandler, evas_object_del); +} diff --git a/src/Window.hpp b/src/Window.hpp new file mode 100644 index 0000000..eb96451 --- /dev/null +++ b/src/Window.hpp @@ -0,0 +1,29 @@ +#ifndef WINDOW_HPP +#define WINDOW_HPP + +#include +#include +#include + +#include + +class Window +{ +public: + static std::shared_ptr instance(); + static std::shared_ptr getHandler(); + void inputRegionOn(); + void inputRegionOff(); + int getWidth(); + int getHeight(); + +private: + Window(); + + std::shared_ptr handler; + Ecore_Wl_Window *waylandWindow; + int screenWidth; + int screenHeight; +}; + +#endif diff --git a/src/universalswitch.cpp b/src/universalswitch.cpp index 0e607ff..b3627f0 100644 --- a/src/universalswitch.cpp +++ b/src/universalswitch.cpp @@ -1,69 +1,41 @@ #include "UniversalSwitchLog.hpp" -#include "SQLiteConfiguration.hpp" +#include "Window.hpp" #include -#include +#include +#include -#include -bool service_app_create(void *data) +bool app_create(void *data) { - // Todo: add your code here. + DEBUG("App create"); + return true; } -void service_app_terminate(void *data) +void app_terminate(void *data) { - // Todo: add your code here. - return; -} + DEBUG("app termination procedure"); -void service_app_control(app_control_h app_control, void *data) -{ - // Todo: add your code here. return; } -static void -service_app_lang_changed(app_event_info_h event_info, void *user_data) +void app_control(app_control_h app_control, void *data) { - /*APP_EVENT_LANGUAGE_CHANGED*/ return; } -static void -service_app_region_changed(app_event_info_h event_info, void *user_data) -{ - /*APP_EVENT_REGION_FORMAT_CHANGED*/ -} - -static void -service_app_low_battery(app_event_info_h event_info, void *user_data) -{ - /*APP_EVENT_LOW_BATTERY*/ -} - -static void -service_app_low_memory(app_event_info_h event_info, void *user_data) -{ - /*APP_EVENT_LOW_MEMORY*/ -} - int main(int argc, char *argv[]) { DEBUG("main function called"); - char ad[50] = {0,}; - service_app_lifecycle_callback_s event_callback; - app_event_handler_h handlers[5] = {NULL, }; - - event_callback.create = service_app_create; - event_callback.terminate = service_app_terminate; - event_callback.app_control = service_app_control; - service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad); - service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad); - service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad); - service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad); + ui_app_lifecycle_callback_s event_callback = { + .create = app_create, + .terminate = app_terminate, + .pause = nullptr, + .resume = nullptr, + .app_control = app_control + }; - return service_app_main(argc, argv, &event_callback, ad); + return ui_app_main(argc, argv, &event_callback, nullptr); } -- 2.7.4 From 93cef4ad4fa7c086684ad8a07be943eec0e0c785 Mon Sep 17 00:00:00 2001 From: Mariusz Wachowicz Date: Tue, 23 May 2017 14:40:23 +0200 Subject: [PATCH 12/16] PointScanner & ScreenScannerManager provide information about selected position on screen TODO: tests do not pass due to problems with vconf configuration in test environment Change-Id: Ic5709b18806602d84028d4a56d8d9f832af6d182 --- packaging/org.tizen.universal-switch.spec | 2 +- src/CMakeLists.txt | 3 + src/PointScanner.cpp | 197 +++++++++++++++++++++++++ src/PointScanner.hpp | 12 ++ src/ScanningProperties.cpp | 238 ++++++++++++++++++++++++++++++ src/ScanningProperties.hpp | 77 ++++++++++ src/ScreenScanner.hpp | 16 +- src/ScreenScannerManager.cpp | 88 +++++++++-- src/ScreenScannerManager.hpp | 30 ++-- src/UIElement.cpp | 14 +- src/UIElement.hpp | 13 +- src/UniversalSwitchLog.hpp | 10 ++ src/universalswitch.cpp | 6 + 13 files changed, 664 insertions(+), 42 deletions(-) create mode 100644 src/PointScanner.cpp create mode 100644 src/PointScanner.hpp create mode 100644 src/ScanningProperties.cpp create mode 100644 src/ScanningProperties.hpp diff --git a/packaging/org.tizen.universal-switch.spec b/packaging/org.tizen.universal-switch.spec index d6c10be..b75b657 100644 --- a/packaging/org.tizen.universal-switch.spec +++ b/packaging/org.tizen.universal-switch.spec @@ -11,11 +11,11 @@ BuildRequires: cmake BuildRequires: pkgconfig(capi-appfw-service-application) BuildRequires: pkgconfig(ecore) BuildRequires: pkgconfig(elementary) +BuildRequires: pkgconfig(efl-extension) BuildRequires: pkgconfig(sqlite3) BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(efl-extension) BuildRequires: pkgconfig(capi-ui-efl-util) - #Required for tests BuildRequires: net-config diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2336046..bdb1988 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,10 +12,13 @@ SET(SRCS SwitchManager.cpp SwitchId.cpp ScreenScannerManager.cpp + ScanningProperties.cpp SelectActivity.cpp ActivityFactory.cpp UIActivity.cpp Window.cpp + UIElement.cpp + PointScanner.cpp ) ADD_LIBRARY(universal-switch-obj OBJECT ${SRCS}) diff --git a/src/PointScanner.cpp b/src/PointScanner.cpp new file mode 100644 index 0000000..376ae76 --- /dev/null +++ b/src/PointScanner.cpp @@ -0,0 +1,197 @@ +#include "UniversalSwitchLog.hpp" +#include "PointScanner.hpp" +#include "Window.hpp" + +#include + +static const int DEFAULT_LINE_WIDTH = 10; +static const int SPEED_UNIT_FACTOR = 10; + +class PointScannerImpl : public ScreenScanner +{ +public: + PointScannerImpl(ScanningProperties &properties); + virtual ~PointScannerImpl(); + virtual std::shared_ptr acceptAutoscanning(); + +private: + enum class State {END, VERTICAL, HORIZONTAL, NONE}; + State state = State::NONE; + + class TransitData + { + public: + Elm_Transit *transit = nullptr; + Evas_Object *line = nullptr; + Evas_Object *rect = nullptr; + + TransitData() = default; + TransitData(Elm_Transit *t, Evas_Object *l, Evas_Object *r); + + void reset(); + }; + TransitData horizontal; + TransitData vertical; + int pointX = 0; + int pointY = 0; + + TransitData createLine(int x0, int y0, int x1, int y1, int dx, int dy); + void startHorizontalLine(); + void stopHorizontalLine(); + void startVerticalLine(); + void stopVerticalLine(); +}; + +std::unique_ptr PointScanner::instance(ScanningProperties &properties) +{ + return std::unique_ptr(new PointScannerImpl(properties)); +} + +PointScannerImpl::PointScannerImpl(ScanningProperties &properties) +{ + directionVertical = properties.getDirectionVertical(); + directionHorizontal = properties.getDirectionHorizontal(); + loopLimitToInaction = properties.getLoopLimitToInaction(); + speed = properties.getSpeed(); +} + +PointScannerImpl::~PointScannerImpl() +{ + horizontal.reset(); + vertical.reset(); +} + +std::shared_ptr PointScannerImpl::acceptAutoscanning() +{ + switch (state) { + case State::NONE: + startHorizontalLine(); + state = State::HORIZONTAL; + break; + case State::HORIZONTAL: + stopHorizontalLine(); + startVerticalLine(); + state = State::VERTICAL; + break; + case State::VERTICAL: + stopVerticalLine(); + + DEBUG("Scanning complete. Position x = %d y = %d", pointX, pointY); + + horizontal.reset(); + vertical.reset(); + + state = State::END; + return std::make_shared(pointX, pointY); + default: + ERROR("default case shuld not be reached"); + return nullptr; + } + + return nullptr; +} + +PointScannerImpl::TransitData PointScannerImpl::createLine(int x0, int y0, int x1, int y1, int dx, int dy) +{ + auto evasCanvas = evas_object_evas_get(Window::instance()->getHandler().get()); + auto rect = evas_object_rectangle_add(evasCanvas); + if (y0 == y1) { //horiziontal line + evas_object_move(rect, x0, y0 - DEFAULT_LINE_WIDTH / 2); + evas_object_resize(rect, x1, DEFAULT_LINE_WIDTH); + } else if (x0 == x1) { //vertical line + evas_object_move(rect, x0 - DEFAULT_LINE_WIDTH / 2, y0); + evas_object_resize(rect, DEFAULT_LINE_WIDTH, y1); + } else { + ERROR("Unhandled line animation"); + } + evas_object_color_set(rect, 0, 255, 0, 255); + evas_object_show(rect); + + auto line = evas_object_line_add(evasCanvas); + evas_object_line_xy_set(line, x0, y0, x1, y1); + evas_object_color_set(line, 255, 0, 0, 255); + evas_object_show(line); + + auto transit = elm_transit_add(); + elm_transit_object_add(transit, line); + elm_transit_object_add(transit, rect); + elm_transit_effect_translation_add(transit, 0, 0, dx, dy); + int distance = std::max(std::abs(dx), std::abs(dy)); + ASSERT(speed > 0); + double time = distance / (speed * SPEED_UNIT_FACTOR); + ASSERT(loopLimitToInaction > 0); + inactivityCbTime = 2 * time * loopLimitToInaction; //this parameter is used for calculation of disable time + elm_transit_duration_set(transit, time); + elm_transit_auto_reverse_set(transit, EINA_TRUE); + elm_transit_repeat_times_set(transit, -1); + elm_transit_go(transit); + + return TransitData(transit, line, rect); +} + +void PointScannerImpl::startVerticalLine() +{ + DEBUG("Starting vertical line"); + auto screenWidth = Window::instance()->getWidth(); + auto screenHeight = Window::instance()->getHeight(); + switch (directionHorizontal) { + case HorizontalScanningDirection::TO_RIGHT: + vertical = createLine(0, 0, 0, screenHeight, screenWidth, 0); + break; + case HorizontalScanningDirection::TO_LEFT: + vertical = createLine(screenWidth, 0, screenWidth, screenHeight, -screenWidth, 0); + break; + default: + ASSERT(0, "Unhandled direction"); + break; + } +} + +void PointScannerImpl::stopVerticalLine() +{ + elm_transit_paused_set(vertical.transit, true); + evas_object_geometry_get(vertical.line, &pointX, nullptr, nullptr, nullptr); +} + +void PointScannerImpl::startHorizontalLine() +{ + DEBUG("Starting horizontal line"); + auto screenWidth = Window::instance()->getWidth(); + auto screenHeight = Window::instance()->getHeight(); + switch (directionVertical) { + case VerticalScanningDirection::TO_BOTTOM: + horizontal = createLine(0, 0, screenWidth, 0, 0, screenHeight); + break; + case VerticalScanningDirection::TO_TOP: + horizontal = createLine(0, screenHeight, screenWidth, screenHeight, 0, -screenHeight); + break; + default: + ASSERT(0, "Unhandled direction"); + break; + } +} + +void PointScannerImpl::stopHorizontalLine() +{ + elm_transit_paused_set(horizontal.transit, true); + evas_object_geometry_get(horizontal.line, nullptr, &pointY, nullptr, nullptr); +} + +PointScannerImpl::TransitData::TransitData(Elm_Transit *t, Evas_Object *l, Evas_Object *r) + : transit(t), line(l), rect(r) {} + +void PointScannerImpl::TransitData::reset() +{ + if (transit) { + elm_transit_del(transit); + transit = nullptr; + } + if (line) { + evas_object_del(line); + line = nullptr; + } + if (rect) { + evas_object_del(rect); + rect = nullptr; + } +} diff --git a/src/PointScanner.hpp b/src/PointScanner.hpp new file mode 100644 index 0000000..637c48d --- /dev/null +++ b/src/PointScanner.hpp @@ -0,0 +1,12 @@ +#ifndef SCREEN_SCANNER_POINT_HPP +#define SCREEN_SCANNER_POINT_HPP + +#include "ScreenScanner.hpp" + +class PointScanner +{ +public: + static std::unique_ptr instance(ScanningProperties &properties); +}; + +#endif \ No newline at end of file diff --git a/src/ScanningProperties.cpp b/src/ScanningProperties.cpp new file mode 100644 index 0000000..46b67b4 --- /dev/null +++ b/src/ScanningProperties.cpp @@ -0,0 +1,238 @@ +#include "ScanningProperties.hpp" +#include "UniversalSwitchLog.hpp" +#include + +#define VCONF_PREFIX "" + +namespace +{ + template + void updateData(keynode_t *key, void *data, CastFunctionType castFunction, SetMethodType setMethod) + { + auto val = vconf_keynode_get_int(key); + + DEBUG("Vconf key name: %s has value: %d\n", vconf_keynode_get_name(key), val); + + auto castedData = castFunction(val); + auto obj = reinterpret_cast(data); + std::bind(setMethod, obj, castedData)(); + } + + template + void updateData(std::string key, ScanningProperties *obj, CastFunctionType castFunction, SetMethodType setMethod) + { + int val = 0; + auto status = vconf_get_int(key.c_str(), &val); + if (status != 0) + ERROR("Vconf error: cant get value for key: %s", key.c_str()); + auto castedData = castFunction(val); + std::bind(setMethod, obj, castedData)(); + } + + ScanningMethod toScanningMethod(int val) + { + switch (val) { + case 1: + return ScanningMethod::POINT; + case 2: + return ScanningMethod::ROW; + default: + ERROR("Unknown scanning method. Default method used"); + return ScanningMethod::POINT; + } + } + + VerticalScanningDirection toScanningDirectionVertical(int val) + { + switch (val) { + case 1: + return VerticalScanningDirection::TO_BOTTOM; + case 2: + return VerticalScanningDirection::TO_TOP; + default: + ERROR("Unknown scanning direction. Default direction used"); + return VerticalScanningDirection::TO_BOTTOM; + } + } + + HorizontalScanningDirection toScanningDirectionHorizontal(int val) + { + switch (val) { + case 1: + return HorizontalScanningDirection::TO_RIGHT; + case 2: + return HorizontalScanningDirection::TO_LEFT; + default: + ERROR("Unknown scanning direction. Default direction used"); + return HorizontalScanningDirection::TO_RIGHT; + } + } + + int toInt(int val) + { + return val; + } +} + +ScanningProperties &ScanningProperties::instance() +{ + static ScanningProperties instance; + return instance; +} + +ScanningProperties::ScanningProperties() +{ + registerCallbacks(); + getScanningProperties(); +} + +ScanningProperties::~ScanningProperties() +{ + unregisterCallbacks(); +} + +ScanningMethod ScanningProperties::getMethod() +{ + if (localMethodActive) { + localMethodActive = false; + return methodLocal; + } + return method; +} + +VerticalScanningDirection ScanningProperties::getDirectionVertical() +{ + return directionVertical; +} + +HorizontalScanningDirection ScanningProperties::getDirectionHorizontal() +{ + return directionHorizontal; +} + +int ScanningProperties::getLoopLimitToInaction() +{ + return loopLimitToInaction; +} + +int ScanningProperties::getSpeed() +{ + return speed; +} + +int ScanningProperties::getJumpTime() +{ + return jumpTime; +} + +int ScanningProperties::getJumpTimeExtend() +{ + return jumpTimeExtend; +} + +void ScanningProperties::setLocalScanningMethod(ScanningMethod method) +{ + methodLocal = method; + localMethodActive = true; +} + +void ScanningProperties::registerCallbacks() +{ + vconf_notify_key_changed(VCONF_PREFIX "__KEY_ACCESSIBILITY_UNIVERSAL_SWITCH_SETTINGS_SCAN_METHOD", methodCb, this); + vconf_notify_key_changed(VCONF_PREFIX "__KEY_ACCESSIBILITY_UNIVERSAL_SWITCH_SETTINGS_SCAN_DIR_VERTICAL", directionVerticalCb, this); + vconf_notify_key_changed(VCONF_PREFIX "__KEY_ACCESSIBILITY_UNIVERSAL_SWITCH_SETTINGS_SCAN_DIR_HORIZONTAL", directionHorizontalCb, this); + vconf_notify_key_changed(VCONF_PREFIX "__KEY_ACCESSIBILITY_UNIVERSAL_SWITCH_SETTINGS_NUM_OF_SCANS", loopLimitToInactionCb, this); + vconf_notify_key_changed(VCONF_PREFIX "__KEY_ACCESSIBILITY_UNIVERSAL_SWITCH_SETTINGS_SCAN_PT_SPEED", speedCb, this); + //TODO: add keys for RowScanner +} + +void ScanningProperties::unregisterCallbacks() +{ + vconf_ignore_key_changed(VCONF_PREFIX "__KEY_ACCESSIBILITY_UNIVERSAL_SWITCH_SETTINGS_SCAN_METHOD", methodCb); + vconf_ignore_key_changed(VCONF_PREFIX "__KEY_ACCESSIBILITY_UNIVERSAL_SWITCH_SETTINGS_SCAN_DIR_VERTICAL", directionVerticalCb); + vconf_ignore_key_changed(VCONF_PREFIX "__KEY_ACCESSIBILITY_UNIVERSAL_SWITCH_SETTINGS_SCAN_DIR_HORIZONTAL", directionHorizontalCb); + vconf_ignore_key_changed(VCONF_PREFIX "__KEY_ACCESSIBILITY_UNIVERSAL_SWITCH_SETTINGS_NUM_OF_SCANS", loopLimitToInactionCb); + vconf_ignore_key_changed(VCONF_PREFIX "__KEY_ACCESSIBILITY_UNIVERSAL_SWITCH_SETTINGS_SCAN_PT_SPEED", speedCb); + //TODO: add keys for RowScanner +} + +void ScanningProperties::getScanningProperties() +{ + updateData(VCONF_PREFIX "__KEY_ACCESSIBILITY_UNIVERSAL_SWITCH_SETTINGS_SCAN_METHOD", this, toScanningMethod, &ScanningProperties::setCachedScanningMethod); + + updateData(VCONF_PREFIX "__KEY_ACCESSIBILITY_UNIVERSAL_SWITCH_SETTINGS_SCAN_DIR_VERTICAL", this, toScanningDirectionVertical, &ScanningProperties::setCachedScanningDirectionVertical); + + updateData(VCONF_PREFIX "__KEY_ACCESSIBILITY_UNIVERSAL_SWITCH_SETTINGS_SCAN_DIR_HORIZONTAL", this, toScanningDirectionHorizontal, &ScanningProperties::setCachedScanningDirectionHorizontal); + + updateData(VCONF_PREFIX "__KEY_ACCESSIBILITY_UNIVERSAL_SWITCH_SETTINGS_NUM_OF_SCANS", this, toInt, &ScanningProperties::setCachedLoopLimitToInaction); + + updateData(VCONF_PREFIX "__KEY_ACCESSIBILITY_UNIVERSAL_SWITCH_SETTINGS_SCAN_PT_SPEED", this, toInt, &ScanningProperties::setCachedScanningSpeed); + + //TODO: add keys for RowScanner + setCachedScanningJumpTime(0); + setCachedScanningJumpTimeExtended(0); +} + +void ScanningProperties::methodCb(keynode_t *key, void *data) +{ + updateData(key, data, toScanningMethod, &ScanningProperties::setCachedScanningMethod); +} + +void ScanningProperties::directionVerticalCb(keynode_t *key, void *data) +{ + updateData(key, data, toScanningDirectionVertical, &ScanningProperties::setCachedScanningDirectionVertical); +} + +void ScanningProperties::directionHorizontalCb(keynode_t *key, void *data) +{ + updateData(key, data, toScanningDirectionHorizontal, &ScanningProperties::setCachedScanningDirectionHorizontal); +} + +void ScanningProperties::loopLimitToInactionCb(keynode_t *key, void *data) +{ + updateData(key, data, toInt, &ScanningProperties::setCachedLoopLimitToInaction); +} + +void ScanningProperties::speedCb(keynode_t *key, void *data) +{ + updateData(key, data, toInt, &ScanningProperties::setCachedScanningSpeed); +} + +void ScanningProperties::setCachedScanningMethod(ScanningMethod mode) +{ + method = mode; +} + +void ScanningProperties::setCachedScanningDirectionVertical(VerticalScanningDirection direction) +{ + directionVertical = direction; +} + +void ScanningProperties::setCachedScanningDirectionHorizontal(HorizontalScanningDirection direction) +{ + directionHorizontal = direction; +} + +void ScanningProperties::setCachedLoopLimitToInaction(int limit) +{ + loopLimitToInaction = (limit < 1) ? DEFAULT_LOOP_COUNT_TO_INACTION : limit; + DEBUG("passed: %d, current: %d", limit, loopLimitToInaction); +} + +void ScanningProperties::setCachedScanningSpeed(int velocity) +{ + speed = (velocity <= 0) ? DEFAULT_SPEED : velocity; + DEBUG("passed: %d, current: %d", velocity, speed); +} + +void ScanningProperties::setCachedScanningJumpTime(int time) +{ + jumpTime = (time <= 0) ? DEFAULT_TIME : time; + DEBUG("passed: %d, current: %d", time, jumpTime); +} + +void ScanningProperties::setCachedScanningJumpTimeExtended(int time) +{ + jumpTimeExtend = (time <= 0) ? 0 : time; + DEBUG("passed: %d, current: %d", time, jumpTimeExtend); +} diff --git a/src/ScanningProperties.hpp b/src/ScanningProperties.hpp new file mode 100644 index 0000000..84995fe --- /dev/null +++ b/src/ScanningProperties.hpp @@ -0,0 +1,77 @@ +#ifndef SCANNING_PROPERTIES_HPP +#define SCANNING_PROPERTIES_HPP + +#include "vconf.h" + +enum class ScanningMethod { + POINT, + ROW +}; + +enum class VerticalScanningDirection { + TO_BOTTOM, + TO_TOP +}; + +enum class HorizontalScanningDirection { + TO_RIGHT, + TO_LEFT +}; + +class ScanningProperties +{ +public: + static ScanningProperties &instance(); + + ScanningMethod getMethod(); + VerticalScanningDirection getDirectionVertical(); + HorizontalScanningDirection getDirectionHorizontal(); + int getLoopLimitToInaction(); + int getSpeed(); + int getJumpTime(); + int getJumpTimeExtend(); + + void setLocalScanningMethod(ScanningMethod method); + +private: + ScanningProperties(); + ScanningProperties(const ScanningProperties &) = delete; + ScanningProperties(ScanningProperties &&) = delete; + ScanningProperties &operator=(const ScanningProperties &) = delete; + ScanningProperties &operator=(ScanningProperties &&) = delete; + ~ScanningProperties(); + + void registerCallbacks(); + void unregisterCallbacks(); + void getScanningProperties(); + + static void methodCb(keynode_t *key, void *data); + static void directionVerticalCb(keynode_t *key, void *data); + static void directionHorizontalCb(keynode_t *key, void *data); + static void loopLimitToInactionCb(keynode_t *key, void *data); + static void speedCb(keynode_t *key, void *data); + + void setCachedScanningMethod(ScanningMethod mode); + void setCachedScanningDirectionVertical(VerticalScanningDirection direction); + void setCachedScanningDirectionHorizontal(HorizontalScanningDirection direction); + void setCachedLoopLimitToInaction(int limit); + void setCachedScanningSpeed(int velocity); + void setCachedScanningJumpTime(int time); + void setCachedScanningJumpTimeExtended(int time); + + ScanningMethod method = ScanningMethod::POINT; + ScanningMethod methodLocal = method; + bool localMethodActive = false; + VerticalScanningDirection directionVertical = VerticalScanningDirection::TO_BOTTOM; + HorizontalScanningDirection directionHorizontal = HorizontalScanningDirection::TO_RIGHT; + int loopLimitToInaction = DEFAULT_LOOP_COUNT_TO_INACTION; + int speed = 0; + int jumpTime = 0; + int jumpTimeExtend = 0; + + const int DEFAULT_SPEED = 30; + const int DEFAULT_TIME = 20; + const int DEFAULT_LOOP_COUNT_TO_INACTION = 5; +}; + +#endif diff --git a/src/ScreenScanner.hpp b/src/ScreenScanner.hpp index 84baf4d..3de61c2 100644 --- a/src/ScreenScanner.hpp +++ b/src/ScreenScanner.hpp @@ -1,6 +1,7 @@ #ifndef SCREEN_SCANNER_HPP #define SCREEN_SCANNER_HPP +#include "ScanningProperties.hpp" #include "UIElement.hpp" #include @@ -8,12 +9,23 @@ class ScreenScanner { public: - virtual ~ScreenScanner(); + virtual ~ScreenScanner() = default; virtual std::shared_ptr acceptAutoscanning() = 0; + double getinactivityCbTime() + { + return inactivityCbTime; + } + protected: - unsigned int speed; + VerticalScanningDirection directionVertical = VerticalScanningDirection::TO_BOTTOM; + HorizontalScanningDirection directionHorizontal = HorizontalScanningDirection::TO_RIGHT; + int loopLimitToInaction = 0; + int speed = 0; + int timeHandicap = 0; + + double inactivityCbTime = 0.0; }; #endif diff --git a/src/ScreenScannerManager.cpp b/src/ScreenScannerManager.cpp index 86d1d33..60ed4b2 100644 --- a/src/ScreenScannerManager.cpp +++ b/src/ScreenScannerManager.cpp @@ -1,11 +1,9 @@ #include "ScreenScannerManager.hpp" +#include "UniversalSwitchLog.hpp" +#include "Window.hpp" -ScreenScannerManager::ScreenScannerManager() - : scanningMethod(ScanningMethod::ROW) -{ - //TODO: register for scanning parameteres in vconf. - // vconf key are defined in universal-switch UI in settings information -} +static const double DELAYEDACCEPT_TIME_S = 0.2; +static const double TIMER_DISABLE = -1.0; ScreenScannerManager &ScreenScannerManager::instance() { @@ -13,37 +11,95 @@ ScreenScannerManager &ScreenScannerManager::instance() return *instance; } -void ScreenScannerManager::changeScanningMethod() +ScreenScannerManager::ScreenScannerManager() + : properties(ScanningProperties::instance()) { - + eext_object_event_callback_add(Window::instance()->getHandler().get(), EEXT_CALLBACK_BACK, restartCb, (void *) this); } -void ScreenScannerManager::changeScanningDirection() +ScreenScannerManager::~ScreenScannerManager() { - + setInactivityCbTime(TIMER_DISABLE); + eext_object_event_callback_del(Window::instance()->getHandler().get(), EEXT_CALLBACK_BACK, restartCb); } -void ScreenScannerManager::changeScanningSpeed(unsigned speed) + +void ScreenScannerManager::startAutoscanning() { + switch (properties.getMethod()) { + case ScanningMethod::POINT: + screenScanner.reset(PointScanner::instance(properties).release()); + ecore_timer_add(DELAYEDACCEPT_TIME_S, delayedAccept, (void *) this); //deley is required because of bad rendering + break; + case ScanningMethod::ROW: + //TODO: create RowScanner instance + break; + default: + ASSERT(0, "Invalid scanning method"); + } +} +void ScreenScannerManager::stopAutoscanning() +{ + screenScanner.reset(); } -void ScreenScannerManager::changeScanningLoopLimit(unsigned limit) +void ScreenScannerManager::acceptAutoscanning() { + if (!screenScanner) { + ERROR("no scanner selected"); + return; + } + auto element = screenScanner->acceptAutoscanning(); + if (!element) { + DEBUG("Element still not found"); + setInactivityCbTime(screenScanner->getinactivityCbTime()); + return; + } + DEBUG("Element found. x: %d y: %d", element->getX(), element->getY()); + stopAutoscanning(); + notify(element); +} +Eina_Bool ScreenScannerManager::delayedAccept(void *data) +{ + auto ssm = reinterpret_cast(data); + ssm->acceptAutoscanning(); //start scanning + ssm->setInactivityCbTime(ssm->screenScanner->getinactivityCbTime()); + return ECORE_CALLBACK_CANCEL; } -void ScreenScannerManager::startAutoscanning() +void ScreenScannerManager::changeScanningMethod(ScanningMethod mathod) { + properties.setLocalScanningMethod(mathod); +} +void ScreenScannerManager::restartCb(void *data, Evas_Object *obj, void *event) +{ + DEBUG("Hardware back button activated => Callback active"); + auto ssm = reinterpret_cast(data); + ssm->setInactivityCbTime(TIMER_DISABLE); + ssm->stopAutoscanning(); + ssm->startAutoscanning(); } -void ScreenScannerManager::stopAutoscanning() +void ScreenScannerManager::setInactivityCbTime(double time) { + if (timer) { + ecore_timer_del(timer); + timer = nullptr; + } + if (time > 0) + timer = ecore_timer_add(time, inactivityCb, this); } -void ScreenScannerManager::acceptAutoscanning() +Eina_Bool ScreenScannerManager::inactivityCb(void *data) { - + DEBUG("Scanner inactive for long time"); + auto ssm = reinterpret_cast(data); + ssm->setInactivityCbTime(TIMER_DISABLE); + if (ssm->screenScanner) + ssm->stopAutoscanning(); + return 0; } diff --git a/src/ScreenScannerManager.hpp b/src/ScreenScannerManager.hpp index 1a7c680..d47523e 100644 --- a/src/ScreenScannerManager.hpp +++ b/src/ScreenScannerManager.hpp @@ -3,32 +3,40 @@ #include "Subject.hpp" #include "UIElement.hpp" -#include "ScreenScanner.hpp" +#include "PointScanner.hpp" +#include "ScanningProperties.hpp" -enum class ScanningMethod { - ROW, - POINT -}; +#include class ScreenScannerManager : public Subject { public: static ScreenScannerManager &instance(); - void changeScanningMethod(); - void changeScanningDirection(); - void changeScanningSpeed(unsigned int speed); - void changeScanningLoopLimit(unsigned int limit); - void startAutoscanning(); void stopAutoscanning(); void acceptAutoscanning(); + void changeScanningMethod(ScanningMethod mathod); + private: ScreenScannerManager(); + ScreenScannerManager(const ScreenScannerManager &) = delete; + ScreenScannerManager(ScreenScannerManager &&) = delete; + ~ScreenScannerManager(); + ScreenScannerManager &operator=(const ScreenScannerManager &) = delete; + ScreenScannerManager &operator=(ScreenScannerManager &&) = delete; + + static Eina_Bool delayedAccept(void *data); + + static void restartCb(void *data, Evas_Object *obj, void *event); + void setInactivityCbTime(double time); + static Eina_Bool inactivityCb(void *data); - ScanningMethod scanningMethod; std::shared_ptr screenScanner; + ScanningProperties &properties; + + Ecore_Timer *timer = nullptr; }; #endif diff --git a/src/UIElement.cpp b/src/UIElement.cpp index bd02c0b..b859bc3 100644 --- a/src/UIElement.cpp +++ b/src/UIElement.cpp @@ -1,16 +1,18 @@ #include "UIElement.hpp" -int32_t UIElement::getX() +UIElement::UIElement(int x, int y) : obj(nullptr), x(x), y(y) {} + +std::shared_ptr UIElement::getObject() const { - return x; + return obj; } -int32_t UIElement::getY() +int UIElement::getX() const { - return y; + return x; } -std::shared_ptr UIElement::getObject() +int UIElement::getY() const { - return obj; + return y; } diff --git a/src/UIElement.hpp b/src/UIElement.hpp index 68b3174..eb69f8c 100644 --- a/src/UIElement.hpp +++ b/src/UIElement.hpp @@ -1,21 +1,22 @@ #ifndef UIELEMENT_HPP #define UIELEMENT_HPP -#include #include #include class UIElement { public: - int32_t getX(); - int32_t getY(); - std::shared_ptr getObject(); + UIElement(int x, int y); + + std::shared_ptr getObject() const; + int getX() const; + int getY() const; private: - int32_t x; - int32_t y; std::shared_ptr obj; + int x; + int y; }; #endif diff --git a/src/UniversalSwitchLog.hpp b/src/UniversalSwitchLog.hpp index ff18eec..d64a3a5 100644 --- a/src/UniversalSwitchLog.hpp +++ b/src/UniversalSwitchLog.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -30,6 +31,15 @@ #define DEBUG_PRINTING_LEVEL_INFO 3 #define DEBUG_PRINTING_LEVEL_DEBUG 4 +#define ASSERT(x, msg...) \ + do { \ + if(!(x)) { \ + ERROR("ASSERT FAILED"); \ + ERROR(msg); \ + assert(x); \ + } \ + } while (0) + //defined in file CMakeList.txt #ifndef DEBUG_PRINTING_LEVEL #define DEBUG_PRINTING_LEVEL DEBUG_PRINTING_LEVEL_DEBUG diff --git a/src/universalswitch.cpp b/src/universalswitch.cpp index b3627f0..3d6e766 100644 --- a/src/universalswitch.cpp +++ b/src/universalswitch.cpp @@ -1,6 +1,8 @@ #include "UniversalSwitchLog.hpp" #include "Window.hpp" +#include "ScreenScannerManager.hpp" + #include #include #include @@ -10,6 +12,8 @@ bool app_create(void *data) { DEBUG("App create"); + ScreenScannerManager::instance().startAutoscanning(); + return true; } @@ -17,6 +21,8 @@ void app_terminate(void *data) { DEBUG("app termination procedure"); + ScreenScannerManager::instance().stopAutoscanning(); + return; } -- 2.7.4 From 23924f41322f6c1d824faf79e6a9e82af4b903c3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Stawicki?= Date: Fri, 16 Jun 2017 12:43:41 +0200 Subject: [PATCH 13/16] set app-control.operation for implicit launch request Change-Id: I3d34099ef4d68d8d1bf4a90f56129d09d8e252f2 --- CMakeLists.txt | 1 + org.tizen.universal-switch.xml | 4 ++++ packaging/org.tizen.universal-switch.spec | 1 + 3 files changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index cec670f..45f6123 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ FIND_PACKAGE(PkgConfig REQUIRED) pkg_check_modules(pkgs REQUIRED capi-appfw-service-application + capi-appfw-application elementary ecore sqlite3 diff --git a/org.tizen.universal-switch.xml b/org.tizen.universal-switch.xml index 9b02ae9..354dece 100644 --- a/org.tizen.universal-switch.xml +++ b/org.tizen.universal-switch.xml @@ -4,6 +4,10 @@ universal-switch.png + + + + http://tizen.org/privilege/window.priority.set diff --git a/packaging/org.tizen.universal-switch.spec b/packaging/org.tizen.universal-switch.spec index b75b657..7002ca8 100644 --- a/packaging/org.tizen.universal-switch.spec +++ b/packaging/org.tizen.universal-switch.spec @@ -9,6 +9,7 @@ BuildRequires: at-spi2-core BuildRequires: at-spi2-core-devel BuildRequires: cmake BuildRequires: pkgconfig(capi-appfw-service-application) +BuildRequires: pkgconfig(capi-appfw-application) BuildRequires: pkgconfig(ecore) BuildRequires: pkgconfig(elementary) BuildRequires: pkgconfig(efl-extension) -- 2.7.4 From acdc89d8dec7e840bf46a17d0630e0b910f5a9b3 Mon Sep 17 00:00:00 2001 From: Mariusz Wachowicz Date: Mon, 26 Jun 2017 16:27:13 +0200 Subject: [PATCH 14/16] Singleton and MockableSingleton provide 2 new classes MocableSingleton allow global access to DefaultImplementation with specific Interface however it is possible to override DefaultImplementation Change-Id: I72903e314baf83939f18b1a8d440e14d5936ff9a --- src/Singleton.hpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/Singleton.hpp diff --git a/src/Singleton.hpp b/src/Singleton.hpp new file mode 100644 index 0000000..d346fdc --- /dev/null +++ b/src/Singleton.hpp @@ -0,0 +1,40 @@ +#ifndef SINGLETON_HPP +#define SINGLETON_HPP + +#include "UniversalSwitchLog.hpp" + +#include + +class Singleton +{ +protected: + Singleton() = default; + Singleton(const Singleton &) = delete; + Singleton(Singleton &&) = delete; + Singleton &operator = (const Singleton &) = delete; + Singleton &operator = (Singleton &&) = delete; +}; + +template +class MockableSingleton : public Singleton +{ +public: + static Interface &instance() + { + ASSERT(implementation, "implementation in MockableSingleton do not properly initialized"); + return *implementation; + } + + static void setImplementation(std::unique_ptr &&impl) + { + implementation = std::move(impl); + } + +private: + static std::unique_ptr implementation; +}; + +template +std::unique_ptr MockableSingleton::implementation = std::unique_ptr(new DefaultImpl); + +#endif -- 2.7.4 From c564042e9fedb7afc2a4aa842aebd654823df425 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Stawicki?= Date: Fri, 30 Jun 2017 20:20:54 +0200 Subject: [PATCH 15/16] Add VConfSingleton. Integration tests moved into a separate rpm. Change-Id: Ibe015dbd44bfd4c96aceaf155aae70e7cbe56af5 --- packaging/org.tizen.universal-switch.spec | 16 ++ src/CMakeLists.txt | 1 + src/Singleton.hpp | 31 ++- src/VConf.cpp | 192 +++++++++++++++++++ src/VConf.hpp | 41 ++++ tests/CMakeLists.txt | 19 +- tests/VConfImplTests.cpp | 300 ++++++++++++++++++++++++++++++ tests/VConfImplTests_init.sh | 10 + 8 files changed, 598 insertions(+), 12 deletions(-) create mode 100644 src/VConf.cpp create mode 100644 src/VConf.hpp create mode 100644 tests/VConfImplTests.cpp create mode 100755 tests/VConfImplTests_init.sh diff --git a/packaging/org.tizen.universal-switch.spec b/packaging/org.tizen.universal-switch.spec index 7002ca8..48ffbed 100644 --- a/packaging/org.tizen.universal-switch.spec +++ b/packaging/org.tizen.universal-switch.spec @@ -23,6 +23,14 @@ BuildRequires: net-config %description An utility library for developers of the menu screen. + +%package tests +Summary: Universal Switch Assistive Technology - tests + +%description tests +Universal Switch tests + + %prep %setup -q @@ -56,3 +64,11 @@ rm -rf %{buildroot} %license LICENSE %{AppDir}/bin/universal-switch %{DataDir}/packages/%{name}.xml + + +%post tests +%{AppDir}/tests/VConfImplTests_init.sh + +%files tests +%{AppDir}/tests/* +%license LICENSE diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bdb1988..8664d87 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,6 +19,7 @@ SET(SRCS Window.cpp UIElement.cpp PointScanner.cpp + VConf.cpp ) ADD_LIBRARY(universal-switch-obj OBJECT ${SRCS}) diff --git a/src/Singleton.hpp b/src/Singleton.hpp index d346fdc..975a969 100644 --- a/src/Singleton.hpp +++ b/src/Singleton.hpp @@ -4,6 +4,7 @@ #include "UniversalSwitchLog.hpp" #include +#include class Singleton { @@ -15,26 +16,44 @@ protected: Singleton &operator = (Singleton &&) = delete; }; -template +template class MockableSingleton : public Singleton { public: - static Interface &instance() + using InterfaceType = Interface; + + static InterfaceType &instance() { ASSERT(implementation, "implementation in MockableSingleton do not properly initialized"); return *implementation; } - static void setImplementation(std::unique_ptr &&impl) + static void setImplementation(std::unique_ptr &&impl) { implementation = std::move(impl); } private: - static std::unique_ptr implementation; + static std::unique_ptr implementation; }; -template -std::unique_ptr MockableSingleton::implementation = std::unique_ptr(new DefaultImpl); +template +class CallbackHandlerKeeper +{ +public: + ~CallbackHandlerKeeper() + { + for (auto e : handlers) + MockableSingletonType::instance().unregisterCb(e); + } + + void add(typename MockableSingletonType::InterfaceType::CallbackHandle handler) + { + handlers.push_back(std::move(handler)); + } + +private: + std::vector handlers; +}; #endif diff --git a/src/VConf.cpp b/src/VConf.cpp new file mode 100644 index 0000000..683ed7b --- /dev/null +++ b/src/VConf.cpp @@ -0,0 +1,192 @@ + +#include "VConf.hpp" +#include +#include + +namespace +{ + template + using CallbackMap = std::unordered_map>; + template + using KeyMap = std::unordered_map>; + + static int idxCount = 0; + static KeyMap intCallbackMap; + static KeyMap boolCallbackMap; + static KeyMap doubleCallbackMap; + static KeyMap stringCallbackMap; + + template + inline int vconfRegisterKeyChangedCb(KeyMapType &keyMap, VconfCbType &vconfCb, const std::string &key, CallbackType &&callback) + { + auto idx = ++idxCount; + auto iterator = keyMap.find(key); + if (iterator == keyMap.end()) { + iterator = keyMap.emplace(key, typename KeyMapType::mapped_type()).first; + if (vconf_notify_key_changed(key.c_str(), vconfCb, &iterator->second) != VCONF_OK) { + ERROR("Vconf: cant register callback for key: %s", key.c_str()); + return idx; + } + } + iterator->second.emplace(idx, callback); + return idx; + } + + template + inline void vconfUnregisterCb(KeyMapType &keyMap, VconfCbType &vconfCb, const std::string &key, int idx) + { + auto iterator = keyMap.find(key); + if (iterator == keyMap.end()) { + ERROR("Vconf: unregister, key: %s not found", key.c_str()); + return; + } + if (iterator->second.erase(idx) != 1) + ERROR("Vconf: could not remove callback for key: %s with idx: %d", key.c_str(), idx); + if (iterator->second.empty()) { + keyMap.erase(iterator); + if (vconf_ignore_key_changed(key.c_str(), vconfCb) != VCONF_OK) { + ERROR("Vconf: cant unregister callback for key: %s", key.c_str()); + } + } + } + + template + inline void vconfCb(KeyMapType &keyMap, const vconfGetType &vconfGetFunction, const char *expectedType, int vconfType, keynode_t *keynode, void *data) + { + if (vconf_keynode_get_type(keynode) != vconfType) { + ERROR("vconf error: got '%s' changed with type: %d, expected type: %s", vconf_keynode_get_name(keynode), vconf_keynode_get_type(keynode), expectedType); + return; + } + auto callbackMapPtr = (typename KeyMapType::mapped_type *)data; + for (auto &idx : *callbackMapPtr) { + auto &callback = idx.second; + callback(vconfGetFunction(keynode)); + } + } + + void vconfIntCb(keynode_t *key, void *data) + { + vconfCb(intCallbackMap, vconf_keynode_get_int, "int", VCONF_TYPE_INT, key, data); + } + void vconfBoolCb(keynode_t *key, void *data) + { + vconfCb(boolCallbackMap, vconf_keynode_get_bool, "bool", VCONF_TYPE_BOOL, key, data); + } + void vconfDoubleCb(keynode_t *key, void *data) + { + vconfCb(doubleCallbackMap, vconf_keynode_get_dbl, "double", VCONF_TYPE_DOUBLE, key, data); + } + void vconfStringCb(keynode_t *key, void *data) + { + vconfCb(stringCallbackMap, vconf_keynode_get_str, "char*", VCONF_TYPE_STRING, key, data); + } + +} + +class VConfImpl : public VConfInterface +{ +public: + + int get(const std::string &key, int value) const override + { + if (vconf_get_int(key.c_str(), &value) != VCONF_OK) + INFO("Vconf: cant get value for key: %s, returning default value = %d", key.c_str(), value); + return value; + } + + bool get(const std::string &key, bool defaultValue) const override + { + int value = defaultValue; + if (vconf_get_bool(key.c_str(), &value) != VCONF_OK) + INFO("Vconf: cant get value for key: %s, returning default value = %s", key.c_str(), value ? "true" : "false"); + return value; + } + + double get(const std::string &key, double value) const override + { + if (vconf_get_dbl(key.c_str(), &value) != VCONF_OK) + INFO("Vconf: cant get value for key: %s, returning default value = %lf", key.c_str(), value); + return value; + } + + std::string get(const std::string &key, const std::string &value) const override + { + char *str = vconf_get_str(key.c_str()); + if (str) + return str; + + INFO("Vconf: cant get value for key: %s, returning default value = %s", key.c_str(), value.c_str()); + return value; + } + + + void set(const std::string &key, int value) override + { + if (vconf_set_int(key.c_str(), value) != VCONF_OK) + ERROR("Vconf: cant set value for key: %s, value = %d", key.c_str(), value); + } + + void set(const std::string &key, bool value) override + { + if (vconf_set_bool(key.c_str(), (int)value) != VCONF_OK) + ERROR("Vconf: cant set value for key: %s, value = %s", key.c_str(), value ? "true" : "false"); + } + + void set(const std::string &key, double value) override + { + if (vconf_set_dbl(key.c_str(), value) != VCONF_OK) + ERROR("Vconf: cant set value for key: %s, value = %lf", key.c_str(), value); + } + + void set(const std::string &key, const std::string &value) override + { + if (vconf_set_str(key.c_str(), value.c_str()) != VCONF_OK) + ERROR("Vconf: cant set value for key: %s, value = %s", key.c_str(), value.c_str()); + } + + + CallbackHandle registerKeyChangedCb(const std::string &key, std::function callback) override + { + auto idx = vconfRegisterKeyChangedCb(intCallbackMap, vconfIntCb, key, std::move(callback)); + return std::make_tuple(key, idx, VConfRecordType::INT); + } + + CallbackHandle registerKeyChangedCb(const std::string &key, std::function callback) override + { + auto idx = vconfRegisterKeyChangedCb(boolCallbackMap, vconfBoolCb, key, std::move(callback)); + return std::make_tuple(key, idx, VConfRecordType::BOOL); + } + + CallbackHandle registerKeyChangedCb(const std::string &key, std::function callback) override + { + auto idx = vconfRegisterKeyChangedCb(doubleCallbackMap, vconfDoubleCb, key, std::move(callback)); + return std::make_tuple(key, idx, VConfRecordType::DOUBLE); + } + + CallbackHandle registerKeyChangedCb(const std::string &key, std::function callback) override + { + auto idx = vconfRegisterKeyChangedCb(stringCallbackMap, vconfStringCb, key, std::move(callback)); + return std::make_tuple(key, idx, VConfRecordType::STRING); + } + + + void unregisterCb(const CallbackHandle &ch) override + { + auto &key = std::get<0>(ch); + auto idx = std::get<1>(ch); + switch (std::get<2>(ch)) { + case VConfRecordType::INT: + return vconfUnregisterCb(intCallbackMap, vconfIntCb, key, idx); + case VConfRecordType::BOOL: + return vconfUnregisterCb(boolCallbackMap, vconfBoolCb, key, idx); + case VConfRecordType::DOUBLE: + return vconfUnregisterCb(doubleCallbackMap, vconfDoubleCb, key, idx); + case VConfRecordType::STRING: + return vconfUnregisterCb(stringCallbackMap, vconfStringCb, key, idx); + } + } +}; + + +template<> +std::unique_ptr VConfSingleton::implementation = std::unique_ptr(new VConfImpl); diff --git a/src/VConf.hpp b/src/VConf.hpp new file mode 100644 index 0000000..997e793 --- /dev/null +++ b/src/VConf.hpp @@ -0,0 +1,41 @@ +#ifndef VCONF_HPP +#define VCONF_HPP + +#include "Singleton.hpp" +#include +#include +#include + +class VConfInterface +{ +public: + enum class VConfRecordType {INT, BOOL, DOUBLE, STRING}; + using CallbackHandle = std::tuple; + + virtual int get(const std::string &key, int defaultValue) const = 0; + virtual bool get(const std::string &key, bool defaultValue) const = 0; + virtual double get(const std::string &key, double defaultValue) const = 0; + virtual std::string get(const std::string &key, const std::string &defaultValue) const = 0; + + virtual void set(const std::string &key, int value) = 0; + virtual void set(const std::string &key, bool value) = 0; + virtual void set(const std::string &key, double value) = 0; + virtual void set(const std::string &key, const std::string &value) = 0; + + virtual CallbackHandle registerKeyChangedCb(const std::string &key, std::function) = 0; + virtual CallbackHandle registerKeyChangedCb(const std::string &key, std::function) = 0; + virtual CallbackHandle registerKeyChangedCb(const std::string &key, std::function) = 0; + virtual CallbackHandle registerKeyChangedCb(const std::string &key, std::function) = 0; + virtual void unregisterCb(const CallbackHandle &cb) = 0; + + template + inline CallbackHandle registerKeyChangedCb(const std::string &key, std::function callback) + { + return registerKeyChangedCb(key, callback); + } + +}; + +using VConfSingleton = MockableSingleton; + +#endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 809da3f..5dc470b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,16 +6,23 @@ INCLUDE_DIRECTORIES(googletest/googlemock/include/) INCLUDE_DIRECTORIES(${pkgs_INCLUDE_DIRS}) -ADD_LIBRARY(universal-switch-test SHARED $) +SET(TESTS_INSTALL_DIR tests/) + +ADD_LIBRARY(universal-switch-test STATIC $) TARGET_LINK_LIBRARIES(universal-switch-test ${pkgs_LDFLAGS}) -FUNCTION(CREATE_TEST_FROM_SOURCE TEST_SRC_FILE) +FUNCTION(CREATE_TEST TEST_SRC_FILE) GET_FILENAME_COMPONENT(TESTNAME ${TEST_SRC_FILE} NAME_WE) ADD_EXECUTABLE(test_${TESTNAME} ${TEST_SRC_FILE}) TARGET_LINK_LIBRARIES(test_${TESTNAME} gtest_main universal-switch-test) - ADD_TEST(test_${TESTNAME} test_${TESTNAME}) -ENDFUNCTION(CREATE_TEST_FROM_SOURCE) + INSTALL(TARGETS test_${TESTNAME} DESTINATION ${TESTS_INSTALL_DIR}) +ENDFUNCTION() + + +INSTALL(PROGRAMS VConfImplTests_init.sh DESTINATION ${TESTS_INSTALL_DIR}) +CREATE_TEST(VConfImplTests.cpp) + +CREATE_TEST(ConfigurationTests.cpp) +CREATE_TEST(ActivityDispatchingTests.cpp) -CREATE_TEST_FROM_SOURCE(ConfigurationTests.cpp) -CREATE_TEST_FROM_SOURCE(ActivityDispatchingTests.cpp) diff --git a/tests/VConfImplTests.cpp b/tests/VConfImplTests.cpp new file mode 100644 index 0000000..ae1e81f --- /dev/null +++ b/tests/VConfImplTests.cpp @@ -0,0 +1,300 @@ +#include "VConf.hpp" +#include +#include + + +void restoreDefault() +{ + VConfSingleton::instance().set("VconfImplTest/int/20", 20); + VConfSingleton::instance().set("VconfImplTest/string/foo", std::string("foo")); + VConfSingleton::instance().set("VconfImplTest/bool/true", true); + VConfSingleton::instance().set("VconfImplTest/double/3.14", 3.14); +} + +TEST(VconfImplTest, get) +{ + EXPECT_EQ(VConfSingleton::instance().get("not_defined", 10), 10); + + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/int/20", 10), 20); + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/int/20", true), true); + + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/string/foo", 10), 10); + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/string/foo", std::string("bar")), std::string("foo")); + + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/bool/true", false), true); + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/bool/true", 10.1), 10.1); + + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/double/3.14", 1.1), 3.14); + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/double/3.14", 1), 1); +} + +TEST(VconfImplTest, set) +{ + VConfSingleton::instance().set("not_defined", 20); + EXPECT_EQ(VConfSingleton::instance().get("not_defined", 8), 8); + + + VConfSingleton::instance().set("VconfImplTest/int/20", 13); + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/int/20", 7), 13); + //change type + VConfSingleton::instance().set("VconfImplTest/int/20", 1.1); + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/int/20", 12.6), 1.1); + + VConfSingleton::instance().set("VconfImplTest/string/foo", std::string("bar2")); + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/string/foo", std::string("bar1")), std::string("bar2")); + //change type + VConfSingleton::instance().set("VconfImplTest/string/foo", 12); + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/string/foo", 13), 12); + + VConfSingleton::instance().set("VconfImplTest/bool/true", false); + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/bool/true", true), false); + VConfSingleton::instance().set("VconfImplTest/bool/true", true); + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/bool/true", true), true); + //change type + VConfSingleton::instance().set("VconfImplTest/bool/true", 12.1); + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/bool/true", 8.1), 12.1); + + VConfSingleton::instance().set("VconfImplTest/double/3.14", 12.1); + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/double/3.14", 1.1), 12.1); + //change type + VConfSingleton::instance().set("VconfImplTest/double/3.14", 8); + EXPECT_EQ(VConfSingleton::instance().get("VconfImplTest/double/3.14", 7), 8); + + restoreDefault(); +} + + +namespace eventLoop +{ + std::function mainFunction; + GMainLoop *event_loop; + gboolean mainLoopQuittimer(gpointer data) + { + g_main_loop_quit(event_loop); + return 0; + } + + gboolean mainLoopIdle(gpointer data) + { + mainFunction(); + g_timeout_add(1, mainLoopQuittimer, NULL); + return 0; + } + + void run(std::function f) + { + event_loop = g_main_loop_new(NULL, FALSE); + mainFunction = f; + g_idle_add(mainLoopIdle, NULL); + g_main_loop_run(event_loop); + g_main_loop_unref(event_loop); + } +} + +int testCallbackValue = 0; +void testCallback(int x) +{ + testCallbackValue = x; +} + +TEST(VconfImplTest, registerKeyChangedCb_int) +{ + INFO("registerKeyChangedCb_int"); + VConfInterface::CallbackHandle h = VConfSingleton::instance().registerKeyChangedCb("VconfImplTest/int/20", testCallback); + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", 132); + }); + EXPECT_EQ(testCallbackValue, 132); + VConfSingleton::instance().unregisterCb(h); + +} + +TEST(VconfImplTest, registerKeyChangedCb_int_lambda) +{ + int v = 0; + VConfInterface::CallbackHandle h = VConfSingleton::instance().registerKeyChangedCb("VconfImplTest/int/20", [&](int x) { + v = x; + }); + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", 133); + }); + EXPECT_EQ(v, 133); + VConfSingleton::instance().unregisterCb(h); + +} + +TEST(VconfImplTest, registerKeyChangedCb_bool_lambda) +{ + bool v = false; + VConfInterface::CallbackHandle h = VConfSingleton::instance().registerKeyChangedCb("VconfImplTest/int/20", [&](bool x) { + v = x; + }); + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", true); + }); + EXPECT_EQ(v, true); + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", false); + }); + EXPECT_EQ(v, false); + VConfSingleton::instance().unregisterCb(h); +} + +TEST(VconfImplTest, registerKeyChangedCb_double_lambda) +{ + double v = 0.0; + VConfInterface::CallbackHandle h = VConfSingleton::instance().registerKeyChangedCb("VconfImplTest/int/20", [&](double x) { + v = x; + }); + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", 13.6); + }); + EXPECT_EQ(v, 13.6); + VConfSingleton::instance().unregisterCb(h); +} + +TEST(VconfImplTest, registerKeyChangedCb_string_lambda) +{ + std::string v; + VConfInterface::CallbackHandle h = VConfSingleton::instance().registerKeyChangedCb("VconfImplTest/int/20", [&](std::string x) { + v = x; + }); + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", std::string("foo2")); + }); + EXPECT_EQ(v, "foo2"); + VConfSingleton::instance().unregisterCb(h); +} + + +TEST(VconfImplTest, registerKeyChangedCb_int_unregister) +{ + int v = 0; + { + VConfInterface::CallbackHandle h = VConfSingleton::instance().registerKeyChangedCb("VconfImplTest/int/20", [&](int x) { + v = x; + }); + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", 1); + }); + EXPECT_EQ(v, 1); + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", 2); + }); + EXPECT_EQ(v, 2); + VConfSingleton::instance().unregisterCb(h); + } + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", 3); + }); + EXPECT_EQ(v, 2); +} + +TEST(VconfImplTest, registerKeyChangedCb_int_unregister_2x) +{ + { + int v = 0; + { + VConfInterface::CallbackHandle h = VConfSingleton::instance().registerKeyChangedCb("VconfImplTest/int/20", [&](int x) { + v = x; + }); + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", 11); + }); + EXPECT_EQ(v, 11); + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", 12); + }); + EXPECT_EQ(v, 12); + VConfSingleton::instance().unregisterCb(h); + } + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", 13); + }); + EXPECT_EQ(v, 12); + } + + { + int v = 0; + { + VConfInterface::CallbackHandle h = VConfSingleton::instance().registerKeyChangedCb("VconfImplTest/int/20", [&](int x) { + v = x; + }); + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", 21); + }); + EXPECT_EQ(v, 21); + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", 22); + }); + EXPECT_EQ(v, 22); + VConfSingleton::instance().unregisterCb(h); + } + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", 23); + }); + EXPECT_EQ(v, 22); + } + + restoreDefault(); +} + + +TEST(VconfImplTest, registerKeyChangedCb_all) +{ + int i = 0; + bool b = false; + double v = 0.0; + std::string s("empty"); + + VConfInterface::CallbackHandle h1 = VConfSingleton::instance().registerKeyChangedCb("VconfImplTest/int/20", [&](bool x) { + b = x; + }); + VConfInterface::CallbackHandle h2 = VConfSingleton::instance().registerKeyChangedCb("VconfImplTest/int/20", [&](int x) { + i = x; + }); + VConfInterface::CallbackHandle h3 = VConfSingleton::instance().registerKeyChangedCb("VconfImplTest/int/20", [&](double x) { + v = x; + }); + VConfInterface::CallbackHandle h4 = VConfSingleton::instance().registerKeyChangedCb("VconfImplTest/int/20", [&](std::string x) { + s = x; + }); + + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", 10); + }); + EXPECT_EQ(i, 10); + EXPECT_EQ(b, false); + EXPECT_EQ(v, 0.0); + EXPECT_EQ(s, "empty"); + + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", 20.2); + }); + EXPECT_EQ(i, 10); + EXPECT_EQ(b, false); + EXPECT_EQ(v, 20.2); + EXPECT_EQ(s, "empty"); + + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", true); + }); + EXPECT_EQ(i, 10); + EXPECT_EQ(b, true); + EXPECT_EQ(v, 20.2); + EXPECT_EQ(s, "empty"); + + eventLoop::run([]() { + VConfSingleton::instance().set("VconfImplTest/int/20", std::string("foo")); + }); + EXPECT_EQ(i, 10); + EXPECT_EQ(b, true); + EXPECT_EQ(v, 20.2); + EXPECT_EQ(s, "foo"); + + VConfSingleton::instance().unregisterCb(h1); + VConfSingleton::instance().unregisterCb(h2); + VConfSingleton::instance().unregisterCb(h3); + VConfSingleton::instance().unregisterCb(h4); + restoreDefault(); +} diff --git a/tests/VConfImplTests_init.sh b/tests/VConfImplTests_init.sh new file mode 100755 index 0000000..eba396a --- /dev/null +++ b/tests/VConfImplTests_init.sh @@ -0,0 +1,10 @@ +#!/bin/bash + + +vconftool set -f -t int VconfImplTest/int/20 20 +vconftool set -f -t string VconfImplTest/string/foo "foo" +vconftool set -f -t bool VconfImplTest/bool/true 1 +vconftool set -f -t double VconfImplTest/double/3.14 3.14 + + + -- 2.7.4 From 77f6611e375ba9f320460f1a9fe27652ac38cf63 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Stawicki?= Date: Wed, 5 Jul 2017 16:41:04 +0200 Subject: [PATCH 16/16] Bugfix tests: rpath added to make universal-switch a shared library Change-Id: Ibe93abe150dbeaa87a4e8c0273653b17b160a9a3 --- tests/CMakeLists.txt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5dc470b..278c018 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,15 +6,18 @@ INCLUDE_DIRECTORIES(googletest/googlemock/include/) INCLUDE_DIRECTORIES(${pkgs_INCLUDE_DIRS}) -SET(TESTS_INSTALL_DIR tests/) +SET(TESTS_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/tests/) -ADD_LIBRARY(universal-switch-test STATIC $) +# universal-switch-test library has to be SHARED otherwise ActivityFactory.instance().createActivity() will not work +ADD_LIBRARY(universal-switch-test SHARED $) TARGET_LINK_LIBRARIES(universal-switch-test ${pkgs_LDFLAGS}) +INSTALL(TARGETS universal-switch-test DESTINATION ${TESTS_INSTALL_DIR}) FUNCTION(CREATE_TEST TEST_SRC_FILE) GET_FILENAME_COMPONENT(TESTNAME ${TEST_SRC_FILE} NAME_WE) ADD_EXECUTABLE(test_${TESTNAME} ${TEST_SRC_FILE}) + SET_TARGET_PROPERTIES(test_${TESTNAME} PROPERTIES INSTALL_RPATH "${TESTS_INSTALL_DIR}") TARGET_LINK_LIBRARIES(test_${TESTNAME} gtest_main universal-switch-test) INSTALL(TARGETS test_${TESTNAME} DESTINATION ${TESTS_INSTALL_DIR}) ENDFUNCTION() -- 2.7.4