1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
|
/*
Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
Copyright (C) 2009 Torch Mobile Inc.
Copyright (C) 2009 Girish Ramakrishnan <girish@forwardbias.in>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <qtest.h>
#include "../util.h"
#include <qpainter.h>
#include <qwebview.h>
#include <qwebpage.h>
#include <qnetworkrequest.h>
#include <qdiriterator.h>
#include <qwebelement.h>
#include <qwebframe.h>
#define VERIFY_INPUTMETHOD_HINTS(actual, expect) \
QVERIFY(actual == expect);
class tst_QWebView : public QObject
{
Q_OBJECT
public Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void init();
void cleanup();
private Q_SLOTS:
void renderingAfterMaxAndBack();
void renderHints();
void getWebKitVersion();
void reusePage_data();
void reusePage();
void microFocusCoordinates();
void focusInputTypes();
void horizontalScrollbarTest();
void crashTests();
#if !(defined(WTF_USE_QT_MOBILE_THEME) && WTF_USE_QT_MOBILE_THEME)
void setPalette_data();
void setPalette();
#endif
};
// This will be called before the first test function is executed.
// It is only called once.
void tst_QWebView::initTestCase()
{
}
// This will be called after the last test function is executed.
// It is only called once.
void tst_QWebView::cleanupTestCase()
{
}
// This will be called before each test function is executed.
void tst_QWebView::init()
{
}
// This will be called after every test function.
void tst_QWebView::cleanup()
{
}
void tst_QWebView::renderHints()
{
QWebView webView;
// default is only text antialiasing + smooth pixmap transform
QVERIFY(!(webView.renderHints() & QPainter::Antialiasing));
QVERIFY(webView.renderHints() & QPainter::TextAntialiasing);
QVERIFY(webView.renderHints() & QPainter::SmoothPixmapTransform);
QVERIFY(!(webView.renderHints() & QPainter::HighQualityAntialiasing));
webView.setRenderHint(QPainter::Antialiasing, true);
QVERIFY(webView.renderHints() & QPainter::Antialiasing);
QVERIFY(webView.renderHints() & QPainter::TextAntialiasing);
QVERIFY(webView.renderHints() & QPainter::SmoothPixmapTransform);
QVERIFY(!(webView.renderHints() & QPainter::HighQualityAntialiasing));
webView.setRenderHint(QPainter::Antialiasing, false);
QVERIFY(!(webView.renderHints() & QPainter::Antialiasing));
QVERIFY(webView.renderHints() & QPainter::TextAntialiasing);
QVERIFY(webView.renderHints() & QPainter::SmoothPixmapTransform);
QVERIFY(!(webView.renderHints() & QPainter::HighQualityAntialiasing));
webView.setRenderHint(QPainter::SmoothPixmapTransform, true);
QVERIFY(!(webView.renderHints() & QPainter::Antialiasing));
QVERIFY(webView.renderHints() & QPainter::TextAntialiasing);
QVERIFY(webView.renderHints() & QPainter::SmoothPixmapTransform);
QVERIFY(!(webView.renderHints() & QPainter::HighQualityAntialiasing));
webView.setRenderHint(QPainter::SmoothPixmapTransform, false);
QVERIFY(webView.renderHints() & QPainter::TextAntialiasing);
QVERIFY(!(webView.renderHints() & QPainter::SmoothPixmapTransform));
QVERIFY(!(webView.renderHints() & QPainter::HighQualityAntialiasing));
}
void tst_QWebView::getWebKitVersion()
{
QVERIFY(qWebKitVersion().toDouble() > 0);
}
void tst_QWebView::reusePage_data()
{
QTest::addColumn<QString>("html");
QTest::newRow("WithoutPlugin") << "<html><body id='b'>text</body></html>";
QTest::newRow("WindowedPlugin") << QString("<html><body id='b'>text<embed src='resources/test.swf'></embed></body></html>");
QTest::newRow("WindowlessPlugin") << QString("<html><body id='b'>text<embed src='resources/test.swf' wmode=\"transparent\"></embed></body></html>");
}
void tst_QWebView::reusePage()
{
if (!QDir(TESTS_SOURCE_DIR).exists())
W_QSKIP(QString("This test requires access to resources found in '%1'").arg(TESTS_SOURCE_DIR).toLatin1().constData(), SkipAll);
QDir::setCurrent(TESTS_SOURCE_DIR);
QFETCH(QString, html);
QWebView* view1 = new QWebView;
QPointer<QWebPage> page = new QWebPage;
view1->setPage(page.data());
page.data()->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
QWebFrame* mainFrame = page.data()->mainFrame();
mainFrame->setHtml(html, QUrl::fromLocalFile(TESTS_SOURCE_DIR));
if (html.contains("</embed>")) {
// some reasonable time for the PluginStream to feed test.swf to flash and start painting
waitForSignal(view1, SIGNAL(loadFinished(bool)), 2000);
}
view1->show();
QTest::qWaitForWindowExposed(view1);
delete view1;
QVERIFY(page != 0); // deleting view must not have deleted the page, since it's not a child of view
QWebView *view2 = new QWebView;
view2->setPage(page.data());
view2->show(); // in Windowless mode, you should still be able to see the plugin here
QTest::qWaitForWindowExposed(view2);
delete view2;
delete page.data(); // must not crash
QDir::setCurrent(QApplication::applicationDirPath());
}
// Class used in crashTests
class WebViewCrashTest : public QObject {
Q_OBJECT
QWebView* m_view;
public:
bool m_executed;
WebViewCrashTest(QWebView* view)
: m_view(view)
, m_executed(false)
{
view->connect(view, SIGNAL(loadProgress(int)), this, SLOT(loading(int)));
}
private Q_SLOTS:
void loading(int progress)
{
if (progress >= 20 && progress < 90) {
QVERIFY(!m_executed);
m_view->stop();
m_executed = true;
}
}
};
// Should not crash.
void tst_QWebView::crashTests()
{
// Test if loading can be stopped in loadProgress handler without crash.
// Test page should have frames.
QWebView view;
WebViewCrashTest tester(&view);
QUrl url("qrc:///resources/index.html");
view.load(url);
QTRY_VERIFY(tester.m_executed); // If fail it means that the test wasn't executed.
}
void tst_QWebView::microFocusCoordinates()
{
QWebPage* page = new QWebPage;
QWebView* webView = new QWebView;
webView->setPage( page );
page->mainFrame()->setHtml("<html><body>" \
"<input type='text' id='input1' style='font--family: serif' value='' maxlength='20'/><br>" \
"<canvas id='canvas1' width='500' height='500'></canvas>" \
"<input type='password'/><br>" \
"<canvas id='canvas2' width='500' height='500'></canvas>" \
"</body></html>");
page->mainFrame()->setFocus();
QVariant initialMicroFocus = page->inputMethodQuery(Qt::ImMicroFocus);
QVERIFY(initialMicroFocus.isValid());
page->mainFrame()->scroll(0,50);
QVariant currentMicroFocus = page->inputMethodQuery(Qt::ImMicroFocus);
QVERIFY(currentMicroFocus.isValid());
QCOMPARE(initialMicroFocus.toRect().translated(QPoint(0,-50)), currentMicroFocus.toRect());
}
void tst_QWebView::focusInputTypes()
{
QWebView webView;
webView.show();
QTest::qWaitForWindowExposed(&webView);
QUrl url("qrc:///resources/input_types.html");
QWebFrame* const mainFrame = webView.page()->mainFrame();
mainFrame->load(url);
mainFrame->setFocus();
QVERIFY(waitForSignal(&webView, SIGNAL(loadFinished(bool))));
// 'text' type
QWebElement inputElement = mainFrame->documentElement().findFirst(QLatin1String("input[type=text]"));
QTest::mouseClick(&webView, Qt::LeftButton, 0, inputElement.geometry().center());
QVERIFY(webView.inputMethodHints() == Qt::ImhNone);
QVERIFY(webView.testAttribute(Qt::WA_InputMethodEnabled));
// 'password' field
inputElement = mainFrame->documentElement().findFirst(QLatin1String("input[type=password]"));
QTest::mouseClick(&webView, Qt::LeftButton, 0, inputElement.geometry().center());
VERIFY_INPUTMETHOD_HINTS(webView.inputMethodHints(), Qt::ImhHiddenText);
QVERIFY(webView.testAttribute(Qt::WA_InputMethodEnabled));
// 'tel' field
inputElement = mainFrame->documentElement().findFirst(QLatin1String("input[type=tel]"));
QTest::mouseClick(&webView, Qt::LeftButton, 0, inputElement.geometry().center());
VERIFY_INPUTMETHOD_HINTS(webView.inputMethodHints(), Qt::ImhDialableCharactersOnly);
QVERIFY(webView.testAttribute(Qt::WA_InputMethodEnabled));
// 'number' field
inputElement = mainFrame->documentElement().findFirst(QLatin1String("input[type=number]"));
QTest::mouseClick(&webView, Qt::LeftButton, 0, inputElement.geometry().center());
VERIFY_INPUTMETHOD_HINTS(webView.inputMethodHints(), Qt::ImhDigitsOnly);
QVERIFY(webView.testAttribute(Qt::WA_InputMethodEnabled));
// 'email' field
inputElement = mainFrame->documentElement().findFirst(QLatin1String("input[type=email]"));
QTest::mouseClick(&webView, Qt::LeftButton, 0, inputElement.geometry().center());
VERIFY_INPUTMETHOD_HINTS(webView.inputMethodHints(), Qt::ImhEmailCharactersOnly);
QVERIFY(webView.testAttribute(Qt::WA_InputMethodEnabled));
// 'url' field
inputElement = mainFrame->documentElement().findFirst(QLatin1String("input[type=url]"));
QTest::mouseClick(&webView, Qt::LeftButton, 0, inputElement.geometry().center());
VERIFY_INPUTMETHOD_HINTS(webView.inputMethodHints(), Qt::ImhUrlCharactersOnly);
QVERIFY(webView.testAttribute(Qt::WA_InputMethodEnabled));
// 'password' field
inputElement = mainFrame->documentElement().findFirst(QLatin1String("input[type=password]"));
QTest::mouseClick(&webView, Qt::LeftButton, 0, inputElement.geometry().center());
VERIFY_INPUTMETHOD_HINTS(webView.inputMethodHints(), Qt::ImhHiddenText);
QVERIFY(webView.testAttribute(Qt::WA_InputMethodEnabled));
// 'text' type
inputElement = mainFrame->documentElement().findFirst(QLatin1String("input[type=text]"));
QTest::mouseClick(&webView, Qt::LeftButton, 0, inputElement.geometry().center());
QVERIFY(webView.inputMethodHints() == Qt::ImhNone);
QVERIFY(webView.testAttribute(Qt::WA_InputMethodEnabled));
// 'password' field
inputElement = mainFrame->documentElement().findFirst(QLatin1String("input[type=password]"));
QTest::mouseClick(&webView, Qt::LeftButton, 0, inputElement.geometry().center());
VERIFY_INPUTMETHOD_HINTS(webView.inputMethodHints(), Qt::ImhHiddenText);
QVERIFY(webView.testAttribute(Qt::WA_InputMethodEnabled));
// 'text area' field
inputElement = mainFrame->documentElement().findFirst(QLatin1String("textarea"));
QTest::mouseClick(&webView, Qt::LeftButton, 0, inputElement.geometry().center());
QVERIFY(webView.inputMethodHints() == Qt::ImhNone);
QVERIFY(webView.testAttribute(Qt::WA_InputMethodEnabled));
}
void tst_QWebView::horizontalScrollbarTest()
{
QWebView webView;
webView.resize(600, 600);
webView.show();
QTest::qWaitForWindowExposed(&webView);
QUrl url("qrc:///resources/scrolltest_page.html");
QWebFrame* const mainFrame = webView.page()->mainFrame();
mainFrame->load(url);
mainFrame->setFocus();
QVERIFY(waitForSignal(&webView, SIGNAL(loadFinished(bool))));
QVERIFY(webView.page()->mainFrame()->scrollPosition() == QPoint(0, 0));
// Note: The test below assumes that the layout direction is Qt::LeftToRight.
QTest::mouseClick(&webView, Qt::LeftButton, 0, QPoint(550, 595));
QVERIFY(webView.page()->mainFrame()->scrollPosition().x() > 0);
// Note: The test below assumes that the layout direction is Qt::LeftToRight.
QTest::mouseClick(&webView, Qt::LeftButton, 0, QPoint(20, 595));
QVERIFY(webView.page()->mainFrame()->scrollPosition() == QPoint(0, 0));
}
#if !(defined(WTF_USE_QT_MOBILE_THEME) && WTF_USE_QT_MOBILE_THEME)
void tst_QWebView::setPalette_data()
{
QTest::addColumn<bool>("active");
QTest::addColumn<bool>("background");
QTest::newRow("activeBG") << true << true;
QTest::newRow("activeFG") << true << false;
QTest::newRow("inactiveBG") << false << true;
QTest::newRow("inactiveFG") << false << false;
}
// Render a QWebView to a QImage twice, each time with a different palette set,
// verify that images rendered are not the same, confirming WebCore usage of
// custom palette on selections.
void tst_QWebView::setPalette()
{
QString html = "<html><head></head>"
"<body>"
"Some text here"
"</body>"
"</html>";
QFETCH(bool, active);
QFETCH(bool, background);
QWidget* activeView = 0;
// Use controlView to manage active/inactive state of test views by raising
// or lowering their position in the window stack.
QWebView controlView;
controlView.setHtml(html);
QWebView view1;
QPalette palette1;
QBrush brush1(Qt::red);
brush1.setStyle(Qt::SolidPattern);
if (active && background) {
// Rendered image must have red background on an active QWebView.
palette1.setBrush(QPalette::Active, QPalette::Highlight, brush1);
} else if (active && !background) {
// Rendered image must have red foreground on an active QWebView.
palette1.setBrush(QPalette::Active, QPalette::HighlightedText, brush1);
} else if (!active && background) {
// Rendered image must have red background on an inactive QWebView.
palette1.setBrush(QPalette::Inactive, QPalette::Highlight, brush1);
} else if (!active && !background) {
// Rendered image must have red foreground on an inactive QWebView.
palette1.setBrush(QPalette::Inactive, QPalette::HighlightedText, brush1);
}
view1.setPalette(palette1);
view1.setHtml(html);
view1.page()->setViewportSize(view1.page()->currentFrame()->contentsSize());
view1.show();
QTest::qWaitForWindowExposed(&view1);
if (!active) {
controlView.show();
QTest::qWaitForWindowExposed(&controlView);
activeView = &controlView;
controlView.activateWindow();
} else {
view1.activateWindow();
activeView = &view1;
}
QTRY_COMPARE(QApplication::activeWindow(), activeView);
view1.page()->triggerAction(QWebPage::SelectAll);
QImage img1(view1.page()->viewportSize(), QImage::Format_ARGB32);
QPainter painter1(&img1);
view1.page()->currentFrame()->render(&painter1);
painter1.end();
view1.close();
controlView.close();
QWebView view2;
QPalette palette2;
QBrush brush2(Qt::blue);
brush2.setStyle(Qt::SolidPattern);
if (active && background) {
// Rendered image must have blue background on an active QWebView.
palette2.setBrush(QPalette::Active, QPalette::Highlight, brush2);
} else if (active && !background) {
// Rendered image must have blue foreground on an active QWebView.
palette2.setBrush(QPalette::Active, QPalette::HighlightedText, brush2);
} else if (!active && background) {
// Rendered image must have blue background on an inactive QWebView.
palette2.setBrush(QPalette::Inactive, QPalette::Highlight, brush2);
} else if (!active && !background) {
// Rendered image must have blue foreground on an inactive QWebView.
palette2.setBrush(QPalette::Inactive, QPalette::HighlightedText, brush2);
}
view2.setPalette(palette2);
view2.setHtml(html);
view2.page()->setViewportSize(view2.page()->currentFrame()->contentsSize());
view2.show();
QTest::qWaitForWindowExposed(&view2);
if (!active) {
controlView.show();
QTest::qWaitForWindowExposed(&controlView);
activeView = &controlView;
controlView.activateWindow();
} else {
view2.activateWindow();
activeView = &view2;
}
QTRY_COMPARE(QApplication::activeWindow(), activeView);
view2.page()->triggerAction(QWebPage::SelectAll);
QImage img2(view2.page()->viewportSize(), QImage::Format_ARGB32);
QPainter painter2(&img2);
view2.page()->currentFrame()->render(&painter2);
painter2.end();
view2.close();
controlView.close();
QVERIFY(img1 != img2);
}
#endif
void tst_QWebView::renderingAfterMaxAndBack()
{
QUrl url = QUrl("data:text/html,<html><head></head>"
"<body width=1024 height=768 bgcolor=red>"
"</body>"
"</html>");
QWebView view;
view.page()->mainFrame()->load(url);
QVERIFY(waitForSignal(&view, SIGNAL(loadFinished(bool))));
view.show();
view.page()->settings()->setMaximumPagesInCache(3);
QTest::qWaitForWindowExposed(&view);
QPixmap reference(view.page()->viewportSize());
reference.fill(Qt::red);
QPixmap image(view.page()->viewportSize());
QPainter painter(&image);
view.page()->currentFrame()->render(&painter);
QCOMPARE(image, reference);
QUrl url2 = QUrl("data:text/html,<html><head></head>"
"<body width=1024 height=768 bgcolor=blue>"
"</body>"
"</html>");
view.page()->mainFrame()->load(url2);
QVERIFY(waitForSignal(&view, SIGNAL(loadFinished(bool))));
view.showMaximized();
QTest::qWaitForWindowExposed(&view);
QPixmap reference2(view.page()->viewportSize());
reference2.fill(Qt::blue);
QPixmap image2(view.page()->viewportSize());
QPainter painter2(&image2);
view.page()->currentFrame()->render(&painter2);
QCOMPARE(image2, reference2);
view.back();
QPixmap reference3(view.page()->viewportSize());
reference3.fill(Qt::red);
QPixmap image3(view.page()->viewportSize());
QPainter painter3(&image3);
view.page()->currentFrame()->render(&painter3);
QCOMPARE(image3, reference3);
}
QTEST_MAIN(tst_QWebView)
#include "tst_qwebview.moc"
|