Implements WatchBase
[platform/core/appfw/widget-viewer.git] / unittest / src / test_watch_mirror.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_mirror.hh>
22 #include <watch.hh>
23 #include <vconf_mock.h>
24
25 using namespace std;
26 using namespace tizen_base;
27 using namespace watch_holder;
28
29 class WatchMirrorStub : public WatchMirror {
30  public:
31   WatchMirrorStub(Evas_Object* win)
32     : WatchMirror(win) {
33   }
34
35   void OnChanged(const ISharableWatchBase& watch) override {}
36   void OnAdded(const ISharableWatchBase& watch) override {}
37   void OnUpdated(const ISharableWatchBase& watch) override {}
38   void OnRemoved(const ISharableWatchBase& watch) override {}
39 };
40
41 class SharableWatchStub : public ISharableWatchBase {
42  public:
43
44   void Resume() override;
45   void Pause() override;
46   std::shared_ptr<screen_connector::WlBuffer> GetCurrentImage() const override;
47   bool IsBound() const override;
48   std::string GetAppId() const override;
49   int GetPid() const override;
50   virtual tizen_base::Bundle GetExtra() const override;
51   bool IsFaulted() const override;
52   void BlockUpdate(bool enable) override;
53 };
54
55 static int __vconf_notify_key_changed_fake(const char* key, vconf_callback_fn cb,
56   void* data) {
57   return 0;
58 }
59
60 static int __vconf_notify_key_changed_fake2(const char* key, vconf_callback_fn cb,
61   void* data) {
62   return -1;
63 }
64
65 static char* __vconf_get_str_fake(const char* key) {
66   bundle_raw* raw;
67   int len;
68   bundle* b = bundle_create();
69   bundle_add_str(b, NOTIFY_CHANGED_EVENT_APPID_KEY, "appid");
70   bundle_add_str(b, NOTIFY_CHANGED_EVENT_RID_KEY, "rid");
71
72   bundle_encode(b, &raw, &len);
73   bundle_free(b);
74   return (char*)raw;
75 }
76
77
78 class WatchMirrorTest : public ::testing::Test {
79  public:
80   WatchMirrorStub* mirrorstub;
81
82   virtual void SetUp() {
83     Evas_Object* mirwin = elm_win_add(NULL, "Watch Mirror", ELM_WIN_BASIC);
84     mirrorstub = new WatchMirrorStub(mirwin);
85   }
86   virtual void TearDown() {
87     delete mirrorstub;
88   }
89 };
90
91  TEST_F(WatchMirrorTest, CreateMirrorInstance) {
92    EXPECT_NE(WatchMirrorTest::mirrorstub, nullptr);
93  }
94
95 TEST_F(WatchMirrorTest, MirrorListen) {
96   int ret;
97
98   vconf_notify_key_changed_fake.custom_fake = __vconf_notify_key_changed_fake;
99   vconf_get_str_fake.custom_fake = __vconf_get_str_fake;
100
101   ret = WatchMirrorTest::mirrorstub->Listen();
102   EXPECT_EQ(ret, 0);
103 }
104
105 TEST_F(WatchMirrorTest, MirrorListen_N) {
106   int ret;
107
108   vconf_notify_key_changed_fake.custom_fake = __vconf_notify_key_changed_fake2;
109
110   ret = WatchMirrorTest::mirrorstub->Listen();
111   EXPECT_NE(ret, 0);
112 }
113
114 TEST_F(WatchMirrorTest, MirrorGetCurrent_N) {
115   shared_ptr<ISharableWatchBase> sw;
116   sw = WatchMirrorTest::mirrorstub->GetCurrent();
117   EXPECT_EQ(sw, nullptr);
118 }
119
120 TEST_F(WatchMirrorTest, OnChanged) {
121   Evas_Object* watchwin = elm_win_add(NULL, "Watch", ELM_WIN_BASIC);
122   Watch *w = new Watch(string("test"), watchwin, nullptr, WatchMirrorTest::mirrorstub);
123   EXPECT_NE(w, nullptr);
124   WatchMirror *mr = new WatchMirror(watchwin);
125   EXPECT_NE(mr, nullptr);
126   mr->OnChanged(*w);
127   delete w;
128   delete mr;
129 }
130
131 TEST_F(WatchMirrorTest, OnAdded) {
132   Evas_Object* watchwin = elm_win_add(NULL, "Watch", ELM_WIN_BASIC);
133   SharableWatchStub *w = nullptr;
134
135   WatchMirror *mr = new WatchMirror(watchwin);
136   EXPECT_NE(mr, nullptr);
137   mr->OnAdded(*w);
138
139   delete mr;
140 }
141
142 TEST_F(WatchMirrorTest, OnUpdated) {
143   Evas_Object* watchwin = elm_win_add(NULL, "Watch", ELM_WIN_BASIC);
144   SharableWatchStub *w = nullptr;
145
146   WatchMirror *mr = new WatchMirror(watchwin);
147   EXPECT_NE(mr, nullptr);
148   mr->OnUpdated(*w);
149
150   delete mr;
151 }
152
153 TEST_F(WatchMirrorTest, OnRemoved) {
154   Evas_Object* watchwin = elm_win_add(NULL, "Watch", ELM_WIN_BASIC);
155   SharableWatchStub *w = nullptr;
156
157   WatchMirror *mr = new WatchMirror(watchwin);
158   EXPECT_NE(mr, nullptr);
159   mr->OnRemoved(*w);
160
161   delete mr;
162 }
163
164 TEST_F(WatchMirrorTest, OnAmbientChanged) {
165   Bundle *b = nullptr;
166   Evas_Object* watchwin = elm_win_add(NULL, "Watch", ELM_WIN_BASIC);
167   WatchMirror *mr = new WatchMirror(watchwin);
168   EXPECT_NE(mr, nullptr);
169   mr->OnAmbientChanged(true, *b);
170   delete mr;
171 }
172
173 TEST_F(WatchMirrorTest, OnAmbientEvent) {
174   Bundle b;
175   b.Add("__APP_AMBIENT_EVENT__", "1");
176   Evas_Object* watchwin = elm_win_add(NULL, "Watch", ELM_WIN_BASIC);
177   WatchMirror *mr = new WatchMirror(watchwin);
178   EXPECT_NE(mr, nullptr);
179   mr->OnAmbientEvent(AmbientListener::EVENT_AOD_READY, string("sender"), b);
180   delete mr;
181 }