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 GLIB_CHECK_VERSION(2, 16, 0) && GTK_CHECK_VERSION(2, 14, 0) 25 26static 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 108static 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 198static 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 269int main(int argc, char** argv) 270{ 271 g_thread_init(NULL); 272 gtk_test_init(&argc, &argv, NULL); 273 274 g_test_bug_base("https://bugs.webkit.org/"); 275 g_test_add_func("/webkit/webbackforwardlist/add_item", test_webkit_web_back_forward_list_add_item); 276 g_test_add_func("/webkit/webbackforwardlist/list_order", test_webkit_web_back_forward_list_order); 277 g_test_add_func("/webkit/webhistoryitem/lifetime", test_webkit_web_history_item_lifetime); 278 return g_test_run (); 279} 280 281#else 282int main(int argc, char** argv) 283{ 284 g_critical("You will need at least glib-2.16.0 and gtk-2.14.0 to run the unit tests. Doing nothing now."); 285 return 0; 286} 287 288#endif 289