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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/timer/mock_timer.h" | 9 #include "base/timer/mock_timer.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 #include "chrome/browser/extensions/extension_apitest.h" | 11 #include "chrome/browser/extensions/extension_apitest.h" |
12 #include "chrome/browser/extensions/extension_function_test_utils.h" | 12 #include "chrome/browser/extensions/extension_function_test_utils.h" |
13 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
15 #include "components/cast_channel/cast_socket.h" | |
16 #include "components/cast_channel/cast_test_util.h" | |
17 #include "components/cast_channel/logger.h" | |
18 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
19 #include "extensions/browser/api/cast_channel/cast_channel_api.h" | 16 #include "extensions/browser/api/cast_channel/cast_channel_api.h" |
| 17 #include "extensions/browser/api/cast_channel/cast_socket.h" |
| 18 #include "extensions/browser/api/cast_channel/cast_test_util.h" |
| 19 #include "extensions/browser/api/cast_channel/logger.h" |
20 #include "extensions/common/api/cast_channel.h" | 20 #include "extensions/common/api/cast_channel.h" |
21 #include "extensions/common/api/cast_channel/cast_channel.pb.h" | 21 #include "extensions/common/api/cast_channel/cast_channel.pb.h" |
22 #include "extensions/common/switches.h" | 22 #include "extensions/common/switches.h" |
23 #include "extensions/common/test_util.h" | 23 #include "extensions/common/test_util.h" |
24 #include "extensions/test/extension_test_message_listener.h" | 24 #include "extensions/test/extension_test_message_listener.h" |
25 #include "extensions/test/result_catcher.h" | 25 #include "extensions/test/result_catcher.h" |
26 #include "net/base/completion_callback.h" | 26 #include "net/base/completion_callback.h" |
27 #include "net/base/ip_address.h" | 27 #include "net/base/ip_address.h" |
28 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
29 #include "net/log/test_net_log.h" | 29 #include "net/log/test_net_log.h" |
30 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
31 #include "testing/gmock_mutant.h" | 31 #include "testing/gmock_mutant.h" |
32 | 32 |
33 using ::cast_channel::CastMessage; | 33 // TODO(mfoltz): Mock out the ApiResourceManager to resolve threading issues |
34 using ::cast_channel::CastSocket; | 34 // (crbug.com/398242) and simulate unloading of the extension. |
35 using ::cast_channel::CastTransport; | 35 |
36 using ::cast_channel::ChannelAuthType; | 36 using ::cast_channel::ChannelAuthType; |
37 using ::cast_channel::ChannelError; | 37 using ::cast_channel::ChannelError; |
38 using ::cast_channel::CreateIPEndPointForTest; | |
39 using ::cast_channel::LastErrors; | |
40 using ::cast_channel::Logger; | |
41 using ::cast_channel::MockCastSocket; | |
42 using ::cast_channel::MockCastTransport; | |
43 using ::cast_channel::ReadyState; | 38 using ::cast_channel::ReadyState; |
| 39 |
| 40 using extensions::api::cast_channel::CastMessage; |
| 41 using extensions::api::cast_channel::CastSocket; |
| 42 using extensions::api::cast_channel::CastTransport; |
| 43 using extensions::api::cast_channel::CreateIPEndPointForTest; |
44 using extensions::api::cast_channel::ErrorInfo; | 44 using extensions::api::cast_channel::ErrorInfo; |
| 45 using extensions::api::cast_channel::LastErrors; |
| 46 using extensions::api::cast_channel::Logger; |
45 using extensions::api::cast_channel::MessageInfo; | 47 using extensions::api::cast_channel::MessageInfo; |
| 48 using extensions::api::cast_channel::MockCastSocket; |
| 49 using extensions::api::cast_channel::MockCastTransport; |
46 using extensions::Extension; | 50 using extensions::Extension; |
47 | 51 |
48 namespace utils = extension_function_test_utils; | 52 namespace utils = extension_function_test_utils; |
49 | 53 |
50 using ::testing::_; | 54 using ::testing::_; |
51 using ::testing::A; | 55 using ::testing::A; |
52 using ::testing::DoAll; | 56 using ::testing::DoAll; |
53 using ::testing::Invoke; | 57 using ::testing::Invoke; |
54 using ::testing::InSequence; | 58 using ::testing::InSequence; |
55 using ::testing::NotNull; | 59 using ::testing::NotNull; |
56 using ::testing::Return; | 60 using ::testing::Return; |
57 using ::testing::ReturnRef; | 61 using ::testing::ReturnRef; |
58 using ::testing::ReturnPointee; | 62 using ::testing::ReturnPointee; |
59 using ::testing::SaveArg; | 63 using ::testing::SaveArg; |
60 | 64 |
61 namespace { | 65 namespace { |
62 | 66 |
63 const char kTestExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf"; | |
64 | |
65 static void FillCastMessage(const std::string& message, | 67 static void FillCastMessage(const std::string& message, |
66 CastMessage* cast_message) { | 68 CastMessage* cast_message) { |
67 cast_message->set_namespace_("foo"); | 69 cast_message->set_namespace_("foo"); |
68 cast_message->set_source_id("src"); | 70 cast_message->set_source_id("src"); |
69 cast_message->set_destination_id("dest"); | 71 cast_message->set_destination_id("dest"); |
70 cast_message->set_payload_utf8(message); | 72 cast_message->set_payload_utf8(message); |
71 cast_message->set_payload_type(CastMessage::STRING); | 73 cast_message->set_payload_type(CastMessage::STRING); |
72 } | 74 } |
73 | 75 |
74 ACTION_TEMPLATE(InvokeCompletionCallback, | 76 ACTION_TEMPLATE(InvokeCompletionCallback, |
75 HAS_1_TEMPLATE_PARAMS(int, k), | 77 HAS_1_TEMPLATE_PARAMS(int, k), |
76 AND_1_VALUE_PARAMS(result)) { | 78 AND_1_VALUE_PARAMS(result)) { |
77 ::std::tr1::get<k>(args).Run(result); | 79 ::std::tr1::get<k>(args).Run(result); |
78 } | 80 } |
79 | 81 |
80 } // namespace | 82 } // namespace |
81 | 83 |
82 class CastChannelAPITest : public ExtensionApiTest { | 84 class CastChannelAPITest : public ExtensionApiTest { |
83 public: | 85 public: |
84 CastChannelAPITest() : ip_endpoint_(CreateIPEndPointForTest()) {} | 86 CastChannelAPITest() : ip_endpoint_(CreateIPEndPointForTest()) {} |
85 | 87 |
86 void SetUpCommandLine(base::CommandLine* command_line) override { | 88 void SetUpCommandLine(base::CommandLine* command_line) override { |
87 ExtensionApiTest::SetUpCommandLine(command_line); | 89 ExtensionApiTest::SetUpCommandLine(command_line); |
88 command_line->AppendSwitchASCII( | 90 command_line->AppendSwitchASCII( |
89 extensions::switches::kWhitelistedExtensionID, kTestExtensionId); | 91 extensions::switches::kWhitelistedExtensionID, |
| 92 extensions::api::cast_channel::kTestExtensionId); |
90 } | 93 } |
91 | 94 |
92 void SetUpMockCastSocket() { | 95 void SetUpMockCastSocket() { |
93 extensions::CastChannelAPI* api = GetApi(); | 96 extensions::CastChannelAPI* api = GetApi(); |
94 timeout_timer_ = new base::MockTimer(true, false); | 97 timeout_timer_ = new base::MockTimer(true, false); |
95 api->SetPingTimeoutTimerForTest(base::WrapUnique(timeout_timer_)); | 98 api->SetPingTimeoutTimerForTest(base::WrapUnique(timeout_timer_)); |
96 | 99 |
97 net::IPEndPoint ip_endpoint(net::IPAddress(192, 168, 1, 1), 8009); | 100 net::IPEndPoint ip_endpoint(net::IPAddress(192, 168, 1, 1), 8009); |
98 mock_cast_socket_ = new MockCastSocket; | 101 mock_cast_socket_ = new MockCastSocket; |
99 // Transfers ownership of the socket. | 102 // Transfers ownership of the socket. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 .WillOnce(Return(ReadyState::CLOSED)); | 151 .WillOnce(Return(ReadyState::CLOSED)); |
149 } | 152 } |
150 } | 153 } |
151 | 154 |
152 extensions::CastChannelAPI* GetApi() { | 155 extensions::CastChannelAPI* GetApi() { |
153 return extensions::CastChannelAPI::Get(profile()); | 156 return extensions::CastChannelAPI::Get(profile()); |
154 } | 157 } |
155 | 158 |
156 // Logs some bogus error details and calls the OnError handler. | 159 // Logs some bogus error details and calls the OnError handler. |
157 void DoCallOnError(extensions::CastChannelAPI* api) { | 160 void DoCallOnError(extensions::CastChannelAPI* api) { |
158 api->GetLogger()->LogSocketEventWithRv(mock_cast_socket_->id(), | 161 api->GetLogger()->LogSocketEventWithRv( |
159 ::cast_channel::proto::SOCKET_WRITE, | 162 mock_cast_socket_->id(), |
160 net::ERR_FAILED); | 163 extensions::api::cast_channel::proto::SOCKET_WRITE, net::ERR_FAILED); |
161 message_delegate_->OnError(ChannelError::CONNECT_ERROR); | 164 message_delegate_->OnError(ChannelError::CONNECT_ERROR); |
162 } | 165 } |
163 | 166 |
164 protected: | 167 protected: |
165 void CallOnMessage(const std::string& message) { | 168 void CallOnMessage(const std::string& message) { |
166 content::BrowserThread::PostTask( | 169 content::BrowserThread::PostTask( |
167 content::BrowserThread::IO, FROM_HERE, | 170 content::BrowserThread::IO, FROM_HERE, |
168 base::Bind(&CastChannelAPITest::DoCallOnMessage, base::Unretained(this), | 171 base::Bind(&CastChannelAPITest::DoCallOnMessage, base::Unretained(this), |
169 GetApi(), mock_cast_socket_, message)); | 172 GetApi(), mock_cast_socket_, message)); |
170 } | 173 } |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 "\"keepAlive\": true, " | 434 "\"keepAlive\": true, " |
432 "\"audioOnly\": false, " | 435 "\"audioOnly\": false, " |
433 "\"connectInfo\": " | 436 "\"connectInfo\": " |
434 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " | 437 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " |
435 "\"auth\": \"ssl_verified\"}, \"readyState\": \"open\"}, " | 438 "\"auth\": \"ssl_verified\"}, \"readyState\": \"open\"}, " |
436 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", " | 439 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", " |
437 "\"destinationId\": \"\", \"data\": \"data\"}]", | 440 "\"destinationId\": \"\", \"data\": \"data\"}]", |
438 browser()); | 441 browser()); |
439 EXPECT_EQ(error, "message_info.destination_id is required"); | 442 EXPECT_EQ(error, "message_info.destination_id is required"); |
440 } | 443 } |
OLD | NEW |