Chromium Code Reviews| 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/basictypes.h" | 5 #include "base/base64.h" |
| 6 #include "base/command_line.h" | |
| 6 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 7 #include "base/memory/scoped_ptr.h" | 8 #include "content/browser/devtools/devtools_protocol.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" | |
| 10 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/test/browser_test_utils.h" | |
| 11 #include "content/public/test/content_browser_test.h" | 14 #include "content/public/test/content_browser_test.h" |
| 12 #include "content/public/test/content_browser_test_utils.h" | |
| 13 #include "content/shell/browser/shell.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" | |
| 14 | 19 |
| 15 namespace content { | 20 namespace content { |
| 16 | 21 |
| 17 class RendererOverridesHandlerTest : public ContentBrowserTest { | 22 class RendererOverridesHandlerTest : public ContentBrowserTest, |
| 23 public DevToolsClientHost { | |
| 18 protected: | 24 protected: |
| 19 scoped_refptr<DevToolsProtocol::Response> SendCommand( | 25 void SendCommand(const std::string& method, |
| 20 const std::string& method, | 26 base::DictionaryValue* params) { |
| 21 base::DictionaryValue* params) { | 27 EXPECT_TRUE(DevToolsManager::GetInstance()->DispatchOnInspectorBackend(this, |
| 22 scoped_ptr<RendererOverridesHandler> handler(CreateHandler()); | 28 DevToolsProtocol::CreateCommand(1, method, params)->Serialize())); |
| 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()); | |
| 36 base::MessageLoop::current()->Run(); | 29 base::MessageLoop::current()->Run(); |
| 37 } | 30 } |
| 38 | 31 |
| 39 bool HasValue(const std::string& path) { | 32 bool HasValue(const std::string& path) { |
| 40 base::Value* value = 0; | 33 base::Value* value = 0; |
| 41 return result_->Get(path, &value); | 34 return result_->Get(path, &value); |
| 42 } | 35 } |
| 43 | 36 |
| 44 bool HasListItem(const std::string& path_to_list, | 37 bool HasListItem(const std::string& path_to_list, |
| 45 const std::string& name, | 38 const std::string& name, |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 57 return false; | 50 return false; |
| 58 if (id == value) | 51 if (id == value) |
| 59 return true; | 52 return true; |
| 60 } | 53 } |
| 61 return false; | 54 return false; |
| 62 } | 55 } |
| 63 | 56 |
| 64 scoped_ptr<base::DictionaryValue> result_; | 57 scoped_ptr<base::DictionaryValue> result_; |
| 65 | 58 |
| 66 private: | 59 private: |
| 67 RendererOverridesHandler* CreateHandler() { | 60 void SetUpOnMainThread() { |
| 68 RenderViewHost* rvh = shell()->web_contents()->GetRenderViewHost(); | 61 DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor( |
| 69 DevToolsAgentHost* agent = DevToolsAgentHost::GetOrCreateFor(rvh).get(); | 62 DevToolsAgentHost::GetOrCreateFor( |
| 70 scoped_ptr<RendererOverridesHandler> handler( | 63 shell()->web_contents()->GetRenderViewHost()).get(), |
| 71 new RendererOverridesHandler(agent)); | 64 this); |
| 72 handler->SetNotifier(base::Bind( | |
| 73 &RendererOverridesHandlerTest::OnMessageSent, base::Unretained(this))); | |
| 74 return handler.release(); | |
| 75 } | 65 } |
| 76 | 66 |
| 77 void OnMessageSent(const std::string& message) { | 67 void TearDownOnMainThread() { |
| 68 DevToolsManager::GetInstance()->ClientHostClosing(this); | |
| 69 } | |
| 70 | |
| 71 virtual void DispatchOnInspectorFrontend( | |
| 72 const std::string& message) OVERRIDE { | |
| 78 scoped_ptr<base::DictionaryValue> root( | 73 scoped_ptr<base::DictionaryValue> root( |
| 79 static_cast<base::DictionaryValue*>(base::JSONReader::Read(message))); | 74 static_cast<base::DictionaryValue*>(base::JSONReader::Read(message))); |
| 80 base::DictionaryValue* result; | 75 base::DictionaryValue* result; |
| 81 root->GetDictionary("result", &result); | 76 EXPECT_TRUE(root->GetDictionary("result", &result)); |
| 82 result_.reset(result->DeepCopy()); | 77 result_.reset(result->DeepCopy()); |
| 83 base::MessageLoop::current()->QuitNow(); | 78 base::MessageLoop::current()->QuitNow(); |
| 84 } | 79 } |
| 80 | |
| 81 virtual void InspectedContentsClosing() OVERRIDE { | |
| 82 EXPECT_TRUE(false); | |
| 83 } | |
| 84 | |
| 85 virtual void ReplacedWithAnotherClient() OVERRIDE { | |
| 86 EXPECT_TRUE(false); | |
| 87 } | |
| 85 }; | 88 }; |
| 86 | 89 |
| 87 IN_PROC_BROWSER_TEST_F(RendererOverridesHandlerTest, QueryUsageAndQuota) { | 90 IN_PROC_BROWSER_TEST_F(RendererOverridesHandlerTest, QueryUsageAndQuota) { |
| 88 base::DictionaryValue* params = new base::DictionaryValue(); | 91 base::DictionaryValue* params = new base::DictionaryValue(); |
| 89 params->SetString("securityOrigin", "http://example.com"); | 92 params->SetString("securityOrigin", "http://example.com"); |
| 90 SendAsyncCommand("Page.queryUsageAndQuota", params); | 93 SendCommand("Page.queryUsageAndQuota", params); |
| 91 | 94 |
| 92 EXPECT_TRUE(HasValue("quota.persistent")); | 95 EXPECT_TRUE(HasValue("quota.persistent")); |
| 93 EXPECT_TRUE(HasValue("quota.temporary")); | 96 EXPECT_TRUE(HasValue("quota.temporary")); |
| 94 EXPECT_TRUE(HasListItem("usage.temporary", "id", "appcache")); | 97 EXPECT_TRUE(HasListItem("usage.temporary", "id", "appcache")); |
| 95 EXPECT_TRUE(HasListItem("usage.temporary", "id", "database")); | 98 EXPECT_TRUE(HasListItem("usage.temporary", "id", "database")); |
| 96 EXPECT_TRUE(HasListItem("usage.temporary", "id", "indexeddatabase")); | 99 EXPECT_TRUE(HasListItem("usage.temporary", "id", "indexeddatabase")); |
| 97 EXPECT_TRUE(HasListItem("usage.temporary", "id", "filesystem")); | 100 EXPECT_TRUE(HasListItem("usage.temporary", "id", "filesystem")); |
| 98 EXPECT_TRUE(HasListItem("usage.persistent", "id", "filesystem")); | 101 EXPECT_TRUE(HasListItem("usage.persistent", "id", "filesystem")); |
| 99 } | 102 } |
| 100 | 103 |
| 104 class CaptureScreenshotTest : public RendererOverridesHandlerTest { | |
| 105 private: | |
| 106 void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE { | |
| 107 command_line->AppendSwitch(switches::kEnablePixelOutputInTests); | |
| 108 } | |
| 109 }; | |
| 110 | |
| 111 IN_PROC_BROWSER_TEST_F(CaptureScreenshotTest, CaptureScreenshot) { | |
| 112 shell()->LoadURL(GURL("about:blank")); | |
| 113 EXPECT_TRUE(content::ExecuteScript( | |
|
Vladislav Kaznacheev
2014/06/16 08:25:27
Does this test fail with the old implementation of
vkuzkokov
2014/06/16 10:01:24
The same test with old implementation fails for me
| |
| 114 shell()->web_contents()->GetRenderViewHost(), | |
| 115 "document.body.style.background = '#123456'")); | |
| 116 SendCommand("Page.captureScreenshot", new base::DictionaryValue()); | |
| 117 std::string base64; | |
| 118 EXPECT_TRUE(result_->GetString("data", &base64)); | |
| 119 std::string png; | |
| 120 EXPECT_TRUE(base::Base64Decode(base64, &png)); | |
| 121 SkBitmap bitmap; | |
| 122 gfx::PNGCodec::Decode(reinterpret_cast<const unsigned char*>(png.data()), | |
| 123 png.size(), &bitmap); | |
| 124 SkColor color(bitmap.getColor(0, 0)); | |
| 125 EXPECT_EQ(0x12U, SkColorGetR(color)); | |
| 126 EXPECT_EQ(0x34U, SkColorGetG(color)); | |
| 127 EXPECT_EQ(0x56U, SkColorGetB(color)); | |
| 128 } | |
| 129 | |
| 101 } // namespace content | 130 } // namespace content |
| OLD | NEW |