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