Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / shell / renderer / test_runner / mock_screen_orientation_client.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 "content/shell/renderer/test_runner/mock_screen_orientation_client.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "third_party/WebKit/public/web/WebFrame.h"
11
12 namespace content {
13
14 MockScreenOrientationClient::MockScreenOrientationClient()
15     : main_frame_(NULL),
16       current_lock_(blink::WebScreenOrientationLockDefault),
17       device_orientation_(blink::WebScreenOrientationPortraitPrimary),
18       current_orientation_(blink::WebScreenOrientationPortraitPrimary) {
19 }
20
21 MockScreenOrientationClient::~MockScreenOrientationClient() {
22 }
23
24 void MockScreenOrientationClient::ResetData() {
25   current_lock_ = blink::WebScreenOrientationLockDefault;
26   device_orientation_ = blink::WebScreenOrientationPortraitPrimary;
27   current_orientation_ = blink::WebScreenOrientationPortraitPrimary;
28 }
29
30 void MockScreenOrientationClient::UpdateDeviceOrientation(
31     blink::WebFrame* main_frame,
32     blink::WebScreenOrientationType orientation) {
33   main_frame_ = main_frame;
34   if (device_orientation_ == orientation)
35     return;
36   device_orientation_ = orientation;
37   if (!IsOrientationAllowedByCurrentLock(orientation))
38     return;
39   UpdateScreenOrientation(orientation);
40 }
41
42 void MockScreenOrientationClient::UpdateScreenOrientation(
43     blink::WebScreenOrientationType orientation) {
44   if (current_orientation_ == orientation)
45     return;
46   current_orientation_ = orientation;
47   if (main_frame_)
48     main_frame_->sendOrientationChangeEvent();
49 }
50
51 blink::WebScreenOrientationType
52 MockScreenOrientationClient::CurrentOrientationType() const {
53   return current_orientation_;
54 }
55
56 unsigned MockScreenOrientationClient::CurrentOrientationAngle() const {
57   return OrientationTypeToAngle(current_orientation_);
58 }
59
60 unsigned MockScreenOrientationClient::OrientationTypeToAngle(
61     blink::WebScreenOrientationType type) {
62   unsigned angle;
63   // FIXME(ostap): This relationship between orientationType and
64   // orientationAngle is temporary. The test should be able to specify
65   // the angle in addition to the orientation type.
66   switch (type) {
67     case blink::WebScreenOrientationLandscapePrimary:
68       angle = 90;
69       break;
70     case blink::WebScreenOrientationLandscapeSecondary:
71       angle = 270;
72       break;
73     case blink::WebScreenOrientationPortraitSecondary:
74       angle = 180;
75       break;
76     default:
77       angle = 0;
78   }
79   return angle;
80 }
81
82 bool MockScreenOrientationClient::IsOrientationAllowedByCurrentLock(
83     blink::WebScreenOrientationType orientation) {
84   if (current_lock_ == blink::WebScreenOrientationLockDefault ||
85       current_lock_ == blink::WebScreenOrientationLockAny) {
86     return true;
87   }
88
89   switch (orientation) {
90     case blink::WebScreenOrientationPortraitPrimary:
91       return current_lock_ == blink::WebScreenOrientationLockPortraitPrimary ||
92              current_lock_ == blink::WebScreenOrientationLockPortrait;
93     case blink::WebScreenOrientationPortraitSecondary:
94       return current_lock_ ==
95                  blink::WebScreenOrientationLockPortraitSecondary ||
96              current_lock_ == blink::WebScreenOrientationLockPortrait;
97     case blink::WebScreenOrientationLandscapePrimary:
98       return current_lock_ == blink::WebScreenOrientationLockLandscapePrimary ||
99              current_lock_ == blink::WebScreenOrientationLockLandscape;
100     case blink::WebScreenOrientationLandscapeSecondary:
101       return current_lock_ ==
102                  blink::WebScreenOrientationLockLandscapeSecondary ||
103              current_lock_ == blink::WebScreenOrientationLockLandscape;
104     default:
105       return false;
106   }
107 }
108
109 void MockScreenOrientationClient::lockOrientation(
110     blink::WebScreenOrientationLockType orientation,
111     blink::WebLockOrientationCallback* callback) {
112   base::MessageLoop::current()->PostTask(
113       FROM_HERE,
114       base::Bind(&MockScreenOrientationClient::UpdateLockSync,
115                  base::Unretained(this),
116                  orientation,
117                  callback));
118 }
119
120 void MockScreenOrientationClient::unlockOrientation() {
121   base::MessageLoop::current()->PostTask(
122       FROM_HERE,
123       base::Bind(&MockScreenOrientationClient::ResetLockSync,
124                  base::Unretained(this)));
125 }
126
127 void MockScreenOrientationClient::UpdateLockSync(
128     blink::WebScreenOrientationLockType lock,
129     blink::WebLockOrientationCallback* callback) {
130   DCHECK(lock != blink::WebScreenOrientationLockDefault);
131   current_lock_ = lock;
132   if (!IsOrientationAllowedByCurrentLock(current_orientation_))
133     UpdateScreenOrientation(SuitableOrientationForCurrentLock());
134   callback->onSuccess();
135   delete callback;
136 }
137
138 void MockScreenOrientationClient::ResetLockSync() {
139   bool will_screen_orientation_need_updating =
140       !IsOrientationAllowedByCurrentLock(device_orientation_);
141   current_lock_ = blink::WebScreenOrientationLockDefault;
142   if (will_screen_orientation_need_updating)
143     UpdateScreenOrientation(device_orientation_);
144 }
145
146 blink::WebScreenOrientationType
147 MockScreenOrientationClient::SuitableOrientationForCurrentLock() {
148   switch (current_lock_) {
149     case blink::WebScreenOrientationLockPortraitSecondary:
150       return blink::WebScreenOrientationPortraitSecondary;
151     case blink::WebScreenOrientationLockLandscapePrimary:
152     case blink::WebScreenOrientationLockLandscape:
153       return blink::WebScreenOrientationLandscapePrimary;
154     case blink::WebScreenOrientationLockLandscapeSecondary:
155       return blink::WebScreenOrientationLandscapePrimary;
156     default:
157       return blink::WebScreenOrientationPortraitPrimary;
158   }
159 }
160
161 }  // namespace content