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
|
// Copyright (c) 2012 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/renderer/pepper/pepper_plugin_instance_impl.h"
#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "content/renderer/pepper/plugin_delegate.h"
#include "content/renderer/pepper/ppapi_unittest.h"
#include "content/renderer/pepper/ppb_image_data_impl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/safe_integer_conversions.h"
namespace content {
namespace {
// A mock graphics object to bind. We only have to implement enough so that
// GetBitmapForOptimizedPluginPaint runs.
//
// If this is eventually all moved to content, we can simplify this. The
// point here is that we need to just have a bound graphics 2d resource host
// with the right properties.
class MockPlatformGraphics2D : public PluginDelegate::PlatformGraphics2D {
public:
// The image data pointer must outlive this class.
MockPlatformGraphics2D(PPB_ImageData_Impl* image_data, float scale)
: image_data_(image_data),
scale_(scale),
bound_instance_(NULL) {
}
virtual ~MockPlatformGraphics2D() {
if (bound_instance_)
bound_instance_->SetBoundGraphics2DForTest(NULL);
}
virtual bool ReadImageData(PP_Resource image,
const PP_Point* top_left) OVERRIDE {
return false;
}
virtual bool BindToInstance(PepperPluginInstanceImpl* new_instance) OVERRIDE {
bound_instance_ = new_instance;
return true;
}
virtual void Paint(WebKit::WebCanvas* canvas,
const gfx::Rect& plugin_rect,
const gfx::Rect& paint_rect) OVERRIDE {}
virtual void ViewWillInitiatePaint() OVERRIDE {}
virtual void ViewInitiatedPaint() OVERRIDE {}
virtual void ViewFlushedPaint() OVERRIDE {}
virtual bool IsAlwaysOpaque() const OVERRIDE { return true; }
virtual void SetScale(float scale) OVERRIDE {}
virtual float GetScale() const OVERRIDE { return scale_; }
virtual PPB_ImageData_Impl* ImageData() OVERRIDE {
return image_data_;
}
private:
PPB_ImageData_Impl* image_data_;
float scale_;
PepperPluginInstanceImpl* bound_instance_;
DISALLOW_COPY_AND_ASSIGN(MockPlatformGraphics2D);
};
} // namespace
// This class is forward-declared as a friend so can't be in the anonymous
// namespace.
class PpapiPluginInstanceTest : public PpapiUnittest {
public:
bool GetBitmapForOptimizedPluginPaint(const gfx::Rect& paint_bounds,
TransportDIB** dib,
gfx::Rect* location,
gfx::Rect* clip,
float* scale_factor) {
return !!instance()->GetBitmapForOptimizedPluginPaint(
paint_bounds, dib, location, clip, scale_factor);
}
};
// This matches the if condition in ImageDataPlatformBackend::Init as we can't
// use RenderThread in the test.
#if !(defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID))
// Test that GetBitmapForOptimizedPluginPaint doesn't return a bitmap rect
// that's bigger than the actual backing store bitmap.
TEST_F(PpapiPluginInstanceTest, GetBitmap2xScale) {
PP_Size size;
size.width = 3;
size.height = 3;
scoped_refptr<PPB_ImageData_Impl> image_data = new PPB_ImageData_Impl(
instance()->pp_instance(), PPB_ImageData_Impl::PLATFORM);
image_data->Init(PPB_ImageData_Impl::GetNativeImageDataFormat(),
size.width, size.height, true);
ASSERT_TRUE(image_data->Map() != NULL);
float scale = 2.0;
MockPlatformGraphics2D mock_graphics_2d(image_data.get(), 1.0 / scale);
instance()->SetBoundGraphics2DForTest(&mock_graphics_2d);
instance()->SetAlwaysOnTop(true);
SetViewSize(size.width, size.height);
gfx::Rect bounds(0, 0, 1, 1);
gfx::Rect location;
gfx::Rect clip;
float bitmap_scale = 0;
TransportDIB* dib = NULL;
EXPECT_TRUE(GetBitmapForOptimizedPluginPaint(
bounds, &dib, &location, &clip, &bitmap_scale));
EXPECT_EQ(0, location.x());
EXPECT_EQ(0, location.y());
EXPECT_EQ(gfx::ToFlooredInt(size.width / scale), location.width());
EXPECT_EQ(gfx::ToFlooredInt(size.height / scale), location.height());
EXPECT_EQ(0, clip.x());
EXPECT_EQ(0, clip.y());
EXPECT_EQ(size.width, clip.width());
EXPECT_EQ(size.height, clip.height());
EXPECT_EQ(scale, bitmap_scale);
}
#endif
} // namespace content
|