File: remote_suggestions_status_service_impl_unittest.cc

package info (click to toggle)
chromium-browser 70.0.3538.110-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,619,476 kB
  • sloc: cpp: 13,024,755; ansic: 1,349,823; python: 916,672; xml: 314,489; java: 280,047; asm: 276,936; perl: 75,771; objc: 66,634; sh: 45,860; cs: 28,354; php: 11,064; makefile: 10,911; yacc: 9,109; tcl: 8,403; ruby: 4,065; lex: 1,779; pascal: 1,411; lisp: 1,055; awk: 41; jsp: 39; sed: 17; sql: 3
file content (192 lines) | stat: -rw-r--r-- 7,997 bytes parent folder | download
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
// Copyright 2017 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/ntp_snippets/remote/remote_suggestions_status_service_impl.h"

#include <memory>

#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "components/ntp_snippets/features.h"
#include "components/ntp_snippets/ntp_snippets_constants.h"
#include "components/ntp_snippets/pref_names.h"
#include "components/ntp_snippets/remote/test_utils.h"
#include "components/prefs/testing_pref_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/variations/variations_params_manager.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ntp_snippets {

namespace {
const char kTestPrefName[] = "search_suggestions.test_name";

}  // namespace

class RemoteSuggestionsStatusServiceImplTest : public ::testing::Test {
 public:
  RemoteSuggestionsStatusServiceImplTest()
      : last_status_(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN) {
    RemoteSuggestionsStatusServiceImpl::RegisterProfilePrefs(
        utils_.pref_service()->registry());

    // Registering additional test preference for testing serch suggestion based
    // feature disabling.
    utils_.pref_service()->registry()->RegisterBooleanPref(kTestPrefName, true);
  }

  // |list_hiding_enabled| indicates whether kArticleSuggestionsExpandableHeader
  // is enabled.
  std::unique_ptr<RemoteSuggestionsStatusServiceImpl> MakeService(
      bool list_hiding_enabled) {
    // Enabling/disabling the feature.
    if (list_hiding_enabled) {
      feature_list_.InitAndEnableFeature(
          ntp_snippets::kArticleSuggestionsExpandableHeader);
    } else {
      feature_list_.InitAndDisableFeature(
          ntp_snippets::kArticleSuggestionsExpandableHeader);
    }

    auto service = std::make_unique<RemoteSuggestionsStatusServiceImpl>(
        false, utils_.pref_service(),
        list_hiding_enabled ? std::string() : kTestPrefName);
    service->Init(base::BindRepeating(
        &RemoteSuggestionsStatusServiceImplTest::OnStatusChange,
        base::Unretained(this)));
    return service;
  }

  RemoteSuggestionsStatus last_status() const { return last_status_; }

 protected:
  void OnStatusChange(RemoteSuggestionsStatus old_status,
                      RemoteSuggestionsStatus new_status) {
    last_status_ = new_status;
  }

  base::test::ScopedFeatureList feature_list_;
  RemoteSuggestionsStatus last_status_;
  test::RemoteSuggestionsTestUtils utils_;
  variations::testing::VariationParamsManager params_manager_;
};

TEST_F(RemoteSuggestionsStatusServiceImplTest, NoSigninNeeded) {
  auto service = MakeService(/*list_hiding_enabled=*/false);

  // By default, no signin is required.
  EXPECT_EQ(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT, last_status());

  // Signin should cause a state change.
  service->OnSignInStateChanged(/*has_signed_in=*/true);
  EXPECT_EQ(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN, last_status());
}

TEST_F(RemoteSuggestionsStatusServiceImplTest, DisabledViaPref) {
  auto service = MakeService(/*list_hiding_enabled=*/false);

  // The default test setup is signed out. The service is enabled.
  ASSERT_EQ(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT, last_status());

  // Once the enabled pref is set to false, we should be disabled.
  utils_.pref_service()->SetBoolean(prefs::kEnableSnippets, false);
  EXPECT_EQ(RemoteSuggestionsStatus::EXPLICITLY_DISABLED, last_status());

  // The state should not change, even though a signin has occurred.
  service->OnSignInStateChanged(/*has_signed_in=*/true);
  EXPECT_EQ(RemoteSuggestionsStatus::EXPLICITLY_DISABLED, last_status());
}

