1 /* 2 * Copyright (C) 2008 Holger Hans Peter Freyther 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 */ 19 20 #include <glib.h> 21 #include <gtk/gtk.h> 22 #include <webkit/webkit.h> 23 24 #if GTK_CHECK_VERSION(2, 14, 0) 25 26 static void test_webkit_web_history_item_lifetime(void) 27 { 28 WebKitWebView* webView; 29 WebKitWebBackForwardList* backForwardList; 30 WebKitWebHistoryItem* currentItem; 31 WebKitWebHistoryItem* forwardItem; 32 WebKitWebHistoryItem* backItem; 33 WebKitWebHistoryItem* nthItem; 34 WebKitWebHistoryItem* item1; 35 WebKitWebHistoryItem* item2; 36 WebKitWebHistoryItem* item3; 37 WebKitWebHistoryItem* item4; 38 GList* backList = NULL; 39 GList* forwardList = NULL; 40 g_test_bug("19898"); 41 42 webView = WEBKIT_WEB_VIEW(webkit_web_view_new()); 43 g_object_ref_sink(webView); 44 backForwardList = webkit_web_view_get_back_forward_list(webView); 45 g_assert_cmpint(G_OBJECT(backForwardList)->ref_count, ==, 1); 46 47 /* add test items */ 48 item1 = webkit_web_history_item_new_with_data("http://example.com/1/", "Site 1"); 49 webkit_web_back_forward_list_add_item(backForwardList, item1); 50 g_object_unref(item1); 51 52 item2 = webkit_web_history_item_new_with_data("http://example.com/2/", "Site 2"); 53 webkit_web_back_forward_list_add_item(backForwardList, item2); 54 g_object_unref(item2); 55 56 item3 = webkit_web_history_item_new_with_data("http://example.com/3/", "Site 3"); 57 webkit_web_back_forward_list_add_item(backForwardList, item3); 58 g_object_unref(item3); 59 60 item4 = webkit_web_history_item_new_with_data("http://example.com/4/", "Site 4"); 61 webkit_web_back_forward_list_add_item(backForwardList, item4); 62 g_object_unref(item4); 63 64 /* make sure these functions don't add unnecessary ref to the history item */ 65 backItem = webkit_web_back_forward_list_get_back_item(backForwardList); 66 g_object_ref(backItem); 67 g_assert_cmpint(G_OBJECT(backItem)->ref_count, ==, 2); 68 g_object_unref(backItem); 69 g_assert_cmpint(G_OBJECT(backItem)->ref_count, ==, 1); 70 71 currentItem = webkit_web_back_forward_list_get_current_item(backForwardList); 72 g_object_ref(currentItem); 73 g_assert_cmpint(G_OBJECT(currentItem)->ref_count, ==, 2); 74 g_object_unref(currentItem); 75 g_assert_cmpint(G_OBJECT(currentItem)->ref_count, ==, 1); 76 77 webkit_web_back_forward_list_go_to_item(backForwardList, item2); 78 forwardItem = webkit_web_back_forward_list_get_forward_item(backForwardList); 79 g_object_ref(forwardItem); 80 g_assert_cmpint(G_OBJECT(forwardItem)->ref_count, ==, 2); 81 g_object_unref(forwardItem); 82 g_assert_cmpint(G_OBJECT(forwardItem)->ref_count, ==, 1); 83 84 nthItem = webkit_web_back_forward_list_get_nth_item(backForwardList, 1); 85 g_object_ref(nthItem); 86 g_assert_cmpint(G_OBJECT(nthItem)->ref_count, ==, 2); 87 g_object_unref(nthItem); 88 g_assert_cmpint(G_OBJECT(nthItem)->ref_count, ==, 1); 89 90 backList = webkit_web_back_forward_list_get_back_list_with_limit(backForwardList, 5); 91 for (; backList; backList = backList->next) 92 g_assert_cmpint(G_OBJECT(backList->data)->ref_count, ==, 1); 93 94 forwardList = webkit_web_back_forward_list_get_forward_list_with_limit(backForwardList, 5); 95 for (; forwardList; forwardList = forwardList->next) 96 g_assert_cmpint(G_OBJECT(forwardList->data)->ref_count, ==, 1); 97 98 g_list_free(forwardList); 99 g_list_free(backList); 100 g_assert_cmpint(G_OBJECT(item1)->ref_count, ==, 1); 101 g_assert_cmpint(G_OBJECT(item2)->ref_count, ==, 1); 102 g_assert_cmpint(G_OBJECT(item3)->ref_count, ==, 1); 103 g_assert_cmpint(G_OBJECT(item4)->ref_count, ==, 1); 104 g_assert_cmpint(G_OBJECT(backForwardList)->ref_count, ==, 1); 105 g_object_unref(webView); 106 } 107 108 static void test_webkit_web_back_forward_list_order(void) 109 { 110 WebKitWebView* webView; 111 WebKitWebBackForwardList* webBackForwardList; 112 WebKitWebHistoryItem* item1; 113 WebKitWebHistoryItem* item2; 114 WebKitWebHistoryItem* item3; 115 WebKitWebHistoryItem* item4; 116 WebKitWebHistoryItem* currentItem; 117 GList* backList = NULL; 118 GList* forwardList = NULL; 119 g_test_bug("22694"); 120 121 webView = WEBKIT_WEB_VIEW(webkit_web_view_new()); 122 g_object_ref_sink(webView); 123 124 webkit_web_view_set_maintains_back_forward_list(webView, TRUE); 125 webBackForwardList = webkit_web_view_get_back_forward_list(webView); 126 g_assert(webBackForwardList); 127 128 // Check that there is no item. 129 g_assert(!webkit_web_back_forward_list_get_current_item(webBackForwardList)); 130 g_assert_cmpint(webkit_web_back_forward_list_get_forward_length(webBackForwardList), ==, 0); 131 g_assert_cmpint(webkit_web_back_forward_list_get_back_length(webBackForwardList), ==, 0); 132 g_assert(!webkit_web_view_can_go_forward(webView)); 133 g_assert(!webkit_web_view_can_go_back(webView)); 134 135 // Add a new items 136 item1 = webkit_web_history_item_new_with_data("http://example.com/1/", "Site 1"); 137 webkit_web_back_forward_list_add_item(webBackForwardList, item1); 138 g_object_unref(item1); 139 g_assert(webkit_web_back_forward_list_contains_item(webBackForwardList, item1)); 140 141 item2 = webkit_web_history_item_new_with_data("http://example.com/2/", "Site 2"); 142 webkit_web_back_forward_list_add_item(webBackForwardList, item2); 143 g_object_unref(item2); 144 g_assert(webkit_web_back_forward_list_contains_item(webBackForwardList, item2)); 145 146 item3 = webkit_web_history_item_new_with_data("http://example.com/3/", "Site 3"); 147 webkit_web_back_forward_list_add_item(webBackForwardList, item3); 148 g_object_unref(item3); 149 g_assert(webkit_web_back_forward_list_contains_item(webBackForwardList, item3)); 150 151 item4 = webkit_web_history_item_new_with_data("http://example.com/4/", "Site 4"); 152 webkit_web_back_forward_list_add_item(webBackForwardList, item4); 153 g_object_unref(item4); 154 g_assert(webkit_web_back_forward_list_contains_item(webBackForwardList, item4)); 155 156 // check the back list order 157 backList = webkit_web_back_forward_list_get_back_list_with_limit(webBackForwardList, 5); 158 g_assert(backList); 159 160 currentItem = WEBKIT_WEB_HISTORY_ITEM(backList->data); 161 g_assert_cmpstr(webkit_web_history_item_get_uri(currentItem), ==, "http://example.com/3/"); 162 g_assert_cmpstr(webkit_web_history_item_get_title(currentItem), ==, "Site 3"); 163 backList = backList->next; 164 165 currentItem = WEBKIT_WEB_HISTORY_ITEM(backList->data); 166 g_assert_cmpstr(webkit_web_history_item_get_uri(currentItem), ==, "http://example.com/2/"); 167 g_assert_cmpstr(webkit_web_history_item_get_title(currentItem), ==, "Site 2"); 168 backList = backList->next; 169 170 currentItem = WEBKIT_WEB_HISTORY_ITEM(backList->data); 171 g_assert_cmpstr(webkit_web_history_item_get_uri(currentItem), ==, "http://example.com/1/"); 172 g_assert_cmpstr(webkit_web_history_item_get_title(currentItem), ==, "Site 1"); 173 g_list_free(backList); 174 175 // check the forward list order 176 g_assert(webkit_web_view_go_to_back_forward_item(webView, item1)); 177 forwardList = webkit_web_back_forward_list_get_forward_list_with_limit(webBackForwardList,5); 178 g_assert(forwardList); 179 180 currentItem = WEBKIT_WEB_HISTORY_ITEM(forwardList->data); 181 g_assert_cmpstr(webkit_web_history_item_get_uri(currentItem), ==, "http://example.com/4/"); 182 g_assert_cmpstr(webkit_web_history_item_get_title(currentItem), ==, "Site 4"); 183 forwardList = forwardList->next; 184 185 currentItem = WEBKIT_WEB_HISTORY_ITEM(forwardList->data); 186 g_assert_cmpstr(webkit_web_history_item_get_uri(currentItem), ==, "http://example.com/3/"); 187 g_assert_cmpstr(webkit_web_history_item_get_title(currentItem), ==, "Site 3"); 188 forwardList = forwardList->next; 189 190 currentItem = WEBKIT_WEB_HISTORY_ITEM(forwardList->data); 191 g_assert_cmpstr(webkit_web_history_item_get_uri(currentItem), ==, "http://example.com/2/"); 192 g_assert_cmpstr(webkit_web_history_item_get_title(currentItem), ==, "Site 2"); 193 194 g_list_free(forwardList); 195 g_object_unref(webView); 196 } 197 198 static void test_webkit_web_back_forward_list_add_item(void) 199 { 200 WebKitWebView* webView; 201 WebKitWebBackForwardList* webBackForwardList; 202 WebKitWebHistoryItem* addItem1; 203 WebKitWebHistoryItem* addItem2; 204 WebKitWebHistoryItem* backItem; 205 WebKitWebHistoryItem* currentItem; 206 g_test_bug("22988"); 207 208 webView = WEBKIT_WEB_VIEW(webkit_web_view_new()); 209 g_object_ref_sink(webView); 210 211 webkit_web_view_set_maintains_back_forward_list(webView, TRUE); 212 webBackForwardList = webkit_web_view_get_back_forward_list(webView); 213 g_assert(webBackForwardList); 214 215 // Check that there is no item. 216 g_assert(!webkit_web_back_forward_list_get_current_item(webBackForwardList)); 217 g_assert_cmpint(webkit_web_back_forward_list_get_forward_length(webBackForwardList), ==, 0); 218 g_assert_cmpint(webkit_web_back_forward_list_get_back_length(webBackForwardList), ==, 0); 219 g_assert(!webkit_web_view_can_go_forward(webView)); 220 g_assert(!webkit_web_view_can_go_back(webView)); 221 222 // Add a new item 223 addItem1 = webkit_web_history_item_new_with_data("http://example.com/", "Added site"); 224 webkit_web_back_forward_list_add_item(webBackForwardList, addItem1); 225 g_object_unref(addItem1); 226 g_assert(webkit_web_back_forward_list_contains_item(webBackForwardList, addItem1)); 227 228 // Check that the added item is the current item. 229 currentItem = webkit_web_back_forward_list_get_current_item(webBackForwardList); 230 g_assert(currentItem); 231 g_assert_cmpint(webkit_web_back_forward_list_get_forward_length(webBackForwardList), ==, 0); 232 g_assert_cmpint(webkit_web_back_forward_list_get_back_length(webBackForwardList), ==, 0); 233 g_assert(!webkit_web_view_can_go_forward(webView)); 234 g_assert(!webkit_web_view_can_go_back(webView)); 235 g_assert_cmpstr(webkit_web_history_item_get_uri(currentItem), ==, "http://example.com/"); 236 g_assert_cmpstr(webkit_web_history_item_get_title(currentItem), ==, "Added site"); 237 238 // Add another item. 239 addItem2 = webkit_web_history_item_new_with_data("http://example.com/2/", "Added site 2"); 240 webkit_web_back_forward_list_add_item(webBackForwardList, addItem2); 241 g_object_unref(addItem2); 242 g_assert(webkit_web_back_forward_list_contains_item(webBackForwardList, addItem2)); 243 244 // Check that the added item is new current item. 245 currentItem = webkit_web_back_forward_list_get_current_item(webBackForwardList); 246 g_assert(currentItem); 247 g_assert_cmpint(webkit_web_back_forward_list_get_forward_length(webBackForwardList), ==, 0); 248 g_assert_cmpint(webkit_web_back_forward_list_get_back_length(webBackForwardList), ==, 1); 249 g_assert(!webkit_web_view_can_go_forward(webView)); 250 g_assert(webkit_web_view_can_go_back(webView)); 251 g_assert_cmpstr(webkit_web_history_item_get_uri(currentItem), ==, "http://example.com/2/"); 252 g_assert_cmpstr(webkit_web_history_item_get_title(currentItem), ==, "Added site 2"); 253 254 backItem = webkit_web_back_forward_list_get_back_item(webBackForwardList); 255 g_assert(backItem); 256 g_assert_cmpstr(webkit_web_history_item_get_uri(backItem), ==, "http://example.com/"); 257 g_assert_cmpstr(webkit_web_history_item_get_title(backItem), ==, "Added site"); 258 259 // Go to the first added item. 260 g_assert(webkit_web_view_go_to_back_forward_item(webView, addItem1)); 261 g_assert_cmpint(webkit_web_back_forward_list_get_forward_length(webBackForwardList), ==, 1); 262 g_assert_cmpint(webkit_web_back_forward_list_get_back_length(webBackForwardList), ==, 0); 263 g_assert(webkit_web_view_can_go_forward(webView)); 264 g_assert(!webkit_web_view_can_go_back(webView)); 265 266 g_object_unref(webView); 267 } 268 269 static void test_webkit_web_back_forward_list_clear(void) 270 { 271 WebKitWebView* webView; 272 WebKitWebBackForwardList* webBackForwardList; 273 WebKitWebHistoryItem* addItem; 274 g_test_bug("36173"); 275 276 webView = WEBKIT_WEB_VIEW(webkit_web_view_new()); 277 g_object_ref_sink(webView); 278 279 webBackForwardList = webkit_web_view_get_back_forward_list(webView); 280 g_assert(webBackForwardList); 281 282 // Check that there is no item. 283 g_assert_cmpint(webkit_web_back_forward_list_get_forward_length(webBackForwardList), ==, 0); 284 g_assert_cmpint(webkit_web_back_forward_list_get_back_length(webBackForwardList), ==, 0); 285 g_assert(!webkit_web_back_forward_list_get_current_item(webBackForwardList)); 286 g_assert(!webkit_web_view_can_go_forward(webView)); 287 g_assert(!webkit_web_view_can_go_back(webView)); 288 289 // Check that clearing the empty list does not modify counters 290 webkit_web_back_forward_list_clear(webBackForwardList); 291 g_assert_cmpint(webkit_web_back_forward_list_get_forward_length(webBackForwardList), ==, 0); 292 g_assert_cmpint(webkit_web_back_forward_list_get_back_length(webBackForwardList), ==, 0); 293 g_assert(!webkit_web_back_forward_list_get_current_item(webBackForwardList)); 294 g_assert(!webkit_web_view_can_go_forward(webView)); 295 g_assert(!webkit_web_view_can_go_back(webView)); 296 297 // Add a new item 298 addItem = webkit_web_history_item_new_with_data("http://example.com/", "Added site"); 299 webkit_web_back_forward_list_add_item(webBackForwardList, addItem); 300 g_object_unref(addItem); 301 g_assert(webkit_web_back_forward_list_contains_item(webBackForwardList, addItem)); 302 303 // Check that after clearing the list the added item is no longer in the list 304 webkit_web_back_forward_list_clear(webBackForwardList); 305 g_assert(!webkit_web_back_forward_list_contains_item(webBackForwardList, addItem)); 306 307 // Check that after clearing it, the list is empty 308 g_assert_cmpint(webkit_web_back_forward_list_get_forward_length(webBackForwardList), ==, 0); 309 g_assert_cmpint(webkit_web_back_forward_list_get_back_length(webBackForwardList), ==, 0); 310 g_assert(!webkit_web_back_forward_list_get_current_item(webBackForwardList)); 311 g_assert(!webkit_web_view_can_go_forward(webView)); 312 g_assert(!webkit_web_view_can_go_back(webView)); 313 314 g_object_unref(webView); 315 } 316 317 int main(int argc, char** argv) 318 { 319 g_thread_init(NULL); 320 gtk_test_init(&argc, &argv, NULL); 321 322 g_test_bug_base("https://bugs.webkit.org/"); 323 g_test_add_func("/webkit/webbackforwardlist/add_item", test_webkit_web_back_forward_list_add_item); 324 g_test_add_func("/webkit/webbackforwardlist/list_order", test_webkit_web_back_forward_list_order); 325 g_test_add_func("/webkit/webhistoryitem/lifetime", test_webkit_web_history_item_lifetime); 326 g_test_add_func("/webkit/webbackforwardlist/clear", test_webkit_web_back_forward_list_clear); 327 return g_test_run (); 328 } 329 330 #else 331 int main(int argc, char** argv) 332 { 333 g_critical("You will need gtk-2.14.0 to run the unit tests. Doing nothing now."); 334 return 0; 335 } 336 337 #endif 338