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 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
|
// 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 "chromeos/ash/components/phonehub/camera_roll_manager_impl.h"
#include <optional>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "chromeos/ash/components/phonehub/camera_roll_download_manager.h"
#include "chromeos/ash/components/phonehub/camera_roll_item.h"
#include "chromeos/ash/components/phonehub/camera_roll_thumbnail_decoder_impl.h"
#include "chromeos/ash/components/phonehub/fake_camera_roll_download_manager.h"
#include "chromeos/ash/components/phonehub/fake_message_receiver.h"
#include "chromeos/ash/components/phonehub/fake_message_sender.h"
#include "chromeos/ash/components/phonehub/proto/phonehub_api.pb.h"
#include "chromeos/ash/components/phonehub/util/histogram_util.h"
#include "chromeos/ash/services/multidevice_setup/public/cpp/fake_multidevice_setup_client.h"
#include "chromeos/ash/services/multidevice_setup/public/mojom/multidevice_setup.mojom.h"
#include "chromeos/ash/services/secure_channel/public/cpp/client/fake_connection_manager.h"
#include "chromeos/ash/services/secure_channel/public/mojom/secure_channel_types.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
namespace ash::phonehub {
namespace {
using BatchDecodeResult = CameraRollThumbnailDecoder::BatchDecodeResult;
using BatchDecodeCallback =
base::OnceCallback<void(BatchDecodeResult,
const std::vector<CameraRollItem>&)>;
using FeatureState = multidevice_setup::mojom::FeatureState;
using FileTransferStatus = secure_channel::mojom::FileTransferStatus;
const char kDownloadResultMetricName[] =
"PhoneHub.CameraRoll.DownloadItem.Result";
class FakeObserver : public CameraRollManager::Observer {
public:
FakeObserver() = default;
~FakeObserver() override = default;
// CameraRollManager::Observer
void OnCameraRollViewUiStateUpdated() override {
on_camera_roll_items_changed_call_count_++;
}
int on_camera_roll_items_changed_call_count() const {
return on_camera_roll_items_changed_call_count_;
}
void OnCameraRollDownloadError(
CameraRollManager::Observer::DownloadErrorType error_type,
const proto::CameraRollItemMetadata& metadata) override {
last_download_error_ = std::make_optional(error_type);
}
CameraRollManager::Observer::DownloadErrorType last_download_error() const {
return last_download_error_.value();
}
private:
int on_camera_roll_items_changed_call_count_ = 0;
std::optional<CameraRollManager::Observer::DownloadErrorType>
last_download_error_ = std::nullopt;
};
void PopulateItemProto(proto::CameraRollItem* item_proto, std::string key) {
proto::CameraRollItemMetadata* metadata = item_proto->mutable_metadata();
metadata->set_key(key);
metadata->set_mime_type("image/png");
metadata->set_last_modified_millis(123456789L);
metadata->set_file_size_bytes(123456789L);
proto::CameraRollItemThumbnail* thumbnail = item_proto->mutable_thumbnail();
thumbnail->set_format(proto::CameraRollItemThumbnail_Format_JPEG);
thumbnail->set_data("encoded_thumbnail_data");
}
// Verifies that the metadata of a generated item matches the corresponding
// proto input.
void VerifyMetadataEqual(const proto::CameraRollItemMetadata& expected,
const proto::CameraRollItemMetadata& actual) {
EXPECT_EQ(expected.key(), actual.key());
EXPECT_EQ(expected.mime_type(), actual.mime_type());
EXPECT_EQ(expected.last_modified_millis(), actual.last_modified_millis());
EXPECT_EQ(expected.file_size_bytes(), actual.file_size_bytes());
}
} // namespace
class FakeThumbnailDecoder : public CameraRollThumbnailDecoder {
public:
FakeThumbnailDecoder() = default;
~FakeThumbnailDecoder() override = default;
void BatchDecode(const proto::FetchCameraRollItemsResponse& response,
const std::vector<CameraRollItem>& current_items,
BatchDecodeCallback callback) override {
if (!pending_callback_.is_null()) {
CompletePendingCallback(BatchDecodeResult::kCancelled);
}
last_response_ = response;
pending_callback_ = std::move(callback);
}
void CompletePendingCallback(BatchDecodeResult result) {
std::vector<CameraRollItem> items;
if (result == BatchDecodeResult::kCompleted) {
for (const proto::CameraRollItem& item_proto : last_response_.items()) {
SkBitmap test_bitmap;
test_bitmap.allocN32Pixels(1, 1);
gfx::ImageSkia image_skia =
gfx::ImageSkia::CreateFrom1xBitmap(test_bitmap);
image_skia.MakeThreadSafe();
gfx::Image thumbnail(image_skia);
items.emplace_back(item_proto.metadata(), thumbnail);
}
}
std::move(pending_callback_).Run(result, items);
}
private:
proto::FetchCameraRollItemsResponse last_response_;
BatchDecodeCallback pending_callback_;
};
class CameraRollManagerImplTest : public testing::Test {
protected:
CameraRollManagerImplTest() = default;
CameraRollManagerImplTest(const CameraRollManagerImplTest&) = delete;
CameraRollManagerImplTest& operator=(const CameraRollManagerImplTest&) =
delete;
~CameraRollManagerImplTest() override = default;
void SetUp() override {
fake_multidevice_setup_client_ =
std::make_unique<multidevice_setup::FakeMultiDeviceSetupClient>();
fake_connection_manager_ =
std::make_unique<secure_channel::FakeConnectionManager>();
std::unique_ptr<FakeCameraRollDownloadManager>
fake_camera_roll_download_manager =
std::make_unique<FakeCameraRollDownloadManager>();
fake_camera_roll_download_manager_ =
fake_camera_roll_download_manager.get();
SetCameraRollFeatureState(FeatureState::kEnabledByUser);
camera_roll_manager_ = std::make_unique<CameraRollManagerImpl>(
&fake_message_receiver_, &fake_message_sender_,
fake_multidevice_setup_client_.get(), fake_connection_manager_.get(),
std::move(fake_camera_roll_download_manager));
camera_roll_manager_->thumbnail_decoder_ =
std::make_unique<FakeThumbnailDecoder>();
camera_roll_manager_->AddObserver(&fake_observer_);
}
void TearDown() override {
camera_roll_manager_->RemoveObserver(&fake_observer_);
}
int GetOnCameraRollViewUiStateUpdatedCallCount() const {
return fake_observer_.on_camera_roll_items_changed_call_count();
}
CameraRollManager::Observer::DownloadErrorType GetLastDownloadError() const {
return fake_observer_.last_download_error();
}
int GetCurrentItemsCount() const {
return camera_roll_manager_->current_items().size();
}
size_t GetSentFetchCameraRollItemsRequestCount() const {
return fake_message_sender_.GetFetchCameraRollItemsRequestCallCount();
}
const proto::FetchCameraRollItemsRequest& GetSentFetchCameraRollItemsRequest()
const {
return fake_message_sender_.GetRecentFetchCameraRollItemsRequest();
}
size_t GetSentFetchCameraRollItemDataRequestCount() const {
return fake_message_sender_.GetFetchCameraRollItemDataRequestCallCount();
}
const proto::FetchCameraRollItemDataRequest&
GetRecentFetchCameraRollItemDataRequest() const {
return fake_message_sender_.GetRecentFetchCameraRollItemDataRequest();
}
size_t GetSentInitiateCameraRollItemTransferRequestCount() const {
return fake_message_sender_
.GetInitiateCameraRollItemTransferRequestCallCount();
}
const proto::InitiateCameraRollItemTransferRequest&
GetRecentInitiateCameraRollItemTransferRequest() const {
return fake_message_sender_
.GetRecentInitiateCameraRollItemTransferRequest();
}
// Verifies current items match the list of items in the last received
// |FetchCameraRollItemsResponse|, and their thumbnails have been properly
// decoded.
void VerifyCurrentItemsMatchResponse(
const proto::FetchCameraRollItemsResponse& response) const {
EXPECT_EQ(response.items_size(), GetCurrentItemsCount());
for (int i = 0; i < GetCurrentItemsCount(); i++) {
const CameraRollItem& current_item =
camera_roll_manager_->current_items()[i];
VerifyMetadataEqual(response.items(i).metadata(),
current_item.metadata());
EXPECT_FALSE(current_item.thumbnail().IsEmpty());
}
}
void CompleteThumbnailDecoding(BatchDecodeResult result) {
static_cast<FakeThumbnailDecoder*>(
camera_roll_manager_->thumbnail_decoder_.get())
->CompletePendingCallback(result);
}
void SetCameraRollFeatureState(FeatureState feature_state) {
fake_multidevice_setup_client_->SetFeatureState(
multidevice_setup::mojom::Feature::kPhoneHubCameraRoll, feature_state);
}
void UngrantAndroidStoragePermission() {
android_storage_permission_granted_ = false;
}
void SendPhoneStatusSnapshot() {
proto::PhoneStatusSnapshot snapshot;
proto::CameraRollAccessState* access_state =
snapshot.mutable_properties()->mutable_camera_roll_access_state();
access_state->set_storage_permission_granted(
android_storage_permission_granted_);
fake_message_receiver_.NotifyPhoneStatusSnapshotReceived(snapshot);
}
void SendPhoneStatusUpdate(bool has_camera_roll_updates) {
proto::PhoneStatusUpdate update;
update.set_has_camera_roll_updates(has_camera_roll_updates);
proto::CameraRollAccessState* access_state =
update.mutable_properties()->mutable_camera_roll_access_state();
access_state->set_storage_permission_granted(
android_storage_permission_granted_);
fake_message_receiver_.NotifyPhoneStatusUpdateReceived(update);
}
void SendFetchCameraRollItemDataResponse(
const proto::CameraRollItemMetadata& item_metadata,
proto::FetchCameraRollItemDataResponse::FileAvailability
file_availability,
int64_t payload_id) {
proto::FetchCameraRollItemDataResponse response;
*response.mutable_metadata() = item_metadata;
response.set_file_availability(file_availability);
response.set_payload_id(payload_id);
fake_message_receiver_.NotifyFetchCameraRollItemDataResponseReceived(
response);
}
void SendFileTransferUpdate(int64_t payload_id,
FileTransferStatus status,
uint64_t total_bytes,
uint64_t bytes_transferred) {
fake_connection_manager_->SendFileTransferUpdate(
secure_channel::mojom::FileTransferUpdate::New(
payload_id, status, total_bytes, bytes_transferred));
}
void VerifyFileTransferProgress(int64_t payload_id,
FileTransferStatus status,
uint64_t total_bytes,
uint64_t bytes_transferred) {
const secure_channel::mojom::FileTransferUpdatePtr& latest_update =
fake_camera_roll_download_manager_->GetFileTransferUpdates(payload_id)
.back();
EXPECT_EQ(payload_id, latest_update->payload_id);
EXPECT_EQ(status, latest_update->status);
EXPECT_EQ(total_bytes, latest_update->total_bytes);
EXPECT_EQ(bytes_transferred, latest_update->bytes_transferred);
}
void SetCurrentItems(std::vector<std::string> item_keys) {
proto::FetchCameraRollItemsResponse response;
for (const auto& item_key : item_keys) {
PopulateItemProto(response.add_items(), item_key);
}
fake_message_receiver_.NotifyFetchCameraRollItemsResponseReceived(response);
CompleteThumbnailDecoding(BatchDecodeResult::kCompleted);
}
CameraRollManager* camera_roll_manager() {
return camera_roll_manager_.get();
}
secure_channel::FakeConnectionManager* fake_connection_manager() {
return fake_connection_manager_.get();
}
FakeCameraRollDownloadManager* fake_camera_roll_download_manager() {
return fake_camera_roll_download_manager_;
}
FakeMessageReceiver fake_message_receiver_;
std::unique_ptr<multidevice_setup::FakeMultiDeviceSetupClient>
fake_multidevice_setup_client_;
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
base::HistogramTester histogram_tester_;
private:
FakeMessageSender fake_message_sender_;
std::unique_ptr<secure_channel::FakeConnectionManager>
fake_connection_manager_;
raw_ptr<FakeCameraRollDownloadManager, DanglingUntriaged>
fake_camera_roll_download_manager_;
std::unique_ptr<CameraRollManagerImpl> camera_roll_manager_;
FakeObserver fake_observer_;
bool android_storage_permission_granted_ = true;
};
TEST_F(CameraRollManagerImplTest, OnCameraRollItemsReceived) {
SendPhoneStatusSnapshot();
task_environment_.FastForwardBy(base::Seconds(10));
proto::FetchCameraRollItemsResponse response;
PopulateItemProto(response.add_items(), "key3");
PopulateItemProto(response.add_items(), "key2");
PopulateItemProto(response.add_items(), "key1");
fake_message_receiver_.NotifyFetchCameraRollItemsResponseReceived(response);
CompleteThumbnailDecoding(BatchDecodeResult::kCompleted);
EXPECT_EQ(2, GetOnCameraRollViewUiStateUpdatedCallCount());
VerifyCurrentItemsMatchResponse(response);
histogram_tester_.ExpectUniqueTimeSample(
"PhoneHub.CameraRoll.Latency.RefreshItems", base::Seconds(10),
/*expected_bucket_count=*/1);
}
TEST_F(CameraRollManagerImplTest,
OnCameraRollItemsReceivedWithCancelledThumbnailDecodingRequest) {
SendPhoneStatusSnapshot();
task_environment_.FastForwardBy(base::Seconds(10));
proto::FetchCameraRollItemsResponse response;
PopulateItemProto(response.add_items(), "key3");
PopulateItemProto(response.add_items(), "key2");
PopulateItemProto(response.add_items(), "key1");
fake_message_receiver_.NotifyFetchCameraRollItemsResponseReceived(response);
CompleteThumbnailDecoding(BatchDecodeResult::kCancelled);
EXPECT_EQ(1, GetOnCameraRollViewUiStateUpdatedCallCount());
EXPECT_EQ(0, GetCurrentItemsCount());
EXPECT_TRUE(histogram_tester_
.GetAllSamples("PhoneHub.CameraRoll.Latency.RefreshItems")
.empty());
}
TEST_F(CameraRollManagerImplTest,
OnCameraRollItemsReceivedWithPendingThumbnailDecodedRequest) {
SendPhoneStatusSnapshot();
task_environment_.FastForwardBy(base::Seconds(10));
proto::FetchCameraRollItemsResponse first_response;
PopulateItemProto(first_response.add_items(), "key2");
PopulateItemProto(first_response.add_items(), "key1");
fake_message_receiver_.NotifyFetchCameraRollItemsResponseReceived(
first_response);
task_environment_.FastForwardBy(base::Seconds(5));
proto::FetchCameraRollItemsResponse second_response;
PopulateItemProto(second_response.add_items(), "key4");
PopulateItemProto(second_response.add_items(), "key3");
fake_message_receiver_.NotifyFetchCameraRollItemsResponseReceived(
second_response);
CompleteThumbnailDecoding(BatchDecodeResult::kCompleted);
// The first thumbnail decode request should be cancelled and the current item
// set should be updated only once after the second request completes.
EXPECT_EQ(2, GetOnCameraRollViewUiStateUpdatedCallCount());
VerifyCurrentItemsMatchResponse(second_response);
histogram_tester_.ExpectUniqueTimeSample(
"PhoneHub.CameraRoll.Latency.RefreshItems", base::Seconds(15),
/*expected_bucket_count=*/1);
}
TEST_F(CameraRollManagerImplTest, OnCameraRollItemsReceivedWithExistingItems) {
SendPhoneStatusSnapshot();
task_environment_.FastForwardBy(base::Seconds(10));
proto::FetchCameraRollItemsResponse first_response;
PopulateItemProto(first_response.add_items(), "key3");
PopulateItemProto(first_response.add_items(), "key2");
PopulateItemProto(first_response.add_items(), "key1");
fake_message_receiver_.NotifyFetchCameraRollItemsResponseReceived(
first_response);
CompleteThumbnailDecoding(BatchDecodeResult::kCompleted);
VerifyCurrentItemsMatchResponse(first_response);
SendPhoneStatusUpdate(/*has_camera_roll_updates=*/true);
task_environment_.FastForwardBy(base::Seconds(5));
proto::FetchCameraRollItemsResponse second_response;
PopulateItemProto(second_response.add_items(), "key4");
// Thumbnails won't be sent with the proto if an item's data is already
// available and up-to-date.
PopulateItemProto(second_response.add_items(), "key3");
second_response.mutable_items(1)->clear_thumbnail();
PopulateItemProto(second_response.add_items(), "key2");
second_response.mutable_items(2)->clear_thumbnail();
fake_message_receiver_.NotifyFetchCameraRollItemsResponseReceived(
second_response);
CompleteThumbnailDecoding(BatchDecodeResult::kCompleted);
EXPECT_EQ(3, GetOnCameraRollViewUiStateUpdatedCallCount());
VerifyCurrentItemsMatchResponse(second_response);
histogram_tester_.ExpectTimeBucketCount(
"PhoneHub.CameraRoll.Latency.RefreshItems", base::Seconds(10),
/*count=*/1);
histogram_tester_.ExpectTimeBucketCount(
"PhoneHub.CameraRoll.Latency.RefreshItems", base::Seconds(5),
/*count=*/1);
}
TEST_F(CameraRollManagerImplTest,
OnPhoneStatusUpdateReceivedWithoutCameraRollUpdates) {
SendPhoneStatusUpdate(/*has_camera_roll_updates=*/false);
EXPECT_EQ(0UL, GetSentFetchCameraRollItemsRequestCount());
EXPECT_EQ(CameraRollManager::CameraRollUiState::SHOULD_HIDE,
camera_roll_manager()->ui_state());
EXPECT_EQ(1, GetOnCameraRollViewUiStateUpdatedCallCount());
}
TEST_F(CameraRollManagerImplTest,
OnPhoneStatusUpdateReceivedWithCameraRollUpdates) {
SendPhoneStatusUpdate(/*has_camera_roll_updates=*/true);
EXPECT_EQ(1UL, GetSentFetchCameraRollItemsRequestCount());
EXPECT_EQ(0,
GetSentFetchCameraRollItemsRequest().current_item_metadata_size());
EXPECT_EQ(CameraRollManager::CameraRollUiState::SHOULD_HIDE,
camera_roll_manager()->ui_state());
EXPECT_EQ(1, GetOnCameraRollViewUiStateUpdatedCallCount());
}
TEST_F(CameraRollManagerImplTest,
OnPhoneStatusUpdateReceivedWithExistingItems) {
SetCurrentItems({"key1", "key2", "key3"});
SendPhoneStatusUpdate(/*has_camera_roll_updates=*/true);
EXPECT_EQ(1UL, GetSentFetchCameraRollItemsRequestCount());
EXPECT_EQ(CameraRollManager::CameraRollUiState::ITEMS_VISIBLE,
camera_roll_manager()->ui_state());
EXPECT_EQ(2, GetOnCameraRollViewUiStateUpdatedCallCount());
EXPECT_EQ(3,
GetSentFetchCameraRollItemsRequest().current_item_metadata_size());
VerifyMetadataEqual(
camera_roll_manager()->current_items()[0].metadata(),
GetSentFetchCameraRollItemsRequest().current_item_metadata(0));
VerifyMetadataEqual(
camera_roll_manager()->current_items()[1].metadata(),
GetSentFetchCameraRollItemsRequest().current_item_metadata(1));
VerifyMetadataEqual(
camera_roll_manager()->current_items()[2].metadata(),
GetSentFetchCameraRollItemsRequest().current_item_metadata(2));
}
TEST_F(CameraRollManagerImplTest,
OnPhoneStatusUpdateReceivedWithCameraRollSettingsDisabled) {
SetCameraRollFeatureState(FeatureState::kDisabledByUser);
SetCurrentItems({"key1", "key2"});
SendPhoneStatusUpdate(/*has_camera_roll_updates=*/true);
EXPECT_EQ(0UL, GetSentFetchCameraRollItemsRequestCount());
EXPECT_EQ(CameraRollManager::CameraRollUiState::SHOULD_HIDE,
camera_roll_manager()->ui_state());
EXPECT_EQ(4, GetOnCameraRollViewUiStateUpdatedCallCount());
EXPECT_EQ(0, GetCurrentItemsCount());
}
TEST_F(CameraRollManagerImplTest,
OnPhoneStatusUpdateReceivedWithoutStoragePermission) {
SetCurrentItems({"key1", "key2"});
UngrantAndroidStoragePermission();
SendPhoneStatusUpdate(/*has_camera_roll_updates=*/false);
EXPECT_EQ(0UL, GetSentFetchCameraRollItemsRequestCount());
EXPECT_EQ(CameraRollManager::CameraRollUiState::NO_STORAGE_PERMISSION,
camera_roll_manager()->ui_state());
EXPECT_EQ(2, GetOnCameraRollViewUiStateUpdatedCallCount());
EXPECT_EQ(0, GetCurrentItemsCount());
}
TEST_F(CameraRollManagerImplTest, OnPhoneStatusSnapshotReceived) {
SendPhoneStatusSnapshot();
EXPECT_EQ(1UL, GetSentFetchCameraRollItemsRequestCount());
EXPECT_EQ(CameraRollManager::CameraRollUiState::SHOULD_HIDE,
camera_roll_manager()->ui_state());
EXPECT_EQ(1, GetOnCameraRollViewUiStateUpdatedCallCount());
}
TEST_F(CameraRollManagerImplTest,
OnPhoneStatusSnapshotReceivedWithCameraRollSettingDisabled) {
SetCameraRollFeatureState(FeatureState::kDisabledByUser);
SetCurrentItems({"key1", "key2"});
SendPhoneStatusSnapshot();
EXPECT_EQ(0UL, GetSentFetchCameraRollItemsRequestCount());
EXPECT_EQ(CameraRollManager::CameraRollUiState::SHOULD_HIDE,
camera_roll_manager()->ui_state());
EXPECT_EQ(4, GetOnCameraRollViewUiStateUpdatedCallCount());
EXPECT_EQ(0, GetCurrentItemsCount());
}
TEST_F(CameraRollManagerImplTest,
OnPhoneStatusSnapshotReceivedWithoutStoragePermission) {
SetCurrentItems({"key1", "key2"});
UngrantAndroidStoragePermission();
SendPhoneStatusSnapshot();
EXPECT_EQ(0UL, GetSentFetchCameraRollItemsRequestCount());
EXPECT_EQ(CameraRollManager::CameraRollUiState::NO_STORAGE_PERMISSION,
camera_roll_manager()->ui_state());
EXPECT_EQ(2, GetOnCameraRollViewUiStateUpdatedCallCount());
EXPECT_EQ(0, GetCurrentItemsCount());
}
TEST_F(CameraRollManagerImplTest, OnFeatureStatesChangedToDisabled) {
SendPhoneStatusSnapshot();
SetCurrentItems({"key1", "key2"});
SetCameraRollFeatureState(FeatureState::kDisabledByUser);
EXPECT_EQ(CameraRollManager::CameraRollUiState::SHOULD_HIDE,
camera_roll_manager()->ui_state());
EXPECT_EQ(3, GetOnCameraRollViewUiStateUpdatedCallCount());
EXPECT_EQ(0, GetCurrentItemsCount());
}
TEST_F(CameraRollManagerImplTest, FeatureProhibitedByPolicy) {
SetCameraRollFeatureState(FeatureState::kProhibitedByPolicy);
proto::PhoneStatusSnapshot snapshot;
proto::CameraRollAccessState* access_state =
snapshot.mutable_properties()->mutable_camera_roll_access_state();
access_state->set_storage_permission_granted(true);
fake_message_receiver_.NotifyPhoneStatusSnapshotReceived(snapshot);
EXPECT_EQ(CameraRollManager::CameraRollUiState::SHOULD_HIDE,
camera_roll_manager()->ui_state());
}
TEST_F(CameraRollManagerImplTest, DownloadItem) {
SetCurrentItems({"key1"});
const CameraRollItem& item_to_download =
camera_roll_manager()->current_items().back();
// Request to download the item that was added.
camera_roll_manager()->DownloadItem(item_to_download.metadata());
EXPECT_EQ(1UL, GetSentFetchCameraRollItemDataRequestCount());
EXPECT_EQ("key1", GetRecentFetchCameraRollItemDataRequest().metadata().key());
// CameraRollManager should initiate transfer of the item after receiving
// FetchCameraRollItemDataResponse.
SendFetchCameraRollItemDataResponse(
item_to_download.metadata(),
proto::FetchCameraRollItemDataResponse::AVAILABLE,
/*payload_id=*/1234);
EXPECT_EQ(1UL, GetSentInitiateCameraRollItemTransferRequestCount());
EXPECT_EQ("key1",
GetRecentInitiateCameraRollItemTransferRequest().metadata().key());
EXPECT_EQ(1234,
GetRecentInitiateCameraRollItemTransferRequest().payload_id());
// Now the CameraRollManager should be ready to receive updates of the item
// transfer.
SendFileTransferUpdate(/*payload_id=*/1234, FileTransferStatus::kInProgress,
/*total_bytes=*/1000, /*bytes_transferred=*/200);
VerifyFileTransferProgress(/*payload_id=*/1234,
FileTransferStatus::kInProgress,
/*total_bytes=*/1000,
/*bytes_transferred=*/200);
SendFileTransferUpdate(/*payload_id=*/1234, FileTransferStatus::kSuccess,
/*total_bytes=*/1000, /*bytes_transferred=*/1000);
VerifyFileTransferProgress(/*payload_id=*/1234, FileTransferStatus::kSuccess,
/*total_bytes=*/1000,
/*bytes_transferred=*/1000);
histogram_tester_.ExpectBucketCount(kDownloadResultMetricName,
util::CameraRollDownloadResult::kSuccess,
/*expected_count=*/1);
}
TEST_F(CameraRollManagerImplTest, DownloadItemAndCurrentItemsChanged) {
SetCurrentItems({"key1"});
const CameraRollItem& item_to_download =
camera_roll_manager()->current_items().back();
// Request to download the item that was added.
camera_roll_manager()->DownloadItem(item_to_download.metadata());
SendFetchCameraRollItemDataResponse(
item_to_download.metadata(),
proto::FetchCameraRollItemDataResponse::AVAILABLE,
/*payload_id=*/1234);
SendFileTransferUpdate(/*payload_id=*/1234, FileTransferStatus::kInProgress,
/*total_bytes=*/1000, /*bytes_transferred=*/200);
// The item being downloaded is no longer in the current item set, but the
// download should still finish normally.
SendPhoneStatusUpdate(/*has_camera_roll_updates=*/true);
SetCurrentItems({"key2"});
SendFileTransferUpdate(/*payload_id=*/1234, FileTransferStatus::kSuccess,
/*total_bytes=*/1000, /*bytes_transferred=*/1000);
VerifyFileTransferProgress(/*payload_id=*/1234, FileTransferStatus::kSuccess,
/*total_bytes=*/1000,
/*bytes_transferred=*/1000);
}
TEST_F(CameraRollManagerImplTest,
DownloadItemWhenFileNoLongerAvailableOnPhone) {
SetCurrentItems({"key1"});
const CameraRollItem& item_to_download =
camera_roll_manager()->current_items().back();
// Request to download the item that was added.
camera_roll_manager()->DownloadItem(item_to_download.metadata());
SendFetchCameraRollItemDataResponse(
item_to_download.metadata(),
proto::FetchCameraRollItemDataResponse::NOT_FOUND,
/*payload_id=*/1234);
EXPECT_EQ(0UL, GetSentInitiateCameraRollItemTransferRequestCount());
histogram_tester_.ExpectBucketCount(
kDownloadResultMetricName,
util::CameraRollDownloadResult::kFileNotAvailable,
/*expected_count=*/1);
}
TEST_F(CameraRollManagerImplTest, DownloadItemFailedWithInvalidFileName) {
SetCurrentItems({"key1"});
const CameraRollItem& item_to_download =
camera_roll_manager()->current_items().back();
// Request to download the item that was added.
camera_roll_manager()->DownloadItem(item_to_download.metadata());
fake_camera_roll_download_manager()->set_expected_create_payload_files_result(
CameraRollDownloadManager::CreatePayloadFilesResult::kInvalidFileName);
SendFetchCameraRollItemDataResponse(
item_to_download.metadata(),
proto::FetchCameraRollItemDataResponse::AVAILABLE,
/*payload_id=*/1234);
EXPECT_EQ(0UL, GetSentInitiateCameraRollItemTransferRequestCount());
EXPECT_EQ(CameraRollManager::Observer::DownloadErrorType::kGenericError,
GetLastDownloadError());
histogram_tester_.ExpectBucketCount(
kDownloadResultMetricName,
util::CameraRollDownloadResult::kInvalidFileName,
/*expected_count=*/1);
}
TEST_F(CameraRollManagerImplTest, DownloadItemFailedWithInsufficientStorage) {
SetCurrentItems({"key1"});
const CameraRollItem& item_to_download =
camera_roll_manager()->current_items().back();
// Request to download the item that was added.
camera_roll_manager()->DownloadItem(item_to_download.metadata());
fake_camera_roll_download_manager()->set_expected_create_payload_files_result(
CameraRollDownloadManager::CreatePayloadFilesResult::
kInsufficientDiskSpace);
SendFetchCameraRollItemDataResponse(
item_to_download.metadata(),
proto::FetchCameraRollItemDataResponse::AVAILABLE,
/*payload_id=*/1234);
EXPECT_EQ(0UL, GetSentInitiateCameraRollItemTransferRequestCount());
EXPECT_EQ(
CameraRollManager::Observer::DownloadErrorType::kInsufficientStorage,
GetLastDownloadError());
histogram_tester_.ExpectBucketCount(
kDownloadResultMetricName,
util::CameraRollDownloadResult::kInsufficientDiskSpace,
/*expected_count=*/1);
}
TEST_F(CameraRollManagerImplTest, DownloadItemFailedWithDuplicatedPayloadId) {
SetCurrentItems({"key1"});
const CameraRollItem& item_to_download =
camera_roll_manager()->current_items().back();
// Request to download the item that was added.
camera_roll_manager()->DownloadItem(item_to_download.metadata());
fake_camera_roll_download_manager()->set_expected_create_payload_files_result(
CameraRollDownloadManager::CreatePayloadFilesResult::
kPayloadAlreadyExists);
SendFetchCameraRollItemDataResponse(
item_to_download.metadata(),
proto::FetchCameraRollItemDataResponse::AVAILABLE,
/*payload_id=*/1234);
EXPECT_EQ(0UL, GetSentInitiateCameraRollItemTransferRequestCount());
EXPECT_EQ(CameraRollManager::Observer::DownloadErrorType::kGenericError,
GetLastDownloadError());
histogram_tester_.ExpectBucketCount(
kDownloadResultMetricName,
util::CameraRollDownloadResult::kPayloadAlreadyExists,
/*expected_count=*/1);
}
TEST_F(CameraRollManagerImplTest, DownloadItemFailedWitDuplicatedFilePath) {
SetCurrentItems({"key1"});
const CameraRollItem& item_to_download =
camera_roll_manager()->current_items().back();
// Request to download the item that was added.
camera_roll_manager()->DownloadItem(item_to_download.metadata());
fake_camera_roll_download_manager()->set_expected_create_payload_files_result(
CameraRollDownloadManager::CreatePayloadFilesResult::kNotUniqueFilePath);
SendFetchCameraRollItemDataResponse(
item_to_download.metadata(),
proto::FetchCameraRollItemDataResponse::AVAILABLE,
/*payload_id=*/1234);
EXPECT_EQ(0UL, GetSentInitiateCameraRollItemTransferRequestCount());
EXPECT_EQ(CameraRollManager::Observer::DownloadErrorType::kGenericError,
GetLastDownloadError());
histogram_tester_.ExpectBucketCount(
kDownloadResultMetricName,
util::CameraRollDownloadResult::kNotUniqueFilePath,
/*expected_count=*/1);
}
TEST_F(CameraRollManagerImplTest, DownloadItemTransferFailed) {
SetCurrentItems({"key1"});
const CameraRollItem& item_to_download =
camera_roll_manager()->current_items().back();
// Request to download the item that was added.
camera_roll_manager()->DownloadItem(item_to_download.metadata());
SendFetchCameraRollItemDataResponse(
item_to_download.metadata(),
proto::FetchCameraRollItemDataResponse::AVAILABLE,
/*payload_id=*/1234);
SendFileTransferUpdate(/*payload_id=*/1234, FileTransferStatus::kInProgress,
/*total_bytes=*/1000, /*bytes_transferred=*/200);
SendFileTransferUpdate(/*payload_id=*/1234, FileTransferStatus::kFailure,
/*total_bytes=*/0, /*bytes_transferred=*/0);
EXPECT_EQ(CameraRollManager::Observer::DownloadErrorType::kNetworkConnection,
GetLastDownloadError());
histogram_tester_.ExpectBucketCount(
kDownloadResultMetricName,
util::CameraRollDownloadResult::kTransferFailed,
/*expected_count=*/1);
}
TEST_F(CameraRollManagerImplTest, DownloadItemTransferCanceled) {
SetCurrentItems({"key1"});
const CameraRollItem& item_to_download =
camera_roll_manager()->current_items().back();
// Request to download the item that was added.
camera_roll_manager()->DownloadItem(item_to_download.metadata());
SendFetchCameraRollItemDataResponse(
item_to_download.metadata(),
proto::FetchCameraRollItemDataResponse::AVAILABLE,
/*payload_id=*/1234);
SendFileTransferUpdate(/*payload_id=*/1234, FileTransferStatus::kInProgress,
/*total_bytes=*/1000, /*bytes_transferred=*/200);
SendFileTransferUpdate(/*payload_id=*/1234, FileTransferStatus::kCanceled,
/*total_bytes=*/0, /*bytes_transferred=*/0);
EXPECT_EQ(CameraRollManager::Observer::DownloadErrorType::kNetworkConnection,
GetLastDownloadError());
histogram_tester_.ExpectBucketCount(
kDownloadResultMetricName,
util::CameraRollDownloadResult::kTransferCanceled,
/*expected_count=*/1);
}
TEST_F(CameraRollManagerImplTest, DownloadItemAndRegisterPayloadFileFail) {
SetCurrentItems({"key1"});
const CameraRollItem& item_to_download =
camera_roll_manager()->current_items().back();
// Request to download the item that was added.
camera_roll_manager()->DownloadItem(item_to_download.metadata());
fake_connection_manager()->set_register_payload_file_result(false);
SendFetchCameraRollItemDataResponse(
item_to_download.metadata(),
proto::FetchCameraRollItemDataResponse::AVAILABLE,
/*payload_id=*/1234);
EXPECT_EQ(0UL, GetSentInitiateCameraRollItemTransferRequestCount());
histogram_tester_.ExpectBucketCount(
kDownloadResultMetricName,
util::CameraRollDownloadResult::kTargetFileNotAccessible,
/*expected_count=*/1);
}
TEST_F(CameraRollManagerImplTest, ItemsClearedWhenDeviceDisconnected) {
fake_connection_manager()->SetStatus(
secure_channel::ConnectionManager::Status::kConnected);
proto::FetchCameraRollItemsResponse response;
PopulateItemProto(response.add_items(), "key3");
PopulateItemProto(response.add_items(), "key2");
PopulateItemProto(response.add_items(), "key1");
fake_message_receiver_.NotifyFetchCameraRollItemsResponseReceived(response);
CompleteThumbnailDecoding(BatchDecodeResult::kCompleted);
EXPECT_EQ(3, GetCurrentItemsCount());
fake_connection_manager()->SetStatus(
secure_channel::ConnectionManager::Status::kDisconnected);
EXPECT_EQ(0, GetCurrentItemsCount());
}
} // namespace ash::phonehub
|