Home | History | Annotate | Download | only in devtools
      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.
      4 
      5 #include "base/basictypes.h"
      6 #include "base/json/json_reader.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"
     10 #include "content/public/browser/web_contents.h"
     11 #include "content/shell/browser/shell.h"
     12 #include "content/test/content_browser_test.h"
     13 #include "content/test/content_browser_test_utils.h"
     14 
     15 namespace content {
     16 
     17 class RendererOverridesHandlerTest : public ContentBrowserTest {
     18  protected:
     19   scoped_refptr<DevToolsProtocol::Response> SendCommand(
     20       const std::string& method,
     21       DictionaryValue* params) {
     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, DictionaryValue* params) {
     29     scoped_ptr<RendererOverridesHandler> handler(CreateHandler());
     30     scoped_refptr<DevToolsProtocol::Command> command(
     31         DevToolsProtocol::CreateCommand(1, method, params));
     32     scoped_refptr<DevToolsProtocol::Response> response =
     33         handler->HandleCommand(command);
     34     EXPECT_TRUE(response->is_async_promise());
     35     base::MessageLoop::current()->Run();
     36   }
     37 
     38   bool HasValue(const std::string& path) {
     39     base::Value* value = 0;
     40     return result_->Get(path, &value);
     41   }
     42 
     43   bool HasListItem(const std::string& path_to_list,
     44                    const std::string& name,
     45                    const std::string& value) {
     46     base::ListValue* list;
     47     if (!result_->GetList(path_to_list, &list))
     48       return false;
     49 
     50     for (size_t i = 0; i != list->GetSize(); i++) {
     51       base::DictionaryValue* item;
     52       if (!list->GetDictionary(i, &item))
     53         return false;
     54       std::string id;
     55       if (!item->GetString(name, &id))
     56         return false;
     57       if (id == value)
     58         return true;
     59     }
     60     return false;
     61   }
     62 
     63   scoped_ptr<base::DictionaryValue> result_;
     64 
     65  private:
     66   RendererOverridesHandler* CreateHandler() {
     67     RenderViewHost* rvh = shell()->web_contents()->GetRenderViewHost();
     68     DevToolsAgentHost* agent = DevToolsAgentHost::GetOrCreateFor(rvh).get();
     69     scoped_ptr<RendererOverridesHandler> handler(
     70         new RendererOverridesHandler(agent));
     71     handler->SetNotifier(base::Bind(
     72         &RendererOverridesHandlerTest::OnMessageSent, base::Unretained(this)));
     73     return handler.release();
     74   }
     75 
     76   void OnMessageSent(const std::string& message) {
     77     scoped_ptr<base::DictionaryValue> root(
     78         static_cast<base::DictionaryValue*>(base::JSONReader::Read(message)));
     79     base::DictionaryValue* result;
     80     root->GetDictionary("result", &result);
     81     result_.reset(result->DeepCopy());
     82     base::MessageLoop::current()->QuitNow();
     83   }
     84 };
     85 
     86 IN_PROC_BROWSER_TEST_F(RendererOverridesHandlerTest, QueryUsageAndQuota) {
     87   DictionaryValue* params = new DictionaryValue();
     88   params->SetString("securityOrigin", "http://example.com");
     89   SendAsyncCommand("Page.queryUsageAndQuota", params);
     90 
     91   EXPECT_TRUE(HasValue("quota.persistent"));
     92   EXPECT_TRUE(HasValue("quota.temporary"));
     93   EXPECT_TRUE(HasListItem("usage.temporary", "id", "appcache"));
     94   EXPECT_TRUE(HasListItem("usage.temporary", "id", "database"));
     95   EXPECT_TRUE(HasListItem("usage.temporary", "id", "indexeddatabase"));
     96   EXPECT_TRUE(HasListItem("usage.temporary", "id", "filesystem"));
     97   EXPECT_TRUE(HasListItem("usage.persistent", "id", "filesystem"));
     98 }
     99 
    100 }  // namespace content
    101