File: hats_finch_helper_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (229 lines) | stat: -rw-r--r-- 8,585 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2016 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/hats/hats_finch_helper.h"

#include <map>
#include <set>
#include <string>

#include "base/strings/string_number_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/ash/hats/hats_config.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {
namespace {
constexpr char kValidTriggerId[] = "1gksUIDXA0jBnuK8T6R0NfspWBvA";
}  // namespace

class HatsFinchHelperTest : public testing::Test {
 public:
  HatsFinchHelperTest() = default;

  HatsFinchHelperTest(const HatsFinchHelperTest&) = delete;
  HatsFinchHelperTest& operator=(const HatsFinchHelperTest&) = delete;

  void SetFeatureParams(const base::FieldTrialParams& params) {
    scoped_feature_list_.InitAndEnableFeatureWithParameters(
        features::kHappinessTrackingSystem, params);
  }

  base::FieldTrialParams CreateParamMap(
      std::string prob,
      std::string cycle_length,
      std::string start_date,
      std::string reset_survey,
      std::string reset,
      std::string trigger_id,
      std::string custom_client_data = std::string(),
      std::string enabled_for_googlers = std::string()) {
    base::FieldTrialParams params;
    params[HatsFinchHelper::kProbabilityParam] = prob;
    params[HatsFinchHelper::kSurveyCycleLengthParam] = cycle_length;
    params[HatsFinchHelper::kSurveyStartDateMsParam] = start_date;
    params[HatsFinchHelper::kResetSurveyCycleParam] = reset_survey;
    params[HatsFinchHelper::kResetAllParam] = reset;
    params[HatsFinchHelper::kTriggerIdParam] = trigger_id;
    params[HatsFinchHelper::kCustomClientDataParam] = custom_client_data;
    params[HatsFinchHelper::kEnabledForGooglersParam] = enabled_for_googlers;
    return params;
  }

 private:
  // Must outlive |profile_|.
  content::BrowserTaskEnvironment task_environment_;

