Commit 8e231fe0 authored by Caitlin Fischer's avatar Caitlin Fischer Committed by Commit Bot

Add UMA demographic Earl Grey tests.

These tests correspond to those in
metrics_service_user_demographics_browsertest.cc.


(Note that there will be a follow-up CL to remove duplicate code.)

Bug: 1065870
Change-Id: I13df596c3ca62b90ba138a973db8f344011d02f8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2132534
Commit-Queue: Caitlin Fischer <caitlinfischer@google.com>
Reviewed-by: default avatarAlexei Svitkine <asvitkine@chromium.org>
Reviewed-by: default avatarRoger McFarlane <rogerm@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#757852}
parent 1db2ddfa
...@@ -245,6 +245,7 @@ source_set("eg_tests") { ...@@ -245,6 +245,7 @@ source_set("eg_tests") {
"//components/metrics:demographic_metrics_provider", "//components/metrics:demographic_metrics_provider",
"//components/metrics_services_manager", "//components/metrics_services_manager",
"//components/strings", "//components/strings",
"//components/sync/base",
"//components/ukm", "//components/ukm",
"//components/ukm:test_support", "//components/ukm:test_support",
"//components/unified_consent:unified_consent", "//components/unified_consent:unified_consent",
......
...@@ -56,21 +56,48 @@ const NSTimeInterval kSyncUKMOperationsTimeout = 10.0; ...@@ -56,21 +56,48 @@ const NSTimeInterval kSyncUKMOperationsTimeout = 10.0;
+ (void)setNetworkTimeForTesting; + (void)setNetworkTimeForTesting;
// TODO(crbug.com/1066297): Refactor to remove duplicate code. // TODO(crbug.com/1066297): Refactor to remove duplicate code.
// If new data are available, creates a UKM Report and stores it in the // Gets the user's noised birth year, where |rawBirthYear| is the true birth
// year, pre-noise.
+ (int)noisedBirthYear:(int)rawBirthYear;
// TODO(crbug.com/1066297): Refactor to remove duplicate code.
// If data are available, creates a UKM Report and stores it in the
// UKM service's UnsentLogStore. // UKM service's UnsentLogStore.
+ (void)buildAndStoreUKMLog; + (void)buildAndStoreUKMLog;
// TODO(crbug.com/1066297): Refactor to remove duplicate code. // TODO(crbug.com/1066297): Refactor to remove duplicate code.
// Returns YES if the UKM service has logs to send. // Returns YES if the UKM service has logs to send.
+ (BOOL)hasUnsentLogs; + (BOOL)hasUnsentUKMLogs;
// TODO(crbug.com/1066297): Refactor to remove duplicate code.
// Returns YES if the UKM service's report has the expected year and gender. // Returns YES if the UKM service's report has the expected year and gender.
// Gender corresponds to the options in UserDemographicsProto::Gender. // The year is the un-noised birth year, and the gender corresponds to the
// options in UserDemographicsProto::Gender.
+ (BOOL)UKMReportHasBirthYear:(int)year gender:(int)gender; + (BOOL)UKMReportHasBirthYear:(int)year gender:(int)gender;
// TODO(crbug.com/1066297): Refactor to remove duplicate code.
// Returns YES if the UKM service's report has user demographics. // Returns YES if the UKM service's report has user demographics.
+ (BOOL)UKMReportHasUserDemographics; + (BOOL)UKMReportHasUserDemographics;
// TODO(crbug.com/1066297): Refactor to remove duplicate code.
// If data are available, creates an UMA log and stores it in the
// MetricsLogStore.
+ (void)buildAndStoreUMALog;
// TODO(crbug.com/1066297): Refactor to remove duplicate code.
// Returns YES if the metrics service has logs to send.
+ (BOOL)hasUnsentUMALogs;
// TODO(crbug.com/1066297): Refactor to remove duplicate code.
// Returns YES if the UMA log has the expected year and gender. The year is the
// un-noised birth year, and the gender corresponds to the proto enum
// UserDemographicsProto::Gender.
+ (BOOL)UMALogHasBirthYear:(int)year gender:(int)gender;
// TODO(crbug.com/1066297): Refactor to remove duplicate code.
// Returns YES if the UMA log has user demographics.
+ (BOOL)UMALogHasUserDemographics;
// Creates a chrome_test_util::HistogramTester that will record every histogram // Creates a chrome_test_util::HistogramTester that will record every histogram
// sent during test. // sent during test.
+ (NSError*)setupHistogramTester WARN_UNUSED_RESULT; + (NSError*)setupHistogramTester WARN_UNUSED_RESULT;
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include "ios/chrome/browser/metrics/metrics_app_interface.h" #include "ios/chrome/browser/metrics/metrics_app_interface.h"
#include <memory> #include <string>
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/run_loop.h" #include "base/run_loop.h"
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "base/time/default_clock.h" #include "base/time/default_clock.h"
#include "base/time/default_tick_clock.h" #include "base/time/default_tick_clock.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "components/metrics/metrics_service.h"
#include "components/metrics/unsent_log_store.h" #include "components/metrics/unsent_log_store.h"
#include "components/metrics_services_manager/metrics_services_manager.h" #include "components/metrics_services_manager/metrics_services_manager.h"
#include "components/network_time/network_time_tracker.h" #include "components/network_time/network_time_tracker.h"
...@@ -26,8 +27,8 @@ ...@@ -26,8 +27,8 @@
#import "ios/chrome/test/app/histogram_test_util.h" #import "ios/chrome/test/app/histogram_test_util.h"
#import "ios/testing/nserror_util.h" #import "ios/testing/nserror_util.h"
#include "services/metrics/public/cpp/ukm_source_id.h" #include "services/metrics/public/cpp/ukm_source_id.h"
#include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
#include "third_party/metrics_proto/ukm/report.pb.h" #include "third_party/metrics_proto/ukm/report.pb.h"
#include "third_party/metrics_proto/user_demographics.pb.h"
#include "third_party/zlib/google/compression_utils.h" #include "third_party/zlib/google/compression_utils.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
...@@ -39,6 +40,29 @@ namespace { ...@@ -39,6 +40,29 @@ namespace {
bool g_metrics_enabled = false; bool g_metrics_enabled = false;
chrome_test_util::HistogramTester* g_histogram_tester = nullptr; chrome_test_util::HistogramTester* g_histogram_tester = nullptr;
// TODO(crbug.com/1066297): Refactor to remove duplicate code.
// Returns an UMA log if the metrics service has a staged log.
std::unique_ptr<metrics::ChromeUserMetricsExtension> GetLastUmaLog() {
metrics::MetricsLogStore* const log_store =
GetApplicationContext()->GetMetricsService()->LogStoreForTest();
// Decompress the staged log.
std::string uncompressed_log;
if (!compression::GzipUncompress(log_store->staged_log(),
&uncompressed_log)) {
return nullptr;
}
// Deserialize and return the log.
std::unique_ptr<metrics::ChromeUserMetricsExtension> uma_log =
std::make_unique<metrics::ChromeUserMetricsExtension>();
if (!uma_log->ParseFromString(uncompressed_log)) {
return nullptr;
}
return uma_log;
}
} // namespace } // namespace
namespace metrics { namespace metrics {
...@@ -91,13 +115,13 @@ class UkmEGTestHelper { ...@@ -91,13 +115,13 @@ class UkmEGTestHelper {
service->Flush(); service->Flush();
} }
static bool HasUnsentLogs() { static bool HasUnsentUkmLogs() {
ukm::UkmService* service = ukm_service(); ukm::UkmService* service = ukm_service();
return service->reporting_service_.ukm_log_store()->has_unsent_logs(); return service->reporting_service_.ukm_log_store()->has_unsent_logs();
} }
static std::unique_ptr<ukm::Report> GetUKMReport() { static std::unique_ptr<ukm::Report> GetUKMReport() {
if (!HasUnsentLogs()) { if (!HasUnsentUkmLogs()) {
return nullptr; return nullptr;
} }
...@@ -195,25 +219,28 @@ class UkmEGTestHelper { ...@@ -195,25 +219,28 @@ class UkmEGTestHelper {
clock.Now() - latency / 2, resolution, latency, tickClock.NowTicks()); clock.Now() - latency / 2, resolution, latency, tickClock.NowTicks());
} }
+ (int)noisedBirthYear:(int)rawBirthYear {
int birthYearOffset =
GetApplicationContext()
->GetChromeBrowserStateManager()
->GetLastUsedBrowserState()
->GetPrefs()
->GetInteger(syncer::prefs::kSyncDemographicsBirthYearOffset);
return rawBirthYear + birthYearOffset;
}
+ (void)buildAndStoreUKMLog { + (void)buildAndStoreUKMLog {
metrics::UkmEGTestHelper::BuildAndStoreUkmLog(); metrics::UkmEGTestHelper::BuildAndStoreUkmLog();
} }
+ (BOOL)hasUnsentLogs { + (BOOL)hasUnsentUKMLogs {
return metrics::UkmEGTestHelper::HasUnsentLogs(); return metrics::UkmEGTestHelper::HasUnsentUkmLogs();
} }
+ (BOOL)UKMReportHasBirthYear:(int)year gender:(int)gender { + (BOOL)UKMReportHasBirthYear:(int)year gender:(int)gender {
std::unique_ptr<ukm::Report> report = std::unique_ptr<ukm::Report> report =
metrics::UkmEGTestHelper::GetUKMReport(); metrics::UkmEGTestHelper::GetUKMReport();
int noisedBirthYear = [self noisedBirthYear:year];
int birthYearOffset =
GetApplicationContext()
->GetChromeBrowserStateManager()
->GetLastUsedBrowserState()
->GetPrefs()
->GetInteger(syncer::prefs::kSyncDemographicsBirthYearOffset);
int noisedBirthYear = year + birthYearOffset;
return report && gender == report->user_demographics().gender() && return report && gender == report->user_demographics().gender() &&
noisedBirthYear == report->user_demographics().birth_year(); noisedBirthYear == report->user_demographics().birth_year();
...@@ -225,6 +252,36 @@ class UkmEGTestHelper { ...@@ -225,6 +252,36 @@ class UkmEGTestHelper {
return report && report->has_user_demographics(); return report && report->has_user_demographics();
} }
+ (void)buildAndStoreUMALog {
GetApplicationContext()->GetMetricsService()->StageCurrentLogForTest();
}
+ (BOOL)hasUnsentUMALogs {
return GetApplicationContext()
->GetMetricsService()
->LogStoreForTest()
->has_unsent_logs();
}
+ (BOOL)UMALogHasBirthYear:(int)year gender:(int)gender {
if (![self UMALogHasUserDemographics]) {
return NO;
}
std::unique_ptr<metrics::ChromeUserMetricsExtension> log = GetLastUmaLog();
int noisedBirthYear = [self noisedBirthYear:year];
return noisedBirthYear == log->user_demographics().birth_year() &&
gender == log->user_demographics().gender();
}
+ (BOOL)UMALogHasUserDemographics {
if (![self hasUnsentUMALogs]) {
return NO;
}
std::unique_ptr<metrics::ChromeUserMetricsExtension> log = GetLastUmaLog();
return log && log->has_user_demographics();
}
+ (NSError*)setupHistogramTester { + (NSError*)setupHistogramTester {
if (g_histogram_tester) { if (g_histogram_tester) {
return testing::NSErrorWithLocalizedDescription( return testing::NSErrorWithLocalizedDescription(
......
...@@ -155,10 +155,10 @@ id ExecuteJavaScript(NSString* javascript, NSError* __autoreleasing* out_error); ...@@ -155,10 +155,10 @@ id ExecuteJavaScript(NSString* javascript, NSError* __autoreleasing* out_error);
// Stops the sync server. The server should be running when calling this. // Stops the sync server. The server should be running when calling this.
- (void)stopSync; - (void)stopSync;
// Injects user demographics into the fake sync server. The year is the // Injects user demographics into the fake sync server. |rawBirthYear| is the
// un-noised birth year, and the gender corresponds to the options in // true birth year, pre-noise, and the gender corresponds to the proto enum
// UserDemographicsProto::Gender. // UserDemographicsProto::Gender.
- (void)addUserDemographicsToSyncServerWithBirthYear:(int)birthYear - (void)addUserDemographicsToSyncServerWithBirthYear:(int)rawBirthYear
gender:(int)gender; gender:(int)gender;
// Clears the autofill profile for the given |GUID|. // Clears the autofill profile for the given |GUID|.
......
...@@ -621,10 +621,10 @@ GREY_STUB_CLASS_IN_APP_MAIN_QUEUE(ChromeEarlGreyAppInterface) ...@@ -621,10 +621,10 @@ GREY_STUB_CLASS_IN_APP_MAIN_QUEUE(ChromeEarlGreyAppInterface)
[ChromeEarlGreyAppInterface stopSync]; [ChromeEarlGreyAppInterface stopSync];
} }
- (void)addUserDemographicsToSyncServerWithBirthYear:(int)birthYear - (void)addUserDemographicsToSyncServerWithBirthYear:(int)rawBirthYear
gender:(int)gender { gender:(int)gender {
[ChromeEarlGreyAppInterface [ChromeEarlGreyAppInterface
addUserDemographicsToSyncServerWithBirthYear:birthYear addUserDemographicsToSyncServerWithBirthYear:rawBirthYear
gender:gender]; gender:gender];
} }
......
...@@ -305,10 +305,10 @@ ...@@ -305,10 +305,10 @@
// Triggers a sync cycle for a |type|. // Triggers a sync cycle for a |type|.
+ (void)triggerSyncCycleForType:(syncer::ModelType)type; + (void)triggerSyncCycleForType:(syncer::ModelType)type;
// Injects user demographics into the fake sync server. The year is the // Injects user demographics into the fake sync server. |rawBirthYear| is the
// un-noised birth year, and the gender corresponds to the options in // true birth year, pre-noise, and the gender corresponds to the proto enum
// UserDemographicsProto::Gender.. // UserDemographicsProto::Gender.
+ (void)addUserDemographicsToSyncServerWithBirthYear:(int)birthYear + (void)addUserDemographicsToSyncServerWithBirthYear:(int)rawBirthYear
gender:(int)gender; gender:(int)gender;
// Clears the autofill profile for the given |GUID|. // Clears the autofill profile for the given |GUID|.
......
...@@ -500,9 +500,9 @@ NSString* SerializedPref(const PrefService::Preference* pref) { ...@@ -500,9 +500,9 @@ NSString* SerializedPref(const PrefService::Preference* pref) {
chrome_test_util::TriggerSyncCycle(type); chrome_test_util::TriggerSyncCycle(type);
} }
+ (void)addUserDemographicsToSyncServerWithBirthYear:(int)birthYear + (void)addUserDemographicsToSyncServerWithBirthYear:(int)rawBirthYear
gender:(int)gender { gender:(int)gender {
chrome_test_util::AddUserDemographicsToSyncServer(birthYear, gender); chrome_test_util::AddUserDemographicsToSyncServer(rawBirthYear, gender);
} }
+ (void)clearAutofillProfileWithGUID:(NSString*)GUID { + (void)clearAutofillProfileWithGUID:(NSString*)GUID {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment