-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[QuickStart] Add barebones TargetDeviceClient with SendPayload
This CL adds the base client class with public interfaces coming later that integrates with AuthenticatedConnection to prepare requests and parse responses of round trips with Android. go/cros-quick-start-base-client-class TEST=Written unit test initializing and testing SendPayload using a fake_nearby_connection. Bug: b/258680767 Change-Id: Ib44a81574ea1820f40aa16032200d0a3d59237c3 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4031533 Reviewed-by: Curt Clemens <cclem@google.com> Commit-Queue: Jason Rhee <jasonrhee@google.com> Reviewed-by: Brittany Hartmire <bhartmire@google.com> Cr-Commit-Position: refs/heads/main@{#1073645}
- Loading branch information
Jason Rhee
authored and
Chromium LUCI CQ
committed
Nov 19, 2022
1 parent
06ea495
commit 024fb1f
Showing
5 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ hansenmichael@google.com | |
cclem@google.com | ||
bhartmire@google.com | ||
hansberry@chromium.org | ||
jasonrhee@google.com |
42 changes: 42 additions & 0 deletions
42
chrome/browser/ash/login/oobe_quick_start/connectivity/target_device_client_base.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2022 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/browser/ash/login/oobe_quick_start/connectivity/target_device_client_base.h" | ||
|
||
#include "base/json/json_writer.h" | ||
#include "base/values.h" | ||
#include "chrome/browser/ash/login/oobe_quick_start/connectivity/quick_start_decoder.h" | ||
#include "chrome/browser/nearby_sharing/public/cpp/nearby_connection.h" | ||
|
||
namespace ash::quick_start { | ||
|
||
TargetDeviceClientBase::TargetDeviceClientBase( | ||
NearbyConnection* nearby_connection, | ||
QuickStartDecoder* quick_start_decoder) | ||
: nearby_connection_(nearby_connection), | ||
quick_start_decoder_(quick_start_decoder) { | ||
CHECK(nearby_connection_); | ||
// TODO(b/258680767): Uncomment this after we can pass in QuickStartDecoder. | ||
// CHECK(quick_start_decoder_); | ||
} | ||
|
||
TargetDeviceClientBase::~TargetDeviceClientBase() = default; | ||
|
||
void TargetDeviceClientBase::SendPayload( | ||
const base::Value::Dict& message_payload) { | ||
std::string json_serialized_payload; | ||
CHECK(base::JSONWriter::Write(message_payload, &json_serialized_payload)); | ||
std::vector<uint8_t> request_payload(json_serialized_payload.begin(), | ||
json_serialized_payload.end()); | ||
nearby_connection_->Write(request_payload); | ||
nearby_connection_->Read(base::BindOnce(&TargetDeviceClientBase::OnDataRead, | ||
weak_ptr_factory_.GetWeakPtr())); | ||
} | ||
|
||
void TargetDeviceClientBase::OnDataRead( | ||
absl::optional<std::vector<uint8_t>> bytes) { | ||
NOTIMPLEMENTED(); | ||
} | ||
|
||
} // namespace ash::quick_start |
46 changes: 46 additions & 0 deletions
46
chrome/browser/ash/login/oobe_quick_start/connectivity/target_device_client_base.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2022 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CHROME_BROWSER_ASH_LOGIN_OOBE_QUICK_START_CONNECTIVITY_TARGET_DEVICE_CLIENT_BASE_H_ | ||
#define CHROME_BROWSER_ASH_LOGIN_OOBE_QUICK_START_CONNECTIVITY_TARGET_DEVICE_CLIENT_BASE_H_ | ||
|
||
#include "base/values.h" | ||
#include "chrome/browser/ash/login/oobe_quick_start/connectivity/quick_start_decoder.h" | ||
#include "chrome/browser/nearby_sharing/public/cpp/nearby_connection.h" | ||
|
||
namespace ash::quick_start { | ||
|
||
class TargetDeviceClientBaseTest; | ||
|
||
// TargetDeviceClientBase is the parent class for all clients that will prepare | ||
// requests and parse responses for all round trips between the Chromebook | ||
// and Android phone during Quick Start. There will always be only one client | ||
// running at any time. Multiple clients will interfere with each other. | ||
class TargetDeviceClientBase { | ||
public: | ||
TargetDeviceClientBase(const TargetDeviceClientBase&) = delete; | ||
TargetDeviceClientBase& operator=(const TargetDeviceClientBase&) = delete; | ||
~TargetDeviceClientBase(); | ||
|
||
protected: | ||
TargetDeviceClientBase(NearbyConnection* nearby_connection, | ||
QuickStartDecoder* quick_start_decoder); | ||
void SendPayload(const base::Value::Dict& message_payload); | ||
|
||
// passed as a callback to nearby_connection_->Read(); | ||
void OnDataRead(absl::optional<std::vector<uint8_t>> bytes); | ||
|
||
NearbyConnection* nearby_connection_; | ||
QuickStartDecoder* quick_start_decoder_; | ||
|
||
private: | ||
// This will allow TargetDeviceClientBaseTest to call the protected methods | ||
// for unit tests. | ||
friend class TargetDeviceClientBaseTest; | ||
base::WeakPtrFactory<TargetDeviceClientBase> weak_ptr_factory_{this}; | ||
}; | ||
|
||
} // namespace ash::quick_start | ||
|
||
#endif // CHROME_BROWSER_ASH_LOGIN_OOBE_QUICK_START_CONNECTIVITY_TARGET_DEVICE_CLIENT_BASE_H_ |
81 changes: 81 additions & 0 deletions
81
chrome/browser/ash/login/oobe_quick_start/connectivity/target_device_client_base_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2022 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/browser/ash/login/oobe_quick_start/connectivity/target_device_client_base.h" | ||
|
||
#include "base/json/json_reader.h" | ||
#include "chrome/browser/nearby_sharing/fake_nearby_connection.h" | ||
#include "chrome/browser/nearby_sharing/public/cpp/nearby_connection.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace ash::quick_start { | ||
|
||
namespace { | ||
const char kTestMessagePayloadKey[] = "bootstrapOptions"; | ||
const char kTestMessagePayloadValue[] = "testValue"; | ||
|
||
class TestTargetDeviceClient : public TargetDeviceClientBase { | ||
public: | ||
explicit TestTargetDeviceClient(NearbyConnection* nearby_connection, | ||
QuickStartDecoder* quick_start_decoder) | ||
: TargetDeviceClientBase(nearby_connection, quick_start_decoder) {} | ||
}; | ||
|
||
} // namespace | ||
|
||
class TargetDeviceClientBaseTest : public testing::Test { | ||
public: | ||
TargetDeviceClientBaseTest() = default; | ||
TargetDeviceClientBaseTest(TargetDeviceClientBaseTest&) = delete; | ||
TargetDeviceClientBaseTest& operator=(TargetDeviceClientBaseTest&) = delete; | ||
~TargetDeviceClientBaseTest() override = default; | ||
|
||
void SetUp() override { | ||
fake_nearby_connection_ = std::make_unique<FakeNearbyConnection>(); | ||
NearbyConnection* nearby_connection = fake_nearby_connection_.get(); | ||
CreateDeviceClient(nearby_connection); | ||
} | ||
|
||
void CreateDeviceClient(NearbyConnection* nearby_connection) { | ||
target_device_client_base_ = | ||
std::make_unique<TestTargetDeviceClient>(nearby_connection, nullptr); | ||
} | ||
|
||
void SendPayload(const base::Value::Dict& message_payload) { | ||
target_device_client_base_->SendPayload(message_payload); | ||
} | ||
|
||
protected: | ||
std::unique_ptr<FakeNearbyConnection> fake_nearby_connection_; | ||
std::unique_ptr<TargetDeviceClientBase> target_device_client_base_; | ||
}; | ||
|
||
TEST_F(TargetDeviceClientBaseTest, SendPayload) { | ||
base::Value::Dict message_payload; | ||
message_payload.Set(kTestMessagePayloadKey, kTestMessagePayloadValue); | ||
SendPayload(message_payload); | ||
std::vector<uint8_t> written_payload = | ||
fake_nearby_connection_->GetWrittenData(); | ||
std::string written_payload_string(written_payload.begin(), | ||
written_payload.end()); | ||
absl::optional<base::Value> parsed_json = | ||
base::JSONReader::Read(written_payload_string); | ||
ASSERT_TRUE(parsed_json); | ||
ASSERT_TRUE(parsed_json->is_dict()); | ||
base::Value::Dict& parsed_json_dict = parsed_json.value().GetDict(); | ||
EXPECT_EQ(*parsed_json_dict.FindString(kTestMessagePayloadKey), | ||
kTestMessagePayloadValue); | ||
} | ||
|
||
TEST_F(TargetDeviceClientBaseTest, SendPayload_EmptyPayload) { | ||
base::Value::Dict message_payload; | ||
SendPayload(message_payload); | ||
std::vector<uint8_t> written_payload = | ||
fake_nearby_connection_->GetWrittenData(); | ||
std::string written_payload_string(written_payload.begin(), | ||
written_payload.end()); | ||
EXPECT_TRUE(written_payload_string == "{}"); | ||
} | ||
|
||
} // namespace ash::quick_start |