 protected:
  TestingProfile profile_;

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

TEST_F(HatsFinchHelperTest, InitFinchSeed_ValidValues) {
  base::FieldTrialParams params = CreateParamMap(
      "1.0", "7", "1475613895337", "false", "false", kValidTriggerId);
  SetFeatureParams(params);

  HatsFinchHelper hats_finch_helper(&profile_, kHatsGeneralSurvey);

  EXPECT_EQ(hats_finch_helper.probability_of_pick_, 1.0);
  EXPECT_EQ(hats_finch_helper.survey_cycle_length_, 7);
  EXPECT_EQ(hats_finch_helper.first_survey_start_date_,
            base::Time().FromMillisecondsSinceUnixEpoch(1475613895337LL));
  EXPECT_EQ(hats_finch_helper.trigger_id_, kValidTriggerId);
  EXPECT_FALSE(hats_finch_helper.reset_survey_cycle_);
  EXPECT_FALSE(hats_finch_helper.reset_hats_);
}

TEST_F(HatsFinchHelperTest, InitFinchSeed_Invalidalues) {
  base::FieldTrialParams params =
      CreateParamMap("-0.1", "-1", "-1000", "false", "false", "1A2B3C4D5");
  SetFeatureParams(params);

  base::Time current_time = base::Time::Now();
  HatsFinchHelper hats_finch_helper(&profile_, kHatsGeneralSurvey);

  EXPECT_EQ(hats_finch_helper.probability_of_pick_, 0.0);
  EXPECT_EQ(hats_finch_helper.survey_cycle_length_, INT_MAX);
  EXPECT_GE(hats_finch_helper.first_survey_start_date_
                .InMillisecondsFSinceUnixEpoch(),
            2 * current_time.InMillisecondsFSinceUnixEpoch());
}

TEST_F(HatsFinchHelperTest, TestComputeNextDate) {
  base::FieldTrialParams params =
      CreateParamMap("0",
                     "7",  // 7 Days survey cycle length
                     "0", "false", "false", kValidTriggerId);

  SetFeatureParams(params);

  base::Time current_time = base::Time::Now();

  HatsFinchHelper hats_finch_helper(&profile_, kHatsGeneralSurvey);

  // Case 1
  base::Time start_date = current_time - base::Days(10);
  hats_finch_helper.first_survey_start_date_ = start_date;
  base::Time expected_date =
      start_date + base::Days(2 * hats_finch_helper.survey_cycle_length_);
  EXPECT_EQ(
      expected_date.InMillisecondsFSinceUnixEpoch(),
      hats_finch_helper.ComputeNextEndDate().InMillisecondsFSinceUnixEpoch());

  // Case 2
  base::Time future_time = current_time + base::Days(10);
  hats_finch_helper.first_survey_start_date_ = future_time;
  expected_date =
      future_time + base::Days(hats_finch_helper.survey_cycle_length_);
  EXPECT_EQ(
      expected_date.InMillisecondsFSinceUnixEpoch(),
      hats_finch_helper.ComputeNextEndDate().InMillisecondsFSinceUnixEpoch());
}

TEST_F(HatsFinchHelperTest, ResetSurveyCycle) {
  base::FieldTrialParams params = CreateParamMap(
      "0.5", "7", "1475613895337", "true", "false", kValidTriggerId);
  SetFeatureParams(params);

  int64_t initial_timestamp = base::Time::Now().ToInternalValue();
  PrefService* pref_service = profile_.GetPrefs();
  pref_service->SetBoolean(prefs::kHatsDeviceIsSelected, true);
  pref_service->SetInt64(prefs::kHatsSurveyCycleEndTimestamp,
                         initial_timestamp);

  base::Time current_time = base::Time::Now();
  HatsFinchHelper hats_finch_helper(&profile_, kHatsGeneralSurvey);

  EXPECT_EQ(hats_finch_helper.probability_of_pick_, 0);
  EXPECT_EQ(hats_finch_helper.survey_cycle_length_, INT_MAX);
  EXPECT_GE(hats_finch_helper.first_survey_start_date_
                .InMillisecondsFSinceUnixEpoch(),
            2 * current_time.InMillisecondsFSinceUnixEpoch());

  EXPECT_FALSE(pref_service->GetBoolean(prefs::kHatsDeviceIsSelected));
  EXPECT_NE(pref_service->GetInt64(prefs::kHatsSurveyCycleEndTimestamp),
            initial_timestamp);
}

TEST_F(HatsFinchHelperTest, ResetHats) {
  base::FieldTrialParams params = CreateParamMap(
      "0.5", "7", "1475613895337", "false", "true", kValidTriggerId);
  SetFeatureParams(params);

  int64_t initial_timestamp = base::Time::Now().ToInternalValue();
  PrefService* pref_service = profile_.GetPrefs();
  pref_service->SetBoolean(prefs::kHatsDeviceIsSelected, true);
  pref_service->SetInt64(prefs::kHatsSurveyCycleEndTimestamp,
                         initial_timestamp);
  pref_service->SetInt64(prefs::kHatsLastInteractionTimestamp,
                         initial_timestamp);

  base::Time current_time = base::Time::Now();
  HatsFinchHelper hats_finch_helper(&profile_, kHatsGeneralSurvey);

  EXPECT_EQ(hats_finch_helper.probability_of_pick_, 0);
  EXPECT_EQ(hats_finch_helper.survey_cycle_length_, INT_MAX);
  EXPECT_GE(hats_finch_helper.first_survey_start_date_
                .InMillisecondsFSinceUnixEpoch(),
            2 * current_time.InMillisecondsFSinceUnixEpoch());

  EXPECT_FALSE(pref_service->GetBoolean(prefs::kHatsDeviceIsSelected));
  EXPECT_NE(pref_service->GetInt64(prefs::kHatsSurveyCycleEndTimestamp),
            initial_timestamp);
  EXPECT_NE(pref_service->GetInt64(prefs::kHatsLastInteractionTimestamp),
            initial_timestamp);
}

TEST_F(HatsFinchHelperTest, NoCustomClientData) {
  base::FieldTrialParams params = CreateParamMap(
      "1.0", "7", "1475613895337", "false", "false", kValidTriggerId);
  SetFeatureParams(params);

  HatsFinchHelper hats_finch_helper(&profile_, kHatsGeneralSurvey);

  EXPECT_EQ(hats_finch_helper.GetCustomClientDataAsString(kHatsGeneralSurvey),
            std::string());
}

TEST_F(HatsFinchHelperTest, CustomClientData) {
  base::FieldTrialParams params = CreateParamMap(
      "1.0", "7", "1475613895337", "false", "false", kValidTriggerId, "12345");
  SetFeatureParams(params);

  HatsFinchHelper hats_finch_helper(&profile_, kHatsGeneralSurvey);

  EXPECT_EQ(hats_finch_helper.GetCustomClientDataAsString(kHatsGeneralSurvey),
            "12345");
}

TEST_F(HatsFinchHelperTest, NoEnabledForGooglersConfig) {
  base::FieldTrialParams params = CreateParamMap(
      "1.0", "7", "1475613895337", "false", "false", kValidTriggerId);
  SetFeatureParams(params);

  HatsFinchHelper hats_finch_helper(&profile_, kHatsGeneralSurvey);

  EXPECT_EQ(hats_finch_helper.IsEnabledForGooglers(kHatsGeneralSurvey), false);
}

TEST_F(HatsFinchHelperTest, EnabledForGooglers) {
  base::FieldTrialParams params =
      CreateParamMap("1.0", "7", "1475613895337", "false", "false",
                     kValidTriggerId, "", "true");
  SetFeatureParams(params);

  HatsFinchHelper hats_finch_helper(&profile_, kHatsGeneralSurvey);

  EXPECT_EQ(hats_finch_helper.IsEnabledForGooglers(kHatsGeneralSurvey), true);
}

}  // namespace ash