OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/base64.h" | 5 #include "base/basictypes.h" |
6 #include "base/command_line.h" | |
7 #include "base/json/json_reader.h" | 6 #include "base/json/json_reader.h" |
8 #include "content/browser/devtools/devtools_protocol.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "content/browser/devtools/renderer_overrides_handler.h" |
9 #include "content/public/browser/devtools_agent_host.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" | 10 #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" | 11 #include "content/public/test/content_browser_test.h" |
| 12 #include "content/public/test/content_browser_test_utils.h" |
15 #include "content/shell/browser/shell.h" | 13 #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" | |
19 | 14 |
20 namespace content { | 15 namespace content { |
21 | 16 |
22 class RendererOverridesHandlerTest : public ContentBrowserTest, | 17 class RendererOverridesHandlerTest : public ContentBrowserTest { |
23 public DevToolsClientHost { | |
24 protected: | 18 protected: |
25 void SendCommand(const std::string& method, | 19 scoped_refptr<DevToolsProtocol::Response> SendCommand( |
26 base::DictionaryValue* params) { | 20 const std::string& method, |
27 EXPECT_TRUE(DevToolsManager::GetInstance()->DispatchOnInspectorBackend(this, | 21 base::DictionaryValue* params) { |
28 DevToolsProtocol::CreateCommand(1, method, params)->Serialize())); | 22 scoped_ptr<RendererOverridesHandler> handler(CreateHandler()); |
| 23 scoped_refptr<DevToolsProtocol::Command> command( |
| 24 DevToolsProtocol::CreateCommand(1, method, params)); |
| 25 return handler->HandleCommand(command); |
| 26 } |
| 27 |
| 28 void SendAsyncCommand(const std::string& method, |
| 29 base::DictionaryValue* params) { |
| 30 scoped_ptr<RendererOverridesHandler> handler(CreateHandler()); |
| 31 scoped_refptr<DevToolsProtocol::Command> command( |
| 32 DevToolsProtocol::CreateCommand(1, method, params)); |
| 33 scoped_refptr<DevToolsProtocol::Response> response = |
| 34 handler->HandleCommand(command); |
| 35 EXPECT_TRUE(response->is_async_promise()); |
29 base::MessageLoop::current()->Run(); | 36 base::MessageLoop::current()->Run(); |
30 } | 37 } |
31 | 38 |
32 bool HasValue(const std::string& path) { | 39 bool HasValue(const std::string& path) { |
33 base::Value* value = 0; | 40 base::Value* value = 0; |
34 return result_->Get(path, &value); | 41 return result_->Get(path, &value); |
35 } | 42 } |
36 | 43 |
37 bool HasListItem(const std::string& path_to_list, | 44 bool HasListItem(const std::string& path_to_list, |
38 const std::string& name, | 45 const std::string& name, |
(...skipping 11 matching lines...) Expand all Loading... |
50 return false; | 57 return false; |
51 if (id == value) | 58 if (id == value) |
52 return true; | 59 return true; |
53 } | 60 } |
54 return false; | 61 return false; |
55 } | 62 } |
56 | 63 |
57 scoped_ptr<base::DictionaryValue> result_; | 64 scoped_ptr<base::DictionaryValue> result_; |
58 | 65 |
59 private: | 66 private: |
60 virtual void SetUpOnMainThread() OVERRIDE { | 67 RendererOverridesHandler* CreateHandler() { |
61 DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor( | 68 RenderViewHost* rvh = shell()->web_contents()->GetRenderViewHost(); |
62 DevToolsAgentHost::GetOrCreateFor( | 69 DevToolsAgentHost* agent = DevToolsAgentHost::GetOrCreateFor(rvh).get(); |
63 shell()->web_contents()->GetRenderViewHost()).get(), | 70 scoped_ptr<RendererOverridesHandler> handler( |
64 this); | 71 new RendererOverridesHandler(agent)); |
| 72 handler->SetNotifier(base::Bind( |
| 73 &RendererOverridesHandlerTest::OnMessageSent, base::Unretained(this))); |
| 74 return handler.release(); |
65 } | 75 } |
66 | 76 |
67 virtual void TearDownOnMainThread() OVERRIDE { | 77 void OnMessageSent(const std::string& message) { |
68 DevToolsManager::GetInstance()->ClientHostClosing(this); | |
69 } | |
70 | |
71 virtual void DispatchOnInspectorFrontend( | |
72 const std::string& message) OVERRIDE { | |
73 scoped_ptr<base::DictionaryValue> root( | 78 scoped_ptr<base::DictionaryValue> root( |
74 static_cast<base::DictionaryValue*>(base::JSONReader::Read(message))); | 79 static_cast<base::DictionaryValue*>(base::JSONReader::Read(message))); |
75 base::DictionaryValue* result; | 80 base::DictionaryValue* result; |
76 EXPECT_TRUE(root->GetDictionary("result", &result)); | 81 root->GetDictionary("result", &result); |
77 result_.reset(result->DeepCopy()); | 82 result_.reset(result->DeepCopy()); |
78 base::MessageLoop::current()->QuitNow(); | 83 base::MessageLoop::current()->QuitNow(); |
79 } | 84 } |
80 | |
81 virtual void InspectedContentsClosing() OVERRIDE { | |
82 EXPECT_TRUE(false); | |
83 } | |
84 | |
85 virtual void ReplacedWithAnotherClient() OVERRIDE { | |
86 EXPECT_TRUE(false); | |
87 } | |
88 }; | 85 }; |
89 | 86 |
90 IN_PROC_BROWSER_TEST_F(RendererOverridesHandlerTest, QueryUsageAndQuota) { | 87 IN_PROC_BROWSER_TEST_F(RendererOverridesHandlerTest, QueryUsageAndQuota) { |
91 base::DictionaryValue* params = new base::DictionaryValue(); | 88 base::DictionaryValue* params = new base::DictionaryValue(); |
92 params->SetString("securityOrigin", "http://example.com"); | 89 params->SetString("securityOrigin", "http://example.com"); |
93 SendCommand("Page.queryUsageAndQuota", params); | 90 SendAsyncCommand("Page.queryUsageAndQuota", params); |
94 | 91 |
95 EXPECT_TRUE(HasValue("quota.persistent")); | 92 EXPECT_TRUE(HasValue("quota.persistent")); |
96 EXPECT_TRUE(HasValue("quota.temporary")); | 93 EXPECT_TRUE(HasValue("quota.temporary")); |
97 EXPECT_TRUE(HasListItem("usage.temporary", "id", "appcache")); | 94 EXPECT_TRUE(HasListItem("usage.temporary", "id", "appcache")); |
98 EXPECT_TRUE(HasListItem("usage.temporary", "id", "database")); | 95 EXPECT_TRUE(HasListItem("usage.temporary", "id", "database")); |
99 EXPECT_TRUE(HasListItem("usage.temporary", "id", "indexeddatabase")); | 96 EXPECT_TRUE(HasListItem("usage.temporary", "id", "indexeddatabase")); |
100 EXPECT_TRUE(HasListItem("usage.temporary", "id", "filesystem")); | 97 EXPECT_TRUE(HasListItem("usage.temporary", "id", "filesystem")); |
101 EXPECT_TRUE(HasListItem("usage.persistent", "id", "filesystem")); | 98 EXPECT_TRUE(HasListItem("usage.persistent", "id", "filesystem")); |
102 } | 99 } |
103 | 100 |
104 class CaptureScreenshotTest : public RendererOverridesHandlerTest { | |
105 private: | |
106 #if !defined(OS_ANDROID) | |
107 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE { | |
108 command_line->AppendSwitch(switches::kEnablePixelOutputInTests); | |
109 } | |
110 #endif | |
111 }; | |
112 | |
113 // Does not link on Android | |
114 #if defined(OS_ANDROID) | |
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", new base::DictionaryValue()); | |
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_EQ(0x12U, SkColorGetR(color)); | |
134 EXPECT_EQ(0x34U, SkColorGetG(color)); | |
135 EXPECT_EQ(0x56U, SkColorGetB(color)); | |
136 } | |
137 | |
138 } // namespace content | 101 } // namespace content |
OLD | NEW |