File: assistant_browser_delegate_impl_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 (239 lines) | stat: -rw-r--r-- 9,476 bytes parent folder | download | duplicates (4)
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
// 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/ash/assistant/assistant_browser_delegate_impl.h"

#include <memory>
#include <optional>
#include <set>
#include <string>

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/types/expected.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/web_applications/test/fake_web_app_database_factory.h"
#include "chrome/browser/web_applications/test/fake_web_app_provider.h"
#include "chrome/browser/web_applications/test/fake_web_app_ui_manager.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_management_type.h"
#include "chrome/test/base/chrome_ash_test_base.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/services/assistant/public/cpp/assistant_browser_delegate.h"
#include "chromeos/ash/services/assistant/public/cpp/features.h"
#include "chromeos/services/assistant/public/shared/constants.h"
#include "components/session_manager/core/session_manager.h"
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user_manager.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

class AssistantBrowserDelegateImplTest : public ChromeAshTestBase {
 public:
  AssistantBrowserDelegateImplTest()
      : fake_user_manager_(std::make_unique<ash::FakeChromeUserManager>()) {
    set_start_session(false);
  }
  ~AssistantBrowserDelegateImplTest() override = default;

  void SetUp() override {
    ChromeAshTestBase::SetUp();
    session_manager::SessionManager* session_manager =
        session_manager::SessionManager::Get();
    session_manager->OnUserManagerCreated(fake_user_manager_.Get());

    delegate_.emplace();
    delegate_->SetGoogleChromeBuildForTesting();

    TestingProfile::Builder profile_builder;
    profile_ = profile_builder.Build();
    auto account_id = AccountId::FromUserEmail(profile_->GetProfileUserName());
    auto* user = fake_user_manager_->AddUser(account_id);
    fake_user_manager_->LoginUser(account_id);

    SimulateUserLogin(account_id);

    ash::ProfileHelper::Get()->SetUserToProfileMappingForTesting(
        user, profile_.get());

    session_manager->CreateSession(
        account_id,
        user_manager::FakeUserManager::GetFakeUsernameHash(account_id),
        /*new_user=*/false,
        /*has_active_session=*/false);
    session_manager->SessionStarted();

    web_app::FakeWebAppProvider::Get(profile_.get())
        ->SetDatabaseFactory(
            std::make_unique<web_app::FakeWebAppDatabaseFactory>());
  }

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

 protected:
  Profile* profile() const { return profile_.get(); }

  web_app::FakeWebAppProvider& fake_provider() const {
    return *web_app::FakeWebAppProvider::Get(profile());
  }

  web_app::FakeWebAppDatabaseFactory& database_factory() const {
    return static_cast<web_app::FakeWebAppDatabaseFactory&>(
        fake_provider().GetDatabaseFactory());
  }

  void InstallNewEntryPointApp() {
    const GURL start_url = GURL("https://example.com/path");
    const webapps::AppId app_id =
        web_app::GenerateAppId(/*manifest_id_path=*/std::nullopt, start_url);
    auto web_app = std::make_unique<web_app::WebApp>(app_id);
    web_app->SetStartUrl(start_url);
    web_app->SetName("test app");
    web_app->AddSource(web_app::WebAppManagement::kSystem);
    web_app->SetDisplayMode(web_app::DisplayMode::kStandalone);
    web_app->SetUserDisplayMode(web_app::mojom::UserDisplayMode::kStandalone);

    web_app::Registry registry;
    registry[app_id] = std::move(web_app);
    database_factory().WriteRegistry(registry);

    delegate_->OverrideEntryPointIdForTesting(app_id);
  }

  user_manager::TypedScopedUserManager<ash::FakeChromeUserManager>
      fake_user_manager_;
  std::unique_ptr<TestingProfile> profile_;
  std::optional<AssistantBrowserDelegateImpl> delegate_;

  base::test::TestFuture<apps::AppLaunchParams,
                         web_app::LaunchWebAppWindowSetting>
      app_launch_future_;
};

TEST_F(AssistantBrowserDelegateImplTest, NewEntryPointOpensApp) {
  base::test::ScopedFeatureList scoped_feature_list(
      ash::assistant::features::kEnableNewEntryPoint);

  InstallNewEntryPointApp();
  web_app::test::AwaitStartWebAppProviderAndSubsystems(profile());

  auto* provider = web_app::WebAppProvider::GetForWebApps(profile());
  static_cast<web_app::FakeWebAppUiManager*>(&provider->ui_manager())
      ->SetOnLaunchWebAppCallback(app_launch_future_.GetRepeatingCallback());

  // Try opening new entry point.
  delegate_->OpenNewEntryPoint();

  // New entry point should open.
  ASSERT_TRUE(app_launch_future_.Wait())
      << "New entry point should launch, but didn't.";
}

