Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / screen_orientation / screen_orientation_dispatcher_host_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 "base/logging.h"
6 #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/common/screen_orientation_messages.h"
10 #include "content/public/test/test_utils.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace content {
14
15 class MockScreenOrientationProvider : public ScreenOrientationProvider {
16  public:
17   MockScreenOrientationProvider()
18       : orientation_(blink::WebScreenOrientationLockPortraitPrimary),
19         unlock_called_(false) {}
20
21   virtual void LockOrientation(blink::WebScreenOrientationLockType orientation)
22       OVERRIDE {
23     orientation_ = orientation;
24
25   }
26
27   virtual void UnlockOrientation() OVERRIDE {
28     unlock_called_ = true;
29   }
30
31   blink::WebScreenOrientationLockType orientation() const {
32     return orientation_;
33   }
34
35   bool unlock_called() const {
36     return unlock_called_;
37   }
38
39   virtual ~MockScreenOrientationProvider() {}
40
41  private:
42   blink::WebScreenOrientationLockType orientation_;
43   bool unlock_called_;
44
45   DISALLOW_COPY_AND_ASSIGN(MockScreenOrientationProvider);
46 };
47
48 class ScreenOrientationDispatcherHostTest : public testing::Test {
49  protected:
50   virtual void SetUp() OVERRIDE {
51     provider_ = new MockScreenOrientationProvider();
52
53     dispatcher_ = new ScreenOrientationDispatcherHost();
54     dispatcher_->SetProviderForTests(provider_);
55   }
56
57   // The dispatcher_ owns the provider_ but we still want to access it.
58   MockScreenOrientationProvider* provider_;
59   scoped_refptr<ScreenOrientationDispatcherHost> dispatcher_;
60 };
61
62 // Test that a NULL provider is correctly handled.
63 TEST_F(ScreenOrientationDispatcherHostTest, NullProvider) {
64   dispatcher_->SetProviderForTests(NULL);
65
66   bool message_was_ok = false;
67   bool message_was_handled = dispatcher_->OnMessageReceived(
68       ScreenOrientationHostMsg_Lock(
69           blink::WebScreenOrientationLockPortraitPrimary), &message_was_ok);
70
71   EXPECT_TRUE(message_was_ok);
72   EXPECT_TRUE(message_was_handled);
73 }
74
75 // Test that when receiving a lock message, it is correctly dispatched to the
76 // ScreenOrientationProvider.
77 TEST_F(ScreenOrientationDispatcherHostTest, ProviderLock) {
78   // If we change this array, update |orientationsToTestCount| below.
79   blink::WebScreenOrientationLockType orientationsToTest[] = {
80     blink::WebScreenOrientationLockPortraitPrimary,
81     blink::WebScreenOrientationLockPortraitSecondary,
82     blink::WebScreenOrientationLockLandscapePrimary,
83     blink::WebScreenOrientationLockLandscapeSecondary,
84     blink::WebScreenOrientationLockPortrait,
85     blink::WebScreenOrientationLockLandscapePrimary,
86     blink::WebScreenOrientationLockAny
87   };
88
89   // Unfortunately, initializer list constructor for std::list is not yet
90   // something we can use.
91   // Keep this in sync with |orientationsToTest|.
92   int orientationsToTestCount = 7;
93
94   for (int i = 0; i < orientationsToTestCount; ++i) {
95     bool message_was_ok = false;
96     bool message_was_handled = false;
97     blink::WebScreenOrientationLockType orientation = orientationsToTest[i];
98
99     message_was_handled = dispatcher_->OnMessageReceived(
100         ScreenOrientationHostMsg_Lock(orientation), &message_was_ok);
101
102     EXPECT_TRUE(message_was_ok);
103     EXPECT_TRUE(message_was_handled);
104     EXPECT_EQ(orientation, provider_->orientation());
105   }
106 }
107
108 // Test that when receiving an unlock message, it is correctly dispatched to the
109 // ScreenOrientationProvider.
110 TEST_F(ScreenOrientationDispatcherHostTest, ProviderUnlock) {
111     bool message_was_ok = false;
112     bool message_was_handled = dispatcher_->OnMessageReceived(
113         ScreenOrientationHostMsg_Unlock(), &message_was_ok);
114
115     EXPECT_TRUE(message_was_ok);
116     EXPECT_TRUE(message_was_handled);
117     EXPECT_TRUE(provider_->unlock_called());
118 }
119
120 } // namespace content