| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/i18n/rtl.h" | 11 #include "base/i18n/rtl.h" |
| 12 #include "base/json/json_value_serializer.h" | 12 #include "base/json/json_value_serializer.h" |
| 13 #include "base/md5.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/scoped_temp_dir.h" | 15 #include "base/scoped_temp_dir.h" |
| 15 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 16 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
| 17 #include "base/sys_info.h" | 18 #include "base/sys_info.h" |
| 18 #include "base/test/test_timeouts.h" | 19 #include "base/test/test_timeouts.h" |
| 19 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 20 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 21 #include "chrome/app/chrome_command_ids.h" | 22 #include "chrome/app/chrome_command_ids.h" |
| 22 #include "chrome/browser/ui/view_ids.h" | 23 #include "chrome/browser/ui/view_ids.h" |
| (...skipping 1495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1518 AutomationProxySnapshotTest() { | 1519 AutomationProxySnapshotTest() { |
| 1519 dom_automation_enabled_ = true; | 1520 dom_automation_enabled_ = true; |
| 1520 if (!snapshot_dir_.CreateUniqueTempDir()) | 1521 if (!snapshot_dir_.CreateUniqueTempDir()) |
| 1521 ADD_FAILURE() << "Unable to create temporary directory"; | 1522 ADD_FAILURE() << "Unable to create temporary directory"; |
| 1522 else | 1523 else |
| 1523 snapshot_path_ = snapshot_dir_.path().AppendASCII("snapshot.png"); | 1524 snapshot_path_ = snapshot_dir_.path().AppendASCII("snapshot.png"); |
| 1524 } | 1525 } |
| 1525 | 1526 |
| 1526 // Asserts that the given png file can be read and decoded into the given | 1527 // Asserts that the given png file can be read and decoded into the given |
| 1527 // bitmap. | 1528 // bitmap. |
| 1528 void AssertReadPNG(const FilePath& filename, SkBitmap* bitmap) { | 1529 void AssertReadPNG(const FilePath& filename, std::string* data) { |
| 1529 DCHECK(bitmap); | |
| 1530 ASSERT_TRUE(file_util::PathExists(filename)); | 1530 ASSERT_TRUE(file_util::PathExists(filename)); |
| 1531 | 1531 |
| 1532 int64 size64; | 1532 int64 size64; |
| 1533 ASSERT_TRUE(file_util::GetFileSize(filename, &size64)); | 1533 ASSERT_TRUE(file_util::GetFileSize(filename, &size64)); |
| 1534 // Check that the file is not too big to read in (less than 100 MB). | 1534 // Check that the file is not too big to read in (less than 100 MB). |
| 1535 ASSERT_LT(size64, 1024 * 1024 * 100); | 1535 ASSERT_LT(size64, 1024 * 1024 * 100); |
| 1536 | 1536 |
| 1537 // Read and decode image. | 1537 // Read and decode image. |
| 1538 int size = static_cast<int>(size64); | 1538 int size = static_cast<int>(size64); |
| 1539 scoped_array<char> data(new char[size]); | 1539 data->resize(size); |
| 1540 int bytes_read = file_util::ReadFile(filename, &data[0], size); | 1540 int bytes_read = file_util::ReadFile(filename, &(*data)[0], size); |
| 1541 ASSERT_EQ(size, bytes_read); | 1541 ASSERT_EQ(size, bytes_read); |
| 1542 ASSERT_TRUE(gfx::PNGCodec::Decode( | |
| 1543 reinterpret_cast<unsigned char*>(&data[0]), | |
| 1544 bytes_read, | |
| 1545 bitmap)); | |
| 1546 } | 1542 } |
| 1547 | 1543 |
| 1548 // Returns the file path for the directory for these tests appended with | 1544 // Returns the file path for the directory for these tests appended with |
| 1549 // the given relative path. | 1545 // the given relative path. |
| 1550 FilePath GetTestFilePath(const std::string& relative_path) { | 1546 FilePath GetTestFilePath(const std::string& relative_path) { |
| 1551 FilePath filename(test_data_directory_); | 1547 FilePath filename(test_data_directory_); |
| 1552 return filename.AppendASCII("automation_proxy_snapshot") | 1548 return filename.AppendASCII("automation_proxy_snapshot") |
| 1553 .AppendASCII(relative_path); | 1549 .AppendASCII(relative_path); |
| 1554 } | 1550 } |
| 1555 | 1551 |
| 1556 GURL GetTestUrl(const std::string& relative_path, const std::string& query) { | 1552 GURL GetTestUrl(const std::string& relative_path, const std::string& query) { |
| 1557 FilePath file_path = GetTestFilePath(relative_path); | 1553 FilePath file_path = GetTestFilePath(relative_path); |
| 1558 return ui_test_utils::GetFileUrlWithQuery(file_path, query); | 1554 return ui_test_utils::GetFileUrlWithQuery(file_path, query); |
| 1559 } | 1555 } |
| 1560 | 1556 |
| 1561 FilePath snapshot_path_; | 1557 FilePath snapshot_path_; |
| 1562 ScopedTempDir snapshot_dir_; | 1558 ScopedTempDir snapshot_dir_; |
| 1563 }; | 1559 }; |
| 1564 | 1560 |
| 1565 // See http://crbug.com/63022. | |
| 1566 #if defined(OS_LINUX) | |
| 1567 #define MAYBE_ContentLargerThanView FAILS_ContentLargerThanView | |
| 1568 #else | |
| 1569 #define MAYBE_ContentLargerThanView ContentLargerThanView | |
| 1570 #endif | |
| 1571 // Tests that taking a snapshot when the content is larger than the view | 1561 // Tests that taking a snapshot when the content is larger than the view |
| 1572 // produces a snapshot equal to the content size. | 1562 // produces a snapshot equal to the content size. |
| 1573 TEST_F(AutomationProxySnapshotTest, MAYBE_ContentLargerThanView) { | 1563 TEST_F(AutomationProxySnapshotTest, ContentLargerThanView) { |
| 1564 const char kReferenceMd5[] = "3d594850fd25cb116338cb3610afe18e"; |
| 1574 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); | 1565 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); |
| 1575 ASSERT_TRUE(browser.get()); | 1566 ASSERT_TRUE(browser.get()); |
| 1576 | |
| 1577 // Resize the window to guarantee that the content is larger than the view. | |
| 1578 scoped_refptr<WindowProxy> window(browser->GetWindow()); | |
| 1579 ASSERT_TRUE(window.get()); | |
| 1580 ASSERT_TRUE(window->SetBounds(gfx::Rect(300, 400))); | |
| 1581 | |
| 1582 scoped_refptr<TabProxy> tab(browser->GetTab(0)); | 1567 scoped_refptr<TabProxy> tab(browser->GetTab(0)); |
| 1583 ASSERT_TRUE(tab.get()); | 1568 ASSERT_TRUE(tab.get()); |
| 1584 | 1569 |
| 1585 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, | 1570 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, |
| 1586 tab->NavigateToURL(GetTestUrl("set_size.html", "600,800"))); | 1571 tab->NavigateToURL(GetTestUrl("set_size.html", "2000,2500"))); |
| 1587 | 1572 |
| 1588 ASSERT_TRUE(tab->CaptureEntirePageAsPNG(snapshot_path_)); | 1573 ASSERT_TRUE(tab->CaptureEntirePageAsPNG(snapshot_path_)); |
| 1589 | 1574 |
| 1590 SkBitmap bitmap; | 1575 std::string data; |
| 1591 ASSERT_NO_FATAL_FAILURE(AssertReadPNG(snapshot_path_, &bitmap)); | 1576 ASSERT_NO_FATAL_FAILURE(AssertReadPNG(snapshot_path_, &data)); |
| 1592 ASSERT_EQ(600, bitmap.width()); | 1577 EXPECT_STREQ(kReferenceMd5, base::MD5String(data).c_str()); |
| 1593 ASSERT_EQ(800, bitmap.height()); | 1578 if (CommandLine::ForCurrentProcess()->HasSwitch("dump-test-image")) { |
| 1579 FilePath path(FILE_PATH_LITERAL("snapshot.png")); |
| 1580 EXPECT_EQ(file_util::WriteFile(path, &data[0], data.length()), |
| 1581 static_cast<int>(data.length())); |
| 1582 } |
| 1594 } | 1583 } |
| 1595 | |
| 1596 // Tests taking a large snapshot works. | |
| 1597 #if defined(OS_LINUX) | |
| 1598 // See http://code.google.com/p/chromium/issues/detail?id=89777 | |
| 1599 #define MAYBE_LargeSnapshot DISABLED_LargeSnapshot | |
| 1600 #else | |
| 1601 #define MAYBE_LargeSnapshot LargeSnapshot | |
| 1602 #endif | |
| 1603 TEST_F(AutomationProxySnapshotTest, MAYBE_LargeSnapshot) { | |
| 1604 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); | |
| 1605 ASSERT_TRUE(browser.get()); | |
| 1606 | |
| 1607 scoped_refptr<TabProxy> tab(browser->GetTab(0)); | |
| 1608 ASSERT_TRUE(tab.get()); | |
| 1609 | |
| 1610 // 2000x2000 creates an approximately 15 MB bitmap. | |
| 1611 // Don't increase this too much. At least my linux box has SHMMAX set at | |
| 1612 // 32 MB. | |
| 1613 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, | |
| 1614 tab->NavigateToURL(GetTestUrl("set_size.html", "2000,2000"))); | |
| 1615 | |
| 1616 ASSERT_TRUE(tab->CaptureEntirePageAsPNG(snapshot_path_)); | |
| 1617 | |
| 1618 SkBitmap bitmap; | |
| 1619 ASSERT_NO_FATAL_FAILURE(AssertReadPNG(snapshot_path_, &bitmap)); | |
| 1620 ASSERT_EQ(2000, bitmap.width()); | |
| 1621 ASSERT_EQ(2000, bitmap.height()); | |
| 1622 } | |
| 1623 | |
| 1624 #if defined(OS_MACOSX) | |
| 1625 // Most pixels on mac are slightly off. | |
| 1626 #define MAYBE_ContentsCorrect DISABLED_ContentsCorrect | |
| 1627 #elif defined(OS_LINUX) | |
| 1628 // See http://crbug.com/63022. | |
| 1629 #define MAYBE_ContentsCorrect FAILS_ContentsCorrect | |
| 1630 #else | |
| 1631 #define MAYBE_ContentsCorrect ContentsCorrect | |
| 1632 #endif | |
| 1633 | |
| 1634 // Tests that the snapshot contents are correct. | |
| 1635 TEST_F(AutomationProxySnapshotTest, MAYBE_ContentsCorrect) { | |
| 1636 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); | |
| 1637 ASSERT_TRUE(browser.get()); | |
| 1638 | |
| 1639 const gfx::Size img_size(400, 300); | |
| 1640 scoped_refptr<WindowProxy> window(browser->GetWindow()); | |
| 1641 ASSERT_TRUE(window.get()); | |
| 1642 ASSERT_TRUE(window->SetBounds(gfx::Rect(img_size))); | |
| 1643 | |
| 1644 scoped_refptr<TabProxy> tab(browser->GetTab(0)); | |
| 1645 ASSERT_TRUE(tab.get()); | |
| 1646 | |
| 1647 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, | |
| 1648 tab->NavigateToURL(GetTestUrl("just_image.html", ""))); | |
| 1649 | |
| 1650 ASSERT_TRUE(tab->CaptureEntirePageAsPNG(snapshot_path_)); | |
| 1651 | |
| 1652 SkBitmap snapshot_bmp; | |
| 1653 ASSERT_NO_FATAL_FAILURE(AssertReadPNG(snapshot_path_, &snapshot_bmp)); | |
| 1654 ASSERT_EQ(img_size.width(), snapshot_bmp.width()); | |
| 1655 ASSERT_EQ(img_size.height(), snapshot_bmp.height()); | |
| 1656 | |
| 1657 SkBitmap reference_bmp; | |
| 1658 ASSERT_NO_FATAL_FAILURE(AssertReadPNG(GetTestFilePath("image.png"), | |
| 1659 &reference_bmp)); | |
| 1660 ASSERT_EQ(img_size.width(), reference_bmp.width()); | |
| 1661 ASSERT_EQ(img_size.height(), reference_bmp.height()); | |
| 1662 | |
| 1663 SkAutoLockPixels lock_snapshot(snapshot_bmp); | |
| 1664 SkAutoLockPixels lock_reference(reference_bmp); | |
| 1665 int diff_pixels_count = 0; | |
| 1666 for (int x = 0; x < img_size.width(); ++x) { | |
| 1667 for (int y = 0; y < img_size.height(); ++y) { | |
| 1668 if (*snapshot_bmp.getAddr32(x, y) != *reference_bmp.getAddr32(x, y)) { | |
| 1669 ++diff_pixels_count; | |
| 1670 } | |
| 1671 } | |
| 1672 } | |
| 1673 ASSERT_EQ(diff_pixels_count, 0); | |
| 1674 } | |
| OLD | NEW |