File: search_section_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 (317 lines) | stat: -rw-r--r-- 12,575 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2024 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/ui/webui/ash/settings/pages/search/search_section.h"

#include <memory>

#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "ash/public/cpp/scanner/scanner_delegate.h"
#include "ash/scanner/fake_scanner_profile_scoped_delegate.h"
#include "ash/scanner/scanner_controller.h"
#include "ash/shell.h"
#include "ash/webui/settings/public/constants/setting.mojom-shared.h"
#include "base/auto_reset.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/strcat.h"
#include "base/strings/to_string.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/ash/lobster/lobster_service_provider.h"
#include "chrome/browser/ash/lobster/mock_lobster_system_state_provider.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/ash/settings/search/search_tag_registry.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/chrome_ash_test_base.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/components/browser_context_helper/annotated_account_id.h"
#include "chromeos/ash/components/specialized_features/feature_access_checker.h"
#include "chromeos/components/magic_boost/test/fake_magic_boost_state.h"
#include "chromeos/components/quick_answers/public/cpp/quick_answers_state.h"
#include "chromeos/components/quick_answers/test/fake_quick_answers_state.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user_names.h"
#include "content/public/test/test_web_ui_data_source.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::settings {
namespace {
using ::specialized_features::FeatureAccessFailure;
using ::testing::Return;

FakeScannerProfileScopedDelegate* GetFakeScannerProfileScopedDelegate(
    ScannerController& scanner_controller) {
  return static_cast<FakeScannerProfileScopedDelegate*>(
      scanner_controller.delegate_for_testing()->GetProfileScopedDelegate());
}

}  // namespace

// Test for the search settings page.
class SearchSectionTest : public ChromeAshTestBase {
 public:
  SearchSectionTest()
      : local_search_service_proxy_(
            std::make_unique<
                ash::local_search_service::LocalSearchServiceProxy>(
                /*for_testing=*/true)),
        search_tag_registry_(local_search_service_proxy_.get()) {}

  ~SearchSectionTest() override = default;

  TestingProfile* profile() { return &profile_; }
  ash::settings::SearchTagRegistry* search_tag_registry() {
    return &search_tag_registry_;
  }

 protected:
  void TearDown() override {
    search_section_.reset();
    ChromeAshTestBase::TearDown();
  }

  std::unique_ptr<SearchSection> search_section_;

 private:
  std::unique_ptr<ash::local_search_service::LocalSearchServiceProxy>
      local_search_service_proxy_;
  ash::settings::SearchTagRegistry search_tag_registry_;
  TestingProfile profile_;
};

TEST_F(SearchSectionTest,
       SearchSectionDoesNotIncludesLobsterSettingsByDefault) {
  search_section_ =
      std::make_unique<SearchSection>(profile(), search_tag_registry());
  std::unique_ptr<content::TestWebUIDataSource> html_source =
      content::TestWebUIDataSource::Create("test-search-section");

  chromeos::test::FakeMagicBoostState magic_boost_state;
  magic_boost_state.SetAvailability(true);

  search_section_->AddLoadTimeData(html_source->GetWebUIDataSource());

  EXPECT_FALSE(html_source->GetLocalizedStrings()
                   ->FindBool("isLobsterSettingsToggleVisible")
                   .value());
}

// MagicBoost availability check requires an async operation. There is a short
// period where `MagicBoostState` returns false for its availability even if a
// user/device is eligible, and magic boost is enabled.
TEST_F(SearchSectionTest,
       QuickAnswersSearchConceptsRemovedIfItBecomesUnavailable) {
  const std::string quick_answers_result_id = base::StrCat(
      {base::ToString(chromeos::settings::mojom::Setting::kQuickAnswersOnOff),
       ",", base::ToString(IDS_OS_SETTINGS_TAG_QUICK_ANSWERS)});

  chromeos::test::FakeMagicBoostState magic_boost_state;
  magic_boost_state.SetAvailability(false);
  magic_boost_state.SetMagicBoostEnabled(true);
  FakeQuickAnswersState quick_answers_state;
  quick_answers_state.SetApplicationLocale("en");
  ASSERT_EQ(QuickAnswersState::FeatureType::kQuickAnswers,
            QuickAnswersState::GetFeatureType())
      << "Current feature type is set to kQuickAnswers. This is simulating the "
         "case where MagicBoost availability check async opearation is not "
         "completed.";

  search_section_ =
      std::make_unique<SearchSection>(profile(), search_tag_registry());

  EXPECT_NE(nullptr,
            search_tag_registry()->GetTagMetadata(quick_answers_result_id))
      << "QuickAnswers tag should be registered as the current feature is set "
         "to kQuickAnswers";

  // Simulate that MagicBoost availability check async operation has been
  // completed.
  magic_boost_state.SetAvailability(true);
  ASSERT_EQ(QuickAnswersState::FeatureType::kHmr,
            QuickAnswersState::GetFeatureType());

  EXPECT_EQ(nullptr,
            search_tag_registry()->GetTagMetadata(quick_answers_result_id))
      << "Quick Answers tag should not be found as the current feature type is "
         "set to kHmr";
}

