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