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/devtools_client_host.h"
11 #include "content/public/browser/devtools_manager.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/test/browser_test_utils.h"
14 #include "content/public/test/content_browser_test.h"
15 #include "content/shell/browser/shell.h"
16 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "ui/compositor/compositor_switches.h"
18 #include "ui/gfx/codec/png_codec.h"
22 class RendererOverridesHandlerTest
: public ContentBrowserTest
,
23 public DevToolsClientHost
{
25 void SendCommand(const std::string
& method
,
26 base::DictionaryValue
* params
) {
27 EXPECT_TRUE(DevToolsManager::GetInstance()->DispatchOnInspectorBackend(this,
28 DevToolsProtocol::CreateCommand(1, method
, params
)->Serialize()));
29 base::MessageLoop::current()->Run();
32 bool HasValue(const std::string
& path
) {
33 base::Value
* value
= 0;
34 return result_
->Get(path
, &value
);
37 bool HasListItem(const std::string
& path_to_list
,
38 const std::string
& name
,
39 const std::string
& value
) {
40 base::ListValue
* list
;
41 if (!result_
->GetList(path_to_list
, &list
))
44 for (size_t i
= 0; i
!= list
->GetSize(); i
++) {
45 base::DictionaryValue
* item
;
46 if (!list
->GetDictionary(i
, &item
))
49 if (!item
->GetString(name
, &id
))
57 scoped_ptr
<base::DictionaryValue
> result_
;
60 virtual void SetUpOnMainThread() OVERRIDE
{
61 DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor(
62 DevToolsAgentHost::GetOrCreateFor(
63 shell()->web_contents()->GetRenderViewHost()).get(),
67 virtual void TearDownOnMainThread() OVERRIDE
{
68 DevToolsManager::GetInstance()->ClientHostClosing(this);
71 virtual void DispatchOnInspectorFrontend(
72 const std::string
& message
) OVERRIDE
{
73 scoped_ptr
<base::DictionaryValue
> root(
74 static_cast<base::DictionaryValue
*>(base::JSONReader::Read(message
)));
75 base::DictionaryValue
* result
;
76 EXPECT_TRUE(root
->GetDictionary("result", &result
));
77 result_
.reset(result
->DeepCopy());
78 base::MessageLoop::current()->QuitNow();
81 virtual void InspectedContentsClosing() OVERRIDE
{
85 virtual void ReplacedWithAnotherClient() OVERRIDE
{
90 IN_PROC_BROWSER_TEST_F(RendererOverridesHandlerTest
, QueryUsageAndQuota
) {
91 base::DictionaryValue
* params
= new base::DictionaryValue();
92 params
->SetString("securityOrigin", "http://example.com");
93 SendCommand("Page.queryUsageAndQuota", params
);
95 EXPECT_TRUE(HasValue("quota.persistent"));
96 EXPECT_TRUE(HasValue("quota.temporary"));
97 EXPECT_TRUE(HasListItem("usage.temporary", "id", "appcache"));
98 EXPECT_TRUE(HasListItem("usage.temporary", "id", "database"));
99 EXPECT_TRUE(HasListItem("usage.temporary", "id", "indexeddatabase"));
100 EXPECT_TRUE(HasListItem("usage.temporary", "id", "filesystem"));
101 EXPECT_TRUE(HasListItem("usage.persistent", "id", "filesystem"));
104 class CaptureScreenshotTest
: public RendererOverridesHandlerTest
{
106 #if !defined(OS_ANDROID)
107 virtual void SetUpCommandLine(base::CommandLine
* command_line
) OVERRIDE
{
108 command_line
->AppendSwitch(switches::kEnablePixelOutputInTests
);
113 // Does not link on Android
114 #if defined(OS_ANDROID)
115 #define MAYBE_CaptureScreenshot DISABLED_CaptureScreenshot
117 #define MAYBE_CaptureScreenshot CaptureScreenshot
119 IN_PROC_BROWSER_TEST_F(CaptureScreenshotTest
, MAYBE_CaptureScreenshot
) {
120 shell()->LoadURL(GURL("about:blank"));
121 EXPECT_TRUE(content::ExecuteScript(
122 shell()->web_contents()->GetRenderViewHost(),
123 "document.body.style.background = '#123456'"));
124 SendCommand("Page.captureScreenshot", new base::DictionaryValue());
126 EXPECT_TRUE(result_
->GetString("data", &base64
));
128 EXPECT_TRUE(base::Base64Decode(base64
, &png
));
130 gfx::PNGCodec::Decode(reinterpret_cast<const unsigned char*>(png
.data()),
131 png
.size(), &bitmap
);
132 SkColor
color(bitmap
.getColor(0, 0));
133 EXPECT_TRUE(std::abs(0x12-(int)SkColorGetR(color
)) <= 1);
134 EXPECT_TRUE(std::abs(0x34-(int)SkColorGetG(color
)) <= 1);
135 EXPECT_TRUE(std::abs(0x56-(int)SkColorGetB(color
)) <= 1);
138 } // namespace content