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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
ash / public / cpp / holding_space / holding_space_model_unittest.cc [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/public/cpp/holding_space/holding_space_model.h"
#include <memory>
#include <optional>
#include <vector>
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/holding_space/holding_space_constants.h"
#include "ash/public/cpp/holding_space/holding_space_file.h"
#include "ash/public/cpp/holding_space/holding_space_image.h"
#include "ash/public/cpp/holding_space/holding_space_item.h"
#include "ash/public/cpp/holding_space/holding_space_item_updated_fields.h"
#include "ash/public/cpp/holding_space/holding_space_model_observer.h"
#include "ash/public/cpp/holding_space/holding_space_progress.h"
#include "ash/public/cpp/holding_space/holding_space_section.h"
#include "ash/public/cpp/holding_space/holding_space_util.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/strings/strcat.h"
#include "base/test/scoped_feature_list.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/color/color_id.h"
#include "ui/gfx/paint_vector_icon.h"
namespace ash {
namespace {
// Aliases ---------------------------------------------------------------------
using testing::VariantWith;
// Helpers ---------------------------------------------------------------------
HoldingSpaceItem::InProgressCommand CreateInProgressCommand(
HoldingSpaceCommandId command_id) {
return HoldingSpaceItem::InProgressCommand(command_id, /*label_id=*/-1,
&gfx::kNoneIcon,
/*handler=*/base::DoNothing());
}
std::unique_ptr<HoldingSpaceImage> CreateFakeHoldingSpaceImage(
HoldingSpaceItem::Type type,
const base::FilePath& file_path) {
return std::make_unique<HoldingSpaceImage>(
holding_space_util::GetMaxImageSizeForType(type), file_path,
/*async_bitmap_resolver=*/base::DoNothing());
}
// ScopedModelObservation ------------------------------------------------------
// A class which observes a `HoldingSpaceModel` within its lifetime. Note that
// this class must not outlive the `model` that it observes.
class ScopedModelObservation : public HoldingSpaceModelObserver {
public:
explicit ScopedModelObservation(HoldingSpaceModel* model) {
observation_.Observe(model);
}
ScopedModelObservation(const ScopedModelObservation&) = delete;
ScopedModelObservation& operator=(const ScopedModelObservation&) = delete;
~ScopedModelObservation() override = default;
// Returns the last updated fields for which `OnHoldingSpaceItemUpdated()`
// was called, clearing the cached value.
HoldingSpaceItemUpdatedFields TakeLastUpdatedFields() {
HoldingSpaceItemUpdatedFields result = last_updated_fields_;
last_updated_fields_ = HoldingSpaceItemUpdatedFields();
return result;
}
// Returns the last `HoldingSpaceItem` for which `OnHoldingSpaceItemUpdated()`
// was called, clearing the cached value.
const HoldingSpaceItem* TakeLastUpdatedItem() {
const HoldingSpaceItem* result = last_updated_item_;
last_updated_item_ = nullptr;
return result;
}
// Returns the count of times for which `OnHoldingSpaceItemUpdated()` was
// called, clearing the cached value.
int TakeUpdatedItemCount() {
const int result = updated_item_count_;
updated_item_count_ = 0;
return result;
}
// Returns the id's of `HoldingSpaceItem`s for which
// `OnHoldingSpaceItemRemoved()` was called. Also clears the cached values.
std::vector<std::string> TakeRemovedItems() {
std::vector<std::string> result;
result.swap(removed_item_ids_);
return result;
}
private:
// HoldingSpaceModel::Observer:
void OnHoldingSpaceItemUpdated(
const HoldingSpaceItem* item,
const HoldingSpaceItemUpdatedFields& updated_fields) override {
last_updated_item_ = item;
last_updated_fields_ = updated_fields;
++updated_item_count_;
}
void OnHoldingSpaceItemsRemoved(
const std::vector<const HoldingSpaceItem*>& items) override {
for (const HoldingSpaceItem* item : items)
removed_item_ids_.push_back(item->id());
}
// The last `HoldingSpaceItem` for which `OnHoldingSpaceItemUpdated()` was
// called. May be `nullptr` prior to an update event or following a call to
// `TakeLastUpdatedItem()`.
raw_ptr<const HoldingSpaceItem> last_updated_item_ = nullptr;
// The last updated fields for which `OnHoldingSpaceItemUpdated()` was called.
// May be empty prior to an update event or following a call to
// `TakeLastUpdatedFields()`.
HoldingSpaceItemUpdatedFields last_updated_fields_;
// The count of times for which `OnHoldingSpaceItemUpdated()` was called. May
// be reset following a call to `TakeUpdatedItemCount()`.
int updated_item_count_ = 0;
// A vector of item id's that have been removed.
std::vector<std::string> removed_item_ids_;
base::ScopedObservation<HoldingSpaceModel, HoldingSpaceModelObserver>
observation_{this};
};
} // namespace
// Print out the `HoldingSpaceItem::Type` in the test output.
std::ostream& operator<<(std::ostream& os, const HoldingSpaceItem::Type type) {
return os << holding_space_util::ToString(type);
}
// HoldingSpaceModelTest -------------------------------------------------------
// Base class for `HoldingSpaceModel` tests, parameterized by the set of all
// holding space item types.
class HoldingSpaceModelTest
: public testing::TestWithParam<HoldingSpaceItem::Type> {
public:
// Returns the `HoldingSpaceModel` under test.
HoldingSpaceModel& model() { return model_; }
HoldingSpaceItem::Type GetHoldingSpaceItemType() const { return GetParam(); }
private:
HoldingSpaceModel model_;
};
INSTANTIATE_TEST_SUITE_P(
All,
HoldingSpaceModelTest,
testing::ValuesIn(holding_space_util::GetAllItemTypes()));
// Tests -----------------------------------------------------------------------
// Verifies that updating fields which affect accessible name WAI.
TEST_P(HoldingSpaceModelTest, UpdateItem_AccessibleName) {
ScopedModelObservation observation(&model());
// Verify the `model()` is initially empty.
EXPECT_EQ(model().items().size(), 0u);
// Create a holding space `item`.
auto item = HoldingSpaceItem::CreateFileBackedItem(
/*type=*/GetHoldingSpaceItemType(),
HoldingSpaceFile(base::FilePath("file_path"),
HoldingSpaceFile::FileSystemType::kTest,
GURL("filesystem::file_system_url")),
HoldingSpaceProgress(/*current_bytes=*/0, /*total_bytes=*/100),
/*image_resolver=*/base::BindOnce(&CreateFakeHoldingSpaceImage));
auto* item_ptr = item.get();
// Add `item` to the `model()`.
model().AddItem(std::move(item));
EXPECT_EQ(model().items().size(), 1u);
EXPECT_EQ(model().items()[0].get(), item_ptr);
// Initially accessible name is the lossy display name of the backing file.
EXPECT_EQ(item_ptr->GetAccessibleName(), u"file_path");
// Update text. Because accessible name is not overridden, this should result
// in an update to the computed accessible name field.
HoldingSpaceItemUpdatedFields expected_update;
expected_update.previous_accessible_name = item_ptr->GetAccessibleName();
expected_update.previous_text = item_ptr->GetText();
std::u16string text(u"text");
model().UpdateItem(item_ptr->id())->SetText(text);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(observation.TakeUpdatedItemCount(), 1);
EXPECT_EQ(item_ptr->GetAccessibleName(), text);
// Update secondary text. Because accessible name is not overridden, this
// should result in an update to the computed accessible name field.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_accessible_name = item_ptr->GetAccessibleName();
expected_update.previous_secondary_text = item_ptr->secondary_text();
std::u16string secondary_text(u"secondary_text");
model().UpdateItem(item_ptr->id())->SetSecondaryText(secondary_text);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(observation.TakeUpdatedItemCount(), 1);
EXPECT_EQ(item_ptr->GetAccessibleName(),
base::StrCat({text, u", ", secondary_text}));
// Update accessible name. Note that accessible name field is now overridden
// from its previously computed value.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_accessible_name = item_ptr->GetAccessibleName();
std::optional<std::u16string> accessible_name(u"accessible_name");
model().UpdateItem(item_ptr->id())->SetAccessibleName(accessible_name);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(observation.TakeUpdatedItemCount(), 1);
EXPECT_EQ(item_ptr->GetAccessibleName(), accessible_name);
// Update text. Because accessible name is overridden, this should *not*
// result in an update to the computed accessible name field.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_text = item_ptr->GetText();
text = u"updated_text";
model().UpdateItem(item_ptr->id())->SetText(text);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(observation.TakeUpdatedItemCount(), 1);
EXPECT_EQ(item_ptr->GetAccessibleName(), accessible_name);
// Update secondary text. Because accessible name is overridden, this should
// *not* result in an update to the computed accessible name field.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_secondary_text = item_ptr->secondary_text();
secondary_text = u"updated_secondary_text";
model().UpdateItem(item_ptr->id())->SetSecondaryText(secondary_text);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(observation.TakeUpdatedItemCount(), 1);
EXPECT_EQ(item_ptr->GetAccessibleName(), accessible_name);
// Update accessible name. Note that accessible name field is no longer being
// overridden from its computed value.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_accessible_name = item_ptr->GetAccessibleName();
accessible_name = std::nullopt;
model().UpdateItem(item_ptr->id())->SetAccessibleName(accessible_name);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(observation.TakeUpdatedItemCount(), 1);
EXPECT_EQ(item_ptr->GetAccessibleName(),
base::StrCat({text, u", ", secondary_text}));
}
// Verifies that updating multiple item attributes is atomic.
TEST_P(HoldingSpaceModelTest, UpdateItem_Atomic) {
ScopedModelObservation observation(&model());
// Verify the `model()` is initially empty.
EXPECT_EQ(model().items().size(), 0u);
// Create a holding space `item`.
auto item = HoldingSpaceItem::CreateFileBackedItem(
/*type=*/GetHoldingSpaceItemType(),
HoldingSpaceFile(base::FilePath("file_path"),
HoldingSpaceFile::FileSystemType::kTest,
GURL("filesystem::file_system_url")),
HoldingSpaceProgress(/*current_bytes=*/0, /*total_bytes=*/100),
/*image_resolver=*/base::BindOnce(&CreateFakeHoldingSpaceImage));
auto* item_ptr = item.get();
// Add `item` to the `model()`.
model().AddItem(std::move(item));
EXPECT_EQ(model().items().size(), 1u);
EXPECT_EQ(model().items()[0].get(), item_ptr);
// Update accessible name.
HoldingSpaceItemUpdatedFields expected_update;
expected_update.previous_accessible_name = item_ptr->GetAccessibleName();
std::u16string accessible_name(u"accessible_name");
model().UpdateItem(item_ptr->id())->SetAccessibleName(accessible_name);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(observation.TakeUpdatedItemCount(), 1);
EXPECT_EQ(item_ptr->GetAccessibleName(), accessible_name);
// Update backing file.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_backing_file = item_ptr->file();
expected_update.previous_text = item_ptr->GetText();
HoldingSpaceFile backing_file(base::FilePath("updated_file_path"),
HoldingSpaceFile::FileSystemType::kTest,
GURL("filesystem::updated_file_system_url"));
model().UpdateItem(item_ptr->id())->SetBackingFile(backing_file);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(observation.TakeUpdatedItemCount(), 1);
EXPECT_EQ(item_ptr->file(), backing_file);
// Update in-progress commands.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_in_progress_commands =
item_ptr->in_progress_commands();
std::vector<HoldingSpaceItem::InProgressCommand> in_progress_commands;
in_progress_commands.push_back(
CreateInProgressCommand(HoldingSpaceCommandId::kCancelItem));
model()
.UpdateItem(item_ptr->id())
->SetInProgressCommands(in_progress_commands);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(observation.TakeUpdatedItemCount(), 1);
EXPECT_EQ(item_ptr->in_progress_commands(), in_progress_commands);
// Update progress.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_progress = item_ptr->progress();
HoldingSpaceProgress progress(/*current_bytes=*/50, /*total_bytes=*/100);
model().UpdateItem(item_ptr->id())->SetProgress(progress);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(observation.TakeUpdatedItemCount(), 1);
EXPECT_EQ(item_ptr->progress(), progress);
// Update text.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_text = item_ptr->GetText();
std::u16string text(u"text");
model().UpdateItem(item_ptr->id())->SetText(text);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(observation.TakeUpdatedItemCount(), 1);
EXPECT_EQ(item_ptr->GetText(), text);
// Update secondary text.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_secondary_text = item_ptr->secondary_text();
std::u16string secondary_text(u"secondary_text");
model().UpdateItem(item_ptr->id())->SetSecondaryText(secondary_text);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(observation.TakeUpdatedItemCount(), 1);
EXPECT_EQ(item_ptr->secondary_text(), secondary_text);
// Update secondary text color.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_secondary_text_color_variant =
item_ptr->secondary_text_color_variant();
ui::ColorId secondary_text_color_id(cros_tokens::kTextColorAlert);
model()
.UpdateItem(item_ptr->id())
->SetSecondaryTextColorVariant(secondary_text_color_id);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(observation.TakeUpdatedItemCount(), 1);
EXPECT_THAT(*item_ptr->secondary_text_color_variant(),
VariantWith<ui::ColorId>(secondary_text_color_id));
// Update all attributes.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_accessible_name = item_ptr->GetAccessibleName();
expected_update.previous_backing_file = item_ptr->file();
expected_update.previous_in_progress_commands =
item_ptr->in_progress_commands();
expected_update.previous_progress = item_ptr->progress();
expected_update.previous_secondary_text = item_ptr->secondary_text();
expected_update.previous_secondary_text_color_variant =
item_ptr->secondary_text_color_variant();
expected_update.previous_text = item_ptr->GetText();
accessible_name = u"updated_accessible_name";
backing_file = HoldingSpaceFile(base::FilePath("updated_file_path"),
HoldingSpaceFile::FileSystemType::kLocal,
GURL("file_system::updated_file_system_url"));
in_progress_commands.push_back(
CreateInProgressCommand(HoldingSpaceCommandId::kPauseItem));
progress = HoldingSpaceProgress(/*current_bytes=*/75, /*total_bytes=*/100);
secondary_text = u"updated_secondary_text";
secondary_text_color_id = cros_tokens::kTextColorWarning;
text = u"updated_text";
model()
.UpdateItem(item_ptr->id())
->SetAccessibleName(accessible_name)
.SetBackingFile(backing_file)
.SetInProgressCommands(in_progress_commands)
.SetText(text)
.SetSecondaryText(secondary_text)
.SetSecondaryTextColorVariant(secondary_text_color_id)
.SetProgress(progress);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(observation.TakeUpdatedItemCount(), 1);
EXPECT_EQ(item_ptr->GetAccessibleName(), accessible_name);
EXPECT_EQ(item_ptr->file(), backing_file);
EXPECT_EQ(item_ptr->in_progress_commands(), in_progress_commands);
EXPECT_EQ(item_ptr->progress(), progress);
EXPECT_EQ(item_ptr->GetText(), text);
EXPECT_EQ(item_ptr->secondary_text(), secondary_text);
EXPECT_THAT(*item_ptr->secondary_text_color_variant(),
VariantWith<ui::ColorId>(secondary_text_color_id));
}
// Verifies that updating items will no-op appropriately.
TEST_P(HoldingSpaceModelTest, UpdateItem_Noop) {
ScopedModelObservation observation(&model());
// Verify the `model()` is initially empty.
EXPECT_EQ(model().items().size(), 0u);
// Create a holding space `item`.
auto item = HoldingSpaceItem::CreateFileBackedItem(
/*type=*/GetHoldingSpaceItemType(),
HoldingSpaceFile(base::FilePath("file_path"),
HoldingSpaceFile::FileSystemType::kTest,
GURL("filesystem::file_system_url")),
HoldingSpaceProgress(),
/*image_resolver=*/base::BindOnce(&CreateFakeHoldingSpaceImage));
auto* item_ptr = item.get();
// Add `item` to the `model()`.
model().AddItem(std::move(item));
EXPECT_EQ(model().items().size(), 1u);
EXPECT_EQ(model().items()[0].get(), item_ptr);
// Perform a no-op update. No observers should be notified.
model().UpdateItem(item_ptr->id());
EXPECT_EQ(observation.TakeUpdatedItemCount(), 0);
// Perform another no-op update. No observers should be notified.
model()
.UpdateItem(item_ptr->id())
->SetAccessibleName(std::nullopt)
.SetBackingFile(item_ptr->file())
.SetInProgressCommands({})
.SetText(std::nullopt)
.SetSecondaryText(std::nullopt)
.SetSecondaryTextColorVariant(std::nullopt)
.SetProgress(item_ptr->progress());
EXPECT_EQ(observation.TakeUpdatedItemCount(), 0);
}
// Verifies that updating item in-progress commands as intended.
TEST_P(HoldingSpaceModelTest, UpdateItem_InProgressCommands) {
ScopedModelObservation observation(&model());
// Verify the `model()` is initially empty.
EXPECT_EQ(model().items().size(), 0u);
// Create an in-progress holding space `item`.
auto item = HoldingSpaceItem::CreateFileBackedItem(
/*type=*/GetHoldingSpaceItemType(),
HoldingSpaceFile(base::FilePath("file_path"),
HoldingSpaceFile::FileSystemType::kTest,
GURL("filesystem::file_system_url")),
HoldingSpaceProgress(/*current_bytes=*/0, /*total_bytes=*/100),
/*image_resolver=*/base::BindOnce(&CreateFakeHoldingSpaceImage));
auto* item_ptr = item.get();
// Add `item` to the `model()`.
model().AddItem(std::move(item));
EXPECT_EQ(model().items().size(), 1u);
EXPECT_EQ(model().items()[0].get(), item_ptr);
// Verify the item has no in-progress commands.
EXPECT_TRUE(item_ptr->in_progress_commands().empty());
// Attempt to update in-progress commands to empty. This should no-op.
model().UpdateItem(item_ptr->id())->SetInProgressCommands({});
EXPECT_FALSE(observation.TakeLastUpdatedItem());
EXPECT_TRUE(observation.TakeLastUpdatedFields().IsEmpty());
EXPECT_TRUE(item_ptr->in_progress_commands().empty());
// Update in-progress commands.
HoldingSpaceItemUpdatedFields expected_update;
expected_update.previous_in_progress_commands =
item_ptr->in_progress_commands();
std::vector<HoldingSpaceItem::InProgressCommand> in_progress_commands;
in_progress_commands.push_back(
CreateInProgressCommand(HoldingSpaceCommandId::kCancelItem));
model()
.UpdateItem(item_ptr->id())
->SetInProgressCommands(in_progress_commands);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(item_ptr->in_progress_commands(), in_progress_commands);
// Update in-progress commands again.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_in_progress_commands =
item_ptr->in_progress_commands();
in_progress_commands.push_back(
CreateInProgressCommand(HoldingSpaceCommandId::kPauseItem));
model()
.UpdateItem(item_ptr->id())
->SetInProgressCommands(in_progress_commands);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(item_ptr->in_progress_commands(), in_progress_commands);
// Update in-progress commands and progress to completion. Because the item is
// no longer in progress, in-progress commands should be empty.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_in_progress_commands =
item_ptr->in_progress_commands();
expected_update.previous_progress = item_ptr->progress();
in_progress_commands.push_back(
CreateInProgressCommand(HoldingSpaceCommandId::kResumeItem));
HoldingSpaceProgress progress(/*current_bytes=*/100, /*total_bytes=*/100);
model()
.UpdateItem(item_ptr->id())
->SetInProgressCommands(in_progress_commands)
.SetProgress(progress);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(item_ptr->progress(), progress);
EXPECT_TRUE(item_ptr->in_progress_commands().empty());
// Attempts to update in-progress commands should no-op for completed items.
model()
.UpdateItem(item_ptr->id())
->SetInProgressCommands(in_progress_commands);
EXPECT_FALSE(observation.TakeLastUpdatedItem());
EXPECT_TRUE(observation.TakeLastUpdatedFields().IsEmpty());
EXPECT_TRUE(item_ptr->in_progress_commands().empty());
}
// Verifies that updating item progress works as intended.
TEST_P(HoldingSpaceModelTest, UpdateItem_Progress) {
ScopedModelObservation observation(&model());
// Verify the `model()` is initially empty.
EXPECT_EQ(model().items().size(), 0u);
// Create a holding space `item`.
auto item = HoldingSpaceItem::CreateFileBackedItem(
/*type=*/GetHoldingSpaceItemType(),
HoldingSpaceFile(base::FilePath("file_path"),
HoldingSpaceFile::FileSystemType::kTest,
GURL("filesystem::file_system_url")),
HoldingSpaceProgress(/*current_bytes=*/std::nullopt,
/*total_bytes=*/100),
/*image_resolver=*/base::BindOnce(&CreateFakeHoldingSpaceImage));
auto* item_ptr = item.get();
// Add `item` to the `model()`.
model().AddItem(std::move(item));
EXPECT_EQ(model().items().size(), 1u);
EXPECT_EQ(model().items()[0].get(), item_ptr);
// Verify progress is indeterminate.
EXPECT_TRUE(item_ptr->progress().IsIndeterminate());
// Update progress to a determinate, in-progress value.
HoldingSpaceItemUpdatedFields expected_update;
expected_update.previous_progress = item_ptr->progress();
HoldingSpaceProgress progress(/*current_bytes=*/50, /*total_bytes=*/100);
model().UpdateItem(item_ptr->id())->SetProgress(progress);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(item_ptr->progress(), progress);
EXPECT_FALSE(item_ptr->progress().IsComplete());
EXPECT_FALSE(item_ptr->progress().IsIndeterminate());
// Update progress to the same value. This should no-op.
progress = HoldingSpaceProgress(/*current_bytes=*/50, /*total_bytes=*/100);
model().UpdateItem(item_ptr->id())->SetProgress(progress);
EXPECT_FALSE(observation.TakeLastUpdatedItem());
EXPECT_TRUE(observation.TakeLastUpdatedFields().IsEmpty());
EXPECT_EQ(item_ptr->progress(), progress);
EXPECT_FALSE(item_ptr->progress().IsComplete());
EXPECT_FALSE(item_ptr->progress().IsIndeterminate());
// Update progress to indeterminate.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_progress = item_ptr->progress();
progress = HoldingSpaceProgress(/*current_bytes=*/std::nullopt,
/*total_bytes=*/100);
model().UpdateItem(item_ptr->id())->SetProgress(progress);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(item_ptr->progress(), progress);
EXPECT_FALSE(item_ptr->progress().IsComplete());
EXPECT_TRUE(item_ptr->progress().IsIndeterminate());
// Update progress to complete.
expected_update = HoldingSpaceItemUpdatedFields();
expected_update.previous_progress = item_ptr->progress();
progress = HoldingSpaceProgress(/*current_bytes=*/100, /*total_bytes=*/100);
model().UpdateItem(item_ptr->id())->SetProgress(progress);
EXPECT_EQ(observation.TakeLastUpdatedItem(), item_ptr);
EXPECT_EQ(observation.TakeLastUpdatedFields(), expected_update);
EXPECT_EQ(item_ptr->progress(), progress);
EXPECT_TRUE(item_ptr->progress().IsComplete());
EXPECT_FALSE(item_ptr->progress().IsIndeterminate());
// Update progress to an in-progress value. This should no-op as progress
// becomes read-only after being marked completed.
progress = item_ptr->progress();
model()
.UpdateItem(item_ptr->id())
->SetProgress(
HoldingSpaceProgress(/*current_bytes=*/50, /*total_bytes=*/100));
EXPECT_FALSE(observation.TakeLastUpdatedItem());
EXPECT_TRUE(observation.TakeLastUpdatedFields().IsEmpty());
EXPECT_EQ(item_ptr->progress(), progress);
EXPECT_TRUE(item_ptr->progress().IsComplete());
EXPECT_FALSE(item_ptr->progress().IsIndeterminate());
}
} // namespace ash