Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / browser / devtools / protocol / devtools_protocol_browsertest.cc
blobcbbd46a45d295946600f702d5fff79d678964eb5
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 "base/json/json_writer.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"
18 namespace content {
20 namespace {
22 const char kIdParam[] = "id";
23 const char kMethodParam[] = "method";
24 const char kParamsParam[] = "params";
28 class DevToolsProtocolTest : public ContentBrowserTest,
29 public DevToolsAgentHostClient {
30 protected:
31 void SendCommand(const std::string& method,
32 scoped_ptr<base::DictionaryValue> params) {
33 base::DictionaryValue command;
34 command.SetInteger(kIdParam, 1);
35 command.SetString(kMethodParam, method);
36 if (params)
37 command.Set(kParamsParam, params.release());
39 std::string json_command;
40 base::JSONWriter::Write(&command, &json_command);
41 agent_host_->DispatchProtocolMessage(json_command);
42 base::MessageLoop::current()->Run();
45 bool HasValue(const std::string& path) {
46 base::Value* value = 0;
47 return result_->Get(path, &value);
50 bool HasListItem(const std::string& path_to_list,
51 const std::string& name,
52 const std::string& value) {
53 base::ListValue* list;
54 if (!result_->GetList(path_to_list, &list))
55 return false;
57 for (size_t i = 0; i != list->GetSize(); i++) {
58 base::DictionaryValue* item;
59 if (!list->GetDictionary(i, &item))
60 return false;
61 std::string id;
62 if (!item->GetString(name, &id))
63 return false;
64 if (id == value)
65 return true;
67 return false;
70 scoped_ptr<base::DictionaryValue> result_;
71 scoped_refptr<DevToolsAgentHost> agent_host_;
73 private:
74 void SetUpOnMainThread() override {
75 agent_host_ = DevToolsAgentHost::GetOrCreateFor(shell()->web_contents());
76 agent_host_->AttachClient(this);
79 void TearDownOnMainThread() override {
80 agent_host_->DetachClient();
81 agent_host_ = NULL;
84 void DispatchProtocolMessage(DevToolsAgentHost* agent_host,
85 const std::string& message) override {
86 scoped_ptr<base::DictionaryValue> root(
87 static_cast<base::DictionaryValue*>(base::JSONReader::Read(message)));
88 base::DictionaryValue* result;
89 EXPECT_TRUE(root->GetDictionary("result", &result));
90 result_.reset(result->DeepCopy());
91 base::MessageLoop::current()->QuitNow();
94 void AgentHostClosed(DevToolsAgentHost* agent_host, bool replaced) override {
95 EXPECT_TRUE(false);
99 class CaptureScreenshotTest : public DevToolsProtocolTest {
100 private:
101 #if !defined(OS_ANDROID)
102 void SetUpCommandLine(base::CommandLine* command_line) override {
103 command_line->AppendSwitch(switches::kEnablePixelOutputInTests);
105 #endif
108 // Does not link on Android
109 #if defined(OS_ANDROID)
110 #define MAYBE_CaptureScreenshot DISABLED_CaptureScreenshot
111 #elif defined(OS_MACOSX) // Fails on 10.9. http://crbug.com/430620
112 #define MAYBE_CaptureScreenshot DISABLED_CaptureScreenshot
113 #elif defined(MEMORY_SANITIZER)
114 // Also fails under MSAN. http://crbug.com/423583
115 #define MAYBE_CaptureScreenshot DISABLED_CaptureScreenshot
116 #else
117 #define MAYBE_CaptureScreenshot CaptureScreenshot
118 #endif
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", nullptr);
125 std::string base64;
126 EXPECT_TRUE(result_->GetString("data", &base64));
127 std::string png;
128 EXPECT_TRUE(base::Base64Decode(base64, &png));
129 SkBitmap bitmap;
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