|
Line 0
a/Source/WebKit2/UIProcess/API/gtk/tests/TestBackForwardList.cpp_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2011 Igalia S.L. |
| 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 "config.h" |
| 21 |
|
| 22 |
#include "LoadTrackingTest.h" |
| 23 |
#include "WebKitTestServer.h" |
| 24 |
#include <gtk/gtk.h> |
| 25 |
#include <libsoup/soup.h> |
| 26 |
#include <string.h> |
| 27 |
#include <webkit2/webkit2.h> |
| 28 |
|
| 29 |
// Back forward list limit is 100 by default. |
| 30 |
static const size_t wkBackForwardListLimit = 100; |
| 31 |
|
| 32 |
static WebKitTestServer* kServer; |
| 33 |
|
| 34 |
static void serverCallback(SoupServer* server, SoupMessage* msg, const char* path, GHashTable* query, SoupClientContext* context, gpointer data) |
| 35 |
{ |
| 36 |
if (msg->method != SOUP_METHOD_GET) { |
| 37 |
soup_message_set_status(msg, SOUP_STATUS_NOT_IMPLEMENTED); |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
soup_message_set_status(msg, SOUP_STATUS_OK); |
| 42 |
|
| 43 |
char* body = g_strdup_printf("<html><title>%s</title><body>%s</body></html>", path + 1, path + 1); |
| 44 |
soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, body, strlen(body)); |
| 45 |
|
| 46 |
soup_message_body_complete(msg->response_body); |
| 47 |
} |
| 48 |
|
| 49 |
class BackForwardListTest: public LoadTrackingTest { |
| 50 |
public: |
| 51 |
MAKE_GLIB_TEST_FIXTURE(BackForwardListTest); |
| 52 |
|
| 53 |
enum { |
| 54 |
Backward, |
| 55 |
Forward |
| 56 |
}; |
| 57 |
|
| 58 |
enum { |
| 59 |
CurrentItem = 1 << 0, |
| 60 |
AddedItem = 1 << 1, |
| 61 |
RemovedItems = 1 << 2 |
| 62 |
}; |
| 63 |
|
| 64 |
static void checkItem(WebKitBackForwardListItem* item, const gchar* title, const gchar* uri, const gchar* originalURI) |
| 65 |
{ |
| 66 |
g_assert(item); |
| 67 |
g_assert_cmpstr(webkit_back_forward_list_item_get_uri(item), ==, uri); |
| 68 |
g_assert_cmpstr(webkit_back_forward_list_item_get_title(item), == , title); |
| 69 |
g_assert_cmpstr(webkit_back_forward_list_item_get_original_uri(item), ==, originalURI); |
| 70 |
} |
| 71 |
|
| 72 |
static void checkItemIndex(WebKitBackForwardList* list) |
| 73 |
{ |
| 74 |
g_assert(webkit_back_forward_list_get_nth_item(list, -1) == webkit_back_forward_list_get_back_item(list)); |
| 75 |
g_assert(webkit_back_forward_list_get_nth_item(list, 0) == webkit_back_forward_list_get_current_item(list)); |
| 76 |
g_assert(webkit_back_forward_list_get_nth_item(list, 1) == webkit_back_forward_list_get_forward_item(list)); |
| 77 |
} |
| 78 |
|
| 79 |
static void checkList(WebKitBackForwardList* list, guint type, WebKitBackForwardListItem** items, guint nItems) |
| 80 |
{ |
| 81 |
GList* listItems = type == BackForwardListTest::Backward ? webkit_back_forward_list_get_back_list(list) : |
| 82 |
webkit_back_forward_list_get_forward_list(list); |
| 83 |
g_assert(listItems); |
| 84 |
|
| 85 |
guint i = 0; |
| 86 |
for (GList* listItem = listItems; listItem; listItem = g_list_next(listItem), i++) { |
| 87 |
g_assert_cmpuint(i, <, nItems); |
| 88 |
g_assert(listItem->data == items[i]); |
| 89 |
} |
| 90 |
g_list_free(listItems); |
| 91 |
} |
| 92 |
|
| 93 |
static void backForwardListChanged(WebKitBackForwardList* list, WebKitBackForwardListItem* addedItem, GList* removedItems, BackForwardListTest* test) |
| 94 |
{ |
| 95 |
test->m_hasChanged = true; |
| 96 |
|
| 97 |
if (test->m_changedFlags & BackForwardListTest::AddedItem) { |
| 98 |
g_assert(WEBKIT_IS_BACK_FORWARD_LIST_ITEM(addedItem)); |
| 99 |
test->watchObject(G_OBJECT(addedItem)); |
| 100 |
} else |
| 101 |
g_assert(!addedItem); |
| 102 |
|
| 103 |
if (test->m_changedFlags & BackForwardListTest::RemovedItems) { |
| 104 |
g_assert(removedItems); |
| 105 |
for (GList* iter = removedItems; iter; iter = iter->next) { |
| 106 |
g_assert(WEBKIT_IS_BACK_FORWARD_LIST_ITEM(iter->data)); |
| 107 |
if (test->m_expectedRemovedItems) |
| 108 |
g_assert(g_list_find(test->m_expectedRemovedItems, iter->data)); |
| 109 |
} |
| 110 |
|
| 111 |
} else |
| 112 |
g_assert(!removedItems); |
| 113 |
} |
| 114 |
|
| 115 |
BackForwardListTest() |
| 116 |
: m_list(webkit_web_view_get_back_forward_list(m_webView)) |
| 117 |
, m_changedFlags(0) |
| 118 |
, m_hasChanged(false) |
| 119 |
, m_expectedRemovedItems(0) |
| 120 |
{ |
| 121 |
g_signal_connect(m_list, "changed", G_CALLBACK(backForwardListChanged), this); |
| 122 |
watchObject(G_OBJECT(m_list)); |
| 123 |
} |
| 124 |
|
| 125 |
~BackForwardListTest() |
| 126 |
{ |
| 127 |
g_signal_handlers_disconnect_matched(m_list, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, this); |
| 128 |
} |
| 129 |
|
| 130 |
void waitUntilLoadFinished() |
| 131 |
{ |
| 132 |
m_hasChanged = false; |
| 133 |
LoadTrackingTest::waitUntilLoadFinished(); |
| 134 |
g_assert(m_hasChanged); |
| 135 |
} |
| 136 |
|
| 137 |
void waitUntilLoadFinishedAndCheckRemovedItems(GList* removedItems) |
| 138 |
{ |
| 139 |
m_expectedRemovedItems = removedItems; |
| 140 |
waitUntilLoadFinished(); |
| 141 |
m_expectedRemovedItems = 0; |
| 142 |
} |
| 143 |
|
| 144 |
WebKitBackForwardList* m_list; |
| 145 |
gulong m_changedFlags; |
| 146 |
bool m_hasChanged; |
| 147 |
GList* m_expectedRemovedItems; |
| 148 |
}; |
| 149 |
|
| 150 |
static void testBackForwardListNavigation(BackForwardListTest* test, gconstpointer) |
| 151 |
{ |
| 152 |
WebKitBackForwardListItem* items[1]; |
| 153 |
|
| 154 |
g_assert(!webkit_web_view_can_go_back(test->m_webView)); |
| 155 |
g_assert(!webkit_web_view_can_go_forward(test->m_webView)); |
| 156 |
|
| 157 |
g_assert_cmpuint(webkit_back_forward_list_get_length(test->m_list), ==, 0); |
| 158 |
g_assert(!webkit_back_forward_list_get_current_item(test->m_list)); |
| 159 |
g_assert(!webkit_back_forward_list_get_back_item(test->m_list)); |
| 160 |
g_assert(!webkit_back_forward_list_get_forward_item(test->m_list)); |
| 161 |
BackForwardListTest::checkItemIndex(test->m_list); |
| 162 |
g_assert(!webkit_back_forward_list_get_back_list(test->m_list)); |
| 163 |
g_assert(!webkit_back_forward_list_get_forward_list(test->m_list)); |
| 164 |
|
| 165 |
CString uriPage1 = kServer->getURIForPath("/Page1"); |
| 166 |
test->m_changedFlags = BackForwardListTest::CurrentItem | BackForwardListTest::AddedItem; |
| 167 |
webkit_web_view_load_uri(test->m_webView, uriPage1.data()); |
| 168 |
test->waitUntilLoadFinished(); |
| 169 |
|
| 170 |
g_assert(!webkit_web_view_can_go_back(test->m_webView)); |
| 171 |
g_assert(!webkit_web_view_can_go_forward(test->m_webView)); |
| 172 |
|
| 173 |
g_assert_cmpuint(webkit_back_forward_list_get_length(test->m_list), ==, 1); |
| 174 |
WebKitBackForwardListItem* itemPage1 = webkit_back_forward_list_get_current_item(test->m_list); |
| 175 |
BackForwardListTest::checkItem(itemPage1, "Page1", uriPage1.data(), uriPage1.data()); |
| 176 |
g_assert(!webkit_back_forward_list_get_back_item(test->m_list)); |
| 177 |
g_assert(!webkit_back_forward_list_get_forward_item(test->m_list)); |
| 178 |
BackForwardListTest::checkItemIndex(test->m_list); |
| 179 |
g_assert(!webkit_back_forward_list_get_back_list(test->m_list)); |
| 180 |
g_assert(!webkit_back_forward_list_get_forward_list(test->m_list)); |
| 181 |
|
| 182 |
CString uriPage2 = kServer->getURIForPath("/Page2"); |
| 183 |
test->m_changedFlags = BackForwardListTest::CurrentItem | BackForwardListTest::AddedItem; |
| 184 |
webkit_web_view_load_uri(test->m_webView, uriPage2.data()); |
| 185 |
test->waitUntilLoadFinished(); |
| 186 |
|
| 187 |
g_assert(webkit_web_view_can_go_back(test->m_webView)); |
| 188 |
g_assert(!webkit_web_view_can_go_forward(test->m_webView)); |
| 189 |
|
| 190 |
g_assert_cmpuint(webkit_back_forward_list_get_length(test->m_list), ==, 2); |
| 191 |
WebKitBackForwardListItem* itemPage2 = webkit_back_forward_list_get_current_item(test->m_list); |
| 192 |
BackForwardListTest::checkItem(itemPage2, "Page2", uriPage2.data(), uriPage2.data()); |
| 193 |
g_assert(webkit_back_forward_list_get_back_item(test->m_list) == itemPage1); |
| 194 |
g_assert(!webkit_back_forward_list_get_forward_item(test->m_list)); |
| 195 |
BackForwardListTest::checkItemIndex(test->m_list); |
| 196 |
items[0] = itemPage1; |
| 197 |
BackForwardListTest::checkList(test->m_list, BackForwardListTest::Backward, items, 1); |
| 198 |
g_assert(!webkit_back_forward_list_get_forward_list(test->m_list)); |
| 199 |
|
| 200 |
test->m_changedFlags = BackForwardListTest::CurrentItem; |
| 201 |
webkit_web_view_go_back(test->m_webView); |
| 202 |
test->waitUntilLoadFinished(); |
| 203 |
|
| 204 |
g_assert(!webkit_web_view_can_go_back(test->m_webView)); |
| 205 |
g_assert(webkit_web_view_can_go_forward(test->m_webView)); |
| 206 |
|
| 207 |
g_assert_cmpuint(webkit_back_forward_list_get_length(test->m_list), ==, 2); |
| 208 |
g_assert(itemPage1 == webkit_back_forward_list_get_current_item(test->m_list)); |
| 209 |
BackForwardListTest::checkItem(webkit_back_forward_list_get_current_item(test->m_list), "Page1", uriPage1.data(), uriPage1.data()); |
| 210 |
g_assert(!webkit_back_forward_list_get_back_item(test->m_list)); |
| 211 |
g_assert(webkit_back_forward_list_get_forward_item(test->m_list) == itemPage2); |
| 212 |
BackForwardListTest::checkItemIndex(test->m_list); |
| 213 |
g_assert(!webkit_back_forward_list_get_back_list(test->m_list)); |
| 214 |
items[0] = itemPage2; |
| 215 |
BackForwardListTest::checkList(test->m_list, BackForwardListTest::Forward, items, 1); |
| 216 |
|
| 217 |
test->m_changedFlags = BackForwardListTest::CurrentItem; |
| 218 |
webkit_web_view_go_forward(test->m_webView); |
| 219 |
test->waitUntilLoadFinished(); |
| 220 |
|
| 221 |
g_assert(webkit_web_view_can_go_back(test->m_webView)); |
| 222 |
g_assert(!webkit_web_view_can_go_forward(test->m_webView)); |
| 223 |
|
| 224 |
g_assert_cmpuint(webkit_back_forward_list_get_length(test->m_list), ==, 2); |
| 225 |
g_assert(itemPage2 == webkit_back_forward_list_get_current_item(test->m_list)); |
| 226 |
BackForwardListTest::checkItem(webkit_back_forward_list_get_current_item(test->m_list), "Page2", uriPage2.data(), uriPage2.data()); |
| 227 |
g_assert(webkit_back_forward_list_get_back_item(test->m_list) == itemPage1); |
| 228 |
g_assert(!webkit_back_forward_list_get_forward_item(test->m_list)); |
| 229 |
BackForwardListTest::checkItemIndex(test->m_list); |
| 230 |
items[0] = itemPage1; |
| 231 |
BackForwardListTest::checkList(test->m_list, BackForwardListTest::Backward, items, 1); |
| 232 |
g_assert(!webkit_back_forward_list_get_forward_list(test->m_list)); |
| 233 |
} |
| 234 |
|
| 235 |
static void testBackForwardListLimitAndCache(BackForwardListTest* test, gconstpointer) |
| 236 |
{ |
| 237 |
for (size_t i = 0; i < wkBackForwardListLimit; i++) { |
| 238 |
GOwnPtr<char> path(g_strdup_printf("/Page%d", i)); |
| 239 |
test->m_changedFlags = BackForwardListTest::CurrentItem | BackForwardListTest::AddedItem; |
| 240 |
webkit_web_view_load_uri(test->m_webView, kServer->getURIForPath(path.get()).data()); |
| 241 |
test->waitUntilLoadFinished(); |
| 242 |
} |
| 243 |
|
| 244 |
g_assert_cmpuint(webkit_back_forward_list_get_length(test->m_list), ==, wkBackForwardListLimit); |
| 245 |
WebKitBackForwardListItem* itemPageFirst = webkit_back_forward_list_get_nth_item(test->m_list, -(wkBackForwardListLimit - 1)); |
| 246 |
GOwnPtr<GList> removedItems(g_list_prepend(0, itemPageFirst)); |
| 247 |
|
| 248 |
GOwnPtr<char> path(g_strdup_printf("/Page%d", wkBackForwardListLimit)); |
| 249 |
test->m_changedFlags = BackForwardListTest::CurrentItem | BackForwardListTest::AddedItem | BackForwardListTest::RemovedItems; |
| 250 |
webkit_web_view_load_uri(test->m_webView, kServer->getURIForPath(path.get()).data()); |
| 251 |
test->waitUntilLoadFinishedAndCheckRemovedItems(removedItems.get()); |
| 252 |
|
| 253 |
g_assert_cmpuint(webkit_back_forward_list_get_length(test->m_list), ==, wkBackForwardListLimit); |
| 254 |
} |
| 255 |
|
| 256 |
void beforeAll() |
| 257 |
{ |
| 258 |
kServer = new WebKitTestServer(); |
| 259 |
kServer->run(serverCallback); |
| 260 |
|
| 261 |
BackForwardListTest::add("BackForwardList", "navigation", testBackForwardListNavigation); |
| 262 |
BackForwardListTest::add("BackForwardList", "list-limit-and-cache", testBackForwardListLimitAndCache); |
| 263 |
} |
| 264 |
|
| 265 |
void afterAll() |
| 266 |
{ |
| 267 |
delete kServer; |
| 268 |
} |
| 269 |
|