File: release_notes_storage_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 (216 lines) | stat: -rw-r--r-- 7,463 bytes parent folder | download | duplicates (5)
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
// Copyright 2019 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/release_notes/release_notes_storage.h"

#include <memory>
#include <utility>
#include <vector>

#include "ash/constants/ash_features.h"
#include "base/test/scoped_feature_list.h"
#include "base/version.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/components/install_attributes/stub_install_attributes.h"
#include "chromeos/ash/components/login/login_state/login_state.h"
#include "components/account_id/account_id.h"
#include "components/prefs/pref_service.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/version_info/version_info.h"
#include "content/public/test/browser_task_environment.h"
#include "google_apis/gaia/gaia_id.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

int CurrentMilestone() {
  return version_info::GetVersion().components()[0];
}

}  // namespace

namespace ash {

class ReleaseNotesStorageTest : public testing::Test,
                                public testing::WithParamInterface<bool> {
 public:
  ReleaseNotesStorageTest(const ReleaseNotesStorageTest&) = delete;
  ReleaseNotesStorageTest& operator=(const ReleaseNotesStorageTest&) = delete;

 protected:
  ReleaseNotesStorageTest() = default;
  ~ReleaseNotesStorageTest() override = default;

  void SetUpProfile() {
    TestingProfile::Builder builder;
    if (is_guest_) {
      builder.SetGuestSession();
    } else {
      AccountId account_id_ =
          AccountId::FromUserEmailGaiaId(email_, GaiaId("12345"));
      user_manager_->AddUser(account_id_);
      builder.SetProfileName(email_);

      builder.OverridePolicyConnectorIsManagedForTesting(is_managed_);
      if (is_ephemeral_) {
        // Enabling ephemeral users passes the |IsEphemeralUserProfile| check.
        user_manager_->SetEphemeralModeConfig(
            user_manager::UserManager::EphemeralModeConfig(
                /* included_by_default= */ true,
                /* include_list= */ std::vector<AccountId>{},
                /* exclude_list= */ std::vector<AccountId>{}));
      } else if (is_unicorn_) {
        user_manager_->set_current_user_child(true);
        builder.SetIsSupervisedProfile();
      }
    }
    profile_ = builder.Build();
    release_notes_storage_ =
        std::make_unique<ReleaseNotesStorage>(profile_.get());
  }

  void SetUp() override {
    scoped_feature_list_.InitAndEnableFeature(
        features::kReleaseNotesNotificationAllChannels);
  }

  user_manager::TypedScopedUserManager<FakeChromeUserManager> user_manager_{
      std::make_unique<FakeChromeUserManager>()};
  content::BrowserTaskEnvironment task_environment_;
  base::test::ScopedFeatureList scoped_feature_list_;
  std::unique_ptr<TestingProfile> profile_;
  std::unique_ptr<ReleaseNotesStorage> release_notes_storage_;

  // Data members for SetUpProfile().
  std::string email_ = "test@gmail.com";
  bool is_guest_ = false;
  bool is_managed_ = false;
  bool is_ephemeral_ = false;
  bool is_unicorn_ = false;
};

// Release notes are not shown for profiles that have been created in this
// milestone.
TEST_F(ReleaseNotesStorageTest, ShouldNotShowReleaseNotesOOBE) {
  SetUpProfile();
  profile_.get()->GetPrefs()->SetString(prefs::kProfileCreatedByVersion,
                                        version_info::GetVersion().GetString());

  EXPECT_EQ(false, release_notes_storage_->ShouldNotify());
}

// Release notes are shown for profiles that have been created in an earlier
// version of chrome.
TEST_F(ReleaseNotesStorageTest, ShouldShowReleaseNotesOldProfile) {
  SetUpProfile();
  profile_.get()->GetPrefs()->SetString(prefs::kProfileCreatedByVersion,
                                        "20.0.0.0");

  EXPECT_EQ(true, release_notes_storage_->ShouldNotify());
}

// We have previously seen another notification on an earlier chrome version,
// release notes should be shown.
TEST_F(ReleaseNotesStorageTest, ShouldShowReleaseNotes) {
  SetUpProfile();
  profile_.get()->GetPrefs()->SetInteger(
      prefs::kHelpAppNotificationLastShownMilestone, 20);

  EXPECT_EQ(true, release_notes_storage_->ShouldNotify());
}

// We have already seen the notification on the current chrome version.
TEST_F(ReleaseNotesStorageTest,
       ShouldNotShowReleaseNotesIfShownInCurrentChromeVersion) {
  SetUpProfile();
  profile_.get()->GetPrefs()->SetInteger(
      prefs::kHelpAppNotificationLastShownMilestone, CurrentMilestone());

  EXPECT_EQ(false, release_notes_storage_->ShouldNotify());
}

// Release notes ShouldNotify is false after being shown once.
TEST_F(ReleaseNotesStorageTest, ReleaseNotesShouldOnlyBeNotifiedOnce) {
  SetUpProfile();

  ASSERT_EQ(true, release_notes_storage_->ShouldNotify());

  release_notes_storage_->MarkNotificationShown();

  EXPECT_NE(20, profile_.get()->GetPrefs()->GetInteger(
                    prefs::kHelpAppNotificationLastShownMilestone));
  EXPECT_EQ(false, release_notes_storage_->ShouldNotify());
}

TEST_F(ReleaseNotesStorageTest, ShouldNotShowReleaseNotesForEphemeralProfile) {
  is_ephemeral_ = true;
  SetUpProfile();
  profile_->ScopedCrosSettingsTestHelper()
      ->InstallAttributes()
      ->SetCloudManaged("test_domain", "FAKE_DEVICE_ID");

  EXPECT_EQ(false, release_notes_storage_->ShouldNotify());
}

TEST_F(ReleaseNotesStorageTest, ShouldNotShowReleaseNotesForGuestProfile) {
  is_guest_ = true;
  SetUpProfile();

  EXPECT_EQ(false, release_notes_storage_->ShouldNotify());
}

TEST_F(ReleaseNotesStorageTest, ShouldNotShowReleaseNotesForManagedProfile) {
  is_managed_ = true;
  SetUpProfile();

  EXPECT_EQ(false, release_notes_storage_->ShouldNotify());
}

TEST_F(ReleaseNotesStorageTest, ShouldShowReleaseNotesForGoogler) {
  is_managed_ = true;
  email_ = "test@google.com";
  SetUpProfile();

  EXPECT_EQ(true, release_notes_storage_->ShouldNotify());
}

TEST_F(ReleaseNotesStorageTest, ShouldShowReleaseNotesForUnicornProfile) {
  is_managed_ = true;
  is_unicorn_ = true;
  SetUpProfile();

  EXPECT_EQ(true, release_notes_storage_->ShouldNotify());
}

// Tests that when kReleaseNotesSuggestionChipTimesLeftToShow is 0,
// ReleaseNotesStorage::ShouldShowSuggestionChip returns false.
TEST_F(ReleaseNotesStorageTest, DoesNotShowReleaseNotesSuggestionChip) {
  SetUpProfile();
  profile_.get()->GetPrefs()->SetInteger(
      prefs::kReleaseNotesSuggestionChipTimesLeftToShow, 0);

  EXPECT_EQ(false, release_notes_storage_->ShouldShowSuggestionChip());
}

// Tests that when kReleaseNotesSuggestionChipTimesLeftToShow is greater than 0,
// ReleaseNotesStorage::ShouldShowSuggestionChip returns true, and when
// decreased the method returns false again.
TEST_F(ReleaseNotesStorageTest, ShowReleaseNotesSuggestionChip) {
  SetUpProfile();
  profile_.get()->GetPrefs()->SetInteger(
      prefs::kReleaseNotesSuggestionChipTimesLeftToShow, 1);

  ASSERT_EQ(true, release_notes_storage_->ShouldShowSuggestionChip());

  release_notes_storage_->DecreaseTimesLeftToShowSuggestionChip();

  EXPECT_EQ(0, profile_.get()->GetPrefs()->GetInteger(
                   prefs::kReleaseNotesSuggestionChipTimesLeftToShow));
  EXPECT_EQ(false, release_notes_storage_->ShouldShowSuggestionChip());
}

}  // namespace ash