Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / devtools / device / webrtc / devtools_bridge_client_browsertest.cc
bloba17a24174833069572b6cf32cfb2e956f8466846
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"
16 namespace {
18 const char kGaiaId[] = "stub-user@example.com";
19 const char kUsername[] = "stub-user@example.com";
21 } // namespace
23 class DevToolsBridgeClientBrowserTest::GCDApiFlowMock
24 : public local_discovery::GCDApiFlow {
25 public:
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();
38 std::string type;
39 std::string data;
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) {
51 if (request_.get())
52 request_->OnGCDAPIFlowComplete(*response);
55 private:
56 DevToolsBridgeClientBrowserTest* const test_;
57 const int id_;
58 scoped_ptr<Request> request_;
61 class DevToolsBridgeClientBrowserTest::DevToolsBridgeClientMock
62 : public DevToolsBridgeClient,
63 public base::SupportsWeakPtr<DevToolsBridgeClientMock> {
64 public:
65 explicit DevToolsBridgeClientMock(DevToolsBridgeClientBrowserTest* test)
66 : DevToolsBridgeClient(test->browser()->profile(),
67 test->fake_signin_manager_.get(),
68 test->fake_token_service_.get()),
69 test_(test) {}
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,
96 kUsername);
97 identity_provider().GoogleSigninSucceeded(account_id, kUsername,
98 "password");
101 private:
102 DevToolsBridgeClientBrowserTest* const test_;
105 class DevToolsBridgeClientBrowserTest::MessageHandler
106 : public content::WebUIMessageHandler {
107 public:
108 explicit MessageHandler(DevToolsBridgeClientBrowserTest* test)
109 : test_(test) {}
111 void RegisterMessages() override {
112 web_ui()->RegisterMessageCallback(
113 "signIn", base::Bind(&MessageHandler::SignIn, base::Unretained(this)));
114 web_ui()->RegisterMessageCallback(
115 "gcdApiResponse",
116 base::Bind(&MessageHandler::GCDApiResponse, base::Unretained(this)));
117 web_ui()->RegisterMessageCallback(
118 "queryDevices",
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,
128 kUsername);
129 test_->fake_token_service_->UpdateCredentials(account_id, "token");
132 void GCDApiResponse(const base::ListValue* params) {
133 CHECK(params->GetSize() >= 2);
134 int id;
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_);
148 private:
149 DevToolsBridgeClientBrowserTest* const test_;
152 DevToolsBridgeClientBrowserTest::DevToolsBridgeClientBrowserTest()
153 : last_flow_id_(0) {
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() {
180 if (!handler_.get())
181 handler_.reset(new MessageHandler(this));
182 return handler_.get();