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/public/test/content_browser_test.h"
     12 #include "content/public/test/content_browser_test_utils.h"
     13 #include "content/shell/browser/shell.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       base::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,
     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();
     37   }
     38 
     39   bool HasValue(const std::string& path) {
     40     base::Value* value = 0;
     41     return result_->Get(path, &value);
     42   }
     43 
     44   bool HasListItem(const std::string& path_to_list,
     45                    const std::string& name,
     46                    const std::string& value) {
     47     base::ListValue* list;
     48     if (!result_->GetList(path_to_list, &list))
     49       return false;
     50 
     51     for (size_t i = 0; i != list->GetSize(); i++) {
     52       base::DictionaryValue* item;
     53       if (!list->GetDictionary(i, &item))
     54         return false;
     55       std::string id;
     56       if (!item->GetString(name, &id))
     57         return false;
     58       if (id == value)
     59         return true;
     60     }
     61     return false;
     62   }
     63 
     64   scoped_ptr<base::DictionaryValue> result_;
     65 
     66  private:
     67   RendererOverridesHandler* CreateHandler() {
     68     RenderViewHost* rvh = shell()->web_contents()->GetRenderViewHost();
     69     DevToolsAgentHost* agent = DevToolsAgentHost::GetOrCreateFor(rvh).get();
     70     scoped_ptr<RendererOverridesHandler> handler(
     71         new RendererOverridesHandler(agent));
     72     handler->SetNotifier(base::Bind(
     73         &RendererOverridesHandlerTest::OnMessageSent, base::Unretained(this)));
     74     return handler.release();
     75   }
     76 
     77   void OnMessageSent(const std::string& message) {
     78     scoped_ptr<base::DictionaryValue> root(
     79         static_cast<base::DictionaryValue*>(base::JSONReader::Read(message)));
     80     base::DictionaryValue* result;
     81     root->GetDictionary("result", &result);
     82     result_.reset(result->DeepCopy());
     83     base::MessageLoop::current()->QuitNow();
     84   }
     85 };
     86 
     87 IN_PROC_BROWSER_TEST_F(RendererOverridesHandlerTest, QueryUsageAndQuota) {
     88   base::DictionaryValue* params = new base::DictionaryValue();
     89   params->SetString("securityOrigin", "http://example.com");
     90   SendAsyncCommand("Page.queryUsageAndQuota", params);
     91 
     92   EXPECT_TRUE(HasValue("quota.persistent"));
     93   EXPECT_TRUE(HasValue("quota.temporary"));
     94   EXPECT_TRUE(HasListItem("usage.temporary", "id", "appcache"));
     95   EXPECT_TRUE(HasListItem("usage.temporary", "id", "database"));
     96   EXPECT_TRUE(HasListItem("usage.temporary", "id", "indexeddatabase"));
     97   EXPECT_TRUE(HasListItem("usage.temporary", "id", "filesystem"));
     98   EXPECT_TRUE(HasListItem("usage.persistent", "id", "filesystem"));
     99 }
    100 
    101 }  // namespace content
    102