class SearchSectionTestWithLobsterEnabled : public SearchSectionTest {
 public:
  void SetUp() override {
    SearchSectionTest::SetUp();
    magic_boost_state_.SetAvailability(true);
    feature_list_.InitWithFeatures(
        /*enable_features=*/{ash::features::kLobster,
                             ash::features::kFeatureManagementLobster},
        /*disable_features=*/{});
    AnnotateAccount();

    html_source_ = content::TestWebUIDataSource::Create("test-search-section");
    search_section_ =
        std::make_unique<SearchSection>(profile(), search_tag_registry());
  }

  void TearDown() override {
    magic_boost_state_.RemoveObserver(search_section_.get());
    SearchSectionTest::TearDown();
  }

  content::TestWebUIDataSource* html_source() { return html_source_.get(); }

  void SetUpSystemProviderForLobsterService(
      const ash::LobsterSystemState& system_state) {
    std::unique_ptr<MockLobsterSystemStateProvider> mock_system_state_provider =
        std::make_unique<MockLobsterSystemStateProvider>();
    ON_CALL(*mock_system_state_provider, GetSystemState)
        .WillByDefault(testing::Return(system_state));
    LobsterServiceProvider::GetForProfile(profile())
        ->set_lobster_system_state_provider_for_testing(
            std::move(mock_system_state_provider));
  }

 private:
  void AnnotateAccount() {
    auto* user = fake_user_manager_->AddUser(user_manager::StubAccountId());
    fake_user_manager_->LoginUser(user->GetAccountId());
    ash::AnnotatedAccountId::Set(profile(), user->GetAccountId());
  }

  base::test::ScopedFeatureList feature_list_;
  user_manager::TypedScopedUserManager<ash::FakeChromeUserManager>
      fake_user_manager_{std::make_unique<ash::FakeChromeUserManager>()};
  std::unique_ptr<content::TestWebUIDataSource> html_source_;
  chromeos::test::FakeMagicBoostState magic_boost_state_;
};

TEST_F(SearchSectionTestWithLobsterEnabled,
       DoesNotIncludeLobsterSettingsIfLobsterIsHardBlocked) {
  SetUpSystemProviderForLobsterService(ash::LobsterSystemState(
      ash::LobsterStatus::kBlocked, /*failed_checks=*/{
          ash::LobsterSystemCheck::kInvalidAccountCapabilities}));

  search_section_->AddLoadTimeData(html_source()->GetWebUIDataSource());

  EXPECT_FALSE(html_source()
                   ->GetLocalizedStrings()
                   ->FindBool("isLobsterSettingsToggleVisible")
                   .value());
}

TEST_F(SearchSectionTestWithLobsterEnabled,
       IncludeLobsterSettingsIfLobsterIsSoftBlocked) {
  SetUpSystemProviderForLobsterService(ash::LobsterSystemState(
      ash::LobsterStatus::kBlocked,
      /*failed_checks=*/{ash::LobsterSystemCheck::kSettingsOff}));

  search_section_->AddLoadTimeData(html_source()->GetWebUIDataSource());

  EXPECT_TRUE(html_source()
                  ->GetLocalizedStrings()
                  ->FindBool("isLobsterSettingsToggleVisible")
                  .value());
}

class SearchSectionTestWithScannerEnabled : public ChromeAshTestBase {
 public:
  SearchSectionTestWithScannerEnabled()
      : local_search_service_proxy_(
            std::make_unique<
                ash::local_search_service::LocalSearchServiceProxy>(
                /*for_testing=*/true)),
        search_tag_registry_(local_search_service_proxy_.get()) {}

  ~SearchSectionTestWithScannerEnabled() override = default;

  TestingProfile* profile() { return &profile_; }
  ash::settings::SearchTagRegistry* search_tag_registry() {
    return &search_tag_registry_;
  }

 protected:
  void SetUp() override { ChromeAshTestBase::SetUp(); }
  void TearDown() override { ChromeAshTestBase::TearDown(); }

 private:
  // Required to force the scanner controller to be set up with dogfood
  // config.
  base::test::ScopedFeatureList feature_list_{features::kScannerDogfood};
  std::unique_ptr<ash::local_search_service::LocalSearchServiceProxy>
      local_search_service_proxy_;
  ash::settings::SearchTagRegistry search_tag_registry_;
  TestingProfile profile_;
};

TEST_F(SearchSectionTestWithScannerEnabled,
       DoesNotIncludeScannerSettingsWhenScannerDisabledByAccessChecker) {
  ScannerController* scanner_controller = Shell::Get()->scanner_controller();
  ON_CALL(*GetFakeScannerProfileScopedDelegate(*scanner_controller),
          CheckFeatureAccess)
      .WillByDefault(Return(specialized_features::FeatureAccessFailureSet{
          FeatureAccessFailure::kDisabledInKioskModeCheckFailed}));

  auto search_section =
      std::make_unique<SearchSection>(profile(), search_tag_registry());
  std::unique_ptr<content::TestWebUIDataSource> html_source =
      content::TestWebUIDataSource::Create("test-search-section");
  chromeos::test::FakeMagicBoostState magic_boost_state;
  magic_boost_state.SetAvailability(true);

  search_section->AddLoadTimeData(html_source->GetWebUIDataSource());

  EXPECT_FALSE(html_source->GetLocalizedStrings()
                   ->FindBool("isScannerSettingsToggleVisible")
                   .value());
}

TEST_F(
    SearchSectionTestWithScannerEnabled,
    IncludesScannerSettingsWhenFeatureAccessCheckFailsConsentOrSettingsCheck) {
  ScannerController* scanner_controller = Shell::Get()->scanner_controller();
  ON_CALL(*GetFakeScannerProfileScopedDelegate(*scanner_controller),
          CheckFeatureAccess)
      .WillByDefault(Return(specialized_features::FeatureAccessFailureSet{
          FeatureAccessFailure::kConsentNotAccepted,
          FeatureAccessFailure::kDisabledInSettings,
      }));

  auto search_section =
      std::make_unique<SearchSection>(profile(), search_tag_registry());
  std::unique_ptr<content::TestWebUIDataSource> html_source =
      content::TestWebUIDataSource::Create("test-search-section");
  chromeos::test::FakeMagicBoostState magic_boost_state;
  magic_boost_state.SetAvailability(true);

  search_section->AddLoadTimeData(html_source->GetWebUIDataSource());

  EXPECT_TRUE(html_source->GetLocalizedStrings()
                  ->FindBool("isScannerSettingsToggleVisible")
                  .value());
}

TEST_F(SearchSectionTestWithScannerEnabled,
       IncludeScannerSettingsWhenNoChecksFail) {
  ScannerController* scanner_controller = Shell::Get()->scanner_controller();
  ON_CALL(*GetFakeScannerProfileScopedDelegate(*scanner_controller),
          CheckFeatureAccess)
      .WillByDefault(Return(specialized_features::FeatureAccessFailureSet{}));

  auto search_section =
      std::make_unique<SearchSection>(profile(), search_tag_registry());
  std::unique_ptr<content::TestWebUIDataSource> html_source =
      content::TestWebUIDataSource::Create("test-search-section");
  chromeos::test::FakeMagicBoostState magic_boost_state;
  magic_boost_state.SetAvailability(true);

  search_section->AddLoadTimeData(html_source->GetWebUIDataSource());

  EXPECT_TRUE(html_source->GetLocalizedStrings()
                  ->FindBool("isScannerSettingsToggleVisible")
                  .value());
}

}  // namespace ash::settings