87146f863952cb2949eaa48ac99a32fb6dc17af2
[platform/core/appfw/librua.git] / tests / unittest / rua_unit_test.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 <gtest/gtest.h>
18
19 #include <rua.h>
20
21 #include <string>
22 #include <vector>
23 #include <memory>
24
25 #include "aul_mock.h"
26 #include "sqlite3_mock.h"
27 #include "gio_mock.h"
28 #include "test_fixture.h"
29
30 using ::testing::_;
31 using ::testing::DoAll;
32 using ::testing::SetArgPointee;
33 using ::testing::Return;
34
35 class Mocks : public ::testing::NiceMock<AulMock>,
36               public ::testing::NiceMock<GioMock>,
37               public ::testing::NiceMock<SqlMock> {};
38
39 class RuaTest : public TestFixture {
40  public:
41   RuaTest() : TestFixture(std::make_unique<Mocks>()) {}
42   virtual ~RuaTest() {}
43
44   virtual void SetUp() {
45   }
46
47   virtual void TearDown() {
48   }
49 };
50
51 static void __rua_history_update_cb(char** table, int nrows, int ncols,
52     void* user_data) {
53 }
54
55 TEST_F(RuaTest, rua_add_history_for_uid) {
56   EXPECT_CALL(GetMock<AulMock>(),
57       aul_add_rua_history_for_uid(_, _)).
58           WillOnce(Return(0));
59
60   int ret = rua_add_history_for_uid(const_cast<char*>("test"),
61       const_cast<char*>("/app_path"), const_cast<char*>("arg"), 5001);
62   EXPECT_EQ(ret, 0);
63 }
64
65 TEST_F(RuaTest, rua_delete_history_with_pkgname) {
66   EXPECT_CALL(GetMock<AulMock>(),
67       aul_delete_rua_history_for_uid(_, _)).
68           WillOnce(Return(0));
69
70   int ret = rua_delete_history_with_pkgname(const_cast<char*>("test"));
71   EXPECT_EQ(ret, 0);
72 }
73
74 TEST_F(RuaTest, rua_delete_history_with_apppath) {
75   EXPECT_CALL(GetMock<AulMock>(),
76       aul_delete_rua_history_for_uid(_, _)).
77           WillOnce(Return(0));
78
79   int ret = rua_delete_history_with_apppath(const_cast<char*>("/app_path"));
80   EXPECT_EQ(ret, 0);
81 }
82
83 TEST_F(RuaTest, rua_clear_history) {
84   EXPECT_CALL(GetMock<AulMock>(),
85       aul_delete_rua_history_for_uid(_, _)).
86           WillOnce(Return(0));
87
88   int ret = rua_clear_history();
89   EXPECT_EQ(ret, 0);
90 }
91
92 TEST_F(RuaTest, rua_history_load_db_N) {
93   int ret, nrows, ncols;
94   ret = rua_history_load_db(nullptr, &nrows, &ncols);
95   EXPECT_NE(ret, 0);
96 }
97
98 TEST_F(RuaTest, rua_register_update_cb_N) {
99   EXPECT_CALL(GetMock<GioMock>(),
100       g_dbus_connection_signal_subscribe(_, _, _, _, _, _, _, _, _, _)).
101       WillOnce(Return(0));
102
103   int id;
104   int ret = rua_register_update_cb(__rua_history_update_cb, nullptr, &id);
105   EXPECT_NE(ret, 0);
106 }
107
108 TEST_F(RuaTest, rua_unregister_update_cb_N) {
109   int ret = rua_unregister_update_cb(1);
110   EXPECT_NE(ret, 0);
111 }
112
113 static char** __create_table(void) {
114   char** table = (char** )calloc(10, sizeof(char*));
115   table[0] = strdup("pkgname");
116   if (table[0] == nullptr)
117     goto out;
118
119   table[1] = strdup("apppath");
120   if (table[1] == nullptr)
121     goto out;
122
123   table[2] = strdup("arg");
124   if (table[2] == nullptr)
125     goto out;
126
127   table[3] = strdup("122232");
128   if (table[3] == nullptr)
129     goto out;
130
131   table[4] = strdup("instance_id");
132   if (table[4] == nullptr)
133     goto out;
134
135   table[5] = strdup("instance_name");
136   if (table[5] == nullptr)
137     goto out;
138
139   table[6] = strdup("icon");
140   if (table[6] == nullptr)
141     goto out;
142
143   table[7] = strdup("uri");
144   if (table[7] == nullptr)
145     goto out;
146
147   table[8] = strdup("image");
148   if (table[8] == nullptr)
149     goto out;
150
151   table[9] = strdup("compid");
152   if (table[9] == nullptr)
153     goto out;
154
155   return table;
156
157 out:
158   for (int i = 0; i < 10; i++) {
159     if (table[i])
160       free(table[i]);
161   }
162
163   free(table);
164   return nullptr;
165 }
166
167 TEST_F(RuaTest, rua_history_get_rec) {
168   char** table = __create_table();
169   ASSERT_TRUE(table != nullptr);
170
171   EXPECT_CALL(GetMock<SqlMock>(),
172       sqlite3_open_v2(_, _, _, _)).WillOnce(Return(0));
173   EXPECT_CALL(GetMock<SqlMock>(),
174       sqlite3_close_v2(_)).WillOnce(Return(0));
175   EXPECT_CALL(GetMock<SqlMock>(),
176       sqlite3_free_table(_));
177   EXPECT_CALL(GetMock<SqlMock>(),
178       sqlite3_busy_handler(_, _, _)).WillOnce(Return(0));
179   EXPECT_CALL(GetMock<SqlMock>(),
180       sqlite3_get_table(_, _, _, _, _, _)).
181       WillOnce(DoAll(SetArgPointee<2>(table),
182                      SetArgPointee<3>(1),
183                      SetArgPointee<4>(0), (Return(0))));
184
185   char** get_table = nullptr;
186   int rows = 0;
187   int cols = 0;
188   struct rua_rec record;
189   int ret;
190   ret = rua_history_load_db(&get_table, &rows, &cols);
191   EXPECT_EQ(ret, 0);
192   EXPECT_TRUE(get_table != nullptr);
193
194   ret = rua_history_get_rec(&record, get_table, rows, cols, 0);
195   EXPECT_EQ(ret, 0);
196
197   rua_history_unload_db(&get_table);
198
199   for (int i = 0; i < 10; i++)
200     free(table[i]);
201   free(table);
202 }
203
204 TEST_F(RuaTest, rua_is_latest_app_N) {
205   int ret;
206   ret = rua_is_latest_app("pkgname");
207   EXPECT_NE(ret, 0);
208 }
209
210 TEST_F(RuaTest, rua_delete_history_with_instance_id) {
211   EXPECT_CALL(GetMock<AulMock>(),
212       aul_delete_rua_history_for_uid(_, _)).
213           WillOnce(Return(0));
214
215   int ret;
216   ret = rua_delete_history_with_instance_id("appid", "instanceid");
217   EXPECT_EQ(ret, 0);
218 }