Index: components/metrics/metrics_service_unittest.cc |
diff --git a/components/metrics/metrics_service_unittest.cc b/components/metrics/metrics_service_unittest.cc |
index 73e69be7392114a0b31efa0b208ba5e473c0b60c..9d1d25e805961c9926ef40cca9f5a1108224c87a 100644 |
--- a/components/metrics/metrics_service_unittest.cc |
+++ b/components/metrics/metrics_service_unittest.cc |
@@ -7,21 +7,31 @@ |
#include <string> |
#include "base/bind.h" |
+#include "base/command_line.h" |
#include "base/memory/scoped_ptr.h" |
#include "base/message_loop/message_loop.h" |
#include "base/metrics/statistics_recorder.h" |
#include "base/prefs/testing_pref_service.h" |
+#include "base/strings/string16.h" |
#include "base/threading/platform_thread.h" |
#include "components/metrics/client_info.h" |
#include "components/metrics/compression_utils.h" |
#include "components/metrics/metrics_hashes.h" |
#include "components/metrics/metrics_log.h" |
+#include "components/metrics/metrics_log_uploader.h" |
#include "components/metrics/metrics_pref_names.h" |
#include "components/metrics/metrics_state_manager.h" |
+#include "components/metrics/metrics_switches.h" |
+#include "components/metrics/mock_metrics_service_client.h" |
+#include "components/metrics/proto/system_profile.pb.h" |
#include "components/metrics/test_metrics_service_client.h" |
#include "components/variations/metrics_util.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
+using ::testing::Return; |
+using ::testing::_; |
+ |
namespace metrics { |
namespace { |
@@ -90,6 +100,25 @@ class TestMetricsLog : public MetricsLog { |
DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); |
}; |
+class TestMetricsLogUploader : public MetricsLogUploader { |
+ public: |
+ TestMetricsLogUploader( |
+ const std::string& server_url, |
+ const std::string& mime_type, |
+ const base::Callback<void(int)>& on_upload_complete): |
+ MetricsLogUploader(server_url, mime_type, on_upload_complete) {} |
+ |
+ ~TestMetricsLogUploader() override {} |
+ |
+ bool UploadLog(const std::string& compressed_log_data, |
+ const std::string& log_hash) override { |
+ return true; |
+ } |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(TestMetricsLogUploader); |
+}; |
+ |
class MetricsServiceTest : public testing::Test { |
public: |
MetricsServiceTest() : is_metrics_reporting_enabled_(false) { |
@@ -118,6 +147,21 @@ class MetricsServiceTest : public testing::Test { |
is_metrics_reporting_enabled_ = true; |
} |
+ void PopulateStabilityLog(MetricsServiceClient& client, |
+ bool exited_cleanly) { |
+ // Record stability build time and version from previous session, so that |
+ // stability metrics (including exited cleanly flag) won't be cleared. |
+ testing_local_state_.SetInt64(prefs::kStabilityStatsBuildTime, |
+ MetricsLog::GetBuildTime()); |
+ testing_local_state_.SetString(prefs::kStabilityStatsVersion, |
+ client.GetVersionString()); |
+ |
+ // Set the clean exit flag, as that will otherwise cause a stabilty |
+ // log to be produced, irrespective provider requests. |
+ testing_local_state_.SetBoolean( |
+ prefs::kStabilityExitedCleanly, exited_cleanly); |
+ } |
+ |
// Waits until base::TimeTicks::Now() no longer equals |value|. This should |
// take between 1-15ms per the documented resolution of base::TimeTicks. |
void WaitUntilTimeChanges(const base::TimeTicks& value) { |
@@ -217,16 +261,7 @@ TEST_F(MetricsServiceTest, InitialStabilityLogAtProviderRequest) { |
std::vector<variations::ActiveGroupId>(), |
0); |
- // Record stability build time and version from previous session, so that |
- // stability metrics (including exited cleanly flag) won't be cleared. |
- GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime, |
- MetricsLog::GetBuildTime()); |
- GetLocalState()->SetString(prefs::kStabilityStatsVersion, |
- client.GetVersionString()); |
- |
- // Set the clean exit flag, as that will otherwise cause a stabilty |
- // log to be produced, irrespective provider requests. |
- GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true); |
+ PopulateStabilityLog(client, true); |
TestMetricsService service( |
GetMetricsStateManager(), &client, GetLocalState()); |
@@ -282,15 +317,7 @@ TEST_F(MetricsServiceTest, InitialStabilityLogAfterCrash) { |
log.RecordEnvironment(std::vector<MetricsProvider*>(), |
std::vector<variations::ActiveGroupId>(), |
0); |
- |
- // Record stability build time and version from previous session, so that |
- // stability metrics (including exited cleanly flag) won't be cleared. |
- GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime, |
- MetricsLog::GetBuildTime()); |
- GetLocalState()->SetString(prefs::kStabilityStatsVersion, |
- client.GetVersionString()); |
- |
- GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, false); |
+ PopulateStabilityLog(client, false); |
TestMetricsService service( |
GetMetricsStateManager(), &client, GetLocalState()); |
@@ -386,4 +413,45 @@ TEST_F(MetricsServiceTest, RegisterSyntheticTrial) { |
service.log_manager_.FinishCurrentLog(); |
} |
+TEST_F(MetricsServiceTest, ServerUrlOverrideSet) { |
+ EnableMetricsReporting(); |
+ |
+ std::string server_url = "https://127.0.0.1/v2"; |
+ base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
+ // This is the switch that we want to test. |
+ command_line->AppendSwitchASCII(switches::kOverrideMetricsUploadUrl, |
+ server_url); |
+ |
+ MockMetricsServiceClient client; |
+ EXPECT_CALL(client, GetRegistryBackupKey()) |
+ .WillOnce(Return(base::string16())); |
+ EXPECT_CALL(client, GetChannel()) |
+ .WillRepeatedly(Return(SystemProfileProto::CHANNEL_UNKNOWN)); |
+ // The main expectation of this test: ensures that when the command line |
+ // switch is set, the uploader is created with the correct url. |
+ EXPECT_CALL(client, MockCreateUploader(server_url, _, _)) |
+ .WillOnce(Return(new TestMetricsLogUploader(server_url, "mime_type", |
+ base::Bind(&MockMetricsServiceClient::DoNothing, |
+ base::Unretained(&client))))); |
+ |
+ // Generate a set of logs for upload |
+ TestMetricsLog log("client", 1, &client, GetLocalState()); |
+ log.RecordEnvironment(std::vector<MetricsProvider*>(), |
+ std::vector<variations::ActiveGroupId>(), |
+ 0); |
+ PopulateStabilityLog(client, true); |
+ |
+ MetricsService service(GetMetricsStateManager(), &client, GetLocalState()); |
+ TestMetricsProvider* test_provider = new TestMetricsProvider(true); |
+ service.RegisterMetricsProvider( |
+ scoped_ptr<MetricsProvider>(test_provider)); |
+ service.InitializeMetricsRecordingState(); |
+ EXPECT_TRUE(service.log_manager()->has_unsent_logs()); |
+ |
+ service.log_manager()->StageNextLogForUpload(); |
+ EXPECT_TRUE(service.log_manager()->has_staged_log()); |
+ |
+ service.SendStagedLog(); |
+} |
+ |
} // namespace metrics |