123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- // 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/components/phonehub/message_receiver_impl.h"
- #include <netinet/in.h>
- #include <memory>
- #include "ash/components/phonehub/proto/phonehub_api.pb.h"
- #include "ash/constants/ash_features.h"
- #include "ash/services/secure_channel/public/cpp/client/fake_connection_manager.h"
- #include "base/strings/strcat.h"
- #include "base/test/scoped_feature_list.h"
- #include "testing/gtest/include/gtest/gtest.h"
- namespace ash {
- namespace phonehub {
- namespace {
- class FakeObserver : public MessageReceiver::Observer {
- public:
- FakeObserver() = default;
- ~FakeObserver() override = default;
- size_t snapshot_num_calls() const {
- return phone_status_snapshot_updated_num_calls_;
- }
- size_t status_updated_num_calls() const {
- return phone_status_updated_num_calls_;
- }
- size_t feature_setup_response_num_calls() const {
- return feature_setup_response_num_calls_;
- }
- size_t fetch_camera_roll_items_response_calls() const {
- return fetch_camera_roll_items_response_calls_;
- }
- size_t fetch_camera_roll_item_data_response_calls() const {
- return fetch_camera_roll_item_data_response_calls_;
- }
- proto::PhoneStatusSnapshot last_snapshot() const { return last_snapshot_; }
- proto::PhoneStatusUpdate last_status_update() const {
- return last_status_update_;
- }
- proto::FeatureSetupResponse last_feature_setup_response() const {
- return last_feature_setup_response_;
- }
- proto::FetchCameraRollItemsResponse last_fetch_camera_roll_items_response()
- const {
- return last_fetch_camera_roll_items_response_;
- }
- proto::FetchCameraRollItemDataResponse
- last_fetch_camera_roll_item_data_response() const {
- return last_fetch_camera_roll_item_data_response_;
- }
- // MessageReceiver::Observer:
- void OnPhoneStatusSnapshotReceived(
- proto::PhoneStatusSnapshot phone_status_snapshot) override {
- last_snapshot_ = phone_status_snapshot;
- ++phone_status_snapshot_updated_num_calls_;
- }
- void OnPhoneStatusUpdateReceived(
- proto::PhoneStatusUpdate phone_status_update) override {
- last_status_update_ = phone_status_update;
- ++phone_status_updated_num_calls_;
- }
- void OnFeatureSetupResponseReceived(
- proto::FeatureSetupResponse feature_setup_response) override {
- last_feature_setup_response_ = feature_setup_response;
- ++feature_setup_response_num_calls_;
- }
- void OnFetchCameraRollItemsResponseReceived(
- const proto::FetchCameraRollItemsResponse& response) override {
- last_fetch_camera_roll_items_response_ = response;
- ++fetch_camera_roll_items_response_calls_;
- }
- void OnFetchCameraRollItemDataResponseReceived(
- const proto::FetchCameraRollItemDataResponse& response) override {
- last_fetch_camera_roll_item_data_response_ = response;
- ++fetch_camera_roll_item_data_response_calls_;
- }
- private:
- size_t phone_status_snapshot_updated_num_calls_ = 0;
- size_t phone_status_updated_num_calls_ = 0;
- size_t feature_setup_response_num_calls_ = 0;
- size_t fetch_camera_roll_items_response_calls_ = 0;
- size_t fetch_camera_roll_item_data_response_calls_ = 0;
- proto::PhoneStatusSnapshot last_snapshot_;
- proto::PhoneStatusUpdate last_status_update_;
- proto::FeatureSetupResponse last_feature_setup_response_;
- proto::FetchCameraRollItemsResponse last_fetch_camera_roll_items_response_;
- proto::FetchCameraRollItemDataResponse
- last_fetch_camera_roll_item_data_response_;
- };
- std::string SerializeMessage(proto::MessageType message_type,
- const google::protobuf::MessageLite* request) {
- // Add two space characters, followed by the serialized proto.
- std::string message = base::StrCat({" ", request->SerializeAsString()});
- // Replace the first two characters with |message_type| as a 16-bit int.
- uint16_t* ptr =
- reinterpret_cast<uint16_t*>(const_cast<char*>(message.data()));
- *ptr = htons(static_cast<uint16_t>(message_type));
- return message;
- }
- } // namespace
- class MessageReceiverImplTest : public testing::Test {
- protected:
- MessageReceiverImplTest()
- : fake_connection_manager_(
- std::make_unique<secure_channel::FakeConnectionManager>()) {}
- MessageReceiverImplTest(const MessageReceiverImplTest&) = delete;
- MessageReceiverImplTest& operator=(const MessageReceiverImplTest&) = delete;
- ~MessageReceiverImplTest() override = default;
- void SetUp() override {
- message_receiver_ =
- std::make_unique<MessageReceiverImpl>(fake_connection_manager_.get());
- message_receiver_->AddObserver(&fake_observer_);
- }
- void TearDown() override {
- message_receiver_->RemoveObserver(&fake_observer_);
- }
- size_t GetNumPhoneStatusSnapshotCalls() const {
- return fake_observer_.snapshot_num_calls();
- }
- size_t GetNumPhoneStatusUpdatedCalls() const {
- return fake_observer_.status_updated_num_calls();
- }
- size_t GetNumFeatureSetupResponseCalls() const {
- return fake_observer_.feature_setup_response_num_calls();
- }
- size_t GetNumFetchCameraRollItemsResponseCalls() const {
- return fake_observer_.fetch_camera_roll_items_response_calls();
- }
- size_t GetNumFetchCameraRollItemDataResponseCalls() const {
- return fake_observer_.fetch_camera_roll_item_data_response_calls();
- }
- proto::PhoneStatusSnapshot GetLastSnapshot() const {
- return fake_observer_.last_snapshot();
- }
- proto::PhoneStatusUpdate GetLastStatusUpdate() const {
- return fake_observer_.last_status_update();
- }
- proto::FeatureSetupResponse GetLastFeatureSetupResponse() const {
- return fake_observer_.last_feature_setup_response();
- }
- proto::FetchCameraRollItemsResponse GetLastFetchCameraRollItemsResponse()
- const {
- return fake_observer_.last_fetch_camera_roll_items_response();
- }
- proto::FetchCameraRollItemDataResponse
- GetLastFetchCameraRollItemDataResponse() const {
- return fake_observer_.last_fetch_camera_roll_item_data_response();
- }
- FakeObserver fake_observer_;
- std::unique_ptr<secure_channel::FakeConnectionManager>
- fake_connection_manager_;
- std::unique_ptr<MessageReceiverImpl> message_receiver_;
- };
- TEST_F(MessageReceiverImplTest, OnPhoneStatusSnapshotReceieved) {
- const int32_t expected_battery_percentage = 15;
- auto expected_phone_properties = std::make_unique<proto::PhoneProperties>();
- expected_phone_properties->set_battery_percentage(
- expected_battery_percentage);
- proto::PhoneStatusSnapshot expected_snapshot;
- expected_snapshot.set_allocated_properties(
- expected_phone_properties.release());
- expected_snapshot.add_notifications();
- // Simulate receiving a message.
- const std::string expected_message =
- SerializeMessage(proto::PHONE_STATUS_SNAPSHOT, &expected_snapshot);
- fake_connection_manager_->NotifyMessageReceived(expected_message);
- proto::PhoneStatusSnapshot actual_snapshot = GetLastSnapshot();
- EXPECT_EQ(1u, GetNumPhoneStatusSnapshotCalls());
- EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
- EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());
- EXPECT_EQ(expected_battery_percentage,
- actual_snapshot.properties().battery_percentage());
- EXPECT_EQ(1, actual_snapshot.notifications_size());
- }
- TEST_F(MessageReceiverImplTest, OnPhoneStatusUpdated) {
- const int32_t expected_battery_percentage = 15u;
- auto expected_phone_properties = std::make_unique<proto::PhoneProperties>();
- expected_phone_properties->set_battery_percentage(
- expected_battery_percentage);
- proto::PhoneStatusUpdate expected_update;
- expected_update.set_allocated_properties(expected_phone_properties.release());
- expected_update.add_updated_notifications();
- const int64_t expected_removed_id = 24u;
- expected_update.add_removed_notification_ids(expected_removed_id);
- // Simulate receiving a message.
- const std::string expected_message =
- SerializeMessage(proto::PHONE_STATUS_UPDATE, &expected_update);
- fake_connection_manager_->NotifyMessageReceived(expected_message);
- proto::PhoneStatusUpdate actual_update = GetLastStatusUpdate();
- EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
- EXPECT_EQ(1u, GetNumPhoneStatusUpdatedCalls());
- EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());
- EXPECT_EQ(expected_battery_percentage,
- actual_update.properties().battery_percentage());
- EXPECT_EQ(1, actual_update.updated_notifications_size());
- EXPECT_EQ(expected_removed_id, actual_update.removed_notification_ids()[0]);
- }
- TEST_F(MessageReceiverImplTest,
- OnFeatrueSetupResponseReceivedWithFeatureEnabled) {
- base::test::ScopedFeatureList feature_list;
- feature_list.InitAndEnableFeature(
- features::kPhoneHubFeatureSetupErrorHandling);
- proto::FeatureSetupResponse expected_response;
- expected_response.set_camera_roll_setup_result(
- proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED);
- expected_response.set_notification_setup_result(
- proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED);
- const std::string expected_message =
- SerializeMessage(proto::FEATURE_SETUP_RESPONSE, &expected_response);
- fake_connection_manager_->NotifyMessageReceived(expected_message);
- proto::FeatureSetupResponse actual_response = GetLastFeatureSetupResponse();
- EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
- EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
- EXPECT_EQ(1u, GetNumFeatureSetupResponseCalls());
- EXPECT_EQ(proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED,
- actual_response.camera_roll_setup_result());
- EXPECT_EQ(proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED,
- actual_response.notification_setup_result());
- }
- TEST_F(MessageReceiverImplTest,
- OnFeatrueSetupResponseReceivedWithFeatureDisabled) {
- base::test::ScopedFeatureList feature_list;
- feature_list.InitAndDisableFeature(
- features::kPhoneHubFeatureSetupErrorHandling);
- proto::FeatureSetupResponse expected_response;
- expected_response.set_camera_roll_setup_result(
- proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED);
- expected_response.set_notification_setup_result(
- proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED);
- const std::string expected_message =
- SerializeMessage(proto::FEATURE_SETUP_RESPONSE, &expected_response);
- fake_connection_manager_->NotifyMessageReceived(expected_message);
- proto::FeatureSetupResponse actual_response = GetLastFeatureSetupResponse();
- EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
- EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
- EXPECT_EQ(0u, GetNumFeatureSetupResponseCalls());
- }
- TEST_F(MessageReceiverImplTest,
- OnFetchCameraRollItemsResponseReceivedWthFeatureEnabled) {
- base::test::ScopedFeatureList feature_list;
- feature_list.InitAndEnableFeature(features::kPhoneHubCameraRoll);
- proto::FetchCameraRollItemsResponse expected_response;
- proto::CameraRollItem* item_proto = expected_response.add_items();
- proto::CameraRollItemMetadata* metadata = item_proto->mutable_metadata();
- metadata->set_key("key");
- proto::CameraRollItemThumbnail* thumbnail = item_proto->mutable_thumbnail();
- thumbnail->set_data("encoded_thumbnail_data");
- // Simulate receiving a message.
- const std::string expected_message = SerializeMessage(
- proto::FETCH_CAMERA_ROLL_ITEMS_RESPONSE, &expected_response);
- fake_connection_manager_->NotifyMessageReceived(expected_message);
- proto::FetchCameraRollItemsResponse actual_response =
- GetLastFetchCameraRollItemsResponse();
- EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
- EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
- EXPECT_EQ(0u, GetNumFeatureSetupResponseCalls());
- EXPECT_EQ(1u, GetNumFetchCameraRollItemsResponseCalls());
- EXPECT_EQ(1, actual_response.items_size());
- EXPECT_EQ("key", actual_response.items(0).metadata().key());
- EXPECT_EQ("encoded_thumbnail_data",
- actual_response.items(0).thumbnail().data());
- }
- TEST_F(MessageReceiverImplTest,
- OnFetchCameraRollItemsResponseReceivedWithFeatureDisabled) {
- base::test::ScopedFeatureList feature_list;
- feature_list.InitAndDisableFeature(features::kPhoneHubCameraRoll);
- proto::FetchCameraRollItemsResponse expected_response;
- proto::CameraRollItem* item_proto = expected_response.add_items();
- proto::CameraRollItemMetadata* metadata = item_proto->mutable_metadata();
- metadata->set_key("key");
- proto::CameraRollItemThumbnail* thumbnail = item_proto->mutable_thumbnail();
- thumbnail->set_data("encoded_thumbnail_data");
- // Simulate receiving a message.
- const std::string expected_message = SerializeMessage(
- proto::FETCH_CAMERA_ROLL_ITEMS_RESPONSE, &expected_response);
- fake_connection_manager_->NotifyMessageReceived(expected_message);
- EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
- EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
- EXPECT_EQ(0u, GetNumFeatureSetupResponseCalls());
- EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());
- }
- TEST_F(MessageReceiverImplTest,
- OnFetchCameraRollItemDataResponseReceivedWthFeatureEnabled) {
- base::test::ScopedFeatureList feature_list;
- feature_list.InitAndEnableFeature(features::kPhoneHubCameraRoll);
- proto::FetchCameraRollItemDataResponse expected_response;
- expected_response.mutable_metadata()->set_key("key");
- expected_response.set_file_availability(
- proto::FetchCameraRollItemDataResponse::AVAILABLE);
- expected_response.set_payload_id(1234);
- // Simulate receiving a message.
- const std::string expected_message = SerializeMessage(
- proto::FETCH_CAMERA_ROLL_ITEM_DATA_RESPONSE, &expected_response);
- fake_connection_manager_->NotifyMessageReceived(expected_message);
- proto::FetchCameraRollItemDataResponse actual_response =
- GetLastFetchCameraRollItemDataResponse();
- EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
- EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
- EXPECT_EQ(0u, GetNumFeatureSetupResponseCalls());
- EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());
- EXPECT_EQ(1u, GetNumFetchCameraRollItemDataResponseCalls());
- EXPECT_EQ("key", actual_response.metadata().key());
- EXPECT_EQ(proto::FetchCameraRollItemDataResponse::AVAILABLE,
- actual_response.file_availability());
- EXPECT_EQ(1234, actual_response.payload_id());
- }
- TEST_F(MessageReceiverImplTest,
- OnFetchCameraRollItemDataResponseReceivedWithFeatureDisabled) {
- base::test::ScopedFeatureList feature_list;
- feature_list.InitAndDisableFeature(features::kPhoneHubCameraRoll);
- proto::FetchCameraRollItemDataResponse expected_response;
- expected_response.mutable_metadata()->set_key("key");
- expected_response.set_file_availability(
- proto::FetchCameraRollItemDataResponse::AVAILABLE);
- expected_response.set_payload_id(1234);
- // Simulate receiving a message.
- const std::string expected_message = SerializeMessage(
- proto::FETCH_CAMERA_ROLL_ITEM_DATA_RESPONSE, &expected_response);
- fake_connection_manager_->NotifyMessageReceived(expected_message);
- EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
- EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
- EXPECT_EQ(0u, GetNumFeatureSetupResponseCalls());
- EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());
- EXPECT_EQ(0u, GetNumFetchCameraRollItemDataResponseCalls());
- }
- } // namespace phonehub
- } // namespace ash
|