| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/browser/local_discovery/privetv3_setup_flow.h" | 5 #include "chrome/browser/local_discovery/privetv3_setup_flow.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "chrome/browser/local_discovery/gcd_api_flow.h" | 9 #include "chrome/browser/local_discovery/gcd_api_flow.h" |
| 10 #include "net/http/http_response_headers.h" | 10 #include "net/http/http_response_headers.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 request_context_ = | 40 request_context_ = |
| 41 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); | 41 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); |
| 42 } | 42 } |
| 43 | 43 |
| 44 MOCK_METHOD0(GetName, const std::string&()); | 44 MOCK_METHOD0(GetName, const std::string&()); |
| 45 MOCK_METHOD1( | 45 MOCK_METHOD1( |
| 46 CreateInfoOperationPtr, | 46 CreateInfoOperationPtr, |
| 47 PrivetJSONOperation*(const PrivetJSONOperation::ResultCallback&)); | 47 PrivetJSONOperation*(const PrivetJSONOperation::ResultCallback&)); |
| 48 | 48 |
| 49 virtual void RefreshPrivetToken( | 49 virtual void RefreshPrivetToken( |
| 50 const PrivetURLFetcher::TokenCallback& callback) OVERRIDE { | 50 const PrivetURLFetcher::TokenCallback& callback) override { |
| 51 callback.Run("x-privet-token"); | 51 callback.Run("x-privet-token"); |
| 52 } | 52 } |
| 53 | 53 |
| 54 virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation( | 54 virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation( |
| 55 const PrivetJSONOperation::ResultCallback& callback) OVERRIDE { | 55 const PrivetJSONOperation::ResultCallback& callback) override { |
| 56 return make_scoped_ptr(CreateInfoOperationPtr(callback)); | 56 return make_scoped_ptr(CreateInfoOperationPtr(callback)); |
| 57 } | 57 } |
| 58 | 58 |
| 59 virtual scoped_ptr<PrivetURLFetcher> CreateURLFetcher( | 59 virtual scoped_ptr<PrivetURLFetcher> CreateURLFetcher( |
| 60 const GURL& url, | 60 const GURL& url, |
| 61 net::URLFetcher::RequestType request_type, | 61 net::URLFetcher::RequestType request_type, |
| 62 PrivetURLFetcher::Delegate* delegate) OVERRIDE { | 62 PrivetURLFetcher::Delegate* delegate) override { |
| 63 return make_scoped_ptr(new PrivetURLFetcher( | 63 return make_scoped_ptr(new PrivetURLFetcher( |
| 64 url, request_type, request_context_.get(), delegate)); | 64 url, request_type, request_context_.get(), delegate)); |
| 65 } | 65 } |
| 66 | 66 |
| 67 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 67 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 68 }; | 68 }; |
| 69 | 69 |
| 70 class MockDelegate : public PrivetV3SetupFlow::Delegate { | 70 class MockDelegate : public PrivetV3SetupFlow::Delegate { |
| 71 public: | 71 public: |
| 72 MockDelegate() : privet_client_ptr_(NULL) {} | 72 MockDelegate() : privet_client_ptr_(NULL) {} |
| 73 | 73 |
| 74 class MockGCDApiFlow : public GCDApiFlow { | 74 class MockGCDApiFlow : public GCDApiFlow { |
| 75 public: | 75 public: |
| 76 explicit MockGCDApiFlow(MockDelegate* delegate) : delegate_(delegate) {} | 76 explicit MockGCDApiFlow(MockDelegate* delegate) : delegate_(delegate) {} |
| 77 | 77 |
| 78 virtual void Start(scoped_ptr<Request> request) OVERRIDE { | 78 virtual void Start(scoped_ptr<Request> request) override { |
| 79 ASSERT_FALSE(delegate_->gcd_request_); | 79 ASSERT_FALSE(delegate_->gcd_request_); |
| 80 delegate_->gcd_request_ = request.Pass(); | 80 delegate_->gcd_request_ = request.Pass(); |
| 81 delegate_->ReplyWithToken(); | 81 delegate_->ReplyWithToken(); |
| 82 } | 82 } |
| 83 | 83 |
| 84 private: | 84 private: |
| 85 MockDelegate* delegate_; | 85 MockDelegate* delegate_; |
| 86 }; | 86 }; |
| 87 | 87 |
| 88 MOCK_METHOD1(GetWiFiCredentials, void(const CredentialsCallback&)); | 88 MOCK_METHOD1(GetWiFiCredentials, void(const CredentialsCallback&)); |
| 89 MOCK_METHOD1(SwitchToSetupWiFi, void(const ResultCallback&)); | 89 MOCK_METHOD1(SwitchToSetupWiFi, void(const ResultCallback&)); |
| 90 virtual void CreatePrivetV3Client( | 90 virtual void CreatePrivetV3Client( |
| 91 const std::string& service_name, | 91 const std::string& service_name, |
| 92 const PrivetClientCallback& callback) OVERRIDE { | 92 const PrivetClientCallback& callback) override { |
| 93 scoped_ptr<MockPrivetHTTPClient> privet_client(new MockPrivetHTTPClient()); | 93 scoped_ptr<MockPrivetHTTPClient> privet_client(new MockPrivetHTTPClient()); |
| 94 privet_client_ptr_ = privet_client.get(); | 94 privet_client_ptr_ = privet_client.get(); |
| 95 callback.Run(privet_client.PassAs<PrivetHTTPClient>()); | 95 callback.Run(privet_client.PassAs<PrivetHTTPClient>()); |
| 96 } | 96 } |
| 97 MOCK_METHOD2(ConfirmSecurityCode, | 97 MOCK_METHOD2(ConfirmSecurityCode, |
| 98 void(const std::string&, const ResultCallback&)); | 98 void(const std::string&, const ResultCallback&)); |
| 99 MOCK_METHOD1(RestoreWifi, void(const ResultCallback&)); | 99 MOCK_METHOD1(RestoreWifi, void(const ResultCallback&)); |
| 100 MOCK_METHOD0(OnSetupDone, void()); | 100 MOCK_METHOD0(OnSetupDone, void()); |
| 101 MOCK_METHOD0(OnSetupError, void()); | 101 MOCK_METHOD0(OnSetupError, void()); |
| 102 | 102 |
| 103 virtual scoped_ptr<GCDApiFlow> CreateApiFlow() OVERRIDE { | 103 virtual scoped_ptr<GCDApiFlow> CreateApiFlow() override { |
| 104 scoped_ptr<MockGCDApiFlow> mock_gcd(new MockGCDApiFlow(this)); | 104 scoped_ptr<MockGCDApiFlow> mock_gcd(new MockGCDApiFlow(this)); |
| 105 return mock_gcd.PassAs<GCDApiFlow>(); | 105 return mock_gcd.PassAs<GCDApiFlow>(); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void ReplyWithToken() { | 108 void ReplyWithToken() { |
| 109 scoped_ptr<base::Value> value(base::JSONReader::Read(gcd_server_response_)); | 109 scoped_ptr<base::Value> value(base::JSONReader::Read(gcd_server_response_)); |
| 110 const base::DictionaryValue* dictionary = NULL; | 110 const base::DictionaryValue* dictionary = NULL; |
| 111 value->GetAsDictionary(&dictionary); | 111 value->GetAsDictionary(&dictionary); |
| 112 gcd_request_->OnGCDAPIFlowComplete(*dictionary); | 112 gcd_request_->OnGCDAPIFlowComplete(*dictionary); |
| 113 } | 113 } |
| 114 | 114 |
| 115 std::string gcd_server_response_; | 115 std::string gcd_server_response_; |
| 116 scoped_ptr<GCDApiFlow::Request> gcd_request_; | 116 scoped_ptr<GCDApiFlow::Request> gcd_request_; |
| 117 MockPrivetHTTPClient* privet_client_ptr_; | 117 MockPrivetHTTPClient* privet_client_ptr_; |
| 118 base::Closure quit_closure_; | 118 base::Closure quit_closure_; |
| 119 }; | 119 }; |
| 120 | 120 |
| 121 class PrivetV3SetupFlowTest : public testing::Test { | 121 class PrivetV3SetupFlowTest : public testing::Test { |
| 122 public: | 122 public: |
| 123 PrivetV3SetupFlowTest() : setup_(&delegate_) {} | 123 PrivetV3SetupFlowTest() : setup_(&delegate_) {} |
| 124 | 124 |
| 125 virtual ~PrivetV3SetupFlowTest() {} | 125 virtual ~PrivetV3SetupFlowTest() {} |
| 126 | 126 |
| 127 void ConfirmCode(const MockDelegate::ResultCallback& confirm_callback) { | 127 void ConfirmCode(const MockDelegate::ResultCallback& confirm_callback) { |
| 128 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_); | 128 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_); |
| 129 confirm_callback.Run(true); | 129 confirm_callback.Run(true); |
| 130 } | 130 } |
| 131 | 131 |
| 132 protected: | 132 protected: |
| 133 virtual void SetUp() OVERRIDE { | 133 virtual void SetUp() override { |
| 134 quit_closure_ = run_loop_.QuitClosure(); | 134 quit_closure_ = run_loop_.QuitClosure(); |
| 135 EXPECT_CALL(delegate_, GetWiFiCredentials(_)).Times(0); | 135 EXPECT_CALL(delegate_, GetWiFiCredentials(_)).Times(0); |
| 136 EXPECT_CALL(delegate_, SwitchToSetupWiFi(_)).Times(0); | 136 EXPECT_CALL(delegate_, SwitchToSetupWiFi(_)).Times(0); |
| 137 EXPECT_CALL(delegate_, ConfirmSecurityCode(_, _)).Times(0); | 137 EXPECT_CALL(delegate_, ConfirmSecurityCode(_, _)).Times(0); |
| 138 EXPECT_CALL(delegate_, RestoreWifi(_)).Times(0); | 138 EXPECT_CALL(delegate_, RestoreWifi(_)).Times(0); |
| 139 EXPECT_CALL(delegate_, OnSetupDone()).Times(0); | 139 EXPECT_CALL(delegate_, OnSetupDone()).Times(0); |
| 140 EXPECT_CALL(delegate_, OnSetupError()).Times(0); | 140 EXPECT_CALL(delegate_, OnSetupError()).Times(0); |
| 141 } | 141 } |
| 142 | 142 |
| 143 void SimulateFetch(int response_code, const std::string& response) { | 143 void SimulateFetch(int response_code, const std::string& response) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 WithArgs<1>(Invoke(this, &PrivetV3SetupFlowTest::ConfirmCode))); | 184 WithArgs<1>(Invoke(this, &PrivetV3SetupFlowTest::ConfirmCode))); |
| 185 delegate_.gcd_server_response_ = kRegistrationTicketResponse; | 185 delegate_.gcd_server_response_ = kRegistrationTicketResponse; |
| 186 setup_.Register(kServiceName); | 186 setup_.Register(kServiceName); |
| 187 run_loop_.Run(); | 187 run_loop_.Run(); |
| 188 SimulateFetch(200, "{}"); | 188 SimulateFetch(200, "{}"); |
| 189 } | 189 } |
| 190 | 190 |
| 191 } // namespace | 191 } // namespace |
| 192 | 192 |
| 193 } // namespace local_discovery | 193 } // namespace local_discovery |
| OLD | NEW |