summaryrefslogtreecommitdiffstats
path: root/content/test
diff options
context:
space:
mode:
authorjam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-10 21:07:09 +0000
committerjam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-10 21:07:09 +0000
commit15edee45516a54fd066c2ca60b0f9ca4f5baae28 (patch)
treec6adf8f5d3a68d8bf2d3113f2cdc5c7030043ac2 /content/test
parent9c19439858bb0c4f050187f093631ea01176da46 (diff)
downloadchromium_src-15edee45516a54fd066c2ca60b0f9ca4f5baae28.zip
chromium_src-15edee45516a54fd066c2ca60b0f9ca4f5baae28.tar.gz
chromium_src-15edee45516a54fd066c2ca60b0f9ca4f5baae28.tar.bz2
Move some testing classes used by chrome to content\renderer, to match what we're doing with other test classes.
BUG=98716 Review URL: http://codereview.chromium.org/8216019 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@104781 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/test')
-rw-r--r--content/test/mock_render_process.cc44
-rw-r--r--content/test/mock_render_process.h31
-rw-r--r--content/test/render_view_fake_resources_test.cc2
-rw-r--r--content/test/render_widget_browsertest.cc149
-rw-r--r--content/test/render_widget_browsertest.h58
5 files changed, 283 insertions, 1 deletions
diff --git a/content/test/mock_render_process.cc b/content/test/mock_render_process.cc
new file mode 100644
index 0000000..0bce202
--- /dev/null
+++ b/content/test/mock_render_process.cc
@@ -0,0 +1,44 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/test/mock_render_process.h"
+
+#include "ui/gfx/rect.h"
+#include "ui/gfx/surface/transport_dib.h"
+
+MockRenderProcess::MockRenderProcess()
+ : transport_dib_next_sequence_number_(0) {
+}
+
+MockRenderProcess::~MockRenderProcess() {
+}
+
+skia::PlatformCanvas* MockRenderProcess::GetDrawingCanvas(
+ TransportDIB** memory,
+ const gfx::Rect& rect) {
+ size_t stride = skia::PlatformCanvas::StrideForWidth(rect.width());
+ size_t size = stride * rect.height();
+
+ // Unlike RenderProcessImpl, when we're a test, we can just create transport
+ // DIBs in the current process, since there is no sandbox protecting us (and
+ // no browser process to ask for one in any case).
+ *memory = TransportDIB::Create(size, transport_dib_next_sequence_number_++);
+ if (!*memory)
+ return NULL;
+ return (*memory)->GetPlatformCanvas(rect.width(), rect.height());
+}
+
+void MockRenderProcess::ReleaseTransportDIB(TransportDIB* memory) {
+ delete memory;
+}
+
+bool MockRenderProcess::UseInProcessPlugins() const {
+ return true;
+}
+
+bool MockRenderProcess::HasInitializedMediaLibrary() const {
+ return false;
+}
+
+
diff --git a/content/test/mock_render_process.h b/content/test/mock_render_process.h
new file mode 100644
index 0000000..ef4db6c
--- /dev/null
+++ b/content/test/mock_render_process.h
@@ -0,0 +1,31 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CONTENT_TEST_MOCK_RENDER_PROCESS_H_
+#define CONTENT_TEST_MOCK_RENDER_PROCESS_H_
+#pragma once
+
+#include "content/renderer/render_process.h"
+
+// This class is a mock of the child process singleton which we use during
+// running of the RenderView unit tests.
+class MockRenderProcess : public RenderProcess {
+ public:
+ MockRenderProcess();
+ virtual ~MockRenderProcess();
+
+ // RenderProcess implementation.
+ virtual skia::PlatformCanvas* GetDrawingCanvas(TransportDIB** memory,
+ const gfx::Rect& rect);
+ virtual void ReleaseTransportDIB(TransportDIB* memory);
+ virtual bool UseInProcessPlugins() const;
+ virtual bool HasInitializedMediaLibrary() const;
+
+ private:
+ uint32 transport_dib_next_sequence_number_;
+
+ DISALLOW_COPY_AND_ASSIGN(MockRenderProcess);
+};
+
+#endif // CONTENT_TEST_MOCK_RENDER_PROCESS_H_
diff --git a/content/test/render_view_fake_resources_test.cc b/content/test/render_view_fake_resources_test.cc
index 3be34ad..58b8b1c 100644
--- a/content/test/render_view_fake_resources_test.cc
+++ b/content/test/render_view_fake_resources_test.cc
@@ -13,9 +13,9 @@
#include "content/common/resource_messages.h"
#include "content/common/resource_response.h"
#include "content/common/view_messages.h"
-#include "content/renderer/mock_render_process.h"
#include "content/renderer/render_thread_impl.h"
#include "content/renderer/render_view_impl.h"
+#include "content/test/mock_render_process.h"
#include "googleurl/src/gurl.h"
#include "net/base/upload_data.h"
#include "net/http/http_response_headers.h"
diff --git a/content/test/render_widget_browsertest.cc b/content/test/render_widget_browsertest.cc
new file mode 100644
index 0000000..62ccd72
--- /dev/null
+++ b/content/test/render_widget_browsertest.cc
@@ -0,0 +1,149 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/test/render_widget_browsertest.h"
+
+#include "base/basictypes.h"
+#include "base/file_path.h"
+#include "base/file_util.h"
+#include "base/memory/ref_counted_memory.h"
+#include "base/stringprintf.h"
+#include "content/common/view_messages.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
+#include "ui/gfx/codec/jpeg_codec.h"
+#include "ui/gfx/size.h"
+#include "ui/gfx/surface/transport_dib.h"
+
+const int RenderWidgetTest::kNumBytesPerPixel = 4;
+const int RenderWidgetTest::kLargeWidth = 1024;
+const int RenderWidgetTest::kLargeHeight = 768;
+const int RenderWidgetTest::kSmallWidth = 600;
+const int RenderWidgetTest::kSmallHeight = 450;
+const int RenderWidgetTest::kTextPositionX = 800;
+const int RenderWidgetTest::kTextPositionY = 600;
+const uint32 RenderWidgetTest::kRedARGB = 0xFFFF0000;
+
+RenderWidgetTest::RenderWidgetTest() {}
+
+void RenderWidgetTest::ResizeAndPaint(const gfx::Size& page_size,
+ const gfx::Size& desired_size,
+ SkBitmap* snapshot) {
+ ASSERT_TRUE(snapshot);
+ static int g_sequence_num = 0;
+ // Use a new sequence number for each DIB.
+ scoped_ptr<TransportDIB> pixels(
+ TransportDIB::Create(
+ page_size.width() * page_size.height() * kNumBytesPerPixel,
+ ++g_sequence_num));
+
+ // Go ahead and map the DIB into memory, so that we can use it below
+ // to fill tmp_bitmap. Note that we need to do this before calling
+ // OnMsgPaintAtSize, or the last reference to the shared memory will
+ // be closed and the handle will no longer be valid.
+ scoped_ptr<TransportDIB> mapped_pixels(TransportDIB::Map(pixels->handle()));
+
+ view_->OnMsgPaintAtSize(pixels->handle(), g_sequence_num, page_size,
+ desired_size);
+ ProcessPendingMessages();
+ const IPC::Message* msg = render_thread_.sink().GetUniqueMessageMatching(
+ ViewHostMsg_PaintAtSize_ACK::ID);
+ ASSERT_NE(static_cast<IPC::Message*>(NULL), msg);
+ ViewHostMsg_PaintAtSize_ACK::Param params;
+ ViewHostMsg_PaintAtSize_ACK::Read(msg, &params);
+ render_thread_.sink().ClearMessages();
+ EXPECT_EQ(g_sequence_num, params.a);
+ gfx::Size size = params.b;
+ EXPECT_EQ(desired_size, size);
+
+ SkBitmap tmp_bitmap;
+ tmp_bitmap.setConfig(SkBitmap::kARGB_8888_Config,
+ size.width(), size.height());
+ tmp_bitmap.setPixels(mapped_pixels->memory());
+ // Copy the pixels from the TransportDIB object to the given snapshot.
+ ASSERT_TRUE(tmp_bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config));
+}
+
+void RenderWidgetTest::TestResizeAndPaint() {
+ // Hello World message is only visible if the view size is at least
+ // kTextPositionX x kTextPositionY
+ LoadHTML(StringPrintf(
+ "<html><body><div style='position: absolute; top: %d; left: "
+ "%d; background-color: red;'>Hello World</div></body></html>",
+ kTextPositionY, kTextPositionX).c_str());
+ WebKit::WebSize old_size = view_->webview()->size();
+
+ SkBitmap bitmap;
+ // If we re-size the view to something smaller than where the 'Hello World'
+ // text is displayed we won't see any text in the snapshot. Hence,
+ // the snapshot should not contain any red.
+ gfx::Size size(kSmallWidth, kSmallHeight);
+ ResizeAndPaint(size, size, &bitmap);
+ // Make sure that the view has been re-sized to its old size.
+ EXPECT_TRUE(old_size == view_->webview()->size());
+ EXPECT_EQ(kSmallWidth, bitmap.width());
+ EXPECT_EQ(kSmallHeight, bitmap.height());
+ EXPECT_FALSE(ImageContainsColor(bitmap, kRedARGB));
+
+ // Since we ask for the view to be re-sized to something larger than where the
+ // 'Hello World' text is written the text should be visible in the snapshot.
+ // Hence, the snapshot should contain some red.
+ size.SetSize(kLargeWidth, kLargeHeight);
+ ResizeAndPaint(size, size, &bitmap);
+ EXPECT_TRUE(old_size == view_->webview()->size());
+ EXPECT_EQ(kLargeWidth, bitmap.width());
+ EXPECT_EQ(kLargeHeight, bitmap.height());
+ EXPECT_TRUE(ImageContainsColor(bitmap, kRedARGB));
+
+ // Even if the desired size is smaller than where the text is located we
+ // should still see the 'Hello World' message since the view size is
+ // still large enough.
+ ResizeAndPaint(size, gfx::Size(kSmallWidth, kSmallHeight), &bitmap);
+ EXPECT_TRUE(old_size == view_->webview()->size());
+ EXPECT_EQ(kSmallWidth, bitmap.width());
+ EXPECT_EQ(kSmallHeight, bitmap.height());
+ EXPECT_TRUE(ImageContainsColor(bitmap, kRedARGB));
+}
+
+bool RenderWidgetTest::ImageContainsColor(const SkBitmap& bitmap,
+ uint32 argb_color) {
+ SkAutoLockPixels lock(bitmap);
+ bool ready = bitmap.readyToDraw();
+ EXPECT_TRUE(ready);
+ if (!ready) {
+ return false;
+ }
+ for (int x = 0; x < bitmap.width(); ++x) {
+ for (int y = 0; y < bitmap.height(); ++y) {
+ if (argb_color == *bitmap.getAddr32(x, y)) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+void RenderWidgetTest::OutputBitmapToFile(const SkBitmap& bitmap,
+ const FilePath& file_path) {
+ scoped_refptr<RefCountedBytes> bitmap_data(new RefCountedBytes());
+ SkAutoLockPixels lock(bitmap);
+ ASSERT_TRUE(gfx::JPEGCodec::Encode(
+ reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
+ gfx::JPEGCodec::FORMAT_BGRA,
+ bitmap.width(),
+ bitmap.height(),
+ static_cast<int>(bitmap.rowBytes()),
+ 90 /* quality */,
+ &bitmap_data->data()));
+ ASSERT_LT(0, file_util::WriteFile(
+ file_path,
+ reinterpret_cast<const char*>(bitmap_data->front()),
+ bitmap_data->size()));
+}
+
+TEST_F(RenderWidgetTest, OnMsgPaintAtSize) {
+ TestResizeAndPaint();
+}
diff --git a/content/test/render_widget_browsertest.h b/content/test/render_widget_browsertest.h
new file mode 100644
index 0000000..c3e0def
--- /dev/null
+++ b/content/test/render_widget_browsertest.h
@@ -0,0 +1,58 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CONTENT_TEST_RENDER_WIDGET_BROWSERTEST_H_
+#define CONTENT_TEST_RENDER_WIDGET_BROWSERTEST_H_
+#pragma once
+
+#include "base/basictypes.h"
+#include "base/file_path.h"
+#include "chrome/test/base/render_view_test.h"
+
+namespace gfx {
+class Size;
+}
+
+class SkBitmap;
+class TransportDIB;
+
+class RenderWidgetTest : public RenderViewTest {
+ public:
+ RenderWidgetTest();
+
+ protected:
+ static const int kNumBytesPerPixel;
+ static const int kLargeWidth;
+ static const int kLargeHeight;
+ static const int kSmallWidth;
+ static const int kSmallHeight;
+ static const int kTextPositionX;
+ static const int kTextPositionY;
+ static const uint32 kRedARGB;
+
+ // Helper function which calls OnMsgPaintAtSize and also paints the result
+ // in the given bitmap. The widget is resized to |page_size| before we paint
+ // and the final image is resized to |desired_size|. This method is virtual so
+ // that TestResizeAndPaint() can be reused by subclasses of this test class.
+ virtual void ResizeAndPaint(const gfx::Size& page_size,
+ const gfx::Size& desired_size,
+ SkBitmap* snapshot);
+
+ // Test for ResizeAndPaint.
+ void TestResizeAndPaint();
+
+ // Helper function which returns true if the given bitmap contains the given
+ // ARGB color and false otherwise.
+ bool ImageContainsColor(const SkBitmap& bitmap, uint32 argb_color);
+
+ // This can be used for debugging if you want to output a bitmap
+ // image to a file.
+ // FilePath tmp_path;
+ // file_util::CreateTemporaryFile(&tmp_path);
+ // OutputBitmapToFile(bitmap, tmp_path);
+ // LOG(INFO) << "Bitmap image stored at: " << tmp_path.value();
+ void OutputBitmapToFile(const SkBitmap& bitmap, const FilePath& file_path);
+};
+
+#endif // CONTENT_TEST_RENDER_WIDGET_BROWSERTEST_H_