Chromium Code Reviews| 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 "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.request", 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 OnBrowserListUpdated() 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 test_->fake_signin_manager_->SetAuthenticatedUsername( | |
| 88 "stub-user@example.com"); | |
|
dgozman
2014/12/22 16:38:20
Make this string a local var, pass it twice and co
SeRya
2014/12/22 17:00:14
Done.
| |
| 89 identity_provider().GoogleSigninSucceeded( | |
| 90 "test_account", "stub-user@example.com", "testing"); | |
| 91 } | |
| 92 | |
| 93 private: | |
| 94 DevToolsBridgeClientBrowserTest* const test_; | |
| 95 }; | |
| 96 | |
| 97 class DevToolsBridgeClientBrowserTest::MessageHandler | |
| 98 : public content::WebUIMessageHandler { | |
| 99 public: | |
| 100 explicit MessageHandler(DevToolsBridgeClientBrowserTest* test) : test_(test) { | |
| 101 } | |
| 102 | |
| 103 void RegisterMessages() override { | |
| 104 web_ui()->RegisterMessageCallback( | |
| 105 "signIn", | |
| 106 base::Bind(&MessageHandler::SignIn, | |
| 107 base::Unretained(this))); | |
| 108 web_ui()->RegisterMessageCallback( | |
| 109 "response", | |
| 110 base::Bind(&MessageHandler::Response, | |
| 111 base::Unretained(this))); | |
| 112 web_ui()->RegisterMessageCallback( | |
| 113 "queryDevices", | |
| 114 base::Bind(&MessageHandler::QueryDevices, | |
| 115 base::Unretained(this))); | |
| 116 } | |
| 117 | |
| 118 void SignIn(const base::ListValue*) { | |
| 119 if (test_->client_mock_.get()) | |
| 120 test_->client_mock_->GoogleSigninSucceeded(); | |
| 121 test_->fake_token_service_->UpdateCredentials( | |
| 122 "test_user@gmail.com", "token"); | |
| 123 } | |
| 124 | |
| 125 void Response(const base::ListValue* params) { | |
| 126 CHECK(params->GetSize() >= 2); | |
| 127 int id; | |
| 128 const base::DictionaryValue* response; | |
| 129 CHECK(params->GetInteger(0, &id)); | |
| 130 CHECK(params->GetDictionary(1, &response)); | |
| 131 | |
| 132 auto flow = test_->flows_.find(id); | |
| 133 CHECK(test_->flows_.end() != flow); | |
| 134 flow->second->Respond(response); | |
| 135 } | |
| 136 | |
| 137 void QueryDevices(const base::ListValue*) { | |
| 138 DevToolsBridgeClient::GetDevices(test_->client_mock_); | |
| 139 } | |
| 140 | |
| 141 private: | |
| 142 DevToolsBridgeClientBrowserTest* const test_; | |
| 143 }; | |
| 144 | |
| 145 DevToolsBridgeClientBrowserTest::DevToolsBridgeClientBrowserTest() | |
| 146 : last_flow_id_(0) { | |
| 147 } | |
| 148 | |
| 149 DevToolsBridgeClientBrowserTest::~DevToolsBridgeClientBrowserTest() { | |
| 150 DCHECK(flows_.empty()); | |
| 151 } | |
| 152 | |
| 153 void DevToolsBridgeClientBrowserTest::SetUpOnMainThread() { | |
| 154 WebUIBrowserTest::SetUpOnMainThread(); | |
| 155 | |
| 156 DCHECK(browser()->profile()); | |
| 157 fake_signin_manager_.reset( | |
| 158 new FakeSigninManagerForTesting(browser()->profile())); | |
| 159 fake_token_service_.reset(new FakeProfileOAuth2TokenService()); | |
| 160 client_mock_ = (new DevToolsBridgeClientMock(this))->AsWeakPtr(); | |
| 161 } | |
| 162 | |
| 163 void DevToolsBridgeClientBrowserTest::TearDownOnMainThread() { | |
| 164 if (client_mock_.get()) | |
| 165 client_mock_->DeleteSelf(); | |
| 166 fake_token_service_.reset(); | |
| 167 fake_signin_manager_.reset(); | |
| 168 WebUIBrowserTest::TearDownOnMainThread(); | |
| 169 } | |
| 170 | |
| 171 content::WebUIMessageHandler* | |
| 172 DevToolsBridgeClientBrowserTest::GetMockMessageHandler() { | |
| 173 if (!handler_.get()) | |
| 174 handler_.reset(new MessageHandler(this)); | |
| 175 return handler_.get(); | |
| 176 } | |
| OLD | NEW |