Implements WatchBase
[platform/core/appfw/widget-viewer.git] / unittest / src / test_watch.cc
1 /*
2  * Copyright (c) 2020 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 #include <glib.h>
18 #include <gtest/gtest.h>
19 #include <gmock/gmock.h>
20
21 #include <watch.hh>
22 #include <watch_mirror.hh>
23
24 #include <evas_mock.h>
25
26 using namespace std;
27 using namespace tizen_base;
28 using namespace watch_holder;
29
30 gchar __not_null;
31
32 Ecore_Evas* __ecore_evas_ecore_evas_get_fake(const Evas* obj) {
33   return nullptr;
34 }
35
36 Ecore_Wl2_Window* __ecore_evas_wayland2_window_get_fake(const Ecore_Evas* obj) {
37   return nullptr;
38 }
39
40 int __elm_win_aux_hint_add_fake(Evas_Object* obj, const char* h, const char* v) {
41   return 0;
42 }
43
44 void __evas_object_smart_callback_add_fake(Evas_Object* obj, const char* event,
45   Evas_Smart_Cb func, const void* data) {
46   func((void*)data, nullptr, nullptr);
47 }
48
49 void* __evas_object_smart_callback_del_fake(Evas_Object* obj, const char* event,
50   Evas_Smart_Cb func) {
51   return nullptr;
52 }
53
54 Evas* __evas_object_evas_get_fake(const Eo *obj) {
55   return nullptr;
56 }
57
58 const char* __elm_win_aux_msg_key_get_fake(Evas_Object* obj, Elm_Win_Aux_Message *msg) {
59   return "tz_remote_surface_mng";
60 }
61
62 const char* __elm_win_aux_msg_val_get_fake(Evas_Object* obj, Elm_Win_Aux_Message *msg) {
63   return "prebind";
64 }
65
66 class WatchMirrorStub : public WatchMirror {
67  public:
68   WatchMirrorStub(Evas_Object* win)
69     : WatchMirror(win) {
70   }
71
72   void OnChanged(const ISharableWatchBase& watch) override {}
73   void OnAdded(const ISharableWatchBase& watch) override {}
74   void OnUpdated(const ISharableWatchBase& watch) override {}
75   void OnRemoved(const ISharableWatchBase& watch) override {}
76 };
77
78 class WatchStub : public Watch {
79  public:
80   WatchStub(string appid, Evas_Object* win, WatchMirrorStub* listener)
81     : Watch(appid, win, listener, true) {
82   }
83   WatchStub(int rid, std::string id, string appid, Evas_Object* win,
84       WatchMirrorStub* listener)
85     : Watch(rid, id, appid, win, listener, true) {
86   }
87   WatchStub(string appid, Evas_Object* win,
88       tizen_base::Bundle extra,
89       WatchMirrorStub* listener)
90     : Watch(appid, win, extra, listener, true) {
91   }
92   WatchStub(int rid, std::string id, string appid, Evas_Object* win,
93       tizen_base::Bundle extra,
94       WatchMirrorStub* listener)
95     : Watch(rid, id, appid, win, extra, listener, true) {
96   }
97
98   void InvokeEvasAdded() {
99   }
100
101   void InvokeEvasRemoved() {
102     OnRemoved("TestAppId", "TestInstId", 99, nullptr);
103   }
104
105   void InvokeEvasChanged() {
106   }
107
108   void InvokeSetFaulted(bool faulted) {
109     SetFaulted(faulted);
110   }
111 };
112
113 class WatchTest : public ::testing::Test {
114  public:
115   WatchStub* stub;
116   WatchMirrorStub* mirrorstub;
117
118   virtual void SetUp() {
119     Evas_Object* mirwin = elm_win_add(NULL, "Watch Mirror", ELM_WIN_BASIC);
120     mirrorstub = new WatchMirrorStub(mirwin);
121     Evas_Object* win = elm_win_add(NULL, "Watch", ELM_WIN_BASIC);
122     stub = new WatchStub(string("test"), win, mirrorstub);
123   }
124   virtual void TearDown() {
125     delete stub;
126     delete mirrorstub;
127   }
128 };
129
130 TEST_F(WatchTest, CreateInstance) {
131   EXPECT_NE(WatchTest::stub, nullptr);
132 }
133
134 TEST_F(WatchTest, CreateInstance2) {
135   Evas_Object* mirwin = elm_win_add(NULL, "Watch Mirror", ELM_WIN_BASIC);
136   WatchMirrorStub* mirrorstub = new WatchMirrorStub(mirwin);
137   EXPECT_NE(mirrorstub, nullptr);
138   Evas_Object* win = elm_win_add(NULL, "Watch", ELM_WIN_BASIC);
139   WatchStub* stub = new WatchStub(0, string("id"), string("test"), win, mirrorstub);
140   EXPECT_NE(stub, nullptr);
141   delete stub;
142   delete mirrorstub;
143 }
144
145 TEST_F(WatchTest, CreateInstance3) {
146   Bundle b;
147   b.Add("__APP_AMBIENT_EVENT__", "1");
148   b.Add("__APP_AMBIENT_SENDER__", "test");
149   Evas_Object* mirwin = elm_win_add(NULL, "Watch Mirror", ELM_WIN_BASIC);
150   WatchMirrorStub* mirrorstub = new WatchMirrorStub(mirwin);
151   EXPECT_NE(mirrorstub, nullptr);
152   Evas_Object* win = elm_win_add(NULL, "Watch", ELM_WIN_BASIC);
153   WatchStub* stub = new WatchStub(string("test"), win, b, mirrorstub);
154   EXPECT_NE(stub, nullptr);
155   delete stub;
156   delete mirrorstub;
157 }
158
159 TEST_F(WatchTest, CreateInstance4) {
160   Bundle b;
161   b.Add("__APP_AMBIENT_EVENT__", "1");
162   b.Add("__APP_AMBIENT_SENDER__", "test");
163   Evas_Object* mirwin = elm_win_add(NULL, "Watch Mirror", ELM_WIN_BASIC);
164   WatchMirrorStub* mirrorstub = new WatchMirrorStub(mirwin);
165   EXPECT_NE(mirrorstub, nullptr);
166   Evas_Object* win = elm_win_add(NULL, "Watch", ELM_WIN_BASIC);
167   WatchStub* stub = new WatchStub(0, string("id"),
168       string("test"), win, b, mirrorstub);
169   EXPECT_NE(stub, nullptr);
170   delete stub;
171   delete mirrorstub;
172 }
173
174 TEST_F(WatchTest, Terminate_N) {
175   int ret;
176   ret = WatchTest::stub->Terminate();
177   EXPECT_NE(ret, 0);
178 }
179
180 TEST_F(WatchTest, GetPid) {
181   int ret;
182   ret = WatchTest::stub->GetPid();
183   EXPECT_EQ(ret, 0);
184 }
185
186 TEST_F(WatchTest, IsBound) {
187   bool is_bound;
188   is_bound = WatchTest::stub->IsBound();
189   EXPECT_EQ(is_bound, false);
190 }
191
192 TEST_F(WatchTest, GetAppId) {
193   string appid;
194   appid = WatchTest::stub->GetAppId();
195   EXPECT_EQ(appid, "test");
196 }
197
198 TEST_F(WatchTest, GetExtra) {
199   Bundle b;
200   b = WatchTest::stub->GetExtra();
201   EXPECT_EQ(b.GetCount(), 0);
202 }
203
204 TEST_F(WatchTest, IsFaulted) {
205   bool is_faulted;
206   is_faulted = WatchTest::stub->IsFaulted();
207   EXPECT_EQ(is_faulted, false);
208 }
209
210 TEST_F(WatchTest, SetFaulted) {
211   bool is_faulted;
212   WatchTest::stub->InvokeSetFaulted(true);
213   is_faulted = WatchTest::stub->IsFaulted();
214   EXPECT_EQ(is_faulted, true);
215 }
216
217 TEST_F(WatchTest, BlockUpdate) {
218   EXPECT_NE(WatchTest::stub, nullptr);
219   WatchTest::stub->BlockUpdate(true);
220 }
221
222 TEST_F(WatchTest, NotifyChangedEvent_N) {
223   EXPECT_EQ(-1, WatchTest::stub->NotifyChangedEvent());
224 }
225
226 TEST_F(WatchTest, Bind) {
227   ecore_evas_ecore_evas_get_fake.custom_fake = __ecore_evas_ecore_evas_get_fake;
228   ecore_evas_wayland2_window_get_fake.custom_fake = __ecore_evas_wayland2_window_get_fake;
229   elm_win_aux_hint_add_fake.custom_fake = __elm_win_aux_hint_add_fake;
230   evas_object_smart_callback_add_fake.custom_fake = __evas_object_smart_callback_add_fake;
231   evas_object_smart_callback_del_fake.custom_fake = __evas_object_smart_callback_del_fake;
232   evas_object_evas_get_fake.custom_fake = __evas_object_evas_get_fake;
233   elm_win_aux_msg_key_get_fake.custom_fake = __elm_win_aux_msg_key_get_fake;
234   elm_win_aux_msg_val_get_fake.custom_fake = __elm_win_aux_msg_val_get_fake;
235
236   Evas_Object* bindwin = elm_win_add(NULL, "Bind Window", ELM_WIN_BASIC);
237   EXPECT_NE(WatchTest::stub, nullptr);
238   WatchTest::stub->Bind(bindwin);
239 }
240
241 TEST_F(WatchTest, OnEvasAdded) {
242   EXPECT_NE(WatchTest::stub, nullptr);
243   WatchTest::stub->InvokeEvasAdded();
244 }
245
246 TEST_F(WatchTest, OnEvasRemoved) {
247   EXPECT_NE(WatchTest::stub, nullptr);
248   WatchTest::stub->InvokeEvasRemoved();
249 }
250
251 TEST_F(WatchTest, OnEvasChanged) {
252   EXPECT_NE(WatchTest::stub, nullptr);
253   WatchTest::stub->InvokeEvasChanged();
254 }