File: hats_dialog_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 (164 lines) | stat: -rw-r--r-- 5,120 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
// Copyright 2021 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_dialog.h"

#include <string>

#include "ash/constants/ash_features.h"
#include "base/test/metrics/histogram_tester.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {

namespace {
constexpr char kHistogramName[] = "Some.Kind.Of.Histogram";
}  // namespace

class HatsDialogTest : public testing::Test {
 public:
  void TriggerAction(std::string action) {
    EXPECT_FALSE(
        HatsDialog::HandleClientTriggeredAction(action, kHistogramName));
  }

  void TriggerActionAndClose(std::string action) {
    EXPECT_TRUE(
        HatsDialog::HandleClientTriggeredAction(action, kHistogramName));
  }

  std::vector<base::Bucket> GetHistogramSamples() {
    return histogram_tester_.GetAllSamples(kHistogramName);
  }

 private:
  base::HistogramTester histogram_tester_;
};

TEST_F(HatsDialogTest, HandleClientTriggeredAction_Unknown) {
  // Client sent an invalid action, ignore it
  TriggerAction("Invalid");

  EXPECT_THAT(GetHistogramSamples(), testing::IsEmpty());
}

TEST_F(HatsDialogTest, HandleClientTriggeredAction_Loaded) {
  // Client asks to close the window
  TriggerAction("load");

  std::vector<base::Bucket> expected = {
      {2, 1}};  // 2 is the enumeration for "Displayed".
  EXPECT_EQ(GetHistogramSamples(), expected);
}

TEST_F(HatsDialogTest, HandleClientTriggeredAction_Close) {
  // Client asks to close the window
  TriggerActionAndClose("close");

  EXPECT_THAT(GetHistogramSamples(), testing::IsEmpty());
}

TEST_F(HatsDialogTest, HandleClientTriggeredAction_Complete) {
  // Client asks to close the window
  TriggerActionAndClose("complete");

  std::vector<base::Bucket> expected = {
      {3, 1}};  // 3 is the enumeration for "Complete".
  EXPECT_EQ(GetHistogramSamples(), expected);
}

TEST_F(HatsDialogTest, HandleClientTriggeredAction_Error) {
  // There was an unhandled error, close the window
  TriggerActionAndClose("survey-loading-error-12345");

  EXPECT_THAT(GetHistogramSamples(), testing::IsEmpty());
}

TEST_F(HatsDialogTest, HandleClientTriggeredAction_OldQuestionResponse) {
  TriggerAction("smiley-selected-2");
  TriggerAction("smiley-selected-2");
  TriggerAction("smiley-selected-4");

  EXPECT_THAT(GetHistogramSamples(), testing::IsEmpty());
}

TEST_F(HatsDialogTest, HandleClientTriggeredAction_InvalidQuestion) {
  TriggerAction("answer-a-2");

  EXPECT_THAT(GetHistogramSamples(), testing::IsEmpty());
}

TEST_F(HatsDialogTest, HandleClientTriggeredAction_FirstQuestion) {
  TriggerAction("answer-1-2");

  std::vector<base::Bucket> expected = {{102, 1}};
  EXPECT_EQ(GetHistogramSamples(), expected);
}

TEST_F(HatsDialogTest, HandleClientTriggeredAction_SingleSelectQuestion) {
  TriggerAction("answer-2-4");

  std::vector<base::Bucket> expected = {{204, 1}};
  EXPECT_EQ(GetHistogramSamples(), expected);
}

TEST_F(HatsDialogTest, HandleClientTriggeredAction_MultipleSelectQuestion) {
  TriggerAction("answer-3-2,4,5");

  std::vector<base::Bucket> expected = {{302, 1}, {304, 1}, {305, 1}};
  EXPECT_EQ(GetHistogramSamples(), expected);
}

TEST_F(HatsDialogTest, HandleClientTriggeredAction_FullWorkflow) {
  TriggerAction("load");
  TriggerAction("answer-1-2");
  TriggerAction("answer-2-3");
  TriggerAction("answer-3-4,5");
  TriggerActionAndClose("complete");

  std::vector<base::Bucket> expected = {{2, 1},   {3, 1},   {102, 1},
                                        {203, 1}, {304, 1}, {305, 1}};
  EXPECT_EQ(GetHistogramSamples(), expected);
}

TEST_F(HatsDialogTest, ParseAnswer) {
  int question;
  std::vector<int> scores;

  // Incomplete answers
  EXPECT_FALSE(HatsDialog::ParseAnswer("answer-", &question, &scores));
  EXPECT_FALSE(HatsDialog::ParseAnswer("answer-1", &question, &scores));
  EXPECT_FALSE(HatsDialog::ParseAnswer("answer-1-", &question, &scores));

  // Invalid integers.
  EXPECT_FALSE(HatsDialog::ParseAnswer("answer-a-1,2,3", &question, &scores));
  EXPECT_FALSE(HatsDialog::ParseAnswer("answer-1-a", &question, &scores));

  // Out of range
  EXPECT_FALSE(HatsDialog::ParseAnswer("answer--1-1,2,3", &question, &scores));
  EXPECT_FALSE(HatsDialog::ParseAnswer("answer-1--1", &question, &scores));
  EXPECT_FALSE(HatsDialog::ParseAnswer("answer-0-1,2,3", &question, &scores));
  EXPECT_FALSE(HatsDialog::ParseAnswer("answer-11-1", &question, &scores));
  EXPECT_FALSE(HatsDialog::ParseAnswer("answer-1-101", &question, &scores));

  // Overflow int.
  EXPECT_FALSE(
      HatsDialog::ParseAnswer("answer-2147483648-a", &question, &scores));
  EXPECT_FALSE(
      HatsDialog::ParseAnswer("answer-1-2147483648", &question, &scores));

  EXPECT_TRUE(HatsDialog::ParseAnswer("answer-1-10", &question, &scores));
  EXPECT_EQ(question, 1);
  EXPECT_EQ(scores.size(), 1UL);
  EXPECT_EQ(scores[0], 10);

  scores.clear();
  EXPECT_TRUE(HatsDialog::ParseAnswer("answer-2-1,2", &question, &scores));
  EXPECT_EQ(question, 2);
  EXPECT_EQ(scores.size(), 2UL);
  EXPECT_EQ(scores[0], 1);
  EXPECT_EQ(scores[1], 2);
}

}  // namespace ash