Parsing arguments in main function 30/162630/20
authorLukasz Wlazly <l.wlazly@partner.samsung.com>
Tue, 19 Dec 2017 12:34:52 +0000 (13:34 +0100)
committerRadoslaw Cybulski <r.cybulski@partner.samsung.com>
Wed, 10 Jan 2018 12:04:57 +0000 (13:04 +0100)
Adds support to parse arguments from command line in main function.
Argument passing will be used in test instrumentation.

Change-Id: I109442f16270879fb84313df08742d9a6002c54b

src/UniversalSwitch.cpp
src/UniversalSwitch.hpp
src/main.cpp
src/utils.cpp
src/utils.hpp

index 8040b97..2bf85fa 100644 (file)
 #include "Window.hpp"
 #include "dbusLocators.hpp"
 #include "utils.hpp"
+#include "DBus.hpp"
 
 #include <device/display.h>
 
-void UniversalSwitch::initialize()
+void UniversalSwitch::initialize(const std::array<Optional<std::string>, (size_t)utils::Argument::_count> &arguments)
 {
        setDBusInterface(DBusInterface::create());
        resetA11yDbusProperties();
 
+       if (arguments[(size_t)utils::Argument::SourcePath]) {
+               batchFilePath = *arguments[(size_t)utils::Argument::SourcePath];
+               batchOutputPath = *arguments[(size_t)utils::Argument::OutputPath];
+
+               changeMode(Mode::testExecution);
+               return;
+       }
+
        auto compositeSwitchProvider = std::make_shared<CompositeSwitchProvider>();
        compositeSwitchProvider->add(std::make_shared<ScreenSwitchProvider>());
        compositeSwitchProvider->add(std::make_shared<AccessoriesSwitchProvider>());
@@ -65,35 +74,40 @@ void UniversalSwitch::initialize()
        });
 }
 
-void UniversalSwitch::switchToActiveMode()
-{
-       DEBUG("Universal-switch active mode");
-
-       atspi = std::make_shared<Atspi>();
-       navigationInterface = createNavigationImpl();
-
-       auto activityFactory = ActivityFactory::getInstance();
-       switchManager = SwitchManager::create<SwitchManager>(compositeSwitchProvider, configuration, activityFactory);
-
-       textToSpeech = std::make_shared<TextToSpeech>();
-       soundFeedback = std::make_shared<SoundFeedback>();
-
-       screenScannerManager = std::make_shared<ScreenScannerManager>();
-       screenScannerManager->startAutoscanning();
-}
-
-void UniversalSwitch::switchToPasiveMode()
-{
-       DEBUG("Universal-switch passive mode");
-       screenScannerManager.reset();
-       soundFeedback.reset();
-       textToSpeech.reset();
-       if (switchManager) {
-               switchManager->terminate();
-               switchManager.reset();
+void UniversalSwitch::changeMode(Mode mode)
+{
+       switch (mode) {
+       case Mode::active:
+               DEBUG("Universal-switch active mode");
+
+               atspi = std::make_shared<Atspi>();
+               navigationInterface = createNavigationImpl();
+               switchManager = SwitchManager::create<SwitchManager>(compositeSwitchProvider, configuration, ActivityFactory::getInstance());
+               textToSpeech = std::make_shared<TextToSpeech>();
+               soundFeedback = std::make_shared<SoundFeedback>();
+               screenScannerManager = std::make_shared<ScreenScannerManager>();
+               screenScannerManager->startAutoscanning();
+               break;
+
+       case Mode::passive:
+               DEBUG("Universal-switch passive mode");
+               screenScannerManager.reset();
+               if (switchManager) {
+                       switchManager->terminate();
+                       switchManager.reset();
+               }
+               soundFeedback.reset();
+               textToSpeech.reset();
+               navigationInterface.reset();
+               atspi.reset();
+               break;
+
+       case Mode::testExecution:
+               DEBUG("Universal-switch test mode");
+               atspi = std::make_shared<Atspi>();
+               navigationInterface = createNavigationImpl();
+               break;
        }
-       navigationInterface.reset();
-       atspi.reset();
 }
 
 void UniversalSwitch::resetA11yDbusProperties()
@@ -108,9 +122,9 @@ void UniversalSwitch::resetA11yDbusProperties()
 void UniversalSwitch::updateMode()
 {
        if (isScanningOn && isDisplayOn)
-               switchToActiveMode();
+               changeMode(Mode::active);
        else
-               switchToPasiveMode();
+               changeMode(Mode::passive);
 }
 
 void UniversalSwitch::displayStateChangedCallback(device_callback_e type, void *value, void *user_data)
@@ -211,7 +225,7 @@ void UniversalSwitch::terminate()
 {
        device_remove_callback(DEVICE_CALLBACK_DISPLAY_STATE, displayStateChangedCallback);
        callbackHandle.reset();
-       switchToPasiveMode();
+       changeMode(Mode::passive);
        switchManager.reset();
        compositeSwitchProvider.reset();
        configuration.reset();
index 6b44a50..c55394a 100644 (file)
@@ -19,6 +19,8 @@
 
 #include "Singleton.hpp"
 #include "VConf.hpp"
+#include "Optional.hpp"
+#include "utils.hpp"
 
 #include <device/callback.h>
 
@@ -54,16 +56,16 @@ public:
        std::shared_ptr<ScrollActivitiesData> getScrollActivitiesData() const;
        void setScrollActivitiesData(std::shared_ptr<ScrollActivitiesData>);
 
-       std::string getTestFilePath() const;
-       void setTestFilePath(const std::string &path);
-       void setTestOutputPath(const std::string &path);
-
-       void initialize();
+       void initialize(const std::array<Optional<std::string>, (size_t)utils::Argument::_count> &arguments);
        void terminate();
-
 private:
-       void switchToActiveMode();
-       void switchToPasiveMode();
+       enum class Mode {
+               passive,
+               active,
+               testExecution
+       };
+
+       void changeMode(Mode mode);
        void updateMode();
        void setSwitchManager(const std::shared_ptr<SwitchManager> &sm);
        void setCompositeSwitchProvider(const std::shared_ptr<CompositeSwitchProvider> &csp);
@@ -87,7 +89,8 @@ private:
        VConfInterface::CallbackHandle callbackHandle;
        bool isScanningOn = false;
        bool isDisplayOn = false;
-       std::string testFilePath, testOutputPath;
+       std::string batchFilePath;
+       std::string batchOutputPath;
 };
 
 #endif
index 50aecf2..dd9f747 100644 (file)
@@ -27,6 +27,7 @@
 #include "UniversalSwitch.hpp"
 #include "UniversalSwitchLog.hpp"
 #include "Window.hpp"
+#include "utils.hpp"
 
 #include <app.h>
 #include <Elementary.h>
@@ -44,11 +45,19 @@ static void _setting_time_lang_changed(app_event_info_h event_info, void *data)
        }
 }
 
