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/account_tracker_service_factory.h"
10 #include "chrome/browser/signin/fake_signin_manager_builder.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "components/signin/core/browser/account_tracker_service.h"
13 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
14 #include "content/public/browser/web_ui_message_handler.h"
18 const char kGaiaId
[] = "stub-user@example.com";
19 const char kUsername
[] = "stub-user@example.com";
23 class DevToolsBridgeClientBrowserTest::GCDApiFlowMock
24 : public local_discovery::GCDApiFlow
{
26 explicit GCDApiFlowMock(DevToolsBridgeClientBrowserTest
* test
)
27 : test_(test
), id_(++test
->last_flow_id_
) {
28 test_
->flows_
[id_
] = this;
31 ~GCDApiFlowMock() override
{ test_
->flows_
.erase(id_
); }
33 // Passes request's data to the JS test. Result will be passed back
34 // in MessageHandler::Response.
35 void Start(scoped_ptr
<Request
> request
) override
{
36 request_
= request
.Pass();
40 request_
->GetUploadData(&type
, &data
);
42 ScopedVector
<const base::Value
> params
;
43 params
.push_back(new base::FundamentalValue(id_
));
44 params
.push_back(new base::StringValue(request_
->GetURL().spec()));
45 params
.push_back(new base::StringValue(data
));
47 test_
->RunJavascriptFunction("callbacks.gcdApiRequest", params
);
50 void Respond(const base::DictionaryValue
* response
) {
52 request_
->OnGCDAPIFlowComplete(*response
);
56 DevToolsBridgeClientBrowserTest
* const test_
;
58 scoped_ptr
<Request
> request_
;
61 class DevToolsBridgeClientBrowserTest::DevToolsBridgeClientMock
62 : public DevToolsBridgeClient
,
63 public base::SupportsWeakPtr
<DevToolsBridgeClientMock
> {
65 explicit DevToolsBridgeClientMock(DevToolsBridgeClientBrowserTest
* test
)
66 : DevToolsBridgeClient(test
->browser()->profile(),
67 test
->fake_signin_manager_
.get(),
68 test
->fake_token_service_
.get()),
71 ~DevToolsBridgeClientMock() override
{}
73 void DocumentOnLoadCompletedInMainFrame() override
{
74 DevToolsBridgeClient::DocumentOnLoadCompletedInMainFrame();
76 test_
->RunJavascriptFunction("callbacks.workerLoaded");
79 void OnBrowserListUpdatedForTests() override
{
80 int count
= static_cast<int>(browsers().size());
81 test_
->RunJavascriptFunction("callbacks.browserListUpdated",
82 new base::FundamentalValue(count
));
85 scoped_ptr
<local_discovery::GCDApiFlow
> CreateGCDApiFlow() override
{
86 return make_scoped_ptr(new GCDApiFlowMock(test_
));
89 void GoogleSigninSucceeded() {
90 // This username is checked on Chrome OS.
91 const std::string account_id
=
92 AccountTrackerServiceFactory::GetForProfile(
93 test_
->browser()->profile())
94 ->PickAccountIdForAccount(kGaiaId
, kUsername
);
95 test_
->fake_signin_manager_
->SetAuthenticatedAccountInfo(kGaiaId
,
97 identity_provider().GoogleSigninSucceeded(account_id
, kUsername
,
102 DevToolsBridgeClientBrowserTest
* const test_
;
105 class DevToolsBridgeClientBrowserTest::MessageHandler
106 : public content::WebUIMessageHandler
{
108 explicit MessageHandler(DevToolsBridgeClientBrowserTest
* test
)
111 void RegisterMessages() override
{
112 web_ui()->RegisterMessageCallback(
113 "signIn", base::Bind(&MessageHandler::SignIn
, base::Unretained(this)));
114 web_ui()->RegisterMessageCallback(
116 base::Bind(&MessageHandler::GCDApiResponse
, base::Unretained(this)));
117 web_ui()->RegisterMessageCallback(
119 base::Bind(&MessageHandler::QueryDevices
, base::Unretained(this)));
122 void SignIn(const base::ListValue
*) {
123 if (test_
->client_mock_
.get())
124 test_
->client_mock_
->GoogleSigninSucceeded();
125 const std::string account_id
=
126 AccountTrackerServiceFactory::GetForProfile(
127 test_
->browser()->profile())->PickAccountIdForAccount(kGaiaId
,
129 test_
->fake_token_service_
->UpdateCredentials(account_id
, "token");
132 void GCDApiResponse(const base::ListValue
* params
) {
133 CHECK(params
->GetSize() >= 2);
135 const base::DictionaryValue
* response
;
136 CHECK(params
->GetInteger(0, &id
));
137 CHECK(params
->GetDictionary(1, &response
));
139 auto flow
= test_
->flows_
.find(id
);
140 CHECK(test_
->flows_
.end() != flow
);
141 flow
->second
->Respond(response
);
144 void QueryDevices(const base::ListValue
*) {
145 DevToolsBridgeClient::GetDevices(test_
->client_mock_
);
149 DevToolsBridgeClientBrowserTest
* const test_
;
152 DevToolsBridgeClientBrowserTest::DevToolsBridgeClientBrowserTest()
156 DevToolsBridgeClientBrowserTest::~DevToolsBridgeClientBrowserTest() {
157 DCHECK(flows_
.empty());
160 void DevToolsBridgeClientBrowserTest::SetUpOnMainThread() {
161 WebUIBrowserTest::SetUpOnMainThread();
163 DCHECK(browser()->profile());
164 fake_signin_manager_
.reset(
165 new FakeSigninManagerForTesting(browser()->profile()));
166 fake_token_service_
.reset(new FakeProfileOAuth2TokenService());
167 client_mock_
= (new DevToolsBridgeClientMock(this))->AsWeakPtr();
170 void DevToolsBridgeClientBrowserTest::TearDownOnMainThread() {
171 if (client_mock_
.get())
172 client_mock_
->DeleteSelf();
173 fake_token_service_
.reset();
174 fake_signin_manager_
.reset();
175 WebUIBrowserTest::TearDownOnMainThread();
178 content::WebUIMessageHandler
*
179 DevToolsBridgeClientBrowserTest::GetMockMessageHandler() {
181 handler_
.reset(new MessageHandler(this));
182 return handler_
.get();