Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: chrome/browser/devtools/device/webrtc/devtools_bridge_client_browsertest.cc

Issue 791083005: Browser tests for DevToolsBridgeClient. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "chrome/browser/devtools/device/webrtc/devtools_bridge_client_browserte st.h"
6
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"
13
14 class DevToolsBridgeClientBrowserTest::GCDApiFlowMock
15 : public local_discovery::GCDApiFlow {
16 public:
17 explicit GCDApiFlowMock(DevToolsBridgeClientBrowserTest* test)
18 : test_(test),
19 id_(++test->last_flow_id_) {
20 test_->flows_[id_] = this;
21 }
22
23 ~GCDApiFlowMock() {
24 test_->flows_.erase(id_);
25 }
26
27 // Passes request's data to the JS test. Result will be passed back
28 // in MessageHandler::Response.
29 void Start(scoped_ptr<Request> request) override {
30 request_ = request.Pass();
31
32 std::string type;
33 std::string data;
34 request_->GetUploadData(&type, &data);
35
36 ScopedVector<const base::Value> params;
37 params.push_back(new base::FundamentalValue(id_));
38 params.push_back(new base::StringValue(request_->GetURL().spec()));
39 params.push_back(new base::StringValue(data));
40
41 test_->RunJavascriptFunction("callbacks.gcdApiRequest", params);
42 }
43
44 void Respond(const base::DictionaryValue* response) {
45 if (request_.get())
46 request_->OnGCDAPIFlowComplete(*response);
47 }
48
49 private:
50 DevToolsBridgeClientBrowserTest* const test_;
51 const int id_;
52 scoped_ptr<Request> request_;
53 };
54
55 class DevToolsBridgeClientBrowserTest::DevToolsBridgeClientMock
56 : public DevToolsBridgeClient,
57 public base::SupportsWeakPtr<DevToolsBridgeClientMock> {
58 public:
59 explicit DevToolsBridgeClientMock(DevToolsBridgeClientBrowserTest* test)
60 : DevToolsBridgeClient(
61 test->browser()->profile(),
62 test->fake_signin_manager_.get(),
63 test->fake_token_service_.get()),
64 test_(test) {
65 }
66
67 ~DevToolsBridgeClientMock() override {
68 }
69
70 void DocumentOnLoadCompletedInMainFrame() override {
71 DevToolsBridgeClient::DocumentOnLoadCompletedInMainFrame();
72
73 test_->RunJavascriptFunction("callbacks.workerLoaded");
74 }
75
76 void OnBrowserListUpdatedForTests() override {
77 int count = static_cast<int>(browsers().size());
78 test_->RunJavascriptFunction("callbacks.browserListUpdated",
79 new base::FundamentalValue(count));
80 }
81
82 scoped_ptr<local_discovery::GCDApiFlow> CreateGCDApiFlow() override {
83 return make_scoped_ptr(new GCDApiFlowMock(test_));
84 }
85
86 void GoogleSigninSucceeded() {
87 // This username is checked on Chrome OS.
88 const std::string username = "stub-user@example.com";
89 test_->fake_signin_manager_->SetAuthenticatedUsername(
dgozman 2014/12/22 17:08:38 nit: this should fit one line.
SeRya 2014/12/22 17:14:42 Reformatted CL
90 username);
91 identity_provider().GoogleSigninSucceeded(
92 "test_account", username, "testing");
93 }
94
95 private:
96 DevToolsBridgeClientBrowserTest* const test_;
97 };
98
99 class DevToolsBridgeClientBrowserTest::MessageHandler
100 : public content::WebUIMessageHandler {
101 public:
102 explicit MessageHandler(DevToolsBridgeClientBrowserTest* test) : test_(test) {
103 }
104
105 void RegisterMessages() override {
106 web_ui()->RegisterMessageCallback(
107 "signIn",
108 base::Bind(&MessageHandler::SignIn,
109 base::Unretained(this)));
110 web_ui()->RegisterMessageCallback(
111 "gcdApiResponse",
112 base::Bind(&MessageHandler::GCDApiResponse,
113 base::Unretained(this)));
114 web_ui()->RegisterMessageCallback(
115 "queryDevices",
116 base::Bind(&MessageHandler::QueryDevices,
117 base::Unretained(this)));
118 }
119
120 void SignIn(const base::ListValue*) {
121 if (test_->client_mock_.get())
122 test_->client_mock_->GoogleSigninSucceeded();
123 test_->fake_token_service_->UpdateCredentials(
124 "test_user@gmail.com", "token");
125 }
126
127 void GCDApiResponse(const base::ListValue* params) {
128 CHECK(params->GetSize() >= 2);
129 int id;
130 const base::DictionaryValue* response;
131 CHECK(params->GetInteger(0, &id));
132 CHECK(params->GetDictionary(1, &response));
133
134 auto flow = test_->flows_.find(id);
135 CHECK(test_->flows_.end() != flow);
136 flow->second->Respond(response);
137 }
138
139 void QueryDevices(const base::ListValue*) {
140 DevToolsBridgeClient::GetDevices(test_->client_mock_);
141 }
142
143 private:
144 DevToolsBridgeClientBrowserTest* const test_;
145 };
146
147 DevToolsBridgeClientBrowserTest::DevToolsBridgeClientBrowserTest()
148 : last_flow_id_(0) {
149 }
150
151 DevToolsBridgeClientBrowserTest::~DevToolsBridgeClientBrowserTest() {
152 DCHECK(flows_.empty());
153 }
154
155 void DevToolsBridgeClientBrowserTest::SetUpOnMainThread() {
156 WebUIBrowserTest::SetUpOnMainThread();
157
158 DCHECK(browser()->profile());
159 fake_signin_manager_.reset(
160 new FakeSigninManagerForTesting(browser()->profile()));
161 fake_token_service_.reset(new FakeProfileOAuth2TokenService());
162 client_mock_ = (new DevToolsBridgeClientMock(this))->AsWeakPtr();
163 }
164
165 void DevToolsBridgeClientBrowserTest::TearDownOnMainThread() {
166 if (client_mock_.get())
167 client_mock_->DeleteSelf();
168 fake_token_service_.reset();
169 fake_signin_manager_.reset();
170 WebUIBrowserTest::TearDownOnMainThread();
171 }
172
173 content::WebUIMessageHandler*
174 DevToolsBridgeClientBrowserTest::GetMockMessageHandler() {
175 if (!handler_.get())
176 handler_.reset(new MessageHandler(this));
177 return handler_.get();
178 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698