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 "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"
15 class MockScreenOrientationProvider : public ScreenOrientationProvider {
17 MockScreenOrientationProvider()
18 : orientation_(blink::WebScreenOrientationLockPortraitPrimary),
19 unlock_called_(false) {}
21 virtual void LockOrientation(blink::WebScreenOrientationLockType orientation)
23 orientation_ = orientation;
27 virtual void UnlockOrientation() OVERRIDE {
28 unlock_called_ = true;
31 blink::WebScreenOrientationLockType orientation() const {
35 bool unlock_called() const {
36 return unlock_called_;
39 virtual ~MockScreenOrientationProvider() {}
42 blink::WebScreenOrientationLockType orientation_;
45 DISALLOW_COPY_AND_ASSIGN(MockScreenOrientationProvider);
48 class ScreenOrientationDispatcherHostTest : public testing::Test {
50 virtual void SetUp() OVERRIDE {
51 provider_ = new MockScreenOrientationProvider();
53 dispatcher_ = new ScreenOrientationDispatcherHost();
54 dispatcher_->SetProviderForTests(provider_);
57 // The dispatcher_ owns the provider_ but we still want to access it.
58 MockScreenOrientationProvider* provider_;
59 scoped_refptr<ScreenOrientationDispatcherHost> dispatcher_;
62 // Test that a NULL provider is correctly handled.
63 TEST_F(ScreenOrientationDispatcherHostTest, NullProvider) {
64 dispatcher_->SetProviderForTests(NULL);
66 bool message_was_ok = false;
67 bool message_was_handled = dispatcher_->OnMessageReceived(
68 ScreenOrientationHostMsg_Lock(
69 blink::WebScreenOrientationLockPortraitPrimary), &message_was_ok);
71 EXPECT_TRUE(message_was_ok);
72 EXPECT_TRUE(message_was_handled);
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
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;
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];
99 message_was_handled = dispatcher_->OnMessageReceived(
100 ScreenOrientationHostMsg_Lock(orientation), &message_was_ok);
102 EXPECT_TRUE(message_was_ok);
103 EXPECT_TRUE(message_was_handled);
104 EXPECT_EQ(orientation, provider_->orientation());
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);
115 EXPECT_TRUE(message_was_ok);
116 EXPECT_TRUE(message_was_handled);
117 EXPECT_TRUE(provider_->unlock_called());
120 } // namespace content