| // Copyright 2020 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "components/variations/variations_test_utils.h" |
| |
| #include <string> |
| |
| #include "base/base64.h" |
| #include "base/feature_list.h" |
| #include "base/metrics/field_trial.h" |
| #include "components/variations/proto/client_variations.pb.h" |
| #include "components/variations/variations_associated_data.h" |
| |
| namespace variations { |
| |
| bool ExtractVariationIds(const std::string& variations, |
| std::set<VariationID>* variation_ids, |
| std::set<VariationID>* trigger_ids) { |
| std::string serialized_proto; |
| if (!base::Base64Decode(variations, &serialized_proto)) |
| return false; |
| ClientVariations proto; |
| if (!proto.ParseFromString(serialized_proto)) |
| return false; |
| for (int i = 0; i < proto.variation_id_size(); ++i) |
| variation_ids->insert(proto.variation_id(i)); |
| for (int i = 0; i < proto.trigger_variation_id_size(); ++i) |
| trigger_ids->insert(proto.trigger_variation_id(i)); |
| return true; |
| } |
| |
| scoped_refptr<base::FieldTrial> CreateTrialAndAssociateId( |
| const std::string& trial_name, |
| const std::string& default_group_name, |
| IDCollectionKey key, |
| VariationID id) { |
| AssociateGoogleVariationID(key, trial_name, default_group_name, id); |
| scoped_refptr<base::FieldTrial> trial( |
| base::FieldTrialList::CreateFieldTrial(trial_name, default_group_name)); |
| DCHECK(trial); |
| |
| if (trial) { |
| // Ensure the trial is registered under the correct key so we can look it |
| // up. |
| trial->group(); |
| } |
| |
| return trial; |
| } |
| |
| } // namespace variations |