Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: chrome/browser/extensions/api/cast_channel/cast_channel_apitest.cc

Issue 165293002: Add an API test to exersize the open error case. This is a regression test for (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chrome/browser/extensions/api/cast_channel/cast_channel_api.h" 8 #include "chrome/browser/extensions/api/cast_channel/cast_channel_api.h"
9 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h" 9 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h"
10 #include "chrome/browser/extensions/extension_apitest.h" 10 #include "chrome/browser/extensions/extension_apitest.h"
11 #include "chrome/browser/extensions/extension_service.h" 11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/common/chrome_switches.h" 12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/extensions/api/cast_channel.h" 13 #include "chrome/common/extensions/api/cast_channel.h"
14 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
15 #include "net/base/capturing_net_log.h" 15 #include "net/base/capturing_net_log.h"
16 #include "net/base/completion_callback.h" 16 #include "net/base/completion_callback.h"
17 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
18 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gmock_mutant.h"
19 20
20 namespace cast_channel = extensions::api::cast_channel; 21 namespace cast_channel = extensions::api::cast_channel;
21 using cast_channel::CastSocket; 22 using cast_channel::CastSocket;
23 using cast_channel::ChannelError;
22 using cast_channel::ChannelInfo; 24 using cast_channel::ChannelInfo;
23 using cast_channel::MessageInfo; 25 using cast_channel::MessageInfo;
24 26
27 using ::testing::_;
25 using ::testing::A; 28 using ::testing::A;
26 using ::testing::_; 29 using ::testing::DoAll;
27 using ::testing::Invoke; 30 using ::testing::Invoke;
28 using ::testing::Return; 31 using ::testing::Return;
29 32
30 namespace { 33 namespace {
31 34
32 const char kTestExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf"; 35 const char kTestExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf";
33 const char kTestUrl[] = "cast://192.168.1.1:8009"; 36 const char kTestUrl[] = "cast://192.168.1.1:8009";
34 37
35 static void FillMessageInfo(MessageInfo* message_info, 38 static void FillMessageInfo(MessageInfo* message_info,
36 const std::string& message) { 39 const std::string& message) {
37 message_info->namespace_ = "foo"; 40 message_info->namespace_ = "foo";
38 message_info->source_id = "src"; 41 message_info->source_id = "src";
39 message_info->destination_id = "dest"; 42 message_info->destination_id = "dest";
40 message_info->data.reset(new base::StringValue(message)); 43 message_info->data.reset(new base::StringValue(message));
41 } 44 }
42 45
43 ACTION_TEMPLATE(InvokeCompletionCallback, 46 ACTION_TEMPLATE(InvokeCompletionCallback,
44 HAS_1_TEMPLATE_PARAMS(int, k), 47 HAS_1_TEMPLATE_PARAMS(int, k),
45 AND_1_VALUE_PARAMS(result)) { 48 AND_1_VALUE_PARAMS(result)) {
46 ::std::tr1::get<k>(args).Run(result); 49 ::std::tr1::get<k>(args).Run(result);
47 } 50 }
48 51
52 // mfoltz,wez: Please see this and suggest how to make ACTION_TEMPLATE
53 // to work. I have currently used testing::CreateFunctor, but it will be
54 // better to use ACTION_TEMPLATE for consistency with InvokeCompletionCallback
mark a. foltz 2014/02/24 06:14:05 This looks like it should work, but I am far from
Munjal (Google) 2014/02/26 20:46:33 I figured out a way to use ACTION_P macro.
55 /*
56 ACTION_TEMPLATE(InvokeDelegateOnError,
57 HAS_2_TEMPLATE_PARAMS(extensions::CastChannelAPI*, api,
58 MockCastSocket*, cast_socket),
59 AND_1_VALUE_PARAMS(callback)) {
60 ::std::tr1::get<api, cast_socket>(args).Run(callback);
61 }
62 */
63
49 class MockCastSocket : public CastSocket { 64 class MockCastSocket : public CastSocket {
50 public: 65 public:
51 explicit MockCastSocket(CastSocket::Delegate* delegate, 66 explicit MockCastSocket(CastSocket::Delegate* delegate,
52 net::NetLog* net_log) 67 net::NetLog* net_log)
53 : CastSocket(kTestExtensionId, GURL(kTestUrl), delegate, net_log) {} 68 : CastSocket(kTestExtensionId, GURL(kTestUrl), delegate, net_log) {}
54 virtual ~MockCastSocket() {} 69 virtual ~MockCastSocket() {}
55 70
56 virtual bool CalledOnValidThread() const OVERRIDE { 71 virtual bool CalledOnValidThread() const OVERRIDE {
57 // Always return true in testing. 72 // Always return true in testing.
58 return true; 73 return true;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 channel_info->error_state = cast_channel::CHANNEL_ERROR_NONE; 111 channel_info->error_state = cast_channel::CHANNEL_ERROR_NONE;
97 } 112 }
98 113
99 static void FillChannelInfoForClosedState(ChannelInfo* channel_info) { 114 static void FillChannelInfoForClosedState(ChannelInfo* channel_info) {
100 channel_info->channel_id = 1; 115 channel_info->channel_id = 1;
101 channel_info->url = kTestUrl; 116 channel_info->url = kTestUrl;
102 channel_info->ready_state = cast_channel::READY_STATE_CLOSED; 117 channel_info->ready_state = cast_channel::READY_STATE_CLOSED;
103 channel_info->error_state = cast_channel::CHANNEL_ERROR_NONE; 118 channel_info->error_state = cast_channel::CHANNEL_ERROR_NONE;
104 } 119 }
105 120
121 void CallOnError(extensions::CastChannelAPI* api,
122 const net::CompletionCallback& callback) {
123 api->OnError(mock_cast_socket_, cast_channel::CHANNEL_ERROR_CONNECT_ERROR);
mark a. foltz 2014/02/24 06:14:05 Will api ever != GetApi()? Can we use the GetApi(
Munjal (Google) 2014/02/26 20:46:33 The issue is that if GetAPI is called here then a
124
125 /*
mark a. foltz 2014/02/24 06:14:05 Delete commented out code and method it calls
Munjal (Google) 2014/02/26 20:46:33 Done.
126 content::BrowserThread::PostTask(
127 content::BrowserThread::IO,
128 FROM_HERE,
129 base::Bind(&CastChannelAPITest::DoCallOnError, this,
130 api, mock_cast_socket_));
131 */
132 }
133
134 void DoCallOnError(extensions::CastChannelAPI* api,
135 MockCastSocket* cast_socket) {
136 api->OnError(cast_socket, cast_channel::CHANNEL_ERROR_CONNECT_ERROR);
137 }
138
106 protected: 139 protected:
107 void CallOnMessage(const std::string& message) { 140 void CallOnMessage(const std::string& message) {
108 content::BrowserThread::PostTask( 141 content::BrowserThread::PostTask(
109 content::BrowserThread::IO, 142 content::BrowserThread::IO,
110 FROM_HERE, 143 FROM_HERE,
111 base::Bind(&CastChannelAPITest::DoCallOnMessage, this, 144 base::Bind(&CastChannelAPITest::DoCallOnMessage, this,
112 GetApi(), mock_cast_socket_, message)); 145 mock_cast_socket_, message));
113 } 146 }
114 147
115 void DoCallOnMessage(extensions::CastChannelAPI* api, 148 void DoCallOnMessage(MockCastSocket* cast_socket,
116 MockCastSocket* cast_socket,
117 const std::string& message) { 149 const std::string& message) {
118 MessageInfo message_info; 150 MessageInfo message_info;
119 FillMessageInfo(&message_info, message); 151 FillMessageInfo(&message_info, message);
120 api->OnMessage(cast_socket, message_info); 152 GetApi()->OnMessage(cast_socket, message_info);
121 } 153 }
122 154
123 MockCastSocket* mock_cast_socket_; 155 MockCastSocket* mock_cast_socket_;
124 ChannelInfo channel_info; 156 ChannelInfo channel_info;
125 net::CapturingNetLog capturing_net_log_; 157 net::CapturingNetLog capturing_net_log_;
126 }; 158 };
127 159
128 // TODO(munjal): Win Dbg has a workaround that makes RunExtensionSubtest 160 // TODO(munjal): Win Dbg has a workaround that makes RunExtensionSubtest
129 // always return true without actually running the test. Remove when fixed. 161 // always return true without actually running the test. Remove when fixed.
130 #if defined(OS_WIN) && !defined(NDEBUG) 162 #if defined(OS_WIN) && !defined(NDEBUG)
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 WillOnce(InvokeCompletionCallback<0>(net::OK)); 207 WillOnce(InvokeCompletionCallback<0>(net::OK));
176 208
177 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", 209 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api",
178 "test_open_receive_close.html")); 210 "test_open_receive_close.html"));
179 211
180 ResultCatcher catcher; 212 ResultCatcher catcher;
181 CallOnMessage("some-message"); 213 CallOnMessage("some-message");
182 CallOnMessage("some-message"); 214 CallOnMessage("some-message");
183 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); 215 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
184 } 216 }
217
218 // TODO(munjal): Win Dbg has a workaround that makes RunExtensionSubtest
219 // always return true without actually running the test. Remove when fixed.
220 #if defined(OS_WIN) && !defined(NDEBUG)
221 #define MAYBE_TestOpenError DISABLED_TestOpenError
222 #else
223 #define MAYBE_TestOpenError TestOpenError
224 #endif
225 // Test the case when socket open results in an error.
226 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenError) {
227 SetUpMockCastSocket();
228
229 EXPECT_CALL(*mock_cast_socket_, Connect(_))
230 .WillOnce(DoAll(
231 Invoke(testing::CreateFunctor(
232 this, &CastChannelAPITest::CallOnError, GetApi())),
233 InvokeCompletionCallback<0>(net::ERR_FAILED)));
234 EXPECT_CALL(*mock_cast_socket_, FillChannelInfo(_))
235 .WillOnce(Invoke(FillChannelInfoForClosedState))
236 .WillOnce(Invoke(FillChannelInfoForClosedState));
mark a. foltz 2014/02/24 06:14:05 Is it a typo to invoke FIllChannelInfoForClosedSta
Munjal (Google) 2014/02/26 20:46:33 No, it is actually called twice.
237 EXPECT_CALL(*mock_cast_socket_, Close(_));
238
239 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api",
mark a. foltz 2014/02/24 06:14:05 Can we convert these to use RunExtensionTest inste
Munjal (Google) 2014/02/26 20:46:33 Let me handle this separately.
240 "test_open_error.html"));
241
242 ResultCatcher catcher;
243 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
244 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698