1 // Copyright (c) 2012 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.
5 #include "webkit/glue/webkit_glue.h"
9 #include "base/message_loop.h"
10 #include "base/time.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "third_party/skia/include/core/SkColorPriv.h"
14 #include "webkit/glue/webkitplatformsupport_impl.h"
18 TEST(WebkitGlueTest
, DecodeImageFail
) {
19 std::string
data("not an image");
21 EXPECT_FALSE(webkit_glue::DecodeImage(data
, &image
));
22 EXPECT_TRUE(image
.isNull());
25 TEST(WebkitGlueTest
, DecodeImage
) {
26 std::string
data("GIF87a\x02\x00\x02\x00\xa1\x04\x00\x00\x00\x00\x00\x00\xff"
27 "\xff\x00\x00\x00\xff\x00,\x00\x00\x00\x00\x02\x00\x02\x00"
28 "\x00\x02\x03\x84\x16\x05\x00;", 42);
29 EXPECT_EQ(42u, data
.size());
31 EXPECT_TRUE(webkit_glue::DecodeImage(data
, &image
));
32 EXPECT_FALSE(image
.isNull());
33 EXPECT_EQ(2, image
.width());
34 EXPECT_EQ(2, image
.height());
35 EXPECT_EQ(SkBitmap::kARGB_8888_Config
, image
.config());
37 uint32_t pixel
= *image
.getAddr32(0, 0); // Black
38 EXPECT_EQ(0x00U
, SkGetPackedR32(pixel
));
39 EXPECT_EQ(0x00U
, SkGetPackedG32(pixel
));
40 EXPECT_EQ(0x00U
, SkGetPackedB32(pixel
));
42 pixel
= *image
.getAddr32(1, 0); // Red
43 EXPECT_EQ(0xffU
, SkGetPackedR32(pixel
));
44 EXPECT_EQ(0x00U
, SkGetPackedG32(pixel
));
45 EXPECT_EQ(0x00U
, SkGetPackedB32(pixel
));
47 pixel
= *image
.getAddr32(0, 1); // Green
48 EXPECT_EQ(0x00U
, SkGetPackedR32(pixel
));
49 EXPECT_EQ(0xffU
, SkGetPackedG32(pixel
));
50 EXPECT_EQ(0x00U
, SkGetPackedB32(pixel
));
52 pixel
= *image
.getAddr32(1, 1); // Blue
53 EXPECT_EQ(0x00U
, SkGetPackedR32(pixel
));
54 EXPECT_EQ(0x00U
, SkGetPackedG32(pixel
));
55 EXPECT_EQ(0xffU
, SkGetPackedB32(pixel
));
59 // Derives WebKitPlatformSupportImpl for testing shared timers.
60 class TestWebKitPlatformSupport
61 : public webkit_glue::WebKitPlatformSupportImpl
{
63 TestWebKitPlatformSupport() : mock_monotonically_increasing_time_(0) {
66 // WebKitPlatformSupportImpl implementation
67 virtual base::string16
GetLocalizedString(int) OVERRIDE
{
68 return base::string16();
71 virtual base::StringPiece
GetDataResource(int, ui::ScaleFactor
) OVERRIDE
{
72 return base::StringPiece();
75 virtual void GetPlugins(bool,
76 std::vector
<webkit::WebPluginInfo
,
77 std::allocator
<webkit::WebPluginInfo
> >*) OVERRIDE
{
80 virtual webkit_glue::ResourceLoaderBridge
* CreateResourceLoader(
81 const webkit_glue::ResourceLoaderBridge::RequestInfo
&) OVERRIDE
{
85 virtual webkit_glue::WebSocketStreamHandleBridge
* CreateWebSocketBridge(
86 WebKit::WebSocketStreamHandle
*,
87 webkit_glue::WebSocketStreamHandleDelegate
*) OVERRIDE
{
91 // Returns mock time when enabled.
92 virtual double monotonicallyIncreasingTime() OVERRIDE
{
93 if (mock_monotonically_increasing_time_
> 0.0)
94 return mock_monotonically_increasing_time_
;
95 return webkit_glue::WebKitPlatformSupportImpl::
96 monotonicallyIncreasingTime();
99 virtual void OnStartSharedTimer(base::TimeDelta delay
) OVERRIDE
{
100 shared_timer_delay_
= delay
;
103 base::TimeDelta
shared_timer_delay() {
104 return shared_timer_delay_
;
107 void set_mock_monotonically_increasing_time(double mock_time
) {
108 mock_monotonically_increasing_time_
= mock_time
;
112 base::TimeDelta shared_timer_delay_
;
113 double mock_monotonically_increasing_time_
;
116 TEST(WebkitGlueTest
, SuspendResumeSharedTimer
) {
117 MessageLoop message_loop
;
118 TestWebKitPlatformSupport platform_support
;
120 // Set a timer to fire as soon as possible.
121 platform_support
.setSharedTimerFireInterval(0);
122 // Suspend timers immediately so the above timer wouldn't be fired.
123 platform_support
.SuspendSharedTimer();
124 // The above timer would have posted a task which can be processed out of the
126 message_loop
.RunUntilIdle();
127 // Set a mock time after 1 second to simulate timers suspended for 1 second.
128 double new_time
= base::Time::Now().ToDoubleT() + 1;
129 platform_support
.set_mock_monotonically_increasing_time(new_time
);
130 // Resume timers so that the timer set above will be set again to fire
132 platform_support
.ResumeSharedTimer();
133 EXPECT_TRUE(base::TimeDelta() == platform_support
.shared_timer_delay());