port_forwarding_browsertest.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
1// Copyright 2014 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/command_line.h"
6#include "base/compiler_specific.h"
7#include "base/prefs/pref_service.h"
8#include "base/strings/string_number_conversions.h"
9#include "chrome/browser/devtools/browser_list_tabcontents_provider.h"
10#include "chrome/browser/devtools/device/port_forwarding_controller.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/ui/browser.h"
13#include "chrome/browser/ui/tabs/tab_strip_model.h"
14#include "chrome/common/chrome_switches.h"
15#include "chrome/common/pref_names.h"
16#include "chrome/test/base/in_process_browser_test.h"
17#include "chrome/test/base/ui_test_utils.h"
18#include "content/public/browser/web_contents.h"
19#include "content/public/test/browser_test_utils.h"
20#include "content/public/test/test_utils.h"
21
22namespace {
23const char kPortForwardingTestPage[] =
24    "files/devtools/port_forwarding/main.html";
25
26const int kDefaultDebuggingPort = 9223;
27}
28
29class PortForwardingTest: public InProcessBrowserTest {
30  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
31    InProcessBrowserTest::SetUpCommandLine(command_line);
32    command_line->AppendSwitchASCII(switches::kRemoteDebuggingPort,
33        base::IntToString(kDefaultDebuggingPort));
34  }
35
36 protected:
37  class Listener : public PortForwardingController::Listener {
38   public:
39    explicit Listener(Profile* profile)
40        : profile_(profile) {
41      PortForwardingController::Factory::GetForProfile(profile_)->
42          AddListener(this);
43    }
44
45    virtual ~Listener() {
46      PortForwardingController::Factory::GetForProfile(profile_)->
47          RemoveListener(this);
48    }
49
50    virtual void PortStatusChanged(const DevicesStatus& status) OVERRIDE {
51      if (status.empty())
52        return;
53      base::MessageLoop::current()->PostTask(
54          FROM_HERE, base::MessageLoop::QuitClosure());
55    }
56
57   private:
58    Profile* profile_;
59  };
60};
61
62IN_PROC_BROWSER_TEST_F(PortForwardingTest,
63                       DISABLED_LoadPageWithStyleAnsScript) {
64  Profile* profile = browser()->profile();
65
66  AndroidDeviceManager::DeviceProviders device_providers;
67  device_providers.push_back(
68      AndroidDeviceManager::GetSelfAsDeviceProvider(kDefaultDebuggingPort));
69  DevToolsAndroidBridge::Factory::GetForProfile(profile)->
70      set_device_providers_for_test(device_providers);
71
72  ASSERT_TRUE(test_server()->Start());
73  GURL original_url = test_server()->GetURL(kPortForwardingTestPage);
74
75  std::string forwarding_port("8000");
76  GURL forwarding_url(original_url.scheme() + "://" +
77      original_url.host() + ":" + forwarding_port + original_url.path());
78
79  PrefService* prefs = profile->GetPrefs();
80  prefs->SetBoolean(prefs::kDevToolsPortForwardingEnabled, true);
81
82  base::DictionaryValue config;
83  config.SetString(
84      forwarding_port, original_url.host() + ":" + original_url.port());
85  prefs->Set(prefs::kDevToolsPortForwardingConfig, config);
86
87  Listener wait_for_port_forwarding(profile);
88  content::RunMessageLoop();
89
90  BrowserListTabContentsProvider::EnableTethering();
91
92  ui_test_utils::NavigateToURL(browser(), forwarding_url);
93
94  content::RenderViewHost* rvh = browser()->tab_strip_model()->
95      GetWebContentsAt(0)->GetRenderViewHost();
96
97  std::string result;
98  ASSERT_TRUE(
99      content::ExecuteScriptAndExtractString(
100          rvh,
101          "window.domAutomationController.send(document.title)",
102          &result));
103  ASSERT_EQ("Port forwarding test", result) << "Document has not loaded.";
104
105  ASSERT_TRUE(
106      content::ExecuteScriptAndExtractString(
107          rvh,
108          "window.domAutomationController.send(getBodyTextContent())",
109          &result));
110  ASSERT_EQ("content", result) << "Javascript has not loaded.";
111
112  ASSERT_TRUE(
113      content::ExecuteScriptAndExtractString(
114          rvh,
115          "window.domAutomationController.send(getBodyMarginLeft())",
116          &result));
117  ASSERT_EQ("100px", result) << "CSS has not loaded.";
118}
119