summaryrefslogtreecommitdiffstats
path: root/content/browser/devtools/renderer_overrides_handler_browsertest.cc
blob: a6cd973bc1a3422d272c40ef652d372a075ae23b (plain)
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
131
132
133
134
135
136
// Copyright 2013 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 "base/base64.h"
#include "base/command_line.h"
#include "base/json/json_reader.h"
#include "content/browser/devtools/devtools_protocol.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_client_host.h"
#include "content/public/browser/devtools_manager.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/shell/browser/shell.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/compositor/compositor_switches.h"
#include "ui/gfx/codec/png_codec.h"

namespace content {

class RendererOverridesHandlerTest : public ContentBrowserTest,
                                     public DevToolsClientHost {
 protected:
  void SendCommand(const std::string& method,
                   base::DictionaryValue* params) {
    EXPECT_TRUE(DevToolsManager::GetInstance()->DispatchOnInspectorBackend(this,
        DevToolsProtocol::CreateCommand(1, method, params)->Serialize()));
    base::MessageLoop::current()->Run();
  }

  bool HasValue(const std::string& path) {
    base::Value* value = 0;
    return result_->Get(path, &value);
  }

  bool HasListItem(const std::string& path_to_list,
                   const std::string& name,
                   const std::string& value) {
    base::ListValue* list;
    if (!result_->GetList(path_to_list, &list))
      return false;

    for (size_t i = 0; i != list->GetSize(); i++) {
      base::DictionaryValue* item;
      if (!list->GetDictionary(i, &item))
        return false;
      std::string id;
      if (!item->GetString(name, &id))
        return false;
      if (id == value)
        return true;
    }
    return false;
  }

  scoped_ptr<base::DictionaryValue> result_;

 private:
  virtual void SetUpOnMainThread() OVERRIDE {
    DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor(
        DevToolsAgentHost::GetOrCreateFor(shell()->web_contents()).get(), this);
  }

  virtual void TearDownOnMainThread() OVERRIDE {
    DevToolsManager::GetInstance()->ClientHostClosing(this);
  }

  virtual void DispatchOnInspectorFrontend(
      const std::string& message) OVERRIDE {
    scoped_ptr<base::DictionaryValue> root(
        static_cast<base::DictionaryValue*>(base::JSONReader::Read(message)));
    base::DictionaryValue* result;
    EXPECT_TRUE(root->GetDictionary("result", &result));
    result_.reset(result->DeepCopy());
    base::MessageLoop::current()->QuitNow();
  }

  virtual void InspectedContentsClosing() OVERRIDE {
    EXPECT_TRUE(false);
  }

  virtual void ReplacedWithAnotherClient() OVERRIDE {
    EXPECT_TRUE(false);
  }
};

IN_PROC_BROWSER_TEST_F(RendererOverridesHandlerTest, QueryUsageAndQuota) {
  base::DictionaryValue* params = new base::DictionaryValue();
  params->SetString("securityOrigin", "http://example.com");
  SendCommand("Page.queryUsageAndQuota", params);

  EXPECT_TRUE(HasValue("quota.persistent"));
  EXPECT_TRUE(HasValue("quota.temporary"));
  EXPECT_TRUE(HasListItem("usage.temporary", "id", "appcache"));
  EXPECT_TRUE(HasListItem("usage.temporary", "id", "database"));
  EXPECT_TRUE(HasListItem("usage.temporary", "id", "indexeddatabase"));
  EXPECT_TRUE(HasListItem("usage.temporary", "id", "filesystem"));
  EXPECT_TRUE(HasListItem("usage.persistent", "id", "filesystem"));
}

class CaptureScreenshotTest : public RendererOverridesHandlerTest {
 private:
#if !defined(OS_ANDROID)
  virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE {
    command_line->AppendSwitch(switches::kEnablePixelOutputInTests);
  }
#endif
};

// Does not link on Android
#if defined(OS_ANDROID)
#define MAYBE_CaptureScreenshot DISABLED_CaptureScreenshot
#else
#define MAYBE_CaptureScreenshot CaptureScreenshot
#endif
IN_PROC_BROWSER_TEST_F(CaptureScreenshotTest, MAYBE_CaptureScreenshot) {
  shell()->LoadURL(GURL("about:blank"));
  EXPECT_TRUE(content::ExecuteScript(
      shell()->web_contents()->GetRenderViewHost(),
      "document.body.style.background = '#123456'"));
  SendCommand("Page.captureScreenshot", new base::DictionaryValue());
  std::string base64;
  EXPECT_TRUE(result_->GetString("data", &base64));
  std::string png;
  EXPECT_TRUE(base::Base64Decode(base64, &png));
  SkBitmap bitmap;
  gfx::PNGCodec::Decode(reinterpret_cast<const unsigned char*>(png.data()),
                        png.size(), &bitmap);
  SkColor color(bitmap.getColor(0, 0));
  EXPECT_TRUE(std::abs(0x12-(int)SkColorGetR(color)) <= 1);
  EXPECT_TRUE(std::abs(0x34-(int)SkColorGetG(color)) <= 1);
  EXPECT_TRUE(std::abs(0x56-(int)SkColorGetB(color)) <= 1);
}

}  // namespace content