OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/live_sync/live_sync_test.h" | 5 #include "chrome/test/live_sync/live_sync_test.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/message_loop.h" |
10 #include "base/path_service.h" | 11 #include "base/path_service.h" |
11 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/task.h" |
| 14 #include "base/waitable_event.h" |
| 15 #include "chrome/browser/chrome_thread.h" |
12 #include "chrome/browser/profile.h" | 16 #include "chrome/browser/profile.h" |
13 #include "chrome/browser/profile_manager.h" | 17 #include "chrome/browser/profile_manager.h" |
14 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
15 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/net/url_fetcher.h" |
| 21 #include "chrome/common/net/url_request_context_getter.h" |
| 22 #include "chrome/test/testing_browser_process.h" |
| 23 #include "googleurl/src/gurl.h" |
| 24 #include "net/base/escape.h" |
| 25 #include "net/proxy/proxy_config.h" |
| 26 #include "net/proxy/proxy_config_service_fixed.h" |
| 27 #include "net/proxy/proxy_service.h" |
| 28 #include "net/url_request/url_request_context.h" |
| 29 #include "net/url_request/url_request_status.h" |
16 | 30 |
17 namespace switches { | 31 namespace switches { |
18 const std::string kPasswordFileForTest = "password-file-for-test"; | 32 const std::string kPasswordFileForTest = "password-file-for-test"; |
19 const std::string kSyncUserForTest = "sync-user-for-test"; | 33 const std::string kSyncUserForTest = "sync-user-for-test"; |
20 const std::string kSyncPasswordForTest = "sync-password-for-test"; | 34 const std::string kSyncPasswordForTest = "sync-password-for-test"; |
21 } | 35 } |
22 | 36 |
| 37 class ConfigureURLFectcherDelegate : public URLFetcher::Delegate { |
| 38 public: |
| 39 ConfigureURLFectcherDelegate() : success_(false) {} |
| 40 |
| 41 virtual void OnURLFetchComplete(const URLFetcher* source, |
| 42 const GURL& url, |
| 43 const URLRequestStatus& status, |
| 44 int response_code, |
| 45 const ResponseCookies& cookies, |
| 46 const std::string& data) { |
| 47 if (status.status() == URLRequestStatus::SUCCESS && response_code == 200) |
| 48 success_ = true; |
| 49 MessageLoop::current()->Quit(); |
| 50 } |
| 51 |
| 52 bool success() const { return success_; } |
| 53 |
| 54 private: |
| 55 bool success_; |
| 56 }; |
| 57 |
| 58 class SetProxyConfigTask : public Task { |
| 59 public: |
| 60 SetProxyConfigTask(base::WaitableEvent* done, |
| 61 URLRequestContextGetter* url_request_context_getter, |
| 62 const net::ProxyConfig& proxy_config) |
| 63 : done_(done), |
| 64 url_request_context_getter_(url_request_context_getter), |
| 65 proxy_config_(proxy_config) { |
| 66 } |
| 67 |
| 68 void Run() { |
| 69 net::ProxyService* proxy_service = |
| 70 url_request_context_getter_->GetURLRequestContext()->proxy_service(); |
| 71 proxy_service->ResetConfigService( |
| 72 new net::ProxyConfigServiceFixed(proxy_config_)); |
| 73 done_->Signal(); |
| 74 } |
| 75 |
| 76 private: |
| 77 base::WaitableEvent* done_; |
| 78 URLRequestContextGetter* url_request_context_getter_; |
| 79 net::ProxyConfig proxy_config_; |
| 80 }; |
| 81 |
23 void LiveSyncTest::SetUp() { | 82 void LiveSyncTest::SetUp() { |
24 // At this point, the browser hasn't been launched, and no services are | 83 // At this point, the browser hasn't been launched, and no services are |
25 // available. But we can verify our command line parameters and fail | 84 // available. But we can verify our command line parameters and fail |
26 // early. | 85 // early. |
27 CommandLine* cl = CommandLine::ForCurrentProcess(); | 86 CommandLine* cl = CommandLine::ForCurrentProcess(); |
28 if (cl->HasSwitch(switches::kPasswordFileForTest)) { | 87 if (cl->HasSwitch(switches::kPasswordFileForTest)) { |
29 // Read GAIA credentials from a local password file if specified via the | 88 // Read GAIA credentials from a local password file if specified via the |
30 // "--password-file-for-test" command line switch. Note: The password file | 89 // "--password-file-for-test" command line switch. Note: The password file |
31 // must be a plain text file with exactly two lines -- the username on the | 90 // must be a plain text file with exactly two lines -- the username on the |
32 // first line and the password on the second line. | 91 // first line and the password on the second line. |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 CommandLine* cl = CommandLine::ForCurrentProcess(); | 250 CommandLine* cl = CommandLine::ForCurrentProcess(); |
192 cl->AppendSwitchASCII(switches::kSyncServiceURL, | 251 cl->AppendSwitchASCII(switches::kSyncServiceURL, |
193 StringPrintf("http://%s:%d/chromiumsync", server_.kHostName, | 252 StringPrintf("http://%s:%d/chromiumsync", server_.kHostName, |
194 server_.kOKHTTPSPort)); | 253 server_.kOKHTTPSPort)); |
195 } | 254 } |
196 | 255 |
197 void LiveSyncTest::TearDownLocalTestServer() { | 256 void LiveSyncTest::TearDownLocalTestServer() { |
198 bool success = server_.Stop(); | 257 bool success = server_.Stop(); |
199 ASSERT_TRUE(success); | 258 ASSERT_TRUE(success); |
200 } | 259 } |
| 260 |
| 261 |
| 262 void LiveSyncTest::EnableNetwork(Profile* profile) { |
| 263 SetProxyConfig(profile->GetRequestContext(), |
| 264 net::ProxyConfig::CreateDirect()); |
| 265 } |
| 266 |
| 267 void LiveSyncTest::DisableNetwork(Profile* profile) { |
| 268 // Set the current proxy configuration to a nonexistent proxy to |
| 269 // effectively disable networking. |
| 270 net::ProxyConfig config; |
| 271 config.proxy_rules().ParseFromString("http=127.0.0.1:0"); |
| 272 SetProxyConfig(profile->GetRequestContext(), config); |
| 273 } |
| 274 |
| 275 bool LiveSyncTest::EnsureSyncServerConfiguration() { |
| 276 const CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 277 if (!cl->HasSwitch(switches::kSyncServiceURL)) |
| 278 return true; |
| 279 |
| 280 return ConfigureSyncServer("user_email", username_); |
| 281 } |
| 282 |
| 283 void LiveSyncTest::SetProxyConfig(URLRequestContextGetter* context_getter, |
| 284 const net::ProxyConfig& proxy_config) { |
| 285 base::WaitableEvent done(false, false); |
| 286 ChromeThread::PostTask( |
| 287 ChromeThread::IO, |
| 288 FROM_HERE, |
| 289 new SetProxyConfigTask(&done, |
| 290 context_getter, |
| 291 proxy_config)); |
| 292 done.Wait(); |
| 293 } |
| 294 |
| 295 bool LiveSyncTest::ConfigureSyncServer(const std::string& name, |
| 296 const std::string& value) { |
| 297 std::string url = StringPrintf("http://%s:%d/chromiumsync/configure", |
| 298 server_.kHostName, |
| 299 server_.kOKHTTPSPort); |
| 300 std::string data = EscapePath(name) + "=" + EscapePath(value); |
| 301 ConfigureURLFectcherDelegate delegate; |
| 302 scoped_ptr<URLFetcher> fetcher( |
| 303 URLFetcher::Create(0, GURL(url), URLFetcher::POST, &delegate)); |
| 304 fetcher->set_request_context(Profile::GetDefaultRequestContext()); |
| 305 fetcher->set_upload_data("application/x-www-form-urlencoded", data); |
| 306 fetcher->Start(); |
| 307 MessageLoop::current()->Run(); |
| 308 return delegate.success(); |
| 309 } |
OLD | NEW |