1 // Copyright 2013 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 "base/base64.h"
6 #include "base/command_line.h"
7 #include "base/json/json_reader.h"
8 #include "content/browser/devtools/devtools_protocol.h"
9 #include "content/public/browser/devtools_agent_host.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/test/browser_test_utils.h"
12 #include "content/public/test/content_browser_test.h"
13 #include "content/shell/browser/shell.h"
14 #include "third_party/skia/include/core/SkBitmap.h"
15 #include "ui/compositor/compositor_switches.h"
16 #include "ui/gfx/codec/png_codec.h"
20 class RendererOverridesHandlerTest
: public ContentBrowserTest
,
21 public DevToolsAgentHostClient
{
23 void SendCommand(const std::string
& method
,
24 base::DictionaryValue
* params
) {
25 agent_host_
->DispatchProtocolMessage(
26 DevToolsProtocol::CreateCommand(1, method
, params
)->Serialize());
27 base::MessageLoop::current()->Run();
30 bool HasValue(const std::string
& path
) {
31 base::Value
* value
= 0;
32 return result_
->Get(path
, &value
);
35 bool HasListItem(const std::string
& path_to_list
,
36 const std::string
& name
,
37 const std::string
& value
) {
38 base::ListValue
* list
;
39 if (!result_
->GetList(path_to_list
, &list
))
42 for (size_t i
= 0; i
!= list
->GetSize(); i
++) {
43 base::DictionaryValue
* item
;
44 if (!list
->GetDictionary(i
, &item
))
47 if (!item
->GetString(name
, &id
))
55 scoped_ptr
<base::DictionaryValue
> result_
;
56 scoped_refptr
<DevToolsAgentHost
> agent_host_
;
59 void SetUpOnMainThread() override
{
60 agent_host_
= DevToolsAgentHost::GetOrCreateFor(shell()->web_contents());
61 agent_host_
->AttachClient(this);
64 void TearDownOnMainThread() override
{
65 agent_host_
->DetachClient();
69 void DispatchProtocolMessage(DevToolsAgentHost
* agent_host
,
70 const std::string
& message
) override
{
71 scoped_ptr
<base::DictionaryValue
> root(
72 static_cast<base::DictionaryValue
*>(base::JSONReader::Read(message
)));
73 base::DictionaryValue
* result
;
74 EXPECT_TRUE(root
->GetDictionary("result", &result
));
75 result_
.reset(result
->DeepCopy());
76 base::MessageLoop::current()->QuitNow();
79 void AgentHostClosed(DevToolsAgentHost
* agent_host
, bool replaced
) override
{
84 IN_PROC_BROWSER_TEST_F(RendererOverridesHandlerTest
, QueryUsageAndQuota
) {
85 base::DictionaryValue
* params
= new base::DictionaryValue();
86 params
->SetString("securityOrigin", "http://example.com");
87 SendCommand("Page.queryUsageAndQuota", params
);
89 EXPECT_TRUE(HasValue("quota.persistent"));
90 EXPECT_TRUE(HasValue("quota.temporary"));
91 EXPECT_TRUE(HasListItem("usage.temporary", "id", "appcache"));
92 EXPECT_TRUE(HasListItem("usage.temporary", "id", "database"));
93 EXPECT_TRUE(HasListItem("usage.temporary", "id", "indexeddatabase"));
94 EXPECT_TRUE(HasListItem("usage.temporary", "id", "filesystem"));
95 EXPECT_TRUE(HasListItem("usage.persistent", "id", "filesystem"));
98 class CaptureScreenshotTest
: public RendererOverridesHandlerTest
{
100 #if !defined(OS_ANDROID)
101 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
102 command_line
->AppendSwitch(switches::kEnablePixelOutputInTests
);
107 // Does not link on Android
108 #if defined(OS_ANDROID)
109 #define MAYBE_CaptureScreenshot DISABLED_CaptureScreenshot
110 #elif defined(OS_MACOSX) // Fails on 10.9. http://crbug.com/430620
111 #define MAYBE_CaptureScreenshot DISABLED_CaptureScreenshot
112 #elif defined(MEMORY_SANITIZER)
113 // Also fails under MSAN. http://crbug.com/423583
114 #define MAYBE_CaptureScreenshot DISABLED_CaptureScreenshot
116 #define MAYBE_CaptureScreenshot CaptureScreenshot
118 IN_PROC_BROWSER_TEST_F(CaptureScreenshotTest
, MAYBE_CaptureScreenshot
) {
119 shell()->LoadURL(GURL("about:blank"));
120 EXPECT_TRUE(content::ExecuteScript(
121 shell()->web_contents()->GetRenderViewHost(),
122 "document.body.style.background = '#123456'"));
123 SendCommand("Page.captureScreenshot", new base::DictionaryValue());
125 EXPECT_TRUE(result_
->GetString("data", &base64
));
127 EXPECT_TRUE(base::Base64Decode(base64
, &png
));
129 gfx::PNGCodec::Decode(reinterpret_cast<const unsigned char*>(png
.data()),
130 png
.size(), &bitmap
);
131 SkColor
color(bitmap
.getColor(0, 0));
132 EXPECT_TRUE(std::abs(0x12-(int)SkColorGetR(color
)) <= 1);
133 EXPECT_TRUE(std::abs(0x34-(int)SkColorGetG(color
)) <= 1);
134 EXPECT_TRUE(std::abs(0x56-(int)SkColorGetB(color
)) <= 1);
137 } // namespace content