message_receiver_unittest.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/components/phonehub/message_receiver_impl.h"
  5. #include <netinet/in.h>
  6. #include <memory>
  7. #include "ash/components/phonehub/proto/phonehub_api.pb.h"
  8. #include "ash/constants/ash_features.h"
  9. #include "ash/services/secure_channel/public/cpp/client/fake_connection_manager.h"
  10. #include "base/strings/strcat.h"
  11. #include "base/test/scoped_feature_list.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace ash {
  14. namespace phonehub {
  15. namespace {
  16. class FakeObserver : public MessageReceiver::Observer {
  17. public:
  18. FakeObserver() = default;
  19. ~FakeObserver() override = default;
  20. size_t snapshot_num_calls() const {
  21. return phone_status_snapshot_updated_num_calls_;
  22. }
  23. size_t status_updated_num_calls() const {
  24. return phone_status_updated_num_calls_;
  25. }
  26. size_t feature_setup_response_num_calls() const {
  27. return feature_setup_response_num_calls_;
  28. }
  29. size_t fetch_camera_roll_items_response_calls() const {
  30. return fetch_camera_roll_items_response_calls_;
  31. }
  32. size_t fetch_camera_roll_item_data_response_calls() const {
  33. return fetch_camera_roll_item_data_response_calls_;
  34. }
  35. proto::PhoneStatusSnapshot last_snapshot() const { return last_snapshot_; }
  36. proto::PhoneStatusUpdate last_status_update() const {
  37. return last_status_update_;
  38. }
  39. proto::FeatureSetupResponse last_feature_setup_response() const {
  40. return last_feature_setup_response_;
  41. }
  42. proto::FetchCameraRollItemsResponse last_fetch_camera_roll_items_response()
  43. const {
  44. return last_fetch_camera_roll_items_response_;
  45. }
  46. proto::FetchCameraRollItemDataResponse
  47. last_fetch_camera_roll_item_data_response() const {
  48. return last_fetch_camera_roll_item_data_response_;
  49. }
  50. // MessageReceiver::Observer:
  51. void OnPhoneStatusSnapshotReceived(
  52. proto::PhoneStatusSnapshot phone_status_snapshot) override {
  53. last_snapshot_ = phone_status_snapshot;
  54. ++phone_status_snapshot_updated_num_calls_;
  55. }
  56. void OnPhoneStatusUpdateReceived(
  57. proto::PhoneStatusUpdate phone_status_update) override {
  58. last_status_update_ = phone_status_update;
  59. ++phone_status_updated_num_calls_;
  60. }
  61. void OnFeatureSetupResponseReceived(
  62. proto::FeatureSetupResponse feature_setup_response) override {
  63. last_feature_setup_response_ = feature_setup_response;
  64. ++feature_setup_response_num_calls_;
  65. }
  66. void OnFetchCameraRollItemsResponseReceived(
  67. const proto::FetchCameraRollItemsResponse& response) override {
  68. last_fetch_camera_roll_items_response_ = response;
  69. ++fetch_camera_roll_items_response_calls_;
  70. }
  71. void OnFetchCameraRollItemDataResponseReceived(
  72. const proto::FetchCameraRollItemDataResponse& response) override {
  73. last_fetch_camera_roll_item_data_response_ = response;
  74. ++fetch_camera_roll_item_data_response_calls_;
  75. }
  76. private:
  77. size_t phone_status_snapshot_updated_num_calls_ = 0;
  78. size_t phone_status_updated_num_calls_ = 0;
  79. size_t feature_setup_response_num_calls_ = 0;
  80. size_t fetch_camera_roll_items_response_calls_ = 0;
  81. size_t fetch_camera_roll_item_data_response_calls_ = 0;
  82. proto::PhoneStatusSnapshot last_snapshot_;
  83. proto::PhoneStatusUpdate last_status_update_;
  84. proto::FeatureSetupResponse last_feature_setup_response_;
  85. proto::FetchCameraRollItemsResponse last_fetch_camera_roll_items_response_;
  86. proto::FetchCameraRollItemDataResponse
  87. last_fetch_camera_roll_item_data_response_;
  88. };
  89. std::string SerializeMessage(proto::MessageType message_type,
  90. const google::protobuf::MessageLite* request) {
  91. // Add two space characters, followed by the serialized proto.
  92. std::string message = base::StrCat({" ", request->SerializeAsString()});
  93. // Replace the first two characters with |message_type| as a 16-bit int.
  94. uint16_t* ptr =
  95. reinterpret_cast<uint16_t*>(const_cast<char*>(message.data()));
  96. *ptr = htons(static_cast<uint16_t>(message_type));
  97. return message;
  98. }
  99. } // namespace
  100. class MessageReceiverImplTest : public testing::Test {
  101. protected:
  102. MessageReceiverImplTest()
  103. : fake_connection_manager_(
  104. std::make_unique<secure_channel::FakeConnectionManager>()) {}
  105. MessageReceiverImplTest(const MessageReceiverImplTest&) = delete;
  106. MessageReceiverImplTest& operator=(const MessageReceiverImplTest&) = delete;
  107. ~MessageReceiverImplTest() override = default;
  108. void SetUp() override {
  109. message_receiver_ =
  110. std::make_unique<MessageReceiverImpl>(fake_connection_manager_.get());
  111. message_receiver_->AddObserver(&fake_observer_);
  112. }
  113. void TearDown() override {
  114. message_receiver_->RemoveObserver(&fake_observer_);
  115. }
  116. size_t GetNumPhoneStatusSnapshotCalls() const {
  117. return fake_observer_.snapshot_num_calls();
  118. }
  119. size_t GetNumPhoneStatusUpdatedCalls() const {
  120. return fake_observer_.status_updated_num_calls();
  121. }
  122. size_t GetNumFeatureSetupResponseCalls() const {
  123. return fake_observer_.feature_setup_response_num_calls();
  124. }
  125. size_t GetNumFetchCameraRollItemsResponseCalls() const {
  126. return fake_observer_.fetch_camera_roll_items_response_calls();
  127. }
  128. size_t GetNumFetchCameraRollItemDataResponseCalls() const {
  129. return fake_observer_.fetch_camera_roll_item_data_response_calls();
  130. }
  131. proto::PhoneStatusSnapshot GetLastSnapshot() const {
  132. return fake_observer_.last_snapshot();
  133. }
  134. proto::PhoneStatusUpdate GetLastStatusUpdate() const {
  135. return fake_observer_.last_status_update();
  136. }
  137. proto::FeatureSetupResponse GetLastFeatureSetupResponse() const {
  138. return fake_observer_.last_feature_setup_response();
  139. }
  140. proto::FetchCameraRollItemsResponse GetLastFetchCameraRollItemsResponse()
  141. const {
  142. return fake_observer_.last_fetch_camera_roll_items_response();
  143. }
  144. proto::FetchCameraRollItemDataResponse
  145. GetLastFetchCameraRollItemDataResponse() const {
  146. return fake_observer_.last_fetch_camera_roll_item_data_response();
  147. }
  148. FakeObserver fake_observer_;
  149. std::unique_ptr<secure_channel::FakeConnectionManager>
  150. fake_connection_manager_;
  151. std::unique_ptr<MessageReceiverImpl> message_receiver_;
  152. };
  153. TEST_F(MessageReceiverImplTest, OnPhoneStatusSnapshotReceieved) {
  154. const int32_t expected_battery_percentage = 15;
  155. auto expected_phone_properties = std::make_unique<proto::PhoneProperties>();
  156. expected_phone_properties->set_battery_percentage(
  157. expected_battery_percentage);
  158. proto::PhoneStatusSnapshot expected_snapshot;
  159. expected_snapshot.set_allocated_properties(
  160. expected_phone_properties.release());
  161. expected_snapshot.add_notifications();
  162. // Simulate receiving a message.
  163. const std::string expected_message =
  164. SerializeMessage(proto::PHONE_STATUS_SNAPSHOT, &expected_snapshot);
  165. fake_connection_manager_->NotifyMessageReceived(expected_message);
  166. proto::PhoneStatusSnapshot actual_snapshot = GetLastSnapshot();
  167. EXPECT_EQ(1u, GetNumPhoneStatusSnapshotCalls());
  168. EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
  169. EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());
  170. EXPECT_EQ(expected_battery_percentage,
  171. actual_snapshot.properties().battery_percentage());
  172. EXPECT_EQ(1, actual_snapshot.notifications_size());
  173. }
  174. TEST_F(MessageReceiverImplTest, OnPhoneStatusUpdated) {
  175. const int32_t expected_battery_percentage = 15u;
  176. auto expected_phone_properties = std::make_unique<proto::PhoneProperties>();
  177. expected_phone_properties->set_battery_percentage(
  178. expected_battery_percentage);
  179. proto::PhoneStatusUpdate expected_update;
  180. expected_update.set_allocated_properties(expected_phone_properties.release());
  181. expected_update.add_updated_notifications();
  182. const int64_t expected_removed_id = 24u;
  183. expected_update.add_removed_notification_ids(expected_removed_id);
  184. // Simulate receiving a message.
  185. const std::string expected_message =
  186. SerializeMessage(proto::PHONE_STATUS_UPDATE, &expected_update);
  187. fake_connection_manager_->NotifyMessageReceived(expected_message);
  188. proto::PhoneStatusUpdate actual_update = GetLastStatusUpdate();
  189. EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
  190. EXPECT_EQ(1u, GetNumPhoneStatusUpdatedCalls());
  191. EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());
  192. EXPECT_EQ(expected_battery_percentage,
  193. actual_update.properties().battery_percentage());
  194. EXPECT_EQ(1, actual_update.updated_notifications_size());
  195. EXPECT_EQ(expected_removed_id, actual_update.removed_notification_ids()[0]);
  196. }
  197. TEST_F(MessageReceiverImplTest,
  198. OnFeatrueSetupResponseReceivedWithFeatureEnabled) {
  199. base::test::ScopedFeatureList feature_list;
  200. feature_list.InitAndEnableFeature(
  201. features::kPhoneHubFeatureSetupErrorHandling);
  202. proto::FeatureSetupResponse expected_response;
  203. expected_response.set_camera_roll_setup_result(
  204. proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED);
  205. expected_response.set_notification_setup_result(
  206. proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED);
  207. const std::string expected_message =
  208. SerializeMessage(proto::FEATURE_SETUP_RESPONSE, &expected_response);
  209. fake_connection_manager_->NotifyMessageReceived(expected_message);
  210. proto::FeatureSetupResponse actual_response = GetLastFeatureSetupResponse();
  211. EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
  212. EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
  213. EXPECT_EQ(1u, GetNumFeatureSetupResponseCalls());
  214. EXPECT_EQ(proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED,
  215. actual_response.camera_roll_setup_result());
  216. EXPECT_EQ(proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED,
  217. actual_response.notification_setup_result());
  218. }
  219. TEST_F(MessageReceiverImplTest,
  220. OnFeatrueSetupResponseReceivedWithFeatureDisabled) {
  221. base::test::ScopedFeatureList feature_list;
  222. feature_list.InitAndDisableFeature(
  223. features::kPhoneHubFeatureSetupErrorHandling);
  224. proto::FeatureSetupResponse expected_response;
  225. expected_response.set_camera_roll_setup_result(
  226. proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED);
  227. expected_response.set_notification_setup_result(
  228. proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED);
  229. const std::string expected_message =
  230. SerializeMessage(proto::FEATURE_SETUP_RESPONSE, &expected_response);
  231. fake_connection_manager_->NotifyMessageReceived(expected_message);
  232. proto::FeatureSetupResponse actual_response = GetLastFeatureSetupResponse();
  233. EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
  234. EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
  235. EXPECT_EQ(0u, GetNumFeatureSetupResponseCalls());
  236. }
  237. TEST_F(MessageReceiverImplTest,
  238. OnFetchCameraRollItemsResponseReceivedWthFeatureEnabled) {
  239. base::test::ScopedFeatureList feature_list;
  240. feature_list.InitAndEnableFeature(features::kPhoneHubCameraRoll);
  241. proto::FetchCameraRollItemsResponse expected_response;
  242. proto::CameraRollItem* item_proto = expected_response.add_items();
  243. proto::CameraRollItemMetadata* metadata = item_proto->mutable_metadata();
  244. metadata->set_key("key");
  245. proto::CameraRollItemThumbnail* thumbnail = item_proto->mutable_thumbnail();
  246. thumbnail->set_data("encoded_thumbnail_data");
  247. // Simulate receiving a message.
  248. const std::string expected_message = SerializeMessage(
  249. proto::FETCH_CAMERA_ROLL_ITEMS_RESPONSE, &expected_response);
  250. fake_connection_manager_->NotifyMessageReceived(expected_message);
  251. proto::FetchCameraRollItemsResponse actual_response =
  252. GetLastFetchCameraRollItemsResponse();
  253. EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
  254. EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
  255. EXPECT_EQ(0u, GetNumFeatureSetupResponseCalls());
  256. EXPECT_EQ(1u, GetNumFetchCameraRollItemsResponseCalls());
  257. EXPECT_EQ(1, actual_response.items_size());
  258. EXPECT_EQ("key", actual_response.items(0).metadata().key());
  259. EXPECT_EQ("encoded_thumbnail_data",
  260. actual_response.items(0).thumbnail().data());
  261. }
  262. TEST_F(MessageReceiverImplTest,
  263. OnFetchCameraRollItemsResponseReceivedWithFeatureDisabled) {
  264. base::test::ScopedFeatureList feature_list;
  265. feature_list.InitAndDisableFeature(features::kPhoneHubCameraRoll);
  266. proto::FetchCameraRollItemsResponse expected_response;
  267. proto::CameraRollItem* item_proto = expected_response.add_items();
  268. proto::CameraRollItemMetadata* metadata = item_proto->mutable_metadata();
  269. metadata->set_key("key");
  270. proto::CameraRollItemThumbnail* thumbnail = item_proto->mutable_thumbnail();
  271. thumbnail->set_data("encoded_thumbnail_data");
  272. // Simulate receiving a message.
  273. const std::string expected_message = SerializeMessage(
  274. proto::FETCH_CAMERA_ROLL_ITEMS_RESPONSE, &expected_response);
  275. fake_connection_manager_->NotifyMessageReceived(expected_message);
  276. EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
  277. EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
  278. EXPECT_EQ(0u, GetNumFeatureSetupResponseCalls());
  279. EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());
  280. }
  281. TEST_F(MessageReceiverImplTest,
  282. OnFetchCameraRollItemDataResponseReceivedWthFeatureEnabled) {
  283. base::test::ScopedFeatureList feature_list;
  284. feature_list.InitAndEnableFeature(features::kPhoneHubCameraRoll);
  285. proto::FetchCameraRollItemDataResponse expected_response;
  286. expected_response.mutable_metadata()->set_key("key");
  287. expected_response.set_file_availability(
  288. proto::FetchCameraRollItemDataResponse::AVAILABLE);
  289. expected_response.set_payload_id(1234);
  290. // Simulate receiving a message.
  291. const std::string expected_message = SerializeMessage(
  292. proto::FETCH_CAMERA_ROLL_ITEM_DATA_RESPONSE, &expected_response);
  293. fake_connection_manager_->NotifyMessageReceived(expected_message);
  294. proto::FetchCameraRollItemDataResponse actual_response =
  295. GetLastFetchCameraRollItemDataResponse();
  296. EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
  297. EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
  298. EXPECT_EQ(0u, GetNumFeatureSetupResponseCalls());
  299. EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());
  300. EXPECT_EQ(1u, GetNumFetchCameraRollItemDataResponseCalls());
  301. EXPECT_EQ("key", actual_response.metadata().key());
  302. EXPECT_EQ(proto::FetchCameraRollItemDataResponse::AVAILABLE,
  303. actual_response.file_availability());
  304. EXPECT_EQ(1234, actual_response.payload_id());
  305. }
  306. TEST_F(MessageReceiverImplTest,
  307. OnFetchCameraRollItemDataResponseReceivedWithFeatureDisabled) {
  308. base::test::ScopedFeatureList feature_list;
  309. feature_list.InitAndDisableFeature(features::kPhoneHubCameraRoll);
  310. proto::FetchCameraRollItemDataResponse expected_response;
  311. expected_response.mutable_metadata()->set_key("key");
  312. expected_response.set_file_availability(
  313. proto::FetchCameraRollItemDataResponse::AVAILABLE);
  314. expected_response.set_payload_id(1234);
  315. // Simulate receiving a message.
  316. const std::string expected_message = SerializeMessage(
  317. proto::FETCH_CAMERA_ROLL_ITEM_DATA_RESPONSE, &expected_response);
  318. fake_connection_manager_->NotifyMessageReceived(expected_message);
  319. EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
  320. EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
  321. EXPECT_EQ(0u, GetNumFeatureSetupResponseCalls());
  322. EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());
  323. EXPECT_EQ(0u, GetNumFetchCameraRollItemDataResponseCalls());
  324. }
  325. } // namespace phonehub
  326. } // namespace ash