1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/devtools/browser_list_tabcontents_provider.h"
#include "chrome/browser/devtools/device/devtools_android_bridge.h"
#include "chrome/browser/devtools/device/self_device_provider.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
namespace {
const char kPortForwardingTestPage[] =
"files/devtools/port_forwarding/main.html";
const int kDefaultDebuggingPort = 9223;
}
class PortForwardingTest: public InProcessBrowserTest {
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
InProcessBrowserTest::SetUpCommandLine(command_line);
command_line->AppendSwitchASCII(switches::kRemoteDebuggingPort,
base::IntToString(kDefaultDebuggingPort));
}
protected:
class Listener : public DevToolsAndroidBridge::PortForwardingListener {
public:
explicit Listener(Profile* profile)
: profile_(profile),
skip_empty_devices_(true) {
DevToolsAndroidBridge::Factory::GetForProfile(profile_)->
AddPortForwardingListener(this);
}
virtual ~Listener() {
DevToolsAndroidBridge::Factory::GetForProfile(profile_)->
RemovePortForwardingListener(this);
}
virtual void PortStatusChanged(const DevicesStatus& status) OVERRIDE {
if (status.empty() && skip_empty_devices_)
return;
base::MessageLoop::current()->PostTask(
FROM_HERE, base::MessageLoop::QuitClosure());
}
void set_skip_empty_devices(bool skip_empty_devices) {
skip_empty_devices_ = skip_empty_devices;
}
private:
Profile* profile_;
bool skip_empty_devices_;
};
};
IN_PROC_BROWSER_TEST_F(PortForwardingTest,
LoadPageWithStyleAnsScript) {
Profile* profile = browser()->profile();
AndroidDeviceManager::DeviceProviders device_providers;
device_providers.push_back(new SelfAsDeviceProvider(kDefaultDebuggingPort));
DevToolsAndroidBridge::Factory::GetForProfile(profile)->
set_device_providers_for_test(device_providers);
ASSERT_TRUE(test_server()->Start());
GURL original_url = test_server()->GetURL(kPortForwardingTestPage);
std::string forwarding_port("8000");
GURL forwarding_url(original_url.scheme() + "://" +
original_url.host() + ":" + forwarding_port + original_url.path());
PrefService* prefs = profile->GetPrefs();
prefs->SetBoolean(prefs::kDevToolsPortForwardingEnabled, true);
base::DictionaryValue config;
config.SetString(
forwarding_port, original_url.host() + ":" + original_url.port());
prefs->Set(prefs::kDevToolsPortForwardingConfig, config);
Listener wait_for_port_forwarding(profile);
content::RunMessageLoop();
BrowserListTabContentsProvider::EnableTethering();
ui_test_utils::NavigateToURL(browser(), forwarding_url);
content::RenderViewHost* rvh = browser()->tab_strip_model()->
GetWebContentsAt(0)->GetRenderViewHost();
std::string result;
ASSERT_TRUE(
content::ExecuteScriptAndExtractString(
rvh,
"window.domAutomationController.send(document.title)",
&result));
ASSERT_EQ("Port forwarding test", result) << "Document has not loaded.";
ASSERT_TRUE(
content::ExecuteScriptAndExtractString(
rvh,
"window.domAutomationController.send(getBodyTextContent())",
&result));
ASSERT_EQ("content", result) << "Javascript has not loaded.";
ASSERT_TRUE(
content::ExecuteScriptAndExtractString(
rvh,
"window.domAutomationController.send(getBodyMarginLeft())",
&result));
ASSERT_EQ("100px", result) << "CSS has not loaded.";
// Test that disabling port forwarding is handled normally.
wait_for_port_forwarding.set_skip_empty_devices(false);
prefs->SetBoolean(prefs::kDevToolsPortForwardingEnabled, false);
content::RunMessageLoop();
}
|