-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Misc changes to support testing. E.g., to avoid needing to create a V8 Script h5vcc, introduced cobalt_metrics_uploader_callback abstract class that can be mocked in tests. b/280094891 Change-Id: I0b1a19547b1be2747571fb451286ae67dd31b554
- Loading branch information
Showing
15 changed files
with
513 additions
and
39 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
28 changes: 28 additions & 0 deletions
28
cobalt/browser/metrics/cobalt_h5vcc_metrics_uploader_callback.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,28 @@ | ||
// Copyright 2023 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "cobalt/browser/metrics/cobalt_h5vcc_metrics_uploader_callback.h" | ||
|
||
namespace cobalt { | ||
namespace browser { | ||
namespace metrics { | ||
|
||
void CobaltH5vccMetricsUploaderCallback::Run( | ||
const cobalt::h5vcc::H5vccMetricType& type, const std::string& payload) { | ||
event_handler_->callback.value().Run(type, payload); | ||
} | ||
|
||
} // namespace metrics | ||
} // namespace browser | ||
} // namespace cobalt |
48 changes: 48 additions & 0 deletions
48
cobalt/browser/metrics/cobalt_h5vcc_metrics_uploader_callback.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,48 @@ | ||
// Copyright 2023 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef COBALT_BROWSER_METRICS_COBALT_H5VCC_METRICS_UPLOADER_CALLBACK_H_ | ||
#define COBALT_BROWSER_METRICS_COBALT_H5VCC_METRICS_UPLOADER_CALLBACK_H_ | ||
|
||
#include <string> | ||
|
||
#include "cobalt/browser/metrics/cobalt_metrics_uploader_callback.h" | ||
#include "cobalt/h5vcc/h5vcc_metric_type.h" | ||
#include "cobalt/h5vcc/metric_event_handler_wrapper.h" | ||
|
||
namespace cobalt { | ||
namespace browser { | ||
namespace metrics { | ||
|
||
// An implementation of CobaltMetricsUploaderCallback to support binding an | ||
// H5vcc (JS) callback to be called with a metric payload. | ||
class CobaltH5vccMetricsUploaderCallback | ||
: public CobaltMetricsUploaderCallback { | ||
public: | ||
CobaltH5vccMetricsUploaderCallback( | ||
h5vcc::MetricEventHandlerWrapper* event_handler) | ||
: event_handler_(event_handler) {} | ||
|
||
// Runs the h5vcc event_handler callback for the given type and payload. | ||
void Run(const cobalt::h5vcc::H5vccMetricType& type, | ||
const std::string& payload) override; | ||
|
||
private: | ||
h5vcc::MetricEventHandlerWrapper* event_handler_; | ||
}; | ||
} // namespace metrics | ||
} // namespace browser | ||
} // namespace cobalt | ||
|
||
#endif // COBALT_BROWSER_METRICS_COBALT_H5VCC_METRICS_UPLOADER_CALLBACK_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
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
129 changes: 129 additions & 0 deletions
129
cobalt/browser/metrics/cobalt_metrics_log_uploader_test.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,129 @@ | ||
// Copyright 2023 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "cobalt/browser/metrics/cobalt_metrics_log_uploader.h" | ||
|
||
#include <memory> | ||
|
||
#include "cobalt/browser/metrics/cobalt_metrics_uploader_callback.h" | ||
#include "cobalt/h5vcc/h5vcc_metrics.h" | ||
#include "testing/gmock/include/gmock/gmock.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
#include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h" | ||
#include "third_party/metrics_proto/reporting_info.pb.h" | ||
#include "third_party/zlib/google/compression_utils.h" | ||
|
||
|
||
namespace cobalt { | ||
namespace browser { | ||
namespace metrics { | ||
|
||
using cobalt::h5vcc::H5vccMetrics; | ||
using cobalt::h5vcc::MetricEventHandler; | ||
using cobalt::h5vcc::MetricEventHandlerWrapper; | ||
using ::testing::_; | ||
using ::testing::Eq; | ||
using ::testing::StrEq; | ||
using ::testing::StrictMock; | ||
|
||
class CobaltMetricsLogUploaderTest : public ::testing::Test { | ||
public: | ||
void SetUp() override { | ||
uploader_ = std::make_unique<CobaltMetricsLogUploader>( | ||
::metrics::MetricsLogUploader::MetricServiceType::UMA, | ||
base::Bind(&CobaltMetricsLogUploaderTest::UploadCompleteCallback, | ||
base::Unretained(this))); | ||
} | ||
|
||
void TearDown() override {} | ||
|
||
void UploadCompleteCallback(int response_code, int error_code, | ||
bool was_https) { | ||
callback_count_++; | ||
} | ||
|
||
protected: | ||
std::unique_ptr<CobaltMetricsLogUploader> uploader_; | ||
int callback_count_ = 0; | ||
}; | ||
|
||
class MockMetricsUploaderCallback : public CobaltMetricsUploaderCallback { | ||
public: | ||
MOCK_METHOD2(Run, void(const cobalt::h5vcc::H5vccMetricType& type, | ||
const std::string& payload)); | ||
}; | ||
|
||
TEST_F(CobaltMetricsLogUploaderTest, TriggersUploadHandler) { | ||
StrictMock<MockMetricsUploaderCallback> mock_upload_handler; | ||
uploader_->SetOnUploadHandler(&mock_upload_handler); | ||
::metrics::ReportingInfo dummy_reporting_info; | ||
::metrics::ChromeUserMetricsExtension uma_log; | ||
uma_log.set_session_id(1234); | ||
uma_log.set_client_id(1234); | ||
std::string compressed_message; | ||
compression::GzipCompress(uma_log.SerializeAsString(), &compressed_message); | ||
EXPECT_CALL(mock_upload_handler, | ||
Run(Eq(h5vcc::H5vccMetricType::kH5vccMetricTypeUma), | ||
StrEq(uma_log.SerializeAsString()))) | ||
.Times(1); | ||
uploader_->UploadLog(compressed_message, "fake_hash", dummy_reporting_info); | ||
ASSERT_EQ(callback_count_, 1); | ||
|
||
::metrics::ChromeUserMetricsExtension uma_log2; | ||
uma_log2.set_session_id(456); | ||
uma_log2.set_client_id(567); | ||
std::string compressed_message2; | ||
compression::GzipCompress(uma_log2.SerializeAsString(), &compressed_message2); | ||
EXPECT_CALL(mock_upload_handler, | ||
Run(Eq(h5vcc::H5vccMetricType::kH5vccMetricTypeUma), | ||
StrEq(uma_log2.SerializeAsString()))) | ||
.Times(1); | ||
uploader_->UploadLog(compressed_message2, "fake_hash", dummy_reporting_info); | ||
ASSERT_EQ(callback_count_, 2); | ||
} | ||
|
||
TEST_F(CobaltMetricsLogUploaderTest, UnknownMetricTypeDoesntTriggerUpload) { | ||
uploader_.reset(new CobaltMetricsLogUploader( | ||
::metrics::MetricsLogUploader::MetricServiceType::UKM, | ||
base::Bind(&CobaltMetricsLogUploaderTest::UploadCompleteCallback, | ||
base::Unretained(this)))); | ||
StrictMock<MockMetricsUploaderCallback> mock_upload_handler; | ||
uploader_->SetOnUploadHandler(&mock_upload_handler); | ||
::metrics::ReportingInfo dummy_reporting_info; | ||
::metrics::ChromeUserMetricsExtension uma_log; | ||
uma_log.set_session_id(1234); | ||
uma_log.set_client_id(1234); | ||
std::string compressed_message; | ||
compression::GzipCompress(uma_log.SerializeAsString(), &compressed_message); | ||
EXPECT_CALL(mock_upload_handler, Run(_, _)).Times(0); | ||
uploader_->UploadLog(compressed_message, "fake_hash", dummy_reporting_info); | ||
// Even though we don't upload this log, we still need to trigger the complete | ||
// callback so the metric code can keep running. | ||
ASSERT_EQ(callback_count_, 1); | ||
} | ||
|
||
TEST_F(CobaltMetricsLogUploaderTest, BadCompressedDataSendsEmptyString) { | ||
StrictMock<MockMetricsUploaderCallback> mock_upload_handler; | ||
uploader_->SetOnUploadHandler(&mock_upload_handler); | ||
::metrics::ReportingInfo dummy_reporting_info; | ||
EXPECT_CALL(mock_upload_handler, Run(_, Eq(""))).Times(1); | ||
// "bad data" isn't a compressed serialized proto, should just try to upload | ||
// empty string. | ||
uploader_->UploadLog("bad data", "fake_hash", dummy_reporting_info); | ||
ASSERT_EQ(callback_count_, 1); | ||
} | ||
|
||
} // namespace metrics | ||
} // namespace browser | ||
} // namespace cobalt |
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
Oops, something went wrong.