| OLD | NEW |
| (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 <utility> | |
| 8 | |
| 9 #include "chrome/browser/devtools/device/webrtc/devtools_bridge_client.h" | |
| 10 #include "chrome/browser/local_discovery/gcd_api_flow.h" | |
| 11 #include "chrome/browser/signin/account_tracker_service_factory.h" | |
| 12 #include "chrome/browser/signin/fake_signin_manager_builder.h" | |
| 13 #include "chrome/browser/ui/browser.h" | |
| 14 #include "components/signin/core/browser/account_tracker_service.h" | |
| 15 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | |
| 16 #include "content/public/browser/web_ui_message_handler.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 const char kGaiaId[] = "stub-user@example.com"; | |
| 21 const char kUsername[] = "stub-user@example.com"; | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 class DevToolsBridgeClientBrowserTest::GCDApiFlowMock | |
| 26 : public local_discovery::GCDApiFlow { | |
| 27 public: | |
| 28 explicit GCDApiFlowMock(DevToolsBridgeClientBrowserTest* test) | |
| 29 : test_(test), id_(++test->last_flow_id_) { | |
| 30 test_->flows_[id_] = this; | |
| 31 } | |
| 32 | |
| 33 ~GCDApiFlowMock() override { test_->flows_.erase(id_); } | |
| 34 | |
| 35 // Passes request's data to the JS test. Result will be passed back | |
| 36 // in MessageHandler::Response. | |
| 37 void Start(scoped_ptr<Request> request) override { | |
| 38 request_ = std::move(request); | |
| 39 | |
| 40 std::string type; | |
| 41 std::string data; | |
| 42 request_->GetUploadData(&type, &data); | |
| 43 | |
| 44 ScopedVector<const base::Value> params; | |
| 45 params.push_back(new base::FundamentalValue(id_)); | |
| 46 params.push_back(new base::StringValue(request_->GetURL().spec())); | |
| 47 params.push_back(new base::StringValue(data)); | |
| 48 | |
| 49 test_->RunJavascriptFunction("callbacks.gcdApiRequest", params); | |
| 50 } | |
| 51 | |
| 52 void Respond(const base::DictionaryValue* response) { | |
| 53 if (request_.get()) | |
| 54 request_->OnGCDAPIFlowComplete(*response); | |
| 55 } | |
| 56 | |
| 57 private: | |
| 58 DevToolsBridgeClientBrowserTest* const test_; | |
| 59 const int id_; | |
| 60 scoped_ptr<Request> request_; | |
| 61 }; | |
| 62 | |
| 63 class DevToolsBridgeClientBrowserTest::DevToolsBridgeClientMock | |
| 64 : public DevToolsBridgeClient, | |
| 65 public base::SupportsWeakPtr<DevToolsBridgeClientMock> { | |
| 66 public: | |
| 67 explicit DevToolsBridgeClientMock(DevToolsBridgeClientBrowserTest* test) | |
| 68 : DevToolsBridgeClient(test->browser()->profile(), | |
| 69 test->fake_signin_manager_.get(), | |
| 70 test->fake_token_service_.get()), | |
| 71 test_(test) {} | |
| 72 | |
| 73 ~DevToolsBridgeClientMock() override {} | |
| 74 | |
| 75 void DocumentOnLoadCompletedInMainFrame() override { | |
| 76 DevToolsBridgeClient::DocumentOnLoadCompletedInMainFrame(); | |
| 77 | |
| 78 test_->RunJavascriptFunction("callbacks.workerLoaded"); | |
| 79 } | |
| 80 | |
| 81 void OnBrowserListUpdatedForTests() override { | |
| 82 int count = static_cast<int>(browsers().size()); | |
| 83 test_->RunJavascriptFunction("callbacks.browserListUpdated", | |
| 84 new base::FundamentalValue(count)); | |
| 85 } | |
| 86 | |
| 87 scoped_ptr<local_discovery::GCDApiFlow> CreateGCDApiFlow() override { | |
| 88 return make_scoped_ptr(new GCDApiFlowMock(test_)); | |
| 89 } | |
| 90 | |
| 91 void GoogleSigninSucceeded() { | |
| 92 // This username is checked on Chrome OS. | |
| 93 const std::string account_id = | |
| 94 AccountTrackerServiceFactory::GetForProfile( | |
| 95 test_->browser()->profile()) | |
| 96 ->PickAccountIdForAccount(kGaiaId, kUsername); | |
| 97 test_->fake_signin_manager_->SetAuthenticatedAccountInfo(kGaiaId, | |
| 98 kUsername); | |
| 99 identity_provider().GoogleSigninSucceeded(account_id, kUsername, | |
| 100 "password"); | |
| 101 } | |
| 102 | |
| 103 private: | |
| 104 DevToolsBridgeClientBrowserTest* const test_; | |
| 105 }; | |
| 106 | |
| 107 class DevToolsBridgeClientBrowserTest::MessageHandler | |
| 108 : public content::WebUIMessageHandler { | |
| 109 public: | |
| 110 explicit MessageHandler(DevToolsBridgeClientBrowserTest* test) | |
| 111 : test_(test) {} | |
| 112 | |
| 113 void RegisterMessages() override { | |
| 114 web_ui()->RegisterMessageCallback( | |
| 115 "signIn", base::Bind(&MessageHandler::SignIn, base::Unretained(this))); | |
| 116 web_ui()->RegisterMessageCallback( | |
| 117 "gcdApiResponse", | |
| 118 base::Bind(&MessageHandler::GCDApiResponse, base::Unretained(this))); | |
| 119 web_ui()->RegisterMessageCallback( | |
| 120 "queryDevices", | |
| 121 base::Bind(&MessageHandler::QueryDevices, base::Unretained(this))); | |
| 122 } | |
| 123 | |
| 124 void SignIn(const base::ListValue*) { | |
| 125 if (test_->client_mock_.get()) | |
| 126 test_->client_mock_->GoogleSigninSucceeded(); | |
| 127 const std::string account_id = | |
| 128 AccountTrackerServiceFactory::GetForProfile( | |
| 129 test_->browser()->profile())->PickAccountIdForAccount(kGaiaId, | |
| 130 kUsername); | |
| 131 test_->fake_token_service_->UpdateCredentials(account_id, "token"); | |
| 132 } | |
| 133 | |
| 134 void GCDApiResponse(const base::ListValue* params) { | |
| 135 CHECK(params->GetSize() >= 2); | |
| 136 int id; | |
| 137 const base::DictionaryValue* response; | |
| 138 CHECK(params->GetInteger(0, &id)); | |
| 139 CHECK(params->GetDictionary(1, &response)); | |
| 140 | |
| 141 auto flow = test_->flows_.find(id); | |
| 142 CHECK(test_->flows_.end() != flow); | |
| 143 flow->second->Respond(response); | |
| 144 } | |
| 145 | |
| 146 void QueryDevices(const base::ListValue*) { | |
| 147 DevToolsBridgeClient::GetDevices(test_->client_mock_); | |
| 148 } | |
| 149 | |
| 150 private: | |
| 151 DevToolsBridgeClientBrowserTest* const test_; | |
| 152 }; | |
| 153 | |
| 154 DevToolsBridgeClientBrowserTest::DevToolsBridgeClientBrowserTest() | |
| 155 : last_flow_id_(0) { | |
| 156 } | |
| 157 | |
| 158 DevToolsBridgeClientBrowserTest::~DevToolsBridgeClientBrowserTest() { | |
| 159 DCHECK(flows_.empty()); | |
| 160 } | |
| 161 | |
| 162 void DevToolsBridgeClientBrowserTest::SetUpOnMainThread() { | |
| 163 WebUIBrowserTest::SetUpOnMainThread(); | |
| 164 | |
| 165 DCHECK(browser()->profile()); | |
| 166 fake_signin_manager_.reset( | |
| 167 new FakeSigninManagerForTesting(browser()->profile())); | |
| 168 fake_token_service_.reset(new FakeProfileOAuth2TokenService()); | |
| 169 client_mock_ = (new DevToolsBridgeClientMock(this))->AsWeakPtr(); | |
| 170 } | |
| 171 | |
| 172 void DevToolsBridgeClientBrowserTest::TearDownOnMainThread() { | |
| 173 if (client_mock_.get()) | |
| 174 client_mock_->DeleteSelf(); | |
| 175 fake_token_service_.reset(); | |
| 176 fake_signin_manager_.reset(); | |
| 177 WebUIBrowserTest::TearDownOnMainThread(); | |
| 178 } | |
| 179 | |
| 180 content::WebUIMessageHandler* | |
| 181 DevToolsBridgeClientBrowserTest::GetMockMessageHandler() { | |
| 182 if (!handler_.get()) | |
| 183 handler_.reset(new MessageHandler(this)); | |
| 184 return handler_.get(); | |
| 185 } | |
| OLD | NEW |