| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "app/surface/transport_dib.h" | |
| 6 #include "base/basictypes.h" | |
| 7 #include "base/file_path.h" | |
| 8 #include "base/file_util.h" | |
| 9 #include "base/ref_counted_memory.h" | |
| 10 #include "base/stringprintf.h" | |
| 11 #include "chrome/common/render_messages.h" | |
| 12 #include "chrome/common/render_messages_params.h" | |
| 13 #include "chrome/renderer/render_widget_browsertest.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 #include "third_party/skia/include/core/SkBitmap.h" | |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
| 18 #include "ui/gfx/codec/jpeg_codec.h" | |
| 19 #include "ui/gfx/size.h" | |
| 20 | |
| 21 const int RenderWidgetTest::kNumBytesPerPixel = 4; | |
| 22 const int RenderWidgetTest::kLargeWidth = 1024; | |
| 23 const int RenderWidgetTest::kLargeHeight = 768; | |
| 24 const int RenderWidgetTest::kSmallWidth = 600; | |
| 25 const int RenderWidgetTest::kSmallHeight = 450; | |
| 26 const int RenderWidgetTest::kTextPositionX = 800; | |
| 27 const int RenderWidgetTest::kTextPositionY = 600; | |
| 28 const uint32 RenderWidgetTest::kRedARGB = 0xFFFF0000; | |
| 29 | |
| 30 RenderWidgetTest::RenderWidgetTest() {} | |
| 31 | |
| 32 void RenderWidgetTest::ResizeAndPaint(const gfx::Size& page_size, | |
| 33 const gfx::Size& desired_size, | |
| 34 SkBitmap* snapshot) { | |
| 35 ASSERT_TRUE(snapshot); | |
| 36 static int g_sequence_num = 0; | |
| 37 // Use a new sequence number for each DIB. | |
| 38 scoped_ptr<TransportDIB> pixels( | |
| 39 TransportDIB::Create( | |
| 40 page_size.width() * page_size.height() * kNumBytesPerPixel, | |
| 41 ++g_sequence_num)); | |
| 42 | |
| 43 // Go ahead and map the DIB into memory, so that we can use it below | |
| 44 // to fill tmp_bitmap. Note that we need to do this before calling | |
| 45 // OnMsgPaintAtSize, or the last reference to the shared memory will | |
| 46 // be closed and the handle will no longer be valid. | |
| 47 scoped_ptr<TransportDIB> mapped_pixels(TransportDIB::Map(pixels->handle())); | |
| 48 | |
| 49 view_->OnMsgPaintAtSize(pixels->handle(), g_sequence_num, page_size, | |
| 50 desired_size); | |
| 51 ProcessPendingMessages(); | |
| 52 const IPC::Message* msg = render_thread_.sink().GetUniqueMessageMatching( | |
| 53 ViewHostMsg_PaintAtSize_ACK::ID); | |
| 54 ASSERT_NE(static_cast<IPC::Message*>(NULL), msg); | |
| 55 ViewHostMsg_PaintAtSize_ACK::Param params; | |
| 56 ViewHostMsg_PaintAtSize_ACK::Read(msg, ¶ms); | |
| 57 render_thread_.sink().ClearMessages(); | |
| 58 EXPECT_EQ(g_sequence_num, params.a); | |
| 59 gfx::Size size = params.b; | |
| 60 EXPECT_EQ(desired_size, size); | |
| 61 | |
| 62 SkBitmap tmp_bitmap; | |
| 63 tmp_bitmap.setConfig(SkBitmap::kARGB_8888_Config, | |
| 64 size.width(), size.height()); | |
| 65 tmp_bitmap.setPixels(mapped_pixels->memory()); | |
| 66 // Copy the pixels from the TransportDIB object to the given snapshot. | |
| 67 ASSERT_TRUE(tmp_bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config)); | |
| 68 } | |
| 69 | |
| 70 void RenderWidgetTest::TestResizeAndPaint() { | |
| 71 // Hello World message is only visible if the view size is at least | |
| 72 // kTextPositionX x kTextPositionY | |
| 73 LoadHTML(StringPrintf( | |
| 74 "<html><body><div style='position: absolute; top: %d; left: " | |
| 75 "%d; background-color: red;'>Hello World</div></body></html>", | |
| 76 kTextPositionY, kTextPositionX).c_str()); | |
| 77 WebKit::WebSize old_size = view_->webview()->size(); | |
| 78 | |
| 79 SkBitmap bitmap; | |
| 80 // If we re-size the view to something smaller than where the 'Hello World' | |
| 81 // text is displayed we won't see any text in the snapshot. Hence, | |
| 82 // the snapshot should not contain any red. | |
| 83 gfx::Size size(kSmallWidth, kSmallHeight); | |
| 84 ResizeAndPaint(size, size, &bitmap); | |
| 85 // Make sure that the view has been re-sized to its old size. | |
| 86 EXPECT_TRUE(old_size == view_->webview()->size()); | |
| 87 EXPECT_EQ(kSmallWidth, bitmap.width()); | |
| 88 EXPECT_EQ(kSmallHeight, bitmap.height()); | |
| 89 EXPECT_FALSE(ImageContainsColor(bitmap, kRedARGB)); | |
| 90 | |
| 91 // Since we ask for the view to be re-sized to something larger than where the | |
| 92 // 'Hello World' text is written the text should be visible in the snapshot. | |
| 93 // Hence, the snapshot should contain some red. | |
| 94 size.SetSize(kLargeWidth, kLargeHeight); | |
| 95 ResizeAndPaint(size, size, &bitmap); | |
| 96 EXPECT_TRUE(old_size == view_->webview()->size()); | |
| 97 EXPECT_EQ(kLargeWidth, bitmap.width()); | |
| 98 EXPECT_EQ(kLargeHeight, bitmap.height()); | |
| 99 EXPECT_TRUE(ImageContainsColor(bitmap, kRedARGB)); | |
| 100 | |
| 101 // Even if the desired size is smaller than where the text is located we | |
| 102 // should still see the 'Hello World' message since the view size is | |
| 103 // still large enough. | |
| 104 ResizeAndPaint(size, gfx::Size(kSmallWidth, kSmallHeight), &bitmap); | |
| 105 EXPECT_TRUE(old_size == view_->webview()->size()); | |
| 106 EXPECT_EQ(kSmallWidth, bitmap.width()); | |
| 107 EXPECT_EQ(kSmallHeight, bitmap.height()); | |
| 108 EXPECT_TRUE(ImageContainsColor(bitmap, kRedARGB)); | |
| 109 } | |
| 110 | |
| 111 bool RenderWidgetTest::ImageContainsColor(const SkBitmap& bitmap, | |
| 112 uint32 argb_color) { | |
| 113 SkAutoLockPixels lock(bitmap); | |
| 114 bool ready = bitmap.readyToDraw(); | |
| 115 EXPECT_TRUE(ready); | |
| 116 if (!ready) { | |
| 117 return false; | |
| 118 } | |
| 119 for (int x = 0; x < bitmap.width(); ++x) { | |
| 120 for (int y = 0; y < bitmap.height(); ++y) { | |
| 121 if (argb_color == *bitmap.getAddr32(x, y)) { | |
| 122 return true; | |
| 123 } | |
| 124 } | |
| 125 } | |
| 126 return false; | |
| 127 } | |
| 128 | |
| 129 void RenderWidgetTest::OutputBitmapToFile(const SkBitmap& bitmap, | |
| 130 const FilePath& file_path) { | |
| 131 scoped_refptr<RefCountedBytes> bitmap_data(new RefCountedBytes()); | |
| 132 SkAutoLockPixels lock(bitmap); | |
| 133 ASSERT_TRUE(gfx::JPEGCodec::Encode( | |
| 134 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | |
| 135 gfx::JPEGCodec::FORMAT_BGRA, | |
| 136 bitmap.width(), | |
| 137 bitmap.height(), | |
| 138 static_cast<int>(bitmap.rowBytes()), | |
| 139 90 /* quality */, | |
| 140 &bitmap_data->data)); | |
| 141 ASSERT_LT(0, file_util::WriteFile( | |
| 142 file_path, | |
| 143 reinterpret_cast<const char*>(bitmap_data->front()), | |
| 144 bitmap_data->size())); | |
| 145 } | |
| 146 | |
| 147 TEST_F(RenderWidgetTest, OnMsgPaintAtSize) { | |
| 148 TestResizeAndPaint(); | |
| 149 } | |
| OLD | NEW |