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
3 // found in the LICENSE file.
5 #include "screen_orientation_dispatcher.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "content/common/screen_orientation_messages.h"
10 #include "content/public/test/mock_render_thread.h"
11 #include "content/public/test/test_utils.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/WebKit/public/platform/WebScreenOrientationListener.h"
17 class MockScreenOrientationListener
:
18 public blink::WebScreenOrientationListener
{
20 MockScreenOrientationListener();
21 virtual ~MockScreenOrientationListener() {}
23 virtual void didChangeScreenOrientation(
24 blink::WebScreenOrientationType
) OVERRIDE
;
26 bool did_change_screen_orientation() const {
27 return did_change_screen_orientation_
;
30 blink::WebScreenOrientationType
screen_orientation() const {
31 return screen_orientation_
;
35 bool did_change_screen_orientation_
;
36 blink::WebScreenOrientationType screen_orientation_
;
38 DISALLOW_COPY_AND_ASSIGN(MockScreenOrientationListener
);
41 MockScreenOrientationListener::MockScreenOrientationListener()
42 : did_change_screen_orientation_(false),
43 screen_orientation_(blink::WebScreenOrientationPortraitPrimary
) {
46 void MockScreenOrientationListener::didChangeScreenOrientation(
47 blink::WebScreenOrientationType orientation
) {
48 did_change_screen_orientation_
= true;
49 screen_orientation_
= orientation
;
52 class ScreenOrientationDispatcherTest
: public testing::Test
{
54 virtual void SetUp() OVERRIDE
{
55 render_thread_
.reset(new MockRenderThread
);
56 listener_
.reset(new MockScreenOrientationListener
);
57 dispatcher_
.reset(new ScreenOrientationDispatcher(render_thread_
.get()));
58 dispatcher_
->setListener(listener_
.get());
61 scoped_ptr
<MockRenderThread
> render_thread_
;
62 scoped_ptr
<MockScreenOrientationListener
> listener_
;
63 scoped_ptr
<ScreenOrientationDispatcher
> dispatcher_
;
66 TEST_F(ScreenOrientationDispatcherTest
, ListensToMessages
) {
67 EXPECT_TRUE(render_thread_
->observers().HasObserver(dispatcher_
.get()));
69 render_thread_
->OnControlMessageReceived(
70 ScreenOrientationMsg_OrientationChange(
71 blink::WebScreenOrientationPortraitPrimary
));
73 EXPECT_TRUE(listener_
->did_change_screen_orientation());
76 TEST_F(ScreenOrientationDispatcherTest
, NullListener
) {
77 dispatcher_
->setListener(NULL
);
79 render_thread_
->OnControlMessageReceived(
80 ScreenOrientationMsg_OrientationChange(
81 blink::WebScreenOrientationPortraitPrimary
));
83 EXPECT_FALSE(listener_
->did_change_screen_orientation());
86 TEST_F(ScreenOrientationDispatcherTest
, ValidValues
) {
87 render_thread_
->OnControlMessageReceived(
88 ScreenOrientationMsg_OrientationChange(
89 blink::WebScreenOrientationPortraitPrimary
));
90 EXPECT_EQ(blink::WebScreenOrientationPortraitPrimary
,
91 listener_
->screen_orientation());
93 render_thread_
->OnControlMessageReceived(
94 ScreenOrientationMsg_OrientationChange(
95 blink::WebScreenOrientationLandscapePrimary
));
96 EXPECT_EQ(blink::WebScreenOrientationLandscapePrimary
,
97 listener_
->screen_orientation());
99 render_thread_
->OnControlMessageReceived(
100 ScreenOrientationMsg_OrientationChange(
101 blink::WebScreenOrientationPortraitSecondary
));
102 EXPECT_EQ(blink::WebScreenOrientationPortraitSecondary
,
103 listener_
->screen_orientation());
105 render_thread_
->OnControlMessageReceived(
106 ScreenOrientationMsg_OrientationChange(
107 blink::WebScreenOrientationLandscapeSecondary
));
108 EXPECT_EQ(blink::WebScreenOrientationLandscapeSecondary
,
109 listener_
->screen_orientation());
112 } // namespace content