libaurum: Apply factory pattern for OCP, LSP using TDD 72/305172/3
authorWoochan Lee <wc0917.lee@samsung.com>
Mon, 29 Jan 2024 11:30:01 +0000 (20:30 +0900)
committerwoochan lee <wc0917.lee@samsung.com>
Wed, 31 Jan 2024 04:17:36 +0000 (04:17 +0000)
Adds generateKey method and Tests for this.

apply factory pattern for Key generates it able to be added or changed
without changing exising code.

It also no problem in the operation of the program even if an object of a child
class is replaced with an object of the parent class.

For advantaging of being easy to maintain and highly extensibel.

Change-Id: I5ee504a82f2f7cc5c3ffea9be8914034975bf015

libaurum/inc/Impl/MockDeviceImpl.h
libaurum/inc/Impl/TizenDeviceImpl.h
libaurum/inc/Interface/IDevice.h
libaurum/inc/UiDevice.h
libaurum/src/Impl/MockDeviceImpl.cc
libaurum/src/Impl/TizenDeviceImpl.cc
libaurum/src/UiDevice.cc
org.tizen.aurum-bootstrap/src/Commands/SendKeyCommand.cc
tests/Test_UiDevice.cc

index 655ff7c..6aa62be 100644 (file)
@@ -34,16 +34,6 @@ namespace AurumInternal {
 
 namespace Mock {
 
-enum class MockKeyType {
-    BACK,
-    HOME,
-    MENU,
-    VOLUP,
-    VOLDOWN,
-    POWER,
-    KEY
-};
-
 struct TouchData { int x; int y; long long stamp1; long long stamp2;};
 class MockDeviceImpl : public IDevice {
 public:
@@ -112,42 +102,6 @@ public:
      * @brief TBD
      * @since_tizen 6.5
      */
-    bool pressBack(KeyRequestType type) override;
-
-    /**
-     * @brief TBD
-     * @since_tizen 6.5
-     */
-    bool pressHome(KeyRequestType type) override;
-
-    /**
-     * @brief TBD
-     * @since_tizen 6.5
-     */
-    bool pressMenu(KeyRequestType type) override;
-
-    /**
-     * @brief TBD
-     * @since_tizen 6.5
-     */
-    bool pressVolUp(KeyRequestType type) override;
-
-    /**
-     * @brief TBD
-     * @since_tizen 6.5
-     */
-    bool pressVolDown(KeyRequestType type) override;
-
-    /**
-     * @brief TBD
-     * @since_tizen 6.5
-     */
-    bool pressPower(KeyRequestType type) override;
-
-    /**
-     * @brief TBD
-     * @since_tizen 6.5
-     */
     bool pressKeyCode(std::string keycode, KeyRequestType type) override;
 
      /**
@@ -179,6 +133,20 @@ public:
      */
     std::vector<std::shared_ptr<AccessibleNode>> getWindowRoot() const override;
 
+public:
+
+    /**
+     * @brief Gets list of keys simulated on Mock device
+     *
+     * @return std::tuple<KeyType, KeyRequestType, std::string> vector
+    */
+    std::vector<std::tuple<KeyType, KeyRequestType, std::string>> getGeneratedKeys();
+
+    /**
+     * @brief Clear list of keys simulated on Mock device
+    */
+    void clearGeneratedKeys();
+
 protected:
     /**
      * @brief TBD
@@ -299,7 +267,7 @@ public:
     /**
      * @brief TBD
      */
-    std::vector<std::tuple<MockKeyType, KeyRequestType, std::string>> mKeyDevice;
+    std::vector<std::tuple<KeyType, KeyRequestType, std::string>> mKeyDevice;
 
     /**
      * @brief TBD
index a437838..aefc504 100644 (file)
@@ -79,36 +79,6 @@ public:
     bool wheelDown(int amount, const int durationMs) override;
 
     /**
-     * @copydoc IDevice::pressBack()
-     */
-    bool pressBack(KeyRequestType type) override;
-
-    /**
-     * @copydoc IDevice::pressHome()
-     */
-    bool pressHome(KeyRequestType type) override;
-
-    /**
-     * @copydoc IDevice::pressMenu()
-     */
-    bool pressMenu(KeyRequestType type) override;
-
-    /**
-     * @copydoc IDevice::pressVolUp()
-     */
-    bool pressVolUp(KeyRequestType type) override;
-
-    /**
-     * @copydoc IDevice::pressVolDown()
-     */
-    bool pressVolDown(KeyRequestType type) override;
-
-    /**
-     * @copydoc IDevice::pressPower()
-     */
-    bool pressPower(KeyRequestType type) override;
-
-    /**
      * @copydoc IDevice::pressKeyCode()
      */
     bool pressKeyCode(std::string keycode, KeyRequestType type) override;
index 6338fa5..ccb4ddb 100644 (file)
@@ -50,6 +50,21 @@ enum class KeyRequestType {
 };
 
 /**
+ * @brief KeyType enum class.
+ *
+ * @since_tizen 8.0
+*/
+enum class KeyType {
+    BACK,
+    HOME,
+    MENU,
+    VOLUP,
+    VOLDOWN,
+    POWER,
+    KEY
+};
+
+/**
  * @brief IDevice interface.
  *        It defines common device controls such as touch, key events
  *
@@ -170,72 +185,6 @@ public:
     virtual bool wheelDown(int amount, const int durationMs) = 0;
 
     /**
-     * @brief Simulates a press on the back key.
-     *
-     * @param[in] type one of @KeyRequestType
-     *
-     * @return true if the press back succeeded else false
-     *
-     * @since_tizen 6.5
-     */
-    virtual bool pressBack(KeyRequestType type) = 0;
-
-    /**
-     * @brief Simulates a press on the home key.
-     *
-     * @param[in] type one of @KeyRequestType
-     *
-     * @return true if the press home succeeded else false
-     *
-     * @since_tizen 6.5
-     */
-    virtual bool pressHome(KeyRequestType type) = 0;
-
-    /**
-     * @brief Simulates a press on the menu key.
-     *
-     * @param[in] type one of @KeyRequestType
-     *
-     * @return true if the press menu succeeded else false
-     *
-     * @since_tizen 6.5
-     */
-    virtual bool pressMenu(KeyRequestType type) = 0;
-
-    /**
-     * @brief Simulates a press on the volume up key.
-     *
-     * @param[in] type one of @KeyRequestType
-     *
-     * @return true if the press volume up succeeded else false
-     *
-     * @since_tizen 6.5
-     */
-    virtual bool pressVolUp(KeyRequestType type) = 0;
-
-    /**
-     * @brief Simulates a press on the volume down key.
-     *
-     * @param[in] type one of @KeyRequestType
-     *
-     * @return true if the press volume down succeeded else false
-     *
-     * @since_tizen 6.5
-     */
-    virtual bool pressVolDown(KeyRequestType type) = 0;
-
-    /**
-     * @brief Simulates a press on the power key.
-     *
-     * @param[in] type one of @KeyRequestType
-     *
-     * @return true if the press power succeeded else false
-     *
-     * @since_tizen 6.5
-     */
-    virtual bool pressPower(KeyRequestType type) = 0;
-
-    /**
      * @brief Simulates a press on the given keycode key.
      *
      * @param[in] keycode keycode
index d952f3d..344857c 100644 (file)
@@ -153,72 +153,6 @@ public:
     bool wheelDown(int amount, const int durationMs) override;
 
     /**
-     * @brief Simulates a press on the back key.
-     *
-     * @param[in] type one of @KeyRequestType
-     *
-     * @return true if the press back succeeded else false
-     *
-     * @since_tizen 6.5
-     */
-    bool pressBack(KeyRequestType type) override;
-
-    /**
-     * @brief Simulates a press on the home key.
-     *
-     * @param[in] type one of @KeyRequestType
-     *
-     * @return true if the press home succeeded else false
-     *
-     * @since_tizen 6.5
-     */
-    bool pressHome(KeyRequestType type) override;
-
-    /**
-     * @brief Simulates a press on the menu key.
-     *
-     * @param[in] type one of @KeyRequestType
-     *
-     * @return true if the press menu succeeded else false
-     *
-     * @since_tizen 6.5
-     */
-    bool pressMenu(KeyRequestType type) override;
-
-    /**
-     * @brief Simulates a press on the volume up key.
-     *
-     * @param[in] type one of @KeyRequestType
-     *
-     * @return true if the press volume up succeeded else false
-     *
-     * @since_tizen 6.5
-     */
-    bool pressVolUp(KeyRequestType type) override;
-
-    /**
-     * @brief Simulates a press on the volume down key.
-     *
-     * @param[in] type one of @KeyRequestType
-     *
-     * @return true if the press volume down succeeded else false
-     *
-     * @since_tizen 6.5
-     */
-    bool pressVolDown(KeyRequestType type) override;
-
-    /**
-     * @brief Simulates a press on the power key.
-     *
-     * @param[in] type one of @KeyRequestType
-     *
-     * @return true if the press power succeeded else false
-     *
-     * @since_tizen 6.5
-     */
-    bool pressPower(KeyRequestType type) override;
-
-    /**
      * @brief Simulates a press on the given keycode key.
      *
      * @param[in] keycode keycode
@@ -439,6 +373,18 @@ public:
      */
     int getTargetAngle();
 
+    /**
+     * @brief Simulates a press on the key.
+     *
+     * @param[in] keyType one of @KeyType
+     * @param[in] keyReqestType one of @KeyRequestType
+     *
+     * @return true if the press key succeeded else false
+     *
+     * @since_tizen 8.0
+     */
+    bool generateKey(KeyType keyType, KeyRequestType keyReqestType);
+
 private:
     /**
      * @brief Waits process idle.
index e9bb481..5df8d61 100644 (file)
@@ -146,46 +146,20 @@ bool MockDeviceImpl::wheelDown(int amount, const int durationMs)
 
 }
 
-bool MockDeviceImpl::pressBack(KeyRequestType type)
-{
-    mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::BACK, type, ""));
-    return true;
-}
-
-bool MockDeviceImpl::pressHome(KeyRequestType type)
-{
-    mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::HOME, type, ""));
-    return true;
-}
-
-bool MockDeviceImpl::pressMenu(KeyRequestType type)
-{
-    mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::MENU, type, ""));
-    return true;
-}
-
-bool MockDeviceImpl::pressVolUp(KeyRequestType type)
-{
-    mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::VOLUP, type, ""));
-    return true;
-}
-
-bool MockDeviceImpl::pressVolDown(KeyRequestType type)
+bool MockDeviceImpl::pressKeyCode(std::string keycode, KeyRequestType type)
 {
-    mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::VOLDOWN, type, ""));
+    mKeyDevice.push_back(std::tuple<KeyType, KeyRequestType, std::string>(KeyType::KEY, type, keycode));
     return true;
 }
 
-bool MockDeviceImpl::pressPower(KeyRequestType type)
+std::vector<std::tuple<KeyType, KeyRequestType, std::string>> MockDeviceImpl::getGeneratedKeys()
 {
-    mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::POWER, type, ""));
-    return true;
+    return mKeyDevice;
 }
 
-bool MockDeviceImpl::pressKeyCode(std::string keycode, KeyRequestType type)
+void MockDeviceImpl::clearGeneratedKeys()
 {
-    mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::KEY, type, keycode));
-    return true;
+    mKeyDevice.clear();
 }
 
 bool MockDeviceImpl::repeatKeyCode(std::string keycode, int intervalMs, int durationMs)
index 22eecd1..90e4fc9 100644 (file)
@@ -215,36 +215,6 @@ bool TizenDeviceImpl::drag(const int sx, const int sy, const int ex, const int e
     return true;
 }
 
-bool TizenDeviceImpl::pressBack(KeyRequestType type)
-{
-    return pressKeyCode("XF86Back", type);
-}
-
-bool TizenDeviceImpl::pressHome(KeyRequestType type)
-{
-    return pressKeyCode("XF86Home", type);
-}
-
-bool TizenDeviceImpl::pressMenu(KeyRequestType type)
-{
-    return pressKeyCode("XF86Menu", type);
-}
-
-bool TizenDeviceImpl::pressVolUp(KeyRequestType type)
-{
-    return pressKeyCode("XF86AudioRaiseVolume", type);
-}
-
-bool TizenDeviceImpl::pressVolDown(KeyRequestType type)
-{
-    return pressKeyCode("XF86AudioLowerVolume", type);
-}
-
-bool TizenDeviceImpl::pressPower(KeyRequestType type)
-{
-    return pressKeyCode("XF86PowerOff", type);
-}
-
 bool TizenDeviceImpl::pressKeyCode(std::string keycode, KeyRequestType type)
 {
     LOGI("pressKeyCode(%s)", keycode.c_str());
index 2dacea6..c53ee3f 100644 (file)
 using namespace Aurum;
 using namespace AurumInternal;
 
+class KeyAction {
+public:
+    KeyAction(IDevice *deviceImpl)
+    : mDeviceImpl(deviceImpl) {}
+    virtual ~KeyAction(){};
+
+    virtual bool perform(KeyRequestType type) = 0;
+protected:
+    IDevice *mDeviceImpl;
+};
+
+class BackKeyAction : public KeyAction {
+public:
+    BackKeyAction(IDevice *deviceImpl)
+    : KeyAction(deviceImpl) {}
+
+    virtual ~BackKeyAction(){};
+
+    bool perform(KeyRequestType type) override {
+        return mDeviceImpl->pressKeyCode("XF86Back", type);
+    }
+};
+
+class HomeKeyAction : public KeyAction {
+public:
+    HomeKeyAction(IDevice *deviceImpl)
+    : KeyAction(deviceImpl) {}
+
+    virtual ~HomeKeyAction(){};
+
+    bool perform(KeyRequestType type) override {
+        return mDeviceImpl->pressKeyCode("XF86Home", type);
+    }
+};
+
+class MenuKeyAction : public KeyAction {
+public:
+    MenuKeyAction(IDevice *deviceImpl)
+    : KeyAction(deviceImpl) {}
+
+    virtual ~MenuKeyAction(){};
+
+    bool perform(KeyRequestType type) override {
+        return mDeviceImpl->pressKeyCode("XF86Menu", type);
+    }
+};
+
+class VolUpKeyAction : public KeyAction {
+public:
+    VolUpKeyAction(IDevice *deviceImpl)
+    : KeyAction(deviceImpl) {}
+
+    virtual ~VolUpKeyAction(){};
+
+    bool perform(KeyRequestType type) override {
+        return mDeviceImpl->pressKeyCode("XF86AudioRaiseVolume", type);
+    }
+};
+
+class VolDownKeyAction : public KeyAction {
+public:
+    VolDownKeyAction(IDevice *deviceImpl)
+    : KeyAction(deviceImpl) {}
+
+    virtual ~VolDownKeyAction(){};
+
+    bool perform(KeyRequestType type) override {
+        return mDeviceImpl->pressKeyCode("XF86AudioLowerVolume", type);
+    }
+};
+
+class PowerKeyAction : public KeyAction {
+public:
+    PowerKeyAction(IDevice *deviceImpl)
+    : KeyAction(deviceImpl) {}
+
+    virtual ~PowerKeyAction(){};
+
+    bool perform(KeyRequestType type) override {
+        return mDeviceImpl->pressKeyCode("XF86PowerOff", type);
+    }
+};
+
 std::once_flag UiDevice::mOnceFlag;
 
 UiDevice::UiDevice() : UiDevice(nullptr) {}
@@ -244,45 +327,25 @@ bool UiDevice::wheelDown(int amount, const int durationMs)
     return result;
 }
 
-bool UiDevice::pressBack(KeyRequestType type)
+bool UiDevice::generateKey(KeyType keyType, KeyRequestType keyReqestType)
 {
-    bool result =  mDeviceImpl->pressBack(type);
-    waitForIdle();
-    return result;
-}
-
-bool UiDevice::pressHome(KeyRequestType type)
-{
-    bool result =  mDeviceImpl->pressHome(type);
-    waitForIdle();
-    return result;
-}
-
-bool UiDevice::pressMenu(KeyRequestType type)
-{
-    bool result =  mDeviceImpl->pressMenu(type);
-    waitForIdle();
-    return result;
-}
+    std::unique_ptr<KeyAction> keyAction;
 
-bool UiDevice::pressVolUp(KeyRequestType type)
-{
-    bool result =  mDeviceImpl->pressVolUp(type);
-    waitForIdle();
-    return result;
-}
+    if (keyType == KeyType::BACK)
+        keyAction = std::make_unique<BackKeyAction>(mDeviceImpl);
+    else if (keyType == KeyType::HOME)
+        keyAction = std::make_unique<HomeKeyAction>(mDeviceImpl);
+    else if (keyType == KeyType::MENU)
+        keyAction = std::make_unique<MenuKeyAction>(mDeviceImpl);
+    else if (keyType == KeyType::VOLUP)
+        keyAction = std::make_unique<VolUpKeyAction>(mDeviceImpl);
+    else if (keyType == KeyType::VOLDOWN)
+        keyAction = std::make_unique<VolDownKeyAction>(mDeviceImpl);
+    else if (keyType == KeyType::POWER)
+        keyAction = std::make_unique<PowerKeyAction>(mDeviceImpl);
 
-bool UiDevice::pressVolDown(KeyRequestType type)
-{
-    bool result =  mDeviceImpl->pressVolDown(type);
-    waitForIdle();
-    return result;
-}
+    bool result = keyAction->perform(keyReqestType);
 
-bool UiDevice::pressPower(KeyRequestType type)
-{
-    bool result =  mDeviceImpl->pressPower(type);
-    waitForIdle();
     return result;
 }
 
index 263d1a6..c1beb2d 100644 (file)
@@ -34,17 +34,17 @@ SendKeyCommand::SendKeyCommand(const ::aurum::ReqKey *request,
     KeyRequestType actionType = static_cast<KeyRequestType>(action_type);
 
     if (type == ::aurum::ReqKey_KeyType::ReqKey_KeyType_BACK)
-        mDevice->pressBack(actionType);
+        mDevice->generateKey(KeyType::BACK, actionType);
     else if (type == ::aurum::ReqKey_KeyType::ReqKey_KeyType_HOME)
-        mDevice->pressHome(actionType);
+        mDevice->generateKey(KeyType::HOME, actionType);
     else if (type == ::aurum::ReqKey_KeyType::ReqKey_KeyType_MENU)
-        mDevice->pressMenu(actionType);
+        mDevice->generateKey(KeyType::MENU, actionType);
     else if (type == ::aurum::ReqKey_KeyType::ReqKey_KeyType_VOLUP)
-        mDevice->pressVolUp(actionType);
+        mDevice->generateKey(KeyType::VOLUP, actionType);
     else if (type == ::aurum::ReqKey_KeyType::ReqKey_KeyType_VOLDOWN)
-        mDevice->pressVolDown(actionType);
+        mDevice->generateKey(KeyType::VOLDOWN, actionType);
     else if (type == ::aurum::ReqKey_KeyType::ReqKey_KeyType_POWER)
-        mDevice->pressPower(actionType);
+        mDevice->generateKey(KeyType::POWER, actionType);
     else if (type == ::aurum::ReqKey_KeyType::ReqKey_KeyType_WHEELUP)
         mDevice->wheelUp(1, 167);
     else if (type == ::aurum::ReqKey_KeyType::ReqKey_KeyType_WHEELDOWN)
index c14781c..d9b519f 100644 (file)
@@ -225,4 +225,30 @@ TEST_F(AurumTestUiDevice, getWindowRoot_N1)
 {
     auto windowroot = UiDevice::getInstance()->getWindowRoot();
     ASSERT_NE(windowroot.size(), 0);
-}
\ No newline at end of file
+}
+
+TEST_F(AurumTestUiDevice, pressKeyCode_P1)
+{
+    mDevice->pressKeyCode("BACK", KeyRequestType::PRESS);
+    mDevice->pressKeyCode("HOME", KeyRequestType::PRESS);
+    mDevice->pressKeyCode("MENU", KeyRequestType::PRESS);
+    mDevice->pressKeyCode("VOLUP", KeyRequestType::PRESS);
+    mDevice->pressKeyCode("VOLDOWN", KeyRequestType::PRESS);
+    mDevice->pressKeyCode("POWER", KeyRequestType::PRESS);
+
+    auto listOfKeys = mDevice->getGeneratedKeys();
+    auto key = listOfKeys[0];
+    ASSERT_EQ(std::get<2>(key), "BACK");
+    key = listOfKeys[1];
+    ASSERT_EQ(std::get<2>(key), "HOME");
+    key = listOfKeys[2];
+    ASSERT_EQ(std::get<2>(key), "MENU");
+    key = listOfKeys[3];
+    ASSERT_EQ(std::get<2>(key), "VOLUP");
+    key = listOfKeys[4];
+    ASSERT_EQ(std::get<2>(key), "VOLDOWN");
+    key = listOfKeys[5];
+    ASSERT_EQ(std::get<2>(key), "POWER");
+
+    mDevice->clearGeneratedKeys();
+}