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

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
« no previous file with comments | « no previous file | chrome/test/data/extensions/api_test/cast_channel/api/test_open_error.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
55 /*
56 ACTION_TEMPLATE(InvokeDelegateOnError,
57 HAS_1_TEMPLATE_PARAMS(extensions::CastChannelAPI*, api,
58 MockCastSocket*, cast_socket),
Wez 2014/02/15 01:45:52 This should be HAS_2_TEMPLATE_PARAMS.
Munjal (Google) 2014/02/20 23:27:08 Done. But I don't understand how an action templat
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 content::BrowserThread::PostTask(
124 content::BrowserThread::IO,
125 FROM_HERE,
126 base::Bind(&CastChannelAPITest::DoCallOnError, this,
127 api, mock_cast_socket_));
128 }
129
130 void DoCallOnError(extensions::CastChannelAPI* api,
131 MockCastSocket* cast_socket) {
132 api->OnError(cast_socket, cast_channel::CHANNEL_ERROR_CONNECT_ERROR);
133 }
134
106 protected: 135 protected:
107 void CallOnMessage(const std::string& message) { 136 void CallOnMessage(const std::string& message) {
108 content::BrowserThread::PostTask( 137 content::BrowserThread::PostTask(
109 content::BrowserThread::IO, 138 content::BrowserThread::IO,
110 FROM_HERE, 139 FROM_HERE,
111 base::Bind(&CastChannelAPITest::DoCallOnMessage, this, 140 base::Bind(&CastChannelAPITest::DoCallOnMessage, this,
112 GetApi(), mock_cast_socket_, message)); 141 mock_cast_socket_, message));
113 } 142 }
114 143
115 void DoCallOnMessage(extensions::CastChannelAPI* api, 144 void DoCallOnMessage(MockCastSocket* cast_socket,
116 MockCastSocket* cast_socket,
117 const std::string& message) { 145 const std::string& message) {
118 MessageInfo message_info; 146 MessageInfo message_info;
119 FillMessageInfo(&message_info, message); 147 FillMessageInfo(&message_info, message);
120 api->OnMessage(cast_socket, message_info); 148 GetApi()->OnMessage(cast_socket, message_info);
121 } 149 }
122 150
123 MockCastSocket* mock_cast_socket_; 151 MockCastSocket* mock_cast_socket_;
124 ChannelInfo channel_info; 152 ChannelInfo channel_info;
125 net::CapturingNetLog capturing_net_log_; 153 net::CapturingNetLog capturing_net_log_;
126 }; 154 };
127 155
128 // TODO(munjal): Win Dbg has a workaround that makes RunExtensionSubtest 156 // TODO(munjal): Win Dbg has a workaround that makes RunExtensionSubtest
129 // always return true without actually running the test. Remove when fixed. 157 // always return true without actually running the test. Remove when fixed.
130 #if defined(OS_WIN) && !defined(NDEBUG) 158 #if defined(OS_WIN) && !defined(NDEBUG)
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 WillOnce(InvokeCompletionCallback<0>(net::OK)); 203 WillOnce(InvokeCompletionCallback<0>(net::OK));
176 204
177 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", 205 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api",
178 "test_open_receive_close.html")); 206 "test_open_receive_close.html"));
179 207
180 ResultCatcher catcher; 208 ResultCatcher catcher;
181 CallOnMessage("some-message"); 209 CallOnMessage("some-message");
182 CallOnMessage("some-message"); 210 CallOnMessage("some-message");
183 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); 211 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
184 } 212 }
213
214 // TODO(munjal): Win Dbg has a workaround that makes RunExtensionSubtest
215 // always return true without actually running the test. Remove when fixed.
Wez 2014/02/15 01:45:52 If it's always going to return true then why do we
Munjal (Google) 2014/02/20 23:27:08 I am not aware of the full background here. I borr
216 #if defined(OS_WIN) && !defined(NDEBUG)
217 #define MAYBE_TestOpenError DISABLED_TestOpenError
218 #else
219 #define MAYBE_TestOpenError TestOpenError
220 #endif
221 // Test the case when socket open results in an error.
222 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenError) {
223 SetUpMockCastSocket();
224
225 EXPECT_CALL(*mock_cast_socket_, Connect(_))
Wez 2014/02/15 01:45:52 Indentation
Munjal (Google) 2014/02/20 23:27:08 Done.
226 .WillOnce(DoAll(
227 Invoke(testing::CreateFunctor(
228 this, &CastChannelAPITest::CallOnError, GetApi())),
229 InvokeCompletionCallback<0>(net::OK)));
230 EXPECT_CALL(*mock_cast_socket_, FillChannelInfo(_))
231 .WillOnce(Invoke(FillChannelInfoForClosedState))
232 .WillOnce(Invoke(FillChannelInfoForClosedState));
233
234 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api",
235 "test_open_error.html"));
236
237 ResultCatcher catcher;
238 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
239 }
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/extensions/api_test/cast_channel/api/test_open_error.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698