libaurum: Apply factory pattern for OCP, LSP using TDD
authorWoochan Lee <wc0917.lee@samsung.com>
Mon, 29 Jan 2024 11:30:01 +0000 (20:30 +0900)
committerHosang Kim <hosang12.kim@samsung.com>
Tue, 6 Feb 2024 04:11:27 +0000 (13:11 +0900)
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 655ff7cafa178fe92db74de962b7b704f5a77dc6..6aa62be9a1105f67a15a4e05d7b67a1d8f8eb28a 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:
@@ -108,42 +98,6 @@ public:
      */
     bool wheelDown(int amount, const int durationMs) override;
 
-    /**
-     * @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
@@ -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 a437838902b03d5d12a0ca7acca469b8384fdcb5..aefc5041c5b74e36f5a97f0d45164140c0c7bc77 100644 (file)
@@ -78,36 +78,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()
      */
index 6338fa52eb82e5964ee95c5950cba57bac7d9e7d..ccb4ddbbe9e54821cdcb6f4c34e48c2015a6492f 100644 (file)
@@ -49,6 +49,21 @@ enum class KeyRequestType {
     REPEAT,      //Repeat key press(100ms) and release
 };
 
+/**
+ * @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
@@ -169,72 +184,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.
      *
index d952f3d1d827fb11398083a708a8177d8f19f4ce..344857ce2f199a2cd0dacf89565ae57c70de165d 100644 (file)
@@ -152,72 +152,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.
      *
@@ -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 e9bb481c9bd74b97b0525589f64797a54d2d87c3..5df8d619962879140edc6513a1191927a2119726 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 22eecd1b18b7fdb0311f8f0cd8794ef115b8fa89..90e4fc9fb0bd483c4c0021ca6074f71610e048e2 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 2dacea623c23e24472a7d926757cc8c8c8958043..c53ee3ff29106f0742df15064b3fa101bba1f219 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 263d1a6245001c6ea483a8248c2be351cf5ad054..c1beb2d68d0859090ca63643890e2b18428bc88a 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 c14781cd4b88c9569f746687ee7c645018cca5f6..d9b519ff2065811ef8c17bf359cc46ce395236fb 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();
+}