Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / screen_orientation / screen_orientation_dispatcher_unittest.cc
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.
4
5 #include "screen_orientation_dispatcher.h"
6
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"
14
15 namespace content {
16
17 class MockScreenOrientationListener :
18     public blink::WebScreenOrientationListener {
19  public:
20   MockScreenOrientationListener();
21   virtual ~MockScreenOrientationListener() {}
22
23   virtual void didChangeScreenOrientation(
24       blink::WebScreenOrientationType) OVERRIDE;
25
26   bool did_change_screen_orientation() const {
27     return did_change_screen_orientation_;
28   }
29
30   blink::WebScreenOrientationType screen_orientation() const {
31     return screen_orientation_;
32   }
33
34  private:
35   bool did_change_screen_orientation_;
36   blink::WebScreenOrientationType screen_orientation_;
37
38   DISALLOW_COPY_AND_ASSIGN(MockScreenOrientationListener);
39 };
40
41 MockScreenOrientationListener::MockScreenOrientationListener()
42     : did_change_screen_orientation_(false),
43       screen_orientation_(blink::WebScreenOrientationPortraitPrimary) {
44 }
45
46 void MockScreenOrientationListener::didChangeScreenOrientation(
47     blink::WebScreenOrientationType orientation) {
48   did_change_screen_orientation_ = true;
49   screen_orientation_ = orientation;
50 }
51
52 class ScreenOrientationDispatcherTest : public testing::Test {
53  protected:
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());
59   }
60
61   scoped_ptr<MockRenderThread> render_thread_;
62   scoped_ptr<MockScreenOrientationListener> listener_;
63   scoped_ptr<ScreenOrientationDispatcher> dispatcher_;
64 };
65
66 TEST_F(ScreenOrientationDispatcherTest, ListensToMessages) {
67   EXPECT_TRUE(render_thread_->observers().HasObserver(dispatcher_.get()));
68
69   render_thread_->OnControlMessageReceived(
70       ScreenOrientationMsg_OrientationChange(
71           blink::WebScreenOrientationPortraitPrimary));
72
73   EXPECT_TRUE(listener_->did_change_screen_orientation());
74 }
75
76 TEST_F(ScreenOrientationDispatcherTest, NullListener) {
77   dispatcher_->setListener(NULL);
78
79   render_thread_->OnControlMessageReceived(
80       ScreenOrientationMsg_OrientationChange(
81           blink::WebScreenOrientationPortraitPrimary));
82
83   EXPECT_FALSE(listener_->did_change_screen_orientation());
84 }
85
86 TEST_F(ScreenOrientationDispatcherTest, ValidValues) {
87   render_thread_->OnControlMessageReceived(
88       ScreenOrientationMsg_OrientationChange(
89           blink::WebScreenOrientationPortraitPrimary));
90   EXPECT_EQ(blink::WebScreenOrientationPortraitPrimary,
91                listener_->screen_orientation());
92
93   render_thread_->OnControlMessageReceived(
94       ScreenOrientationMsg_OrientationChange(
95           blink::WebScreenOrientationLandscapePrimary));
96   EXPECT_EQ(blink::WebScreenOrientationLandscapePrimary,
97                listener_->screen_orientation());
98
99   render_thread_->OnControlMessageReceived(
100       ScreenOrientationMsg_OrientationChange(
101           blink::WebScreenOrientationPortraitSecondary));
102   EXPECT_EQ(blink::WebScreenOrientationPortraitSecondary,
103                listener_->screen_orientation());
104
105   render_thread_->OnControlMessageReceived(
106       ScreenOrientationMsg_OrientationChange(
107           blink::WebScreenOrientationLandscapeSecondary));
108   EXPECT_EQ(blink::WebScreenOrientationLandscapeSecondary,
109                listener_->screen_orientation());
110 }
111
112 }  // namespace content