OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/compiler_specific.h" | 5 #include "base/compiler_specific.h" |
6 #include "skia/ext/analysis_canvas.h" | 6 #include "skia/ext/analysis_canvas.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "third_party/skia/include/core/SkShader.h" | 8 #include "third_party/skia/include/core/SkShader.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 std::list<skia::LazyPixelRef*> pixelRefs; | 447 std::list<skia::LazyPixelRef*> pixelRefs; |
448 canvas.consumeLazyPixelRefs(&pixelRefs); | 448 canvas.consumeLazyPixelRefs(&pixelRefs); |
449 | 449 |
450 // We expect to get only lazy pixel refs and only unique results. | 450 // We expect to get only lazy pixel refs and only unique results. |
451 EXPECT_EQ(pixelRefs.size(), 1u); | 451 EXPECT_EQ(pixelRefs.size(), 1u); |
452 if (!pixelRefs.empty()) { | 452 if (!pixelRefs.empty()) { |
453 EXPECT_EQ(pixelRefs.front(), static_cast<LazyPixelRef*>(&lazyPixelRef)); | 453 EXPECT_EQ(pixelRefs.front(), static_cast<LazyPixelRef*>(&lazyPixelRef)); |
454 } | 454 } |
455 } | 455 } |
456 | 456 |
| 457 TEST(AnalysisCanvasTest, HasText) { |
| 458 int width = 200; |
| 459 int height = 100; |
| 460 |
| 461 SkBitmap bitmap; |
| 462 bitmap.setConfig(SkBitmap::kNo_Config, width, height); |
| 463 |
| 464 const char* text = "A"; |
| 465 size_t byteLength = 1; |
| 466 |
| 467 SkPoint point = SkPoint::Make(SkIntToScalar(25), SkIntToScalar(25)); |
| 468 SkPath path; |
| 469 path.moveTo(point); |
| 470 path.lineTo(SkIntToScalar(75), SkIntToScalar(75)); |
| 471 |
| 472 SkPaint paint; |
| 473 paint.setColor(SK_ColorGRAY); |
| 474 paint.setTextSize(SkIntToScalar(10)); |
| 475 |
| 476 { |
| 477 skia::AnalysisDevice device(bitmap); |
| 478 skia::AnalysisCanvas canvas(&device); |
| 479 // Test after initialization. |
| 480 EXPECT_FALSE(canvas.hasText()); |
| 481 // Test drawing anything other than text. |
| 482 canvas.drawRect(SkRect::MakeWH(width/2, height), paint); |
| 483 EXPECT_FALSE(canvas.hasText()); |
| 484 } |
| 485 { |
| 486 // Test SkCanvas::drawText. |
| 487 skia::AnalysisDevice device(bitmap); |
| 488 skia::AnalysisCanvas canvas(&device); |
| 489 canvas.drawText(text, byteLength, point.fX, point.fY, paint); |
| 490 EXPECT_TRUE(canvas.hasText()); |
| 491 } |
| 492 { |
| 493 // Test SkCanvas::drawPosText. |
| 494 skia::AnalysisDevice device(bitmap); |
| 495 skia::AnalysisCanvas canvas(&device); |
| 496 canvas.drawPosText(text, byteLength, &point, paint); |
| 497 EXPECT_TRUE(canvas.hasText()); |
| 498 } |
| 499 { |
| 500 // Test SkCanvas::drawPosTextH. |
| 501 skia::AnalysisDevice device(bitmap); |
| 502 skia::AnalysisCanvas canvas(&device); |
| 503 canvas.drawPosTextH(text, byteLength, &point.fX, point.fY, paint); |
| 504 EXPECT_TRUE(canvas.hasText()); |
| 505 } |
| 506 { |
| 507 // Test SkCanvas::drawTextOnPathHV. |
| 508 skia::AnalysisDevice device(bitmap); |
| 509 skia::AnalysisCanvas canvas(&device); |
| 510 canvas.drawTextOnPathHV(text, byteLength, path, point.fX, point.fY, paint); |
| 511 EXPECT_TRUE(canvas.hasText()); |
| 512 } |
| 513 { |
| 514 // Test SkCanvas::drawTextOnPath. |
| 515 skia::AnalysisDevice device(bitmap); |
| 516 skia::AnalysisCanvas canvas(&device); |
| 517 canvas.drawTextOnPath(text, byteLength, path, NULL, paint); |
| 518 EXPECT_TRUE(canvas.hasText()); |
| 519 } |
| 520 { |
| 521 // Text under opaque rect. |
| 522 skia::AnalysisDevice device(bitmap); |
| 523 skia::AnalysisCanvas canvas(&device); |
| 524 canvas.drawText(text, byteLength, point.fX, point.fY, paint); |
| 525 EXPECT_TRUE(canvas.hasText()); |
| 526 canvas.drawRect(SkRect::MakeWH(width, height), paint); |
| 527 EXPECT_FALSE(canvas.hasText()); |
| 528 } |
| 529 { |
| 530 // Text under translucent rect. |
| 531 skia::AnalysisDevice device(bitmap); |
| 532 skia::AnalysisCanvas canvas(&device); |
| 533 canvas.drawText(text, byteLength, point.fX, point.fY, paint); |
| 534 EXPECT_TRUE(canvas.hasText()); |
| 535 SkPaint translucentPaint; |
| 536 translucentPaint.setColor(0x88FFFFFF); |
| 537 canvas.drawRect(SkRect::MakeWH(width, height), translucentPaint); |
| 538 EXPECT_TRUE(canvas.hasText()); |
| 539 } |
| 540 { |
| 541 // Text under rect in clear mode. |
| 542 skia::AnalysisDevice device(bitmap); |
| 543 skia::AnalysisCanvas canvas(&device); |
| 544 canvas.drawText(text, byteLength, point.fX, point.fY, paint); |
| 545 EXPECT_TRUE(canvas.hasText()); |
| 546 SkPaint clearModePaint; |
| 547 clearModePaint.setXfermodeMode(SkXfermode::kClear_Mode); |
| 548 canvas.drawRect(SkRect::MakeWH(width, height), clearModePaint); |
| 549 EXPECT_FALSE(canvas.hasText()); |
| 550 } |
| 551 { |
| 552 // Clear. |
| 553 skia::AnalysisDevice device(bitmap); |
| 554 skia::AnalysisCanvas canvas(&device); |
| 555 canvas.drawText(text, byteLength, point.fX, point.fY, paint); |
| 556 EXPECT_TRUE(canvas.hasText()); |
| 557 canvas.clear(SK_ColorGRAY); |
| 558 EXPECT_FALSE(canvas.hasText()); |
| 559 } |
| 560 { |
| 561 // Text inside clip region. |
| 562 skia::AnalysisDevice device(bitmap); |
| 563 skia::AnalysisCanvas canvas(&device); |
| 564 canvas.clipRect(SkRect::MakeWH(100, 100)); |
| 565 canvas.drawText(text, byteLength, point.fX, point.fY, paint); |
| 566 EXPECT_TRUE(canvas.hasText()); |
| 567 } |
| 568 { |
| 569 // Text outside clip region. |
| 570 skia::AnalysisDevice device(bitmap); |
| 571 skia::AnalysisCanvas canvas(&device); |
| 572 canvas.clipRect(SkRect::MakeXYWH(100, 0, 100, 100)); |
| 573 canvas.drawText(text, byteLength, point.fX, point.fY, paint); |
| 574 // Analysis device does not do any clipping. |
| 575 // So even when text is outside the clip region, |
| 576 // it is marked as having the text. |
| 577 // TODO(alokp): We may be able to do some trivial rejection. |
| 578 EXPECT_TRUE(canvas.hasText()); |
| 579 } |
| 580 } |
| 581 |
457 } // namespace skia | 582 } // namespace skia |
OLD | NEW |