TEST_F(AssistantBrowserDelegateImplTest, EligibleToNewEntryPoint) {
  base::HistogramTester histogram_tester;
  base::test::ScopedFeatureList scoped_feature_list(
      ash::assistant::features::kEnableNewEntryPoint);

  InstallNewEntryPointApp();
  web_app::test::AwaitStartWebAppProviderAndSubsystems(profile());

  base::expected<bool, ash::assistant::AssistantBrowserDelegate::Error>
      maybe_eligibility = delegate_->IsNewEntryPointEligibleForPrimaryProfile();
  ASSERT_TRUE(maybe_eligibility.has_value());
  EXPECT_TRUE(maybe_eligibility.value());
  // sample=0 is kEligible.
  histogram_tester.ExpectUniqueSample("Assistant.NewEntryPoint.Eligibility",
                                      /*sample=*/0,
                                      /*expected_bucket_count=*/1);
}

TEST_F(AssistantBrowserDelegateImplTest, NotEligibleBecauseOfFlagOff) {
  base::HistogramTester histogram_tester;
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndDisableFeature(
      ash::assistant::features::kEnableNewEntryPoint);

  InstallNewEntryPointApp();
  web_app::test::AwaitStartWebAppProviderAndSubsystems(profile());

  base::expected<bool, ash::assistant::AssistantBrowserDelegate::Error>
      maybe_eligibility = delegate_->IsNewEntryPointEligibleForPrimaryProfile();
  ASSERT_TRUE(maybe_eligibility.has_value());
  EXPECT_FALSE(maybe_eligibility.value());
  // sample=3 is kNotEligibleFlagOff.
  histogram_tester.ExpectUniqueSample("Assistant.NewEntryPoint.Eligibility",
                                      /*sample=*/3,
                                      /*expected_bucket_count=*/1);
}

TEST_F(AssistantBrowserDelegateImplTest, NewEntryPointDoNotOpenIfNotInstalled) {
  base::test::ScopedFeatureList scoped_feature_list(
      ash::assistant::features::kEnableNewEntryPoint);

  web_app::test::AwaitStartWebAppProviderAndSubsystems(profile());

  auto* provider = web_app::WebAppProvider::GetForWebApps(profile());
  static_cast<web_app::FakeWebAppUiManager*>(&provider->ui_manager())
      ->SetOnLaunchWebAppCallback(app_launch_future_.GetRepeatingCallback());

  // Try opening new entry point without installing app.
  delegate_->OpenNewEntryPoint();

  // Wait for things to settle to check if entry point is launched.
  task_environment()->RunUntilIdle();

  // New entry point should not open.
  ASSERT_FALSE(app_launch_future_.IsReady())
      << "New entry point should not launch, but somehow it did.";
}

TEST_F(AssistantBrowserDelegateImplTest, NotEligibleBecauseOfNoEntryPointApp) {
  base::HistogramTester histogram_tester;
  base::test::ScopedFeatureList scoped_feature_list(
      ash::assistant::features::kEnableNewEntryPoint);

  web_app::test::AwaitStartWebAppProviderAndSubsystems(profile());

  base::expected<bool, ash::assistant::AssistantBrowserDelegate::Error>
      maybe_eligibility = delegate_->IsNewEntryPointEligibleForPrimaryProfile();
  ASSERT_TRUE(maybe_eligibility.has_value());
  EXPECT_FALSE(maybe_eligibility.value());
  // sample=4 is kNotEligibleNotInstalled.
  histogram_tester.ExpectUniqueSample("Assistant.NewEntryPoint.Eligibility",
                                      /*sample=*/4,
                                      /*expected_bucket_count=*/1);
}

TEST_F(AssistantBrowserDelegateImplTest, NewEntryPointName) {
  base::test::ScopedFeatureList scoped_feature_list(
      ash::assistant::features::kEnableNewEntryPoint);

  InstallNewEntryPointApp();
  web_app::test::AwaitStartWebAppProviderAndSubsystems(profile());

  EXPECT_EQ("test app", delegate_->GetNewEntryPointName());
}

TEST_F(AssistantBrowserDelegateImplTest,
       NoEntryPointNameBecauseOfNoEntryPointApp) {
  base::test::ScopedFeatureList scoped_feature_list(
      ash::assistant::features::kEnableNewEntryPoint);

  web_app::test::AwaitStartWebAppProviderAndSubsystems(profile());

  EXPECT_EQ(std::nullopt, delegate_->GetNewEntryPointName());
}