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

Side by Side Diff: content/browser/screen_orientation/screen_orientation_dispatcher_host_unittest.cc

Issue 327573002: Properly route screen orientation IPC messages. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@screen_lock_view
Patch Set: nit Created 6 years, 6 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
OLDNEW
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 "content/browser/screen_orientation/screen_orientation_dispatcher_host. h"
6
5 #include "base/logging.h" 7 #include "base/logging.h"
6 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
7 #include "content/browser/screen_orientation/screen_orientation_dispatcher_host. h"
8 #include "content/browser/screen_orientation/screen_orientation_provider.h" 9 #include "content/browser/screen_orientation/screen_orientation_provider.h"
9 #include "content/common/screen_orientation_messages.h" 10 #include "content/common/screen_orientation_messages.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/test/mock_render_process_host.h"
12 #include "content/public/test/test_browser_context.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "content/public/test/test_utils.h" 11 #include "content/public/test/test_utils.h"
15 #include "ipc/ipc_test_sink.h" 12 #include "ipc/ipc_test_sink.h"
16 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
17 14
18 namespace content { 15 namespace content {
19 16
20 class MockScreenOrientationProvider : public ScreenOrientationProvider { 17 class MockScreenOrientationProvider : public ScreenOrientationProvider {
21 public: 18 public:
22 MockScreenOrientationProvider() 19 MockScreenOrientationProvider()
23 : orientation_(blink::WebScreenOrientationLockPortraitPrimary), 20 : orientation_(blink::WebScreenOrientationLockPortraitPrimary),
(...skipping 22 matching lines...) Expand all
46 blink::WebScreenOrientationLockType orientation_; 43 blink::WebScreenOrientationLockType orientation_;
47 bool unlock_called_; 44 bool unlock_called_;
48 45
49 DISALLOW_COPY_AND_ASSIGN(MockScreenOrientationProvider); 46 DISALLOW_COPY_AND_ASSIGN(MockScreenOrientationProvider);
50 }; 47 };
51 48
52 class ScreenOrientationDispatcherHostWithSink FINAL : 49 class ScreenOrientationDispatcherHostWithSink FINAL :
53 public ScreenOrientationDispatcherHost { 50 public ScreenOrientationDispatcherHost {
54 public: 51 public:
55 explicit ScreenOrientationDispatcherHostWithSink(IPC::TestSink* sink) 52 explicit ScreenOrientationDispatcherHostWithSink(IPC::TestSink* sink)
56 : ScreenOrientationDispatcherHost() , sink_(sink) {} 53 : ScreenOrientationDispatcherHost(NULL), sink_(sink) {}
57 54
58 virtual bool Send(IPC::Message* message) OVERRIDE { 55 virtual bool Send(IPC::Message* message) OVERRIDE {
59 return sink_->Send(message); 56 return sink_->Send(message);
60 } 57 }
61 58
62 private: 59 private:
63 virtual ~ScreenOrientationDispatcherHostWithSink() { } 60 virtual ~ScreenOrientationDispatcherHostWithSink() { }
64 61
65 IPC::TestSink* sink_; 62 IPC::TestSink* sink_;
66 }; 63 };
67 64
68 class ScreenOrientationDispatcherHostTest : public testing::Test { 65 class ScreenOrientationDispatcherHostTest : public testing::Test {
69 protected: 66 protected:
70 virtual ScreenOrientationDispatcherHost* CreateDispatcher() { 67 virtual ScreenOrientationDispatcherHost* CreateDispatcher() {
71 return new ScreenOrientationDispatcherHost(); 68 return new ScreenOrientationDispatcherHost(NULL);
72 } 69 }
73 70
74 virtual void SetUp() OVERRIDE { 71 virtual void SetUp() OVERRIDE {
75 provider_ = new MockScreenOrientationProvider(); 72 provider_ = new MockScreenOrientationProvider();
76 73
77 dispatcher_ = CreateDispatcher(); 74 dispatcher_.reset(CreateDispatcher());
78 dispatcher_->SetProviderForTests(provider_); 75 dispatcher_->SetProviderForTests(provider_);
79 } 76 }
80 77
78 int routing_id() const {
79 // We return a fake routing_id() in the context of this test.
80 return 0;
81 }
82
81 // The dispatcher_ owns the provider_ but we still want to access it. 83 // The dispatcher_ owns the provider_ but we still want to access it.
82 MockScreenOrientationProvider* provider_; 84 MockScreenOrientationProvider* provider_;
83 scoped_refptr<ScreenOrientationDispatcherHost> dispatcher_; 85 scoped_ptr<ScreenOrientationDispatcherHost> dispatcher_;
84 }; 86 };
85 87
86 class ScreenOrientationDispatcherHostWithSinkTest : 88 class ScreenOrientationDispatcherHostWithSinkTest :
87 public ScreenOrientationDispatcherHostTest { 89 public ScreenOrientationDispatcherHostTest {
88 protected: 90 protected:
89 virtual ScreenOrientationDispatcherHost* CreateDispatcher() OVERRIDE { 91 virtual ScreenOrientationDispatcherHost* CreateDispatcher() OVERRIDE {
90 return new ScreenOrientationDispatcherHostWithSink(&sink_); 92 return new ScreenOrientationDispatcherHostWithSink(&sink_);
91 } 93 }
92 94
93 const IPC::TestSink& sink() const { 95 const IPC::TestSink& sink() const {
(...skipping 22 matching lines...) Expand all
116 // Unfortunately, initializer list constructor for std::list is not yet 118 // Unfortunately, initializer list constructor for std::list is not yet
117 // something we can use. 119 // something we can use.
118 // Keep this in sync with |orientationsToTest|. 120 // Keep this in sync with |orientationsToTest|.
119 int orientationsToTestCount = 7; 121 int orientationsToTestCount = 7;
120 122
121 for (int i = 0; i < orientationsToTestCount; ++i) { 123 for (int i = 0; i < orientationsToTestCount; ++i) {
122 bool message_was_handled = false; 124 bool message_was_handled = false;
123 blink::WebScreenOrientationLockType orientation = orientationsToTest[i]; 125 blink::WebScreenOrientationLockType orientation = orientationsToTest[i];
124 126
125 message_was_handled = dispatcher_->OnMessageReceived( 127 message_was_handled = dispatcher_->OnMessageReceived(
126 ScreenOrientationHostMsg_LockRequest(orientation, 0)); 128 ScreenOrientationHostMsg_LockRequest(routing_id(), orientation, 0),
129 NULL);
127 130
128 EXPECT_TRUE(message_was_handled); 131 EXPECT_TRUE(message_was_handled);
129 EXPECT_EQ(orientation, provider_->orientation()); 132 EXPECT_EQ(orientation, provider_->orientation());
130 } 133 }
131 } 134 }
132 135
133 // Test that when receiving an unlock message, it is correctly dispatched to the 136 // Test that when receiving an unlock message, it is correctly dispatched to the
134 // ScreenOrientationProvider. 137 // ScreenOrientationProvider.
135 TEST_F(ScreenOrientationDispatcherHostTest, ProviderUnlock) { 138 TEST_F(ScreenOrientationDispatcherHostTest, ProviderUnlock) {
136 bool message_was_handled = dispatcher_->OnMessageReceived( 139 bool message_was_handled = dispatcher_->OnMessageReceived(
137 ScreenOrientationHostMsg_Unlock()); 140 ScreenOrientationHostMsg_Unlock(routing_id()), NULL);
138 141
139 EXPECT_TRUE(message_was_handled); 142 EXPECT_TRUE(message_was_handled);
140 EXPECT_TRUE(provider_->unlock_called()); 143 EXPECT_TRUE(provider_->unlock_called());
141 } 144 }
142 145
143 // Test that when there is no provider, a LockRequest fails with the appropriate 146 // Test that when there is no provider, a LockRequest fails with the appropriate
144 // ErrorType. 147 // ErrorType.
145 TEST_F(ScreenOrientationDispatcherHostWithSinkTest, NoProvider_LockError) { 148 TEST_F(ScreenOrientationDispatcherHostWithSinkTest, NoProvider_LockError) {
146 dispatcher_->SetProviderForTests(NULL); 149 dispatcher_->SetProviderForTests(NULL);
147 150
148 const int request_id = 3; 151 const int request_id = 3;
149 dispatcher_->OnMessageReceived(ScreenOrientationHostMsg_LockRequest( 152 dispatcher_->OnMessageReceived(ScreenOrientationHostMsg_LockRequest(
150 blink::WebScreenOrientationLockPortraitPrimary, request_id)); 153 routing_id(),
154 blink::WebScreenOrientationLockPortraitPrimary,
155 request_id), NULL);
151 156
152 EXPECT_EQ(1u, sink().message_count()); 157 EXPECT_EQ(1u, sink().message_count());
153 158
154 const IPC::Message* msg = sink().GetFirstMessageMatching( 159 const IPC::Message* msg = sink().GetFirstMessageMatching(
155 ScreenOrientationMsg_LockError::ID); 160 ScreenOrientationMsg_LockError::ID);
156 EXPECT_TRUE(msg != NULL); 161 EXPECT_TRUE(msg != NULL);
157 162
158 Tuple2<int, blink::WebLockOrientationCallback::ErrorType> params; 163 Tuple2<int, blink::WebLockOrientationCallback::ErrorType> params;
159 ScreenOrientationMsg_LockError::Read(msg, &params); 164 ScreenOrientationMsg_LockError::Read(msg, &params);
160 EXPECT_EQ(request_id, params.a); 165 EXPECT_EQ(request_id, params.a);
161 EXPECT_EQ(blink::WebLockOrientationCallback::ErrorTypeNotAvailable, params.b); 166 EXPECT_EQ(blink::WebLockOrientationCallback::ErrorTypeNotAvailable, params.b);
162 } 167 }
163 168
164 // Test that when there is a provider, we always send a success response back to 169 // Test that when there is a provider, we always send a success response back to
165 // the renderer. 170 // the renderer.
166 // TODO(mlamouri): we currently do not test the content of the message because 171 // TODO(mlamouri): we currently do not test the content of the message because
167 // it currently contains dummy values. 172 // it currently contains dummy values.
168 TEST_F(ScreenOrientationDispatcherHostWithSinkTest, WithProvider_LockSuccess) { 173 TEST_F(ScreenOrientationDispatcherHostWithSinkTest, WithProvider_LockSuccess) {
169 const int request_id = 42; 174 const int request_id = 42;
170 dispatcher_->OnMessageReceived(ScreenOrientationHostMsg_LockRequest( 175 dispatcher_->OnMessageReceived(ScreenOrientationHostMsg_LockRequest(
171 blink::WebScreenOrientationLockPortraitPrimary, request_id)); 176 routing_id(),
177 blink::WebScreenOrientationLockPortraitPrimary,
178 request_id), NULL);
172 179
173 EXPECT_EQ(1u, sink().message_count()); 180 EXPECT_EQ(1u, sink().message_count());
174 181
175 const IPC::Message* msg = sink().GetFirstMessageMatching( 182 const IPC::Message* msg = sink().GetFirstMessageMatching(
176 ScreenOrientationMsg_LockSuccess::ID); 183 ScreenOrientationMsg_LockSuccess::ID);
177 EXPECT_TRUE(msg != NULL); 184 EXPECT_TRUE(msg != NULL);
178 185
179 Tuple3<int, unsigned, blink::WebScreenOrientationType> params; 186 Tuple3<int, unsigned, blink::WebScreenOrientationType> params;
180 ScreenOrientationMsg_LockSuccess::Read(msg, &params); 187 ScreenOrientationMsg_LockSuccess::Read(msg, &params);
181 EXPECT_EQ(request_id, params.a); 188 EXPECT_EQ(request_id, params.a);
182 } 189 }
183 190
184 } // namespace content 191 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698