OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <list> | 5 #include <list> |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/message_loop.h" | |
11 #include "base/run_loop.h" | |
9 #include "remoting/host/desktop_resizer.h" | 12 #include "remoting/host/desktop_resizer.h" |
10 #include "remoting/host/resizing_host_observer.h" | 13 #include "remoting/host/resizing_host_observer.h" |
11 #include "remoting/host/screen_resolution.h" | 14 #include "remoting/host/screen_resolution.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "third_party/skia/include/core/SkSize.h" | 16 #include "third_party/skia/include/core/SkSize.h" |
14 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" | 17 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" |
15 | 18 |
16 std::ostream& operator<<(std::ostream& os, const SkISize& size) { | 19 std::ostream& operator<<(std::ostream& os, const SkISize& size) { |
17 return os << size.width() << "x" << size.height(); | 20 return os << size.width() << "x" << size.height(); |
18 } | 21 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 | 65 |
63 private: | 66 private: |
64 SkISize initial_size_; | 67 SkISize initial_size_; |
65 SkISize current_size_; | 68 SkISize current_size_; |
66 bool exact_size_supported_; | 69 bool exact_size_supported_; |
67 std::list<SkISize> supported_sizes_; | 70 std::list<SkISize> supported_sizes_; |
68 | 71 |
69 int set_size_call_count_; | 72 int set_size_call_count_; |
70 }; | 73 }; |
71 | 74 |
75 class AutoIncrementNow : public base::RefCountedThreadSafe<AutoIncrementNow> { | |
76 public: | |
77 AutoIncrementNow() : now_(base::Time::Now()) { | |
78 } | |
79 virtual ~AutoIncrementNow() { } | |
80 | |
81 base::Time Now() { | |
82 base::Time result = now_; | |
83 now_ += base::TimeDelta::FromSeconds(10); | |
84 return result; | |
85 } | |
86 | |
87 private: | |
88 base::Time now_; | |
89 }; | |
90 | |
72 class ResizingHostObserverTest : public testing::Test { | 91 class ResizingHostObserverTest : public testing::Test { |
73 public: | 92 public: |
74 ResizingHostObserverTest() : desktop_resizer_(NULL) { | 93 ResizingHostObserverTest() |
94 : desktop_resizer_(NULL), | |
95 now_function_(new AutoIncrementNow) { | |
75 } | 96 } |
76 | 97 |
77 void SetDesktopResizer(scoped_ptr<FakeDesktopResizer> desktop_resizer) { | 98 void SetDesktopResizer(scoped_ptr<FakeDesktopResizer> desktop_resizer) { |
78 CHECK(!desktop_resizer_) << "Call SetDeskopResizer once per test"; | 99 CHECK(!desktop_resizer_) << "Call SetDeskopResizer once per test"; |
79 desktop_resizer_ = desktop_resizer.get(); | 100 desktop_resizer_ = desktop_resizer.get(); |
80 | 101 |
81 resizing_host_observer_.reset( | 102 resizing_host_observer_.reset( |
82 new ResizingHostObserver(desktop_resizer.PassAs<DesktopResizer>())); | 103 new ResizingHostObserver(desktop_resizer.PassAs<DesktopResizer>())); |
104 resizing_host_observer_->SetNowFunctionForTesting( | |
105 base::Bind(&AutoIncrementNow::Now, now_function_)); | |
alexeypa (please no reviews)
2013/06/07 19:50:25
AutoIncrementNow::Now does not need to be in a sep
Jamie
2013/06/07 20:20:14
Done.
| |
83 } | 106 } |
84 | 107 |
85 SkISize GetBestSize(const SkISize& client_size) { | 108 SkISize GetBestSize(const SkISize& client_size) { |
86 resizing_host_observer_->SetScreenResolution(ScreenResolution( | 109 resizing_host_observer_->SetScreenResolution(ScreenResolution( |
87 webrtc::DesktopSize(client_size.width(), client_size.height()), | 110 webrtc::DesktopSize(client_size.width(), client_size.height()), |
88 webrtc::DesktopVector(kDefaultDPI, kDefaultDPI))); | 111 webrtc::DesktopVector(kDefaultDPI, kDefaultDPI))); |
89 return desktop_resizer_->GetCurrentSize(); | 112 return desktop_resizer_->GetCurrentSize(); |
90 } | 113 } |
91 | 114 |
92 void VerifySizes(const SkISize* client_sizes, const SkISize* expected_sizes, | 115 void VerifySizes(const SkISize* client_sizes, const SkISize* expected_sizes, |
93 int number_of_sizes) { | 116 int number_of_sizes) { |
94 for (int i = 0; i < number_of_sizes; ++i) { | 117 for (int i = 0; i < number_of_sizes; ++i) { |
95 SkISize best_size = GetBestSize(client_sizes[i]); | 118 SkISize best_size = GetBestSize(client_sizes[i]); |
96 EXPECT_EQ(expected_sizes[i], best_size) | 119 EXPECT_EQ(expected_sizes[i], best_size) |
97 << "Input size = " << client_sizes[i]; | 120 << "Input size = " << client_sizes[i]; |
98 } | 121 } |
99 } | 122 } |
100 | 123 |
101 private: | 124 protected: |
102 scoped_ptr<ResizingHostObserver> resizing_host_observer_; | 125 scoped_ptr<ResizingHostObserver> resizing_host_observer_; |
103 FakeDesktopResizer* desktop_resizer_; | 126 FakeDesktopResizer* desktop_resizer_; |
127 scoped_refptr<AutoIncrementNow> now_function_; | |
104 }; | 128 }; |
105 | 129 |
106 // Check that the host is not resized if GetSupportedSizes returns an empty | 130 // Check that the host is not resized if GetSupportedSizes returns an empty |
107 // list (even if GetCurrentSize is supported). | 131 // list (even if GetCurrentSize is supported). |
108 TEST_F(ResizingHostObserverTest, EmptyGetSupportedSizes) { | 132 TEST_F(ResizingHostObserverTest, EmptyGetSupportedSizes) { |
109 SkISize initial = { 640, 480 }; | 133 SkISize initial = { 640, 480 }; |
110 scoped_ptr<FakeDesktopResizer> desktop_resizer( | 134 scoped_ptr<FakeDesktopResizer> desktop_resizer( |
111 new FakeDesktopResizer(initial, false, NULL, 0)); | 135 new FakeDesktopResizer(initial, false, NULL, 0)); |
112 SetDesktopResizer(desktop_resizer.Pass()); | 136 SetDesktopResizer(desktop_resizer.Pass()); |
113 | 137 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 new FakeDesktopResizer(SkISize::Make(640, 480), false, | 208 new FakeDesktopResizer(SkISize::Make(640, 480), false, |
185 supported_sizes, arraysize(supported_sizes)); | 209 supported_sizes, arraysize(supported_sizes)); |
186 SetDesktopResizer(scoped_ptr<FakeDesktopResizer>(desktop_resizer)); | 210 SetDesktopResizer(scoped_ptr<FakeDesktopResizer>(desktop_resizer)); |
187 | 211 |
188 SkISize client_sizes[] = { { 640, 640 }, { 1024, 768 }, { 640, 480 } }; | 212 SkISize client_sizes[] = { { 640, 640 }, { 1024, 768 }, { 640, 480 } }; |
189 SkISize expected_sizes[] = { { 640, 480 }, { 640, 480 }, { 640, 480 } }; | 213 SkISize expected_sizes[] = { { 640, 480 }, { 640, 480 }, { 640, 480 } }; |
190 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); | 214 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); |
191 EXPECT_EQ(desktop_resizer->set_size_call_count(), 0); | 215 EXPECT_EQ(desktop_resizer->set_size_call_count(), 0); |
192 } | 216 } |
193 | 217 |
218 class ManualIncrementNow : | |
219 public base::RefCountedThreadSafe<ManualIncrementNow> { | |
220 public: | |
221 ManualIncrementNow() : now_(base::Time::Now()) { | |
222 } | |
223 virtual ~ManualIncrementNow() { } | |
224 | |
225 base::Time Now() { | |
226 return now_; | |
227 } | |
228 | |
229 void Increment(const base::TimeDelta& delta) { | |
230 now_ += delta; | |
231 }; | |
232 | |
233 private: | |
234 base::Time now_; | |
235 }; | |
236 | |
237 // Check that desktop resizes are rate-limited, and that if multiple resize | |
238 // requests are received in the time-out period, the most recent is respected. | |
239 TEST_F(ResizingHostObserverTest, RateLimited) { | |
240 FakeDesktopResizer* desktop_resizer = | |
241 new FakeDesktopResizer(SkISize::Make(640, 480), true, NULL, 0); | |
242 SetDesktopResizer(scoped_ptr<FakeDesktopResizer>(desktop_resizer)); | |
243 scoped_refptr<ManualIncrementNow> now_function(new ManualIncrementNow); | |
244 resizing_host_observer_->SetNowFunctionForTesting( | |
245 base::Bind(&ManualIncrementNow::Now, now_function)); | |
246 | |
247 base::MessageLoop message_loop; | |
248 base::RunLoop run_loop; | |
249 | |
250 EXPECT_EQ(GetBestSize(SkISize::Make(100, 100)), SkISize::Make(100, 100)); | |
251 now_function->Increment(base::TimeDelta::FromMilliseconds(900)); | |
252 EXPECT_EQ(GetBestSize(SkISize::Make(200, 200)), SkISize::Make(100, 100)); | |
253 now_function->Increment(base::TimeDelta::FromMilliseconds(99)); | |
254 EXPECT_EQ(GetBestSize(SkISize::Make(300, 300)), SkISize::Make(100, 100)); | |
255 now_function->Increment(base::TimeDelta::FromMilliseconds(1)); | |
alexeypa (please no reviews)
2013/06/07 19:50:25
nit: remove this line.
Jamie
2013/06/07 20:20:14
It's needed, otherwise when the delayed resize hap
| |
256 | |
257 // The most recent resize was requested 999ms after the first, so it should | |
258 // be processed within 1ms. | |
alexeypa (please no reviews)
2013/06/07 19:50:25
Mention that |kDefaultMinimumResizeIntervalMs| mus
Jamie
2013/06/07 20:20:14
Done.
| |
259 message_loop.PostDelayedTask( | |
260 FROM_HERE, | |
261 run_loop.QuitClosure(), | |
262 base::TimeDelta::FromMilliseconds(1)); | |
alexeypa (please no reviews)
2013/06/07 19:50:25
nit: Make it 2ms
Jamie
2013/06/07 20:20:14
Done.
| |
263 run_loop.Run(); | |
264 | |
265 EXPECT_EQ(desktop_resizer_->GetCurrentSize(), SkISize::Make(300, 300)); | |
266 } | |
267 | |
194 } // namespace remoting | 268 } // namespace remoting |
OLD | NEW |