2 * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
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.
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
23 #include <wifi-manager.h>
24 #include <system_info.h>
27 using ::testing::InitGoogleTest;
28 using ::testing::Test;
29 using ::testing::TestCase;
31 static char ap_name[MAX_AP_LENGTH];
32 static wifi_manager_h wifi = NULL;
33 static wifi_manager_error_e rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
34 wifi_manager_ap_h g_hWifiAP;
37 static bool __check_feature_supported(char *key)
40 int ret = system_info_get_platform_bool(key, &value);
42 EXPECT_EQ(SYSTEM_INFO_ERROR_NONE, ret) << "system_info_get_platform_bool failed";
43 EXPECT_EQ(true, value) << key << " feature is not supported";
48 static void __test_callback(wifi_manager_error_e result, void* user_data)
54 static gboolean __timeout_callback(gpointer data)
56 EXPECT_TRUE(0) << "Wi-Fi Manager callback timeout!";
61 static bool __found_ap_callback(wifi_manager_ap_h ap, void *user_data)
64 char *ap_name_part = (char *)user_data;
65 size_t ap_name_len = strlen(ap_name_part);
66 int ret = WIFI_MANAGER_ERROR_NONE;
69 ret = wifi_manager_ap_get_essid(ap, &ap_name);
70 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Unable to get essid";
75 if (!strncmp(ap_name, ap_name_part, ap_name_len)) {
77 ret = wifi_manager_ap_clone(&g_hWifiAP, ap);
78 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Unable to clone the AP handle";
80 rst = WIFI_MANAGER_ERROR_NONE;
89 TEST(Hal_wifi, Init_p)
91 g_bFeatureWifi = __check_feature_supported((char*)FEATURE_WIFI);
92 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
94 int rv = wifi_manager_initialize(&wifi);
95 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Initialization failure";
98 TEST(Hal_wifi, Activate_p)
100 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
105 rv = wifi_manager_is_activated(wifi, &activated);
106 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to get Wi-Fi device state";
111 rv = wifi_manager_activate(wifi, __test_callback, NULL);
112 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to activate Wi-Fi device";
114 RUN_GMAIN_LOOP(__timeout_callback);
116 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Activetion failure";
117 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
120 rv = access(WIFI_ADDRESS_PATH, F_OK);
121 EXPECT_EQ(0, rv) << "Could not access " << WIFI_ADDRESS_PATH;
124 TEST(Hal_wifi, Scan_p)
126 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
128 int rv = wifi_manager_scan(wifi, __test_callback, NULL);
129 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to scan";
131 RUN_GMAIN_LOOP(__timeout_callback);
133 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Scan failure";
134 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
137 TEST(Hal_wifi, Connect_p)
139 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
141 wifi_manager_connection_state_e connection_state;
142 int ret = WIFI_MANAGER_ERROR_NONE;
144 ret = wifi_manager_get_connection_state(wifi, &connection_state);
145 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to get connection state";
147 if (connection_state == WIFI_MANAGER_CONNECTION_STATE_CONNECTED)
153 ret = wifi_manager_foreach_found_ap(wifi, __found_ap_callback, (char *)ap_name);
154 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Failed to find the AP";
155 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
157 ret = wifi_manager_connect(wifi, g_hWifiAP, __test_callback, NULL);
158 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to connect";
160 RUN_GMAIN_LOOP(__timeout_callback);
162 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Connection failure";
163 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
166 TEST(Hal_wifi, Disconnect_p)
168 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
170 int ret = WIFI_MANAGER_ERROR_NONE;
171 wifi_manager_ap_h hAP = NULL;
173 ret = wifi_manager_get_connected_ap(wifi, &hAP);
174 if (ret == WIFI_MANAGER_ERROR_NO_CONNECTION)
176 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to get connected AP";
178 ret = wifi_manager_disconnect(wifi, hAP, __test_callback, NULL);
179 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to disconnect";
181 RUN_GMAIN_LOOP(__timeout_callback);
183 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Disconnection failure";
184 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
186 ret = wifi_manager_forget_ap(wifi, hAP);
187 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to forget AP";
190 TEST(Hal_wifi, Deactivate_p)
192 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
194 int rv = wifi_manager_deactivate(wifi, __test_callback, NULL);
195 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to deactivate Wi-Fi device";
197 RUN_GMAIN_LOOP(__timeout_callback);
199 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Deactivation failure";
200 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
202 rv = access(WIFI_ADDRESS_PATH, F_OK);
203 EXPECT_EQ(-1, rv) << WIFI_ADDRESS_PATH << " is exist";
206 TEST(Hal_wifi, Deinit_p)
208 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
210 int rv = wifi_manager_deinitialize(wifi);
211 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Deinitialization failure";
214 int main(int argc, char **argv)
216 if (argc > 1 && argv[argc - 1][0] != '-') {
217 g_strlcpy(ap_name, argv[argc - 1], MAX_AP_LENGTH);
221 InitGoogleTest(&argc, argv);
222 return RUN_ALL_TESTS();