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.
5 #include "chrome/browser/devtools/device/webrtc/devtools_bridge_client_browsertest.h"
7 #include "chrome/browser/devtools/device/webrtc/devtools_bridge_client.h"
8 #include "chrome/browser/local_discovery/gcd_api_flow.h"
9 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
10 #include "chrome/browser/signin/fake_signin_manager.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "content/public/browser/web_ui_message_handler.h"
14 class DevToolsBridgeClientBrowserTest::GCDApiFlowMock
15 : public local_discovery::GCDApiFlow
{
17 explicit GCDApiFlowMock(DevToolsBridgeClientBrowserTest
* test
)
18 : test_(test
), id_(++test
->last_flow_id_
) {
19 test_
->flows_
[id_
] = this;
22 ~GCDApiFlowMock() override
{ test_
->flows_
.erase(id_
); }
24 // Passes request's data to the JS test. Result will be passed back
25 // in MessageHandler::Response.
26 void Start(scoped_ptr
<Request
> request
) override
{
27 request_
= request
.Pass();
31 request_
->GetUploadData(&type
, &data
);
33 ScopedVector
<const base::Value
> params
;
34 params
.push_back(new base::FundamentalValue(id_
));
35 params
.push_back(new base::StringValue(request_
->GetURL().spec()));
36 params
.push_back(new base::StringValue(data
));
38 test_
->RunJavascriptFunction("callbacks.gcdApiRequest", params
);
41 void Respond(const base::DictionaryValue
* response
) {
43 request_
->OnGCDAPIFlowComplete(*response
);
47 DevToolsBridgeClientBrowserTest
* const test_
;
49 scoped_ptr
<Request
> request_
;
52 class DevToolsBridgeClientBrowserTest::DevToolsBridgeClientMock
53 : public DevToolsBridgeClient
,
54 public base::SupportsWeakPtr
<DevToolsBridgeClientMock
> {
56 explicit DevToolsBridgeClientMock(DevToolsBridgeClientBrowserTest
* test
)
57 : DevToolsBridgeClient(test
->browser()->profile(),
58 test
->fake_signin_manager_
.get(),
59 test
->fake_token_service_
.get()),
62 ~DevToolsBridgeClientMock() override
{}
64 void DocumentOnLoadCompletedInMainFrame() override
{
65 DevToolsBridgeClient::DocumentOnLoadCompletedInMainFrame();
67 test_
->RunJavascriptFunction("callbacks.workerLoaded");
70 void OnBrowserListUpdatedForTests() override
{
71 int count
= static_cast<int>(browsers().size());
72 test_
->RunJavascriptFunction("callbacks.browserListUpdated",
73 new base::FundamentalValue(count
));
76 scoped_ptr
<local_discovery::GCDApiFlow
> CreateGCDApiFlow() override
{
77 return make_scoped_ptr(new GCDApiFlowMock(test_
));
80 void GoogleSigninSucceeded() {
81 // This username is checked on Chrome OS.
82 const std::string username
= "stub-user@example.com";
83 test_
->fake_signin_manager_
->SetAuthenticatedUsername(username
);
84 identity_provider().GoogleSigninSucceeded("test_account", username
,
89 DevToolsBridgeClientBrowserTest
* const test_
;
92 class DevToolsBridgeClientBrowserTest::MessageHandler
93 : public content::WebUIMessageHandler
{
95 explicit MessageHandler(DevToolsBridgeClientBrowserTest
* test
)
98 void RegisterMessages() override
{
99 web_ui()->RegisterMessageCallback(
100 "signIn", base::Bind(&MessageHandler::SignIn
, base::Unretained(this)));
101 web_ui()->RegisterMessageCallback(
103 base::Bind(&MessageHandler::GCDApiResponse
, base::Unretained(this)));
104 web_ui()->RegisterMessageCallback(
106 base::Bind(&MessageHandler::QueryDevices
, base::Unretained(this)));
109 void SignIn(const base::ListValue
*) {
110 if (test_
->client_mock_
.get())
111 test_
->client_mock_
->GoogleSigninSucceeded();
112 test_
->fake_token_service_
->UpdateCredentials("test_user@gmail.com",
116 void GCDApiResponse(const base::ListValue
* params
) {
117 CHECK(params
->GetSize() >= 2);
119 const base::DictionaryValue
* response
;
120 CHECK(params
->GetInteger(0, &id
));
121 CHECK(params
->GetDictionary(1, &response
));
123 auto flow
= test_
->flows_
.find(id
);
124 CHECK(test_
->flows_
.end() != flow
);
125 flow
->second
->Respond(response
);
128 void QueryDevices(const base::ListValue
*) {
129 DevToolsBridgeClient::GetDevices(test_
->client_mock_
);
133 DevToolsBridgeClientBrowserTest
* const test_
;
136 DevToolsBridgeClientBrowserTest::DevToolsBridgeClientBrowserTest()
140 DevToolsBridgeClientBrowserTest::~DevToolsBridgeClientBrowserTest() {
141 DCHECK(flows_
.empty());
144 void DevToolsBridgeClientBrowserTest::SetUpOnMainThread() {
145 WebUIBrowserTest::SetUpOnMainThread();
147 DCHECK(browser()->profile());
148 fake_signin_manager_
.reset(
149 new FakeSigninManagerForTesting(browser()->profile()));
150 fake_token_service_
.reset(new FakeProfileOAuth2TokenService());
151 client_mock_
= (new DevToolsBridgeClientMock(this))->AsWeakPtr();
154 void DevToolsBridgeClientBrowserTest::TearDownOnMainThread() {
155 if (client_mock_
.get())
156 client_mock_
->DeleteSelf();
157 fake_token_service_
.reset();
158 fake_signin_manager_
.reset();
159 WebUIBrowserTest::TearDownOnMainThread();
162 content::WebUIMessageHandler
*
163 DevToolsBridgeClientBrowserTest::GetMockMessageHandler() {
165 handler_
.reset(new MessageHandler(this));
166 return handler_
.get();