-bool app_create(void *data)
+struct InitData {
+       char **argv;
+       int argc;
+};
+
+bool app_create(void *data_)
 {
+       auto data = static_cast<InitData *>(data_);
+       auto arguments = utils::parseArguments(data->argc, data->argv);
+
        DEBUG("App create");
        ecore_event_init();
-       Singleton<UniversalSwitch>::instance().initialize();
+       Singleton<UniversalSwitch>::instance().initialize(arguments);
        return true;
 }
 
@@ -57,9 +66,14 @@ void app_terminate(void *data)
        DEBUG("app termination procedure");
        Singleton<UniversalSwitch>::instance().terminate();
        ecore_event_shutdown();
-       return;
 }
 
+
+/*
+ * To run Universal-Switch in test mode it should be run this way:
+ * app_launcher -s org.tizen.universal-switch TESTS_PATH [PATH]
+ */
+
 int main(int argc, char *argv[])
 {
        DEBUG("main function called");
@@ -71,9 +85,12 @@ int main(int argc, char *argv[])
                .resume = nullptr,
                .app_control = nullptr
        };
+       InitData data {
+               argv, argc
+       };
 
        app_event_handler_h handler;
        ui_app_add_event_handler(&handler, APP_EVENT_LANGUAGE_CHANGED, _setting_time_lang_changed, nullptr);
 
-       return ui_app_main(argc, argv, &event_callback, nullptr);
+       return ui_app_main(argc, argv, &event_callback, &data);
 }
index 7a63608..dd4b9cf 100644 (file)
 #include "DoneCallback.hpp"
 
 #include <efl_util.h>
+#include <bundle_internal.h>
 
 #include <memory>
+#include <vector>
 
 namespace utils
 {
@@ -118,4 +120,21 @@ namespace utils
 
                return ecore::TimerRepetitionPolicy::cancel;
        }
+
+       std::array<Optional<std::string>, (size_t)Argument::_count> parseArguments(int argc, char **argv)
+       {
+               auto b = std::unique_ptr<bundle, int(*)(bundle *)>(bundle_import_from_argv(argc, argv), bundle_free);
+               std::array<Optional<std::string>, (size_t)Argument::_count> result;
+
+               for (auto key : std::vector<std::pair<std::string, Argument>> {
+               { "TESTS_PATH", Argument::SourcePath },
+               { "TESTS_OUTPUT", Argument::OutputPath },
+       }) {
+                       auto val = bundle_get_val(b.get(), key.first.c_str());
+
+                       if (val)
+                               result[static_cast<size_t>(key.second)] = val;
+               }
+               return result;
+       }
 }
index 2b5f268..ed6586f 100644 (file)
 #define UTILS_HPP
 
 #include "ecore.hpp"
+#include "Optional.hpp"
 
 #include <string>
+#include <array>
 
 class DoneCallback;
 
@@ -86,6 +88,23 @@ namespace utils
                static constexpr double LONG_DELAY = 1.0;
                static constexpr double NO_DELAY = 0.0;
        };
+
+       /**
+        * @brief Enum representing allowed arguments
+        *
+        * Every enum's value is an index into array of values returned by parseArguments function
+        */
+       enum class Argument {
+               SourcePath = 0, OutputPath, _count
+       };
+
+       /**
+        * @brief Parses command line into arguments
+        *
+        * Returns array of allowed arguments indexed by Argument enumeration.
+        * Empty Optional object means given argument werent passed.
+        */
+       std::array<Optional<std::string>, (size_t)Argument::_count> parseArguments(int argc, char **argv);
 }
 
 #endif