2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
26 #include "sqlite3_mock.h"
28 #include "test_fixture.h"
31 using ::testing::DoAll;
32 using ::testing::SetArgPointee;
33 using ::testing::Return;
35 class Mocks : public ::testing::NiceMock<AulMock>,
36 public ::testing::NiceMock<GioMock>,
37 public ::testing::NiceMock<SqlMock> {};
39 class RuaTest : public TestFixture {
41 RuaTest() : TestFixture(std::make_unique<Mocks>()) {}
44 virtual void SetUp() {
47 virtual void TearDown() {
51 static void __rua_history_update_cb(char** table, int nrows, int ncols,
55 TEST_F(RuaTest, rua_add_history_for_uid) {
56 EXPECT_CALL(GetMock<AulMock>(),
57 aul_add_rua_history_for_uid(_, _)).
60 int ret = rua_add_history_for_uid(const_cast<char*>("test"),
61 const_cast<char*>("/app_path"), const_cast<char*>("arg"), 5001);
65 TEST_F(RuaTest, rua_delete_history_with_pkgname) {
66 EXPECT_CALL(GetMock<AulMock>(),
67 aul_delete_rua_history_for_uid(_, _)).
70 int ret = rua_delete_history_with_pkgname(const_cast<char*>("test"));
74 TEST_F(RuaTest, rua_delete_history_with_apppath) {
75 EXPECT_CALL(GetMock<AulMock>(),
76 aul_delete_rua_history_for_uid(_, _)).
79 int ret = rua_delete_history_with_apppath(const_cast<char*>("/app_path"));
83 TEST_F(RuaTest, rua_clear_history) {
84 EXPECT_CALL(GetMock<AulMock>(),
85 aul_delete_rua_history_for_uid(_, _)).
88 int ret = rua_clear_history();
92 TEST_F(RuaTest, rua_history_load_db_N) {
93 int ret, nrows, ncols;
94 ret = rua_history_load_db(nullptr, &nrows, &ncols);
98 TEST_F(RuaTest, rua_register_update_cb_N) {
99 EXPECT_CALL(GetMock<GioMock>(),
100 g_dbus_connection_signal_subscribe(_, _, _, _, _, _, _, _, _, _)).
104 int ret = rua_register_update_cb(__rua_history_update_cb, nullptr, &id);
108 TEST_F(RuaTest, rua_unregister_update_cb_N) {
109 int ret = rua_unregister_update_cb(1);
113 static char** __create_table(void) {
114 char** table = (char** )calloc(10, sizeof(char*));
115 table[0] = strdup("pkgname");
116 if (table[0] == nullptr)
119 table[1] = strdup("apppath");
120 if (table[1] == nullptr)
123 table[2] = strdup("arg");
124 if (table[2] == nullptr)
127 table[3] = strdup("122232");
128 if (table[3] == nullptr)
131 table[4] = strdup("instance_id");
132 if (table[4] == nullptr)
135 table[5] = strdup("instance_name");
136 if (table[5] == nullptr)
139 table[6] = strdup("icon");
140 if (table[6] == nullptr)
143 table[7] = strdup("uri");
144 if (table[7] == nullptr)
147 table[8] = strdup("image");
148 if (table[8] == nullptr)
151 table[9] = strdup("compid");
152 if (table[9] == nullptr)
158 for (int i = 0; i < 10; i++) {
167 TEST_F(RuaTest, rua_history_get_rec) {
168 char** table = __create_table();
169 ASSERT_TRUE(table != nullptr);
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),
183 SetArgPointee<4>(0), (Return(0))));
185 char** get_table = nullptr;
188 struct rua_rec record;
190 ret = rua_history_load_db(&get_table, &rows, &cols);
192 EXPECT_TRUE(get_table != nullptr);
194 ret = rua_history_get_rec(&record, get_table, rows, cols, 0);
197 rua_history_unload_db(&get_table);
199 for (int i = 0; i < 10; i++)
204 TEST_F(RuaTest, rua_is_latest_app_N) {
206 ret = rua_is_latest_app("pkgname");
210 TEST_F(RuaTest, rua_delete_history_with_instance_id) {
211 EXPECT_CALL(GetMock<AulMock>(),
212 aul_delete_rua_history_for_uid(_, _)).
216 ret = rua_delete_history_with_instance_id("appid", "instanceid");