Change unittest package name and improves code coverage
[platform/core/appfw/appcore-widget.git] / include / widget_app.hpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef TIZEN_APPFW_WIDGET_APP_HPP_
18 #define TIZEN_APPFW_WIDGET_APP_HPP_
19
20 #include <widget_app.h>
21 #include <widget_app_efl.h>
22
23 #include <ctime>
24 #include <list>
25 #include <memory>
26
27 #include <app_common.hpp>
28 #include <bundle_cpp.h>
29
30 namespace tizen_appfw {
31
32 class WidgetAppBase : public app_common::AppBase<
33     decltype(widget_app_add_event_handler)*,
34     decltype(widget_app_remove_event_handler)*> {
35  public:
36   using Remover = decltype(widget_app_remove_event_handler)*;
37
38   WidgetAppBase()
39       : AppBase(widget_app_add_event_handler,
40                 widget_app_remove_event_handler) {}
41
42   virtual void OnCreate() {}
43   virtual void OnTerminate() {}
44
45   class InstanceBase {
46    public:
47     class Factory {
48      public:
49       ~Factory() = default;
50       virtual std::unique_ptr<InstanceBase> Create(widget_context_h h) = 0;
51     };
52
53     enum class DestroyType {
54       PERMANENT = 0x00,
55       TEMPORARY = 0x01,
56     };
57
58     InstanceBase(widget_context_h h) : handle_(h) {}
59     virtual ~InstanceBase() = default;
60
61     virtual void OnCreate(const tizen_base::Bundle& content, int w, int h) = 0;
62     virtual void OnDestroy(DestroyType reason, tizen_base::Bundle* content) = 0;
63     virtual void OnPause() = 0;
64     virtual void OnResume() = 0;
65     virtual void OnResize(int w, int h) = 0;
66     virtual void OnUpdate(const tizen_base::Bundle& content, bool force) = 0;
67     widget_context_h GetHandle() const {
68       return handle_;
69     }
70
71     void Finish() {
72       widget_app_terminate_context(handle_);
73     }
74
75     std::string GetId() const {
76       return std::string(widget_app_get_id(handle_));
77     }
78
79     void SetContextInfo(const tizen_base::Bundle& info) {
80       widget_app_context_set_content_info(handle_, info.GetHandle());
81     }
82
83     void SetTitle(const std::string& title) {
84       widget_app_context_set_title(handle_, title.c_str());
85     }
86
87     Evas_Object* GetElmWin() const {
88       Evas_Object* win = nullptr;
89       widget_app_get_elm_win(handle_, &win);
90       return win;
91     }
92
93    private:
94     widget_context_h handle_;
95   };
96
97   int Run(int argc, char** argv,
98       std::unique_ptr<InstanceBase::Factory> factory) {
99     factory_ = std::move(factory);
100 /* LCOV_EXCL_START */
101     widget_app_lifecycle_callback_s callback = {
102       create : [](void *user_data) -> widget_class_h {
103         WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
104         b->OnCreate();
105         widget_instance_lifecycle_callback_s callback = {
106           create : [](widget_context_h context, bundle* content, int w, int h,
107               void* user_data) -> int {
108             WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
109             auto i = b->factory_->Create(context);
110             i->OnCreate(tizen_base::Bundle(content, false, false), w, h);
111             b->instances_.push_back(std::move(i));
112             return 0;
113           },
114           destroy : [](widget_context_h context,
115               widget_app_destroy_type_e reason, bundle* content,
116               void* user_data) -> int {
117             WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
118
119             for (auto& i : b->instances_) {
120               if (i->GetHandle() == context) {
121                 tizen_base::Bundle bd(content, false, false);
122                 i->OnDestroy(
123                     static_cast<InstanceBase::DestroyType>(reason), &bd);
124                 b->instances_.remove(i);
125                 break;
126               }
127             }
128
129             return 0;
130           },
131           pause : [](widget_context_h context, void* user_data) -> int {
132             WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
133             auto* i = b->FindInst(context);
134             if (!i)
135               return -1;
136             i->OnPause();
137             return 0;
138           },
139           resume : [](widget_context_h context, void* user_data) -> int {
140             WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
141             auto* i = b->FindInst(context);
142             if (!i)
143               return -1;
144             i->OnResume();
145             return 0;
146           },
147           resize : [](widget_context_h context, int w, int h,
148               void* user_data) -> int {
149             WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
150             auto* i = b->FindInst(context);
151             if (!i)
152               return -1;
153             i->OnResize(w, h);
154             return 0;
155           },
156           update : [](widget_context_h context, bundle* content, int force,
157               void* user_data) -> int {
158             WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
159             auto* i = b->FindInst(context);
160             if (!i)
161               return -1;
162             i->OnUpdate(tizen_base::Bundle(content), force);
163             return 0;
164           }
165         };
166         auto h = widget_app_class_create(callback, user_data);
167         return h;
168       },
169       terminate : [](void* user_data) {
170         WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
171         b->OnTerminate();
172       }
173     };
174 /* LCOV_EXCL_STOP */
175
176     return widget_app_main(argc, argv, &callback, this);
177   }
178
179   void Exit() noexcept {
180     widget_app_exit();
181   }
182
183  private:
184   InstanceBase* FindInst(widget_context_h h) {
185     for (auto& i : instances_) {
186       if (i->GetHandle() == h) {
187         return i.get();
188       }
189     }
190
191     return nullptr;
192   }
193
194   std::unique_ptr<InstanceBase::Factory> factory_;
195   std::list<std::unique_ptr<InstanceBase>> instances_;
196 };
197
198 }  // namespace tizen_appfw
199
200 #endif  // TIZEN_APPFW_WIDGET_APP_HPP_