Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(792)

Side by Side Diff: remoting/host/resizing_host_observer_unittest.cc

Issue 2038183002: Refactor ResizingHostObserver tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Replace {} with std::vector<ScreenResolution>() Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "remoting/host/resizing_host_observer.h" 5 #include "remoting/host/resizing_host_observer.h"
6 6
7 #include <list> 7 #include <list>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
(...skipping 22 matching lines...) Expand all
33 33
34 const int kDefaultDPI = 96; 34 const int kDefaultDPI = 96;
35 35
36 ScreenResolution MakeResolution(int width, int height) { 36 ScreenResolution MakeResolution(int width, int height) {
37 return ScreenResolution(webrtc::DesktopSize(width, height), 37 return ScreenResolution(webrtc::DesktopSize(width, height),
38 webrtc::DesktopVector(kDefaultDPI, kDefaultDPI)); 38 webrtc::DesktopVector(kDefaultDPI, kDefaultDPI));
39 } 39 }
40 40
41 class FakeDesktopResizer : public DesktopResizer { 41 class FakeDesktopResizer : public DesktopResizer {
42 public: 42 public:
43 FakeDesktopResizer(const ScreenResolution& initial_resolution, 43 struct CallCounts {
44 bool exact_size_supported, 44 int set_resolution = 0;
45 const ScreenResolution* supported_resolutions, 45 int restore_resolution = 0;
46 int num_supported_resolutions, 46 };
47 int* restore_resolution_call_count) 47
48 : initial_resolution_(initial_resolution), 48 FakeDesktopResizer(bool exact_size_supported,
49 current_resolution_(initial_resolution), 49 std::vector<ScreenResolution> supported_resolutions,
50 exact_size_supported_(exact_size_supported), 50 ScreenResolution* current_resolution,
51 set_resolution_call_count_(0), 51 CallCounts* call_counts)
52 restore_resolution_call_count_(restore_resolution_call_count) { 52 : exact_size_supported_(exact_size_supported),
53 for (int i = 0; i < num_supported_resolutions; ++i) { 53 initial_resolution_(*current_resolution),
54 supported_resolutions_.push_back(supported_resolutions[i]); 54 current_resolution_(current_resolution),
55 } 55 supported_resolutions_(std::move(supported_resolutions)),
56 call_counts_(call_counts) {
56 } 57 }
57 58
58 ~FakeDesktopResizer() override { 59 ~FakeDesktopResizer() override {
59 EXPECT_EQ(initial_resolution_, GetCurrentResolution()); 60 EXPECT_EQ(initial_resolution_, GetCurrentResolution());
60 } 61 }
61 62
62 int set_resolution_call_count() { return set_resolution_call_count_; }
63
64 // remoting::DesktopResizer interface 63 // remoting::DesktopResizer interface
65 ScreenResolution GetCurrentResolution() override { 64 ScreenResolution GetCurrentResolution() override {
66 return current_resolution_; 65 return *current_resolution_;
67 } 66 }
68 std::list<ScreenResolution> GetSupportedResolutions( 67 std::list<ScreenResolution> GetSupportedResolutions(
69 const ScreenResolution& preferred) override { 68 const ScreenResolution& preferred) override {
70 std::list<ScreenResolution> result = supported_resolutions_; 69 std::list<ScreenResolution> result(supported_resolutions_.begin(),
70 supported_resolutions_.end());
71 if (exact_size_supported_) { 71 if (exact_size_supported_) {
72 result.push_back(preferred); 72 result.push_back(preferred);
73 } 73 }
74 return result; 74 return result;
75 } 75 }
76 void SetResolution(const ScreenResolution& resolution) override { 76 void SetResolution(const ScreenResolution& resolution) override {
77 current_resolution_ = resolution; 77 *current_resolution_ = resolution;
78 ++set_resolution_call_count_; 78 ++call_counts_->set_resolution;
79 } 79 }
80 void RestoreResolution(const ScreenResolution& resolution) override { 80 void RestoreResolution(const ScreenResolution& resolution) override {
81 current_resolution_ = resolution; 81 *current_resolution_ = resolution;
82 if (restore_resolution_call_count_) 82 ++call_counts_->restore_resolution;
83 ++(*restore_resolution_call_count_);
84 } 83 }
85 84
86 private: 85 private:
86 bool exact_size_supported_;
87 ScreenResolution initial_resolution_; 87 ScreenResolution initial_resolution_;
88 ScreenResolution current_resolution_; 88 ScreenResolution *current_resolution_;
89 bool exact_size_supported_; 89 std::vector<ScreenResolution> supported_resolutions_;
90 std::list<ScreenResolution> supported_resolutions_; 90 CallCounts* call_counts_;
91
92 int set_resolution_call_count_;
93 int* restore_resolution_call_count_;
94 }; 91 };
95 92
96 class ResizingHostObserverTest : public testing::Test { 93 class ResizingHostObserverTest : public testing::Test {
97 public: 94 public:
98 ResizingHostObserverTest() 95 ResizingHostObserverTest()
99 : desktop_resizer_(nullptr), 96 : now_(base::Time::Now()) {
100 now_(base::Time::Now()) {
101 } 97 }
102 98
103 // This needs to be public because the derived test-case class needs to 99 // This needs to be public because the derived test-case class needs to
104 // pass it to Bind, which fails if it's protected. 100 // pass it to Bind, which fails if it's protected.
105 base::Time GetTime() { 101 base::Time GetTime() {
106 return now_; 102 return now_;
107 } 103 }
108 104
109 protected: 105 protected:
110 void SetDesktopResizer(std::unique_ptr<FakeDesktopResizer> desktop_resizer) { 106 void InitDesktopResizer(const ScreenResolution& initial_resolution,
111 CHECK(!desktop_resizer_) << "Call SetDeskopResizer once per test"; 107 bool exact_size_supported,
112 desktop_resizer_ = desktop_resizer.get(); 108 std::vector<ScreenResolution> supported_resolutions) {
113 109 current_resolution_ = initial_resolution;
114 resizing_host_observer_.reset( 110 call_counts_ = FakeDesktopResizer::CallCounts();
115 new ResizingHostObserver(std::move(desktop_resizer))); 111 resizing_host_observer_ = base::MakeUnique<ResizingHostObserver>(
112 base::MakeUnique<FakeDesktopResizer>(exact_size_supported,
113 std::move(supported_resolutions),
114 &current_resolution_,
115 &call_counts_));
116 resizing_host_observer_->SetNowFunctionForTesting( 116 resizing_host_observer_->SetNowFunctionForTesting(
117 base::Bind(&ResizingHostObserverTest::GetTimeAndIncrement, 117 base::Bind(&ResizingHostObserverTest::GetTimeAndIncrement,
118 base::Unretained(this))); 118 base::Unretained(this)));
119 } 119 }
120 120
121 ScreenResolution GetBestResolution(const ScreenResolution& client_size) { 121 ScreenResolution GetBestResolution(const ScreenResolution& client_size) {
122 resizing_host_observer_->SetScreenResolution(client_size); 122 resizing_host_observer_->SetScreenResolution(client_size);
123 return desktop_resizer_->GetCurrentResolution(); 123 return current_resolution_;
124 } 124 }
125 125
126 void VerifySizes(const ScreenResolution* client_sizes, 126 void VerifySizes(const std::vector<ScreenResolution>& client_sizes,
127 const ScreenResolution* expected_sizes, 127 const std::vector<ScreenResolution>& expected_sizes) {
128 int number_of_sizes) { 128 ASSERT_EQ(client_sizes.size(), expected_sizes.size())
129 for (int i = 0; i < number_of_sizes; ++i) { 129 << "Client and expected vectors must have the same length";
130 ScreenResolution best_size = GetBestResolution(client_sizes[i]); 130 for (auto client = client_sizes.begin(), expected = expected_sizes.begin();
131 EXPECT_EQ(expected_sizes[i], best_size) 131 client != client_sizes.end() && expected != expected_sizes.end();
132 << "Input resolution = " << client_sizes[i]; 132 ++client, ++expected) {
133 ScreenResolution best_size = GetBestResolution(*client);
134 EXPECT_EQ(*expected, best_size) << "Input resolution = " << *client;
133 } 135 }
134 } 136 }
135 137
136 base::Time GetTimeAndIncrement() { 138 base::Time GetTimeAndIncrement() {
137 base::Time result = now_; 139 base::Time result = now_;
138 now_ += base::TimeDelta::FromSeconds(1); 140 now_ += base::TimeDelta::FromSeconds(1);
139 return result; 141 return result;
140 } 142 }
141 143
144 ScreenResolution current_resolution_;
145 FakeDesktopResizer::CallCounts call_counts_;
142 std::unique_ptr<ResizingHostObserver> resizing_host_observer_; 146 std::unique_ptr<ResizingHostObserver> resizing_host_observer_;
143 FakeDesktopResizer* desktop_resizer_;
144 base::Time now_; 147 base::Time now_;
145 }; 148 };
146 149
147 // Check that the resolution isn't restored if it wasn't changed by this class. 150 // Check that the resolution isn't restored if it wasn't changed by this class.
148 TEST_F(ResizingHostObserverTest, NoRestoreResolution) { 151 TEST_F(ResizingHostObserverTest, NoRestoreResolution) {
149 int restore_resolution_call_count = 0; 152 InitDesktopResizer(MakeResolution(640, 480), false,
150 ScreenResolution initial = MakeResolution(640, 480); 153 std::vector<ScreenResolution>());
151 std::unique_ptr<FakeDesktopResizer> desktop_resizer(new FakeDesktopResizer(
152 initial, false, nullptr, 0, &restore_resolution_call_count));
153 SetDesktopResizer(std::move(desktop_resizer));
154 VerifySizes(nullptr, nullptr, 0);
155 resizing_host_observer_.reset(); 154 resizing_host_observer_.reset();
156 EXPECT_EQ(0, restore_resolution_call_count); 155 EXPECT_EQ(0, call_counts_.restore_resolution);
157 } 156 }
158 157
159 // Check that the host is not resized if GetSupportedSizes returns an empty 158 // Check that the host is not resized if GetSupportedSizes returns an empty
160 // list (even if GetCurrentSize is supported). 159 // list (even if GetCurrentSize is supported).
161 TEST_F(ResizingHostObserverTest, EmptyGetSupportedSizes) { 160 TEST_F(ResizingHostObserverTest, EmptyGetSupportedSizes) {
162 int restore_resolution_call_count = 0;
163 ScreenResolution initial = MakeResolution(640, 480); 161 ScreenResolution initial = MakeResolution(640, 480);
164 std::unique_ptr<FakeDesktopResizer> desktop_resizer(new FakeDesktopResizer( 162 InitDesktopResizer(initial, false, std::vector<ScreenResolution>());
165 initial, false, nullptr, 0, &restore_resolution_call_count)); 163 VerifySizes({MakeResolution(200, 100), MakeResolution(100, 200)},
166 SetDesktopResizer(std::move(desktop_resizer)); 164 {initial, initial});
167
168 ScreenResolution client_sizes[] = { MakeResolution(200, 100),
169 MakeResolution(100, 200) };
170 ScreenResolution expected_sizes[] = { initial, initial };
171 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes));
172
173 resizing_host_observer_.reset(); 165 resizing_host_observer_.reset();
174 EXPECT_EQ(0, restore_resolution_call_count); 166 EXPECT_EQ(0, call_counts_.set_resolution);
167 EXPECT_EQ(0, call_counts_.restore_resolution);
175 } 168 }
176 169
177 // Check that if the implementation supports exact size matching, it is used. 170 // Check that if the implementation supports exact size matching, it is used.
178 TEST_F(ResizingHostObserverTest, SelectExactSize) { 171 TEST_F(ResizingHostObserverTest, SelectExactSize) {
179 int restore_resolution_call_count = 0; 172 InitDesktopResizer(MakeResolution(640, 480), true,
180 std::unique_ptr<FakeDesktopResizer> desktop_resizer( 173 std::vector<ScreenResolution>());
181 new FakeDesktopResizer(MakeResolution(640, 480), true, nullptr, 0, 174 std::vector<ScreenResolution> client_sizes = {MakeResolution(200, 100),
182 &restore_resolution_call_count)); 175 MakeResolution(100, 200),
183 SetDesktopResizer(std::move(desktop_resizer)); 176 MakeResolution(640, 480),
184 177 MakeResolution(480, 640),
185 ScreenResolution client_sizes[] = { MakeResolution(200, 100), 178 MakeResolution(1280, 1024)};
186 MakeResolution(100, 200), 179 VerifySizes(client_sizes, client_sizes);
187 MakeResolution(640, 480),
188 MakeResolution(480, 640),
189 MakeResolution(1280, 1024) };
190 VerifySizes(client_sizes, client_sizes, arraysize(client_sizes));
191 resizing_host_observer_.reset(); 180 resizing_host_observer_.reset();
192 EXPECT_EQ(1, restore_resolution_call_count); 181 EXPECT_EQ(1, call_counts_.restore_resolution);
193 } 182 }
194 183
195 // Check that if the implementation supports a size that is no larger than 184 // Check that if the implementation supports a size that is no larger than
196 // the requested size, then the largest such size is used. 185 // the requested size, then the largest such size is used.
197 TEST_F(ResizingHostObserverTest, SelectBestSmallerSize) { 186 TEST_F(ResizingHostObserverTest, SelectBestSmallerSize) {
198 ScreenResolution supported_sizes[] = { MakeResolution(639, 479), 187 std::vector<ScreenResolution> supported_sizes = {MakeResolution(639, 479),
199 MakeResolution(640, 480) }; 188 MakeResolution(640, 480)};
200 std::unique_ptr<FakeDesktopResizer> desktop_resizer( 189 InitDesktopResizer(MakeResolution(640, 480), false, supported_sizes);
201 new FakeDesktopResizer(MakeResolution(640, 480), false, supported_sizes, 190 VerifySizes({MakeResolution(639, 479),
202 arraysize(supported_sizes), nullptr)); 191 MakeResolution(640, 480),
203 SetDesktopResizer(std::move(desktop_resizer)); 192 MakeResolution(641, 481),
204 193 MakeResolution(999, 999)},
205 ScreenResolution client_sizes[] = { MakeResolution(639, 479), 194 {supported_sizes[0],
206 MakeResolution(640, 480), 195 supported_sizes[1],
207 MakeResolution(641, 481), 196 supported_sizes[1],
208 MakeResolution(999, 999) }; 197 supported_sizes[1]});
209 ScreenResolution expected_sizes[] = { supported_sizes[0], supported_sizes[1],
210 supported_sizes[1], supported_sizes[1] };
211 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes));
212 } 198 }
213 199
214 // Check that if the implementation supports only sizes that are larger than 200 // Check that if the implementation supports only sizes that are larger than
215 // the requested size, then the one that requires the least down-scaling. 201 // the requested size, then the one that requires the least down-scaling.
216 TEST_F(ResizingHostObserverTest, SelectBestScaleFactor) { 202 TEST_F(ResizingHostObserverTest, SelectBestScaleFactor) {
217 ScreenResolution supported_sizes[] = { MakeResolution(100, 100), 203 std::vector<ScreenResolution> supported_sizes = {MakeResolution(100, 100),
218 MakeResolution(200, 100) }; 204 MakeResolution(200, 100)};
219 std::unique_ptr<FakeDesktopResizer> desktop_resizer( 205 InitDesktopResizer(MakeResolution(200, 100), false, supported_sizes);
220 new FakeDesktopResizer(MakeResolution(200, 100), false, supported_sizes, 206 VerifySizes({MakeResolution(1, 1),
221 arraysize(supported_sizes), nullptr)); 207 MakeResolution(99, 99),
222 SetDesktopResizer(std::move(desktop_resizer)); 208 MakeResolution(199, 99)},
223 209 {supported_sizes[0],
224 ScreenResolution client_sizes[] = { MakeResolution(1, 1), 210 supported_sizes[0],
225 MakeResolution(99, 99), 211 supported_sizes[1]});
226 MakeResolution(199, 99) };
227 ScreenResolution expected_sizes[] = { supported_sizes[0], supported_sizes[0],
228 supported_sizes[1] };
229 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes));
230 } 212 }
231 213
232 // Check that if the implementation supports two sizes that have the same 214 // Check that if the implementation supports two sizes that have the same
233 // resultant scale factor, then the widest one is selected. 215 // resultant scale factor, then the widest one is selected.
234 TEST_F(ResizingHostObserverTest, SelectWidest) { 216 TEST_F(ResizingHostObserverTest, SelectWidest) {
235 ScreenResolution supported_sizes[] = { MakeResolution(640, 480), 217 std::vector<ScreenResolution> supported_sizes = {MakeResolution(640, 480),
236 MakeResolution(480, 640) }; 218 MakeResolution(480, 640)};
237 std::unique_ptr<FakeDesktopResizer> desktop_resizer( 219 InitDesktopResizer(MakeResolution(480, 640), false, supported_sizes);
238 new FakeDesktopResizer(MakeResolution(480, 640), false, supported_sizes, 220 VerifySizes({MakeResolution(100, 100),
239 arraysize(supported_sizes), nullptr)); 221 MakeResolution(480, 480),
240 SetDesktopResizer(std::move(desktop_resizer)); 222 MakeResolution(500, 500),
241 223 MakeResolution(640, 640),
242 ScreenResolution client_sizes[] = { MakeResolution(100, 100), 224 MakeResolution(1000, 1000)},
243 MakeResolution(480, 480), 225 {supported_sizes[0],
244 MakeResolution(500, 500), 226 supported_sizes[0],
245 MakeResolution(640, 640), 227 supported_sizes[0],
246 MakeResolution(1000, 1000) }; 228 supported_sizes[0],
247 ScreenResolution expected_sizes[] = { supported_sizes[0], supported_sizes[0], 229 supported_sizes[0]});
248 supported_sizes[0], supported_sizes[0],
249 supported_sizes[0] };
250 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes));
251 } 230 }
252 231
253 // Check that if the best match for the client size doesn't change, then we 232 // Check that if the best match for the client size doesn't change, then we
254 // don't call SetSize. 233 // don't call SetSize.
255 TEST_F(ResizingHostObserverTest, NoSetSizeForSameSize) { 234 TEST_F(ResizingHostObserverTest, NoSetSizeForSameSize) {
256 ScreenResolution supported_sizes[] = { MakeResolution(640, 480), 235 std::vector<ScreenResolution> supported_sizes = {MakeResolution(640, 480),
257 MakeResolution(480, 640) }; 236 MakeResolution(480, 640)};
258 SetDesktopResizer(base::WrapUnique( 237 InitDesktopResizer(MakeResolution(480, 640), false, supported_sizes);
259 new FakeDesktopResizer(MakeResolution(480, 640), false, supported_sizes, 238 VerifySizes({MakeResolution(640, 640),
260 arraysize(supported_sizes), nullptr))); 239 MakeResolution(1024, 768),
261 240 MakeResolution(640, 480)},
262 ScreenResolution client_sizes[] = { MakeResolution(640, 640), 241 {supported_sizes[0],
263 MakeResolution(1024, 768), 242 supported_sizes[0],
264 MakeResolution(640, 480) }; 243 supported_sizes[0]});
265 ScreenResolution expected_sizes[] = { MakeResolution(640, 480), 244 EXPECT_EQ(1, call_counts_.set_resolution);
266 MakeResolution(640, 480),
267 MakeResolution(640, 480) };
268 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes));
269 EXPECT_EQ(desktop_resizer_->set_resolution_call_count(), 1);
270 } 245 }
271 246
272 // Check that desktop resizes are rate-limited, and that if multiple resize 247 // Check that desktop resizes are rate-limited, and that if multiple resize
273 // requests are received in the time-out period, the most recent is respected. 248 // requests are received in the time-out period, the most recent is respected.
274 TEST_F(ResizingHostObserverTest, RateLimited) { 249 TEST_F(ResizingHostObserverTest, RateLimited) {
275 SetDesktopResizer(base::WrapUnique(new FakeDesktopResizer( 250 InitDesktopResizer(MakeResolution(640, 480), true,
276 MakeResolution(640, 480), true, nullptr, 0, nullptr))); 251 std::vector<ScreenResolution>());
277 resizing_host_observer_->SetNowFunctionForTesting( 252 resizing_host_observer_->SetNowFunctionForTesting(
278 base::Bind(&ResizingHostObserverTest::GetTime, base::Unretained(this))); 253 base::Bind(&ResizingHostObserverTest::GetTime, base::Unretained(this)));
279 254
280 base::MessageLoop message_loop; 255 base::MessageLoop message_loop;
281 base::RunLoop run_loop; 256 base::RunLoop run_loop;
282 257
283 EXPECT_EQ(GetBestResolution(MakeResolution(100, 100)), 258 EXPECT_EQ(MakeResolution(100, 100),
284 MakeResolution(100, 100)); 259 GetBestResolution(MakeResolution(100, 100)));
285 now_ += base::TimeDelta::FromMilliseconds(900); 260 now_ += base::TimeDelta::FromMilliseconds(900);
286 EXPECT_EQ(GetBestResolution(MakeResolution(200, 200)), 261 EXPECT_EQ(MakeResolution(100, 100),
287 MakeResolution(100, 100)); 262 GetBestResolution(MakeResolution(200, 200)));
288 now_ += base::TimeDelta::FromMilliseconds(99); 263 now_ += base::TimeDelta::FromMilliseconds(99);
289 EXPECT_EQ(GetBestResolution(MakeResolution(300, 300)), 264 EXPECT_EQ(MakeResolution(100, 100),
290 MakeResolution(100, 100)); 265 GetBestResolution(MakeResolution(300, 300)));
291 now_ += base::TimeDelta::FromMilliseconds(1); 266 now_ += base::TimeDelta::FromMilliseconds(1);
292 267
293 // Due to the kMinimumResizeIntervalMs constant in resizing_host_observer.cc, 268 // Due to the kMinimumResizeIntervalMs constant in resizing_host_observer.cc,
294 // We need to wait a total of 1000ms for the final resize to be processed. 269 // We need to wait a total of 1000ms for the final resize to be processed.
295 // Since it was queued 900 + 99 ms after the first, we need to wait an 270 // Since it was queued 900 + 99 ms after the first, we need to wait an
296 // additional 1ms. However, since RunLoop is not guaranteed to process tasks 271 // additional 1ms. However, since RunLoop is not guaranteed to process tasks
297 // with the same due time in FIFO order, wait an additional 1ms for safety. 272 // with the same due time in FIFO order, wait an additional 1ms for safety.
298 message_loop.PostDelayedTask( 273 message_loop.PostDelayedTask(
299 FROM_HERE, 274 FROM_HERE,
300 run_loop.QuitClosure(), 275 run_loop.QuitClosure(),
301 base::TimeDelta::FromMilliseconds(2)); 276 base::TimeDelta::FromMilliseconds(2));
302 run_loop.Run(); 277 run_loop.Run();
303 278
304 // If the QuitClosure fired before the final resize, it's a test failure. 279 // If the QuitClosure fired before the final resize, it's a test failure.
305 EXPECT_EQ(desktop_resizer_->GetCurrentResolution(), MakeResolution(300, 300)); 280 EXPECT_EQ(MakeResolution(300, 300), current_resolution_);
306 } 281 }
307 282
308 } // namespace remoting 283 } // namespace remoting
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698