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