TEST_F(RemoteSuggestionsStatusServiceImplTest, DisabledViaAdditionalPref) {
  auto service = MakeService(/*list_hiding_enabled=*/false);

  // The default test setup is signed out. The service is enabled.
  ASSERT_EQ(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT, last_status());

  // Once the additional pref is set to false, we should be disabled.
  utils_.pref_service()->SetBoolean(kTestPrefName, false);
  EXPECT_EQ(RemoteSuggestionsStatus::EXPLICITLY_DISABLED, last_status());

  // The state should not change, even though a signin has occurred.
  service->OnSignInStateChanged(/*has_signed_in=*/true);
  EXPECT_EQ(RemoteSuggestionsStatus::EXPLICITLY_DISABLED, last_status());
}

TEST_F(RemoteSuggestionsStatusServiceImplTest, EnabledAfterListFolded) {
  auto service = MakeService(/*list_hiding_enabled="*/ true);
  // By default, the articles list should be visible.
  EXPECT_TRUE(utils_.pref_service()->GetBoolean(prefs::kArticlesListVisible));

  // The default test setup is signed out. The service is enabled.
  ASSERT_EQ(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT, last_status());

  // When the user toggles the visibility of articles list in UI off the service
  // should still be enabled until the end of the session.
  utils_.pref_service()->SetBoolean(prefs::kArticlesListVisible, false);
  EXPECT_EQ(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT, last_status());

  // Signin should cause a state change.
  service->OnSignInStateChanged(/*has_signed_in=*/true);
  EXPECT_EQ(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN, last_status());
}

TEST_F(RemoteSuggestionsStatusServiceImplTest, DisabledWhenListFoldedOnStart) {
  utils_.pref_service()->SetBoolean(prefs::kArticlesListVisible, false);
  auto service = MakeService(/*list_hiding_enabled="*/ true);

  // The state should be disabled when starting with no list shown.
  EXPECT_EQ(RemoteSuggestionsStatus::EXPLICITLY_DISABLED, last_status());

  // The state should not change, even though a signin has occurred.
  service->OnSignInStateChanged(/*has_signed_in=*/true);
  EXPECT_EQ(RemoteSuggestionsStatus::EXPLICITLY_DISABLED, last_status());
}

TEST_F(RemoteSuggestionsStatusServiceImplTest, EnablingAfterFoldedStart) {
  utils_.pref_service()->SetBoolean(prefs::kArticlesListVisible, false);
  auto service = MakeService(/*list_hiding_enabled="*/ true);

  // The state should be disabled when starting with no list shown.
  EXPECT_EQ(RemoteSuggestionsStatus::EXPLICITLY_DISABLED, last_status());

  // When the user toggles the visibility of articles list in UI on, the service
  // should get enabled.
  utils_.pref_service()->SetBoolean(prefs::kArticlesListVisible, true);
  EXPECT_EQ(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT, last_status());

  // Signin should cause a state change.
  service->OnSignInStateChanged(/*has_signed_in=*/true);
  EXPECT_EQ(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN, last_status());
}

TEST_F(RemoteSuggestionsStatusServiceImplTest,
       EnablingAfterFoldedStartSignedIn) {
  utils_.pref_service()->SetBoolean(prefs::kArticlesListVisible, false);
  auto service = MakeService(/*list_hiding_enabled="*/ true);

  // Signin should not cause a state change, because UI is not visible.
  service->OnSignInStateChanged(/*has_signed_in=*/true);
  EXPECT_EQ(RemoteSuggestionsStatus::EXPLICITLY_DISABLED, last_status());

  // When the user toggles the visibility of articles list in UI on, the service
  // should get enabled.
  utils_.pref_service()->SetBoolean(prefs::kArticlesListVisible, true);
  EXPECT_EQ(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN, last_status());
}

TEST_F(RemoteSuggestionsStatusServiceImplTest,
       DisablingHidingFeatureWhenFolder) {
  utils_.pref_service()->SetBoolean(prefs::kArticlesListVisible, false);
  auto service = MakeService(/*list_hiding_enabled="*/ false);

  // The state should be enabled when hiding is disabled.
  EXPECT_EQ(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT, last_status());

  // Signin should cause a state change.
  service->OnSignInStateChanged(/*has_signed_in=*/true);
  EXPECT_EQ(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN, last_status());
}

}  // namespace ntp_snippets