forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathambient_photo_controller_unittest.cc
421 lines (346 loc) · 14.8 KB
/
ambient_photo_controller_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/ambient/ambient_photo_controller.h"
#include <memory>
#include <utility>
#include "ash/ambient/ambient_constants.h"
#include "ash/ambient/ambient_controller.h"
#include "ash/ambient/model/ambient_backend_model.h"
#include "ash/ambient/model/ambient_backend_model_observer.h"
#include "ash/ambient/test/ambient_ash_test_base.h"
#include "ash/public/cpp/ambient/ambient_backend_controller.h"
#include "ash/public/cpp/ambient/fake_ambient_backend_controller_impl.h"
#include "ash/shell.h"
#include "base/barrier_closure.h"
#include "base/base_paths.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/hash/sha1.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/stl_util.h"
#include "base/system/sys_info.h"
#include "base/test/bind.h"
#include "base/timer/timer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/gfx/image/image_skia.h"
namespace ash {
namespace {
class MockAmbientBackendModelObserver : public AmbientBackendModelObserver {
public:
MockAmbientBackendModelObserver() = default;
~MockAmbientBackendModelObserver() override = default;
// AmbientBackendModelObserver:
MOCK_METHOD(void, OnImageAdded, (), (override));
MOCK_METHOD(void, OnImagesReady, (), (override));
};
} // namespace
class AmbientPhotoControllerTest : public AmbientAshTestBase {
public:
std::vector<int> GetSavedCacheIndices(bool backup = false) {
std::vector<int> result;
const auto& map = backup ? GetBackupCachedFiles() : GetCachedFiles();
for (auto& it : map) {
result.push_back(it.first);
}
return result;
}
const PhotoCacheEntry* GetCacheEntryAtIndex(int cache_index,
bool backup = false) {
const auto& files = backup ? GetBackupCachedFiles() : GetCachedFiles();
auto it = files.find(cache_index);
if (it == files.end())
return nullptr;
else
return &(it->second);
}
void WriteCacheDataBlocking(int cache_index,
const std::string* image = nullptr,
const std::string* details = nullptr,
const std::string* related_image = nullptr) {
base::RunLoop loop;
photo_cache()->WriteFiles(/*cache_index=*/cache_index, /*image=*/image,
/*details=*/details,
/*related_image=*/related_image,
loop.QuitClosure());
loop.Run();
}
void ScheduleFetchBackupImages() {
photo_controller()->ScheduleFetchBackupImages();
}
};
// Test that topics are downloaded when starting screen update.
TEST_F(AmbientPhotoControllerTest, ShouldStartToDownloadTopics) {
auto topics = photo_controller()->ambient_backend_model()->topics();
EXPECT_TRUE(topics.empty());
// Start to refresh images.
photo_controller()->StartScreenUpdate();
topics = photo_controller()->ambient_backend_model()->topics();
EXPECT_TRUE(topics.empty());
FastForwardToNextImage();
topics = photo_controller()->ambient_backend_model()->topics();
EXPECT_FALSE(topics.empty());
// Stop to refresh images.
photo_controller()->StopScreenUpdate();
topics = photo_controller()->ambient_backend_model()->topics();
EXPECT_TRUE(topics.empty());
}
// Test that image is downloaded when starting screen update.
TEST_F(AmbientPhotoControllerTest, ShouldStartToDownloadImages) {
auto image = photo_controller()->ambient_backend_model()->GetNextImage();
EXPECT_TRUE(image.IsNull());
// Start to refresh images.
photo_controller()->StartScreenUpdate();
FastForwardToNextImage();
image = photo_controller()->ambient_backend_model()->GetNextImage();
EXPECT_FALSE(image.IsNull());
// Stop to refresh images.
photo_controller()->StopScreenUpdate();
image = photo_controller()->ambient_backend_model()->GetNextImage();
EXPECT_TRUE(image.IsNull());
}
// Tests that photos are updated periodically when starting screen update.
TEST_F(AmbientPhotoControllerTest, ShouldUpdatePhotoPeriodically) {
PhotoWithDetails image1;
PhotoWithDetails image2;
PhotoWithDetails image3;
// Start to refresh images.
photo_controller()->StartScreenUpdate();
FastForwardToNextImage();
image1 = photo_controller()->ambient_backend_model()->GetNextImage();
EXPECT_FALSE(image1.IsNull());
EXPECT_TRUE(image2.IsNull());
FastForwardToNextImage();
image2 = photo_controller()->ambient_backend_model()->GetNextImage();
EXPECT_FALSE(image2.IsNull());
EXPECT_FALSE(image1.photo.BackedBySameObjectAs(image2.photo));
EXPECT_TRUE(image3.IsNull());
FastForwardToNextImage();
image3 = photo_controller()->ambient_backend_model()->GetNextImage();
EXPECT_FALSE(image3.IsNull());
EXPECT_FALSE(image1.photo.BackedBySameObjectAs(image3.photo));
EXPECT_FALSE(image2.photo.BackedBySameObjectAs(image3.photo));
// Stop to refresh images.
photo_controller()->StopScreenUpdate();
}
// Test that image is saved.
TEST_F(AmbientPhotoControllerTest, ShouldSaveImagesOnDisk) {
// Start to refresh images. It will download two images immediately and write
// them in |ambient_image_path|. It will also download one more image after
// fast forward. It will also download the related images and not cache them.
photo_controller()->StartScreenUpdate();
FastForwardToNextImage();
// Count number of writes to cache. There should be three cache writes during
// this ambient mode session.
auto file_paths = GetSavedCacheIndices();
EXPECT_EQ(file_paths.size(), 3u);
}
// Test that image is save and will not be deleted when stopping ambient mode.
TEST_F(AmbientPhotoControllerTest, ShouldNotDeleteImagesOnDisk) {
// Start to refresh images. It will download two images immediately and write
// them in |ambient_image_path|. It will also download one more image after
// fast forward. It will also download the related images and not cache them.
photo_controller()->StartScreenUpdate();
FastForwardToNextImage();
EXPECT_EQ(GetSavedCacheIndices().size(), 3u);
auto image = photo_controller()->ambient_backend_model()->GetNextImage();
EXPECT_FALSE(image.IsNull());
// Stop to refresh images.
photo_controller()->StopScreenUpdate();
FastForwardToNextImage();
EXPECT_EQ(GetSavedCacheIndices().size(), 3u);
image = photo_controller()->ambient_backend_model()->GetNextImage();
EXPECT_TRUE(image.IsNull());
}
// Test that image is read from disk when no more topics.
TEST_F(AmbientPhotoControllerTest, ShouldReadCacheWhenNoMoreTopics) {
FetchImage();
FastForwardToNextImage();
// Topics is empty. Will read from cache, which is empty.
auto image = photo_controller()->ambient_backend_model()->GetCurrentImage();
EXPECT_TRUE(image.IsNull());
// Save a file to check if it gets read for display.
std::string data("cached image");
WriteCacheDataBlocking(/*cache_index=*/0, &data);
// Reset variables in photo controller.
photo_controller()->StopScreenUpdate();
FetchImage();
FastForwardToNextImage();
image = photo_controller()->ambient_backend_model()->GetCurrentImage();
EXPECT_FALSE(image.IsNull());
}
// Test that will try 100 times to read image from disk when no more topics.
TEST_F(AmbientPhotoControllerTest,
ShouldTry100TimesToReadCacheWhenNoMoreTopics) {
FetchImage();
FastForwardToNextImage();
// Topics is empty. Will read from cache, which is empty.
auto image = photo_controller()->ambient_backend_model()->GetCurrentImage();
EXPECT_TRUE(image.IsNull());
// The initial file name to be read is 0. Save a file with 99.img to check
// if it gets read for display.
std::string data("cached image");
WriteCacheDataBlocking(/*cache_index=*/99, &data);
// Reset variables in photo controller.
photo_controller()->StopScreenUpdate();
FetchImage();
FastForwardToNextImage();
image = photo_controller()->ambient_backend_model()->GetCurrentImage();
EXPECT_FALSE(image.IsNull());
}
// Test that image is read from disk when image downloading failed.
TEST_F(AmbientPhotoControllerTest, ShouldReadCacheWhenImageDownloadingFailed) {
SetDownloadPhotoData("");
FetchTopics();
// Forward a little bit time. FetchTopics() will succeed. Downloading should
// fail. Will read from cache, which is empty.
task_environment()->FastForwardBy(0.2 * kTopicFetchInterval);
auto image = photo_controller()->ambient_backend_model()->GetCurrentImage();
EXPECT_TRUE(image.IsNull());
// Save a file to check if it gets read for display.
std::string data("cached image");
WriteCacheDataBlocking(/*cache_index=*/0, &data);
// Reset variables in photo controller.
photo_controller()->StopScreenUpdate();
FetchTopics();
// Forward a little bit time. FetchTopics() will succeed. Downloading should
// fail. Will read from cache.
task_environment()->FastForwardBy(0.2 * kTopicFetchInterval);
image = photo_controller()->ambient_backend_model()->GetCurrentImage();
EXPECT_FALSE(image.IsNull());
}
// Test that image is read from disk when image decoding failed.
TEST_F(AmbientPhotoControllerTest, ShouldReadCacheWhenImageDecodingFailed) {
SetDecodePhotoImage(gfx::ImageSkia());
FetchTopics();
// Forward a little bit time. FetchTopics() will succeed.
// Downloading succeed and save the data to disk.
// First decoding should fail. Will read from cache, and then succeed.
task_environment()->FastForwardBy(0.2 * kTopicFetchInterval);
auto image = photo_controller()->ambient_backend_model()->GetNextImage();
EXPECT_FALSE(image.IsNull());
}
// Test that image will refresh when have more topics.
TEST_F(AmbientPhotoControllerTest, ShouldResumWhenHaveMoreTopics) {
FetchImage();
FastForwardToNextImage();
// Topics is empty. Will read from cache, which is empty.
auto image = photo_controller()->ambient_backend_model()->GetNextImage();
EXPECT_TRUE(image.IsNull());
FetchTopics();
// Forward a little bit time. FetchTopics() will succeed and refresh image.
task_environment()->FastForwardBy(0.2 * kTopicFetchInterval);
image = photo_controller()->ambient_backend_model()->GetNextImage();
EXPECT_FALSE(image.IsNull());
}
TEST_F(AmbientPhotoControllerTest, ShouldDownloadBackupImagesWhenScheduled) {
std::string expected_data = "backup data";
SetBackupDownloadPhotoData(expected_data);
ScheduleFetchBackupImages();
EXPECT_TRUE(
photo_controller()->backup_photo_refresh_timer_for_testing().IsRunning());
// Timer is running but download has not started yet.
EXPECT_TRUE(GetSavedCacheIndices(/*backup=*/true).empty());
task_environment()->FastForwardBy(kBackupPhotoRefreshDelay);
// Timer should have stopped.
EXPECT_FALSE(
photo_controller()->backup_photo_refresh_timer_for_testing().IsRunning());
// Should have been two cache writes to backup data.
const auto& backup_data = GetBackupCachedFiles();
EXPECT_EQ(backup_data.size(), 2u);
EXPECT_TRUE(base::Contains(backup_data, 0));
EXPECT_TRUE(base::Contains(backup_data, 1));
for (const auto& i : backup_data) {
EXPECT_EQ(*(i.second.image), expected_data);
EXPECT_FALSE(i.second.details);
EXPECT_FALSE(i.second.related_image);
}
}
TEST_F(AmbientPhotoControllerTest, ShouldResetTimerWhenBackupImagesFail) {
ScheduleFetchBackupImages();
EXPECT_TRUE(
photo_controller()->backup_photo_refresh_timer_for_testing().IsRunning());
// Simulate an error in DownloadToFile.
ClearDownloadPhotoData();
task_environment()->FastForwardBy(kBackupPhotoRefreshDelay);
EXPECT_TRUE(GetBackupCachedFiles().empty());
// Timer should have restarted.
EXPECT_TRUE(
photo_controller()->backup_photo_refresh_timer_for_testing().IsRunning());
}
TEST_F(AmbientPhotoControllerTest,
ShouldStartDownloadBackupImagesOnAmbientModeStart) {
ScheduleFetchBackupImages();
EXPECT_TRUE(
photo_controller()->backup_photo_refresh_timer_for_testing().IsRunning());
SetBackupDownloadPhotoData("image data");
photo_controller()->StartScreenUpdate();
// Download should have started immediately.
EXPECT_FALSE(
photo_controller()->backup_photo_refresh_timer_for_testing().IsRunning());
task_environment()->RunUntilIdle();
// Download has triggered and backup cache directory is created. Should be
// two cache writes to backup cache.
const auto& backup_data = GetBackupCachedFiles();
EXPECT_EQ(backup_data.size(), 2u);
EXPECT_TRUE(base::Contains(backup_data, 0));
EXPECT_TRUE(base::Contains(backup_data, 1));
for (const auto& i : backup_data) {
EXPECT_EQ(*(i.second.image), "image data");
EXPECT_FALSE(i.second.details);
EXPECT_FALSE(i.second.related_image);
}
}
TEST_F(AmbientPhotoControllerTest, ShouldNotLoadDuplicateImages) {
testing::NiceMock<MockAmbientBackendModelObserver> mock_backend_observer;
base::ScopedObservation<AmbientBackendModel, AmbientBackendModelObserver>
scoped_observation{&mock_backend_observer};
scoped_observation.Observe(photo_controller()->ambient_backend_model());
// All images downloaded will be identical.
SetDownloadPhotoData("image data");
photo_controller()->StartScreenUpdate();
// Run the clock so the first photo is loaded.
FastForwardTiny();
// Should contain hash of downloaded data.
EXPECT_TRUE(photo_controller()->ambient_backend_model()->IsHashDuplicate(
base::SHA1HashString("image data")));
// Only one image should have been loaded.
EXPECT_FALSE(photo_controller()->ambient_backend_model()->ImagesReady());
// Now expect a call because second image is loaded.
EXPECT_CALL(mock_backend_observer, OnImagesReady).Times(1);
SetDownloadPhotoData("image data 2");
FastForwardToNextImage();
// Second image should have been loaded.
EXPECT_TRUE(photo_controller()->ambient_backend_model()->IsHashDuplicate(
base::SHA1HashString("image data 2")));
EXPECT_TRUE(photo_controller()->ambient_backend_model()->ImagesReady());
}
TEST_F(AmbientPhotoControllerTest, ShouldStartToRefreshWeather) {
auto* model = photo_controller()->ambient_backend_model();
EXPECT_FALSE(model->show_celsius());
EXPECT_TRUE(model->weather_condition_icon().isNull());
WeatherInfo info;
info.show_celsius = true;
info.condition_icon_url = "https://fake-icon-url";
info.temp_f = 70.0f;
backend_controller()->SetWeatherInfo(info);
// Start to refresh weather as screen update starts.
photo_controller()->StartScreenUpdate();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(model->show_celsius());
EXPECT_FALSE(model->weather_condition_icon().isNull());
EXPECT_GT(info.temp_f, 0);
// Refresh weather again after time passes.
info.show_celsius = false;
info.temp_f = -70.0f;
backend_controller()->SetWeatherInfo(info);
FastForwardToRefreshWeather();
EXPECT_FALSE(model->show_celsius());
EXPECT_LT(info.temp_f, 0);
}
} // namespace ash