File: spotlight_crd_manager_impl_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (191 lines) | stat: -rw-r--r-- 7,155 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
// Copyright 2025 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/boca/spotlight/spotlight_crd_manager_impl.h"

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

#include "ash/boca/spotlight/spotlight_notification_bubble_controller.h"
#include "ash/constants/ash_features.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/json/json_writer.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "chrome/browser/ash/policy/remote_commands/crd/public/crd_session_result_codes.h"
#include "chrome/browser/ash/policy/remote_commands/crd/public/shared_crd_session.h"
#include "chrome/browser/ash/system_web_apps/system_web_app_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/dns/mock_host_resolver.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::test::TestFuture;
using ::testing::_;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::NiceMock;
using ::testing::StrictMock;
using ::testing::WithArg;

namespace ash::boca {
namespace {
constexpr char kSpotlightConnectionCode[] = "123";
constexpr char kUserEmail[] = "cat@gmail.com";
constexpr char kCrdResultUma[] = "Enterprise.Boca.Spotlight.Crd.Result";

class MockSharedCrdSession : public policy::SharedCrdSession {
 public:
  ~MockSharedCrdSession() override = default;

  MOCK_METHOD(void,
              StartCrdHost,
              (const SessionParameters& parameters,
               AccessCodeCallback success_callback,
               ErrorCallback error_callback,
               SessionFinishedCallback session_finished_callback),
              (override));
  MOCK_METHOD(void, TerminateSession, (), (override));
};

class SpotlightCrdManagerImplTest : public InProcessBrowserTest {
 protected:
  SpotlightCrdManagerImplTest() {
    scoped_feature_list_.InitWithFeatures(
        {ash::features::kBoca, ash::features::kBocaConsumer,
         ash::features::kBocaSpotlight,
         ash::features::kOnDeviceSpeechRecognition},
        /*disabled_features=*/{});
  }

  void SetUpOnMainThread() override {
    ash::SystemWebAppManager::Get(profile())->InstallSystemAppsForTesting();
    auto crd_session = std::make_unique<NiceMock<MockSharedCrdSession>>();
    crd_session_ = crd_session.get();
    auto notification_bubble_controller =
        std::make_unique<ash::SpotlightNotificationBubbleController>();
    notification_bubble_controller_ = notification_bubble_controller.get();
    manager_ = std::make_unique<SpotlightCrdManagerImpl>(
        std::move(crd_session), std::move(notification_bubble_controller));
    host_resolver()->AddRule("*", "127.0.0.1");
    InProcessBrowserTest::SetUpOnMainThread();
    embedded_test_server()->AddDefaultHandlers(
        base::FilePath(FILE_PATH_LITERAL("content/test/data")));
    ASSERT_TRUE(embedded_test_server()->Start());
  }

  Profile* profile() { return browser()->profile(); }

  base::test::ScopedFeatureList scoped_feature_list_;
  std::unique_ptr<SpotlightCrdManagerImpl> manager_;
  raw_ptr<NiceMock<MockSharedCrdSession>> crd_session_;
  raw_ptr<ash::SpotlightNotificationBubbleController>
      notification_bubble_controller_;
};

IN_PROC_BROWSER_TEST_F(SpotlightCrdManagerImplTest,
                       InitiateSpotlightSessionShouldStartCrdHost) {
  EXPECT_CALL(*crd_session_, StartCrdHost)
      .WillOnce(WithArg<1>(
          Invoke([&](auto callback) { std::move(callback).Run("123"); })));
  TestFuture<const std::string&> success_future;

  manager_->InitiateSpotlightSession(success_future.GetCallback(), kUserEmail);
  ::testing::Mock::VerifyAndClearExpectations(crd_session_);

  EXPECT_EQ(kSpotlightConnectionCode, success_future.Get());
}

IN_PROC_BROWSER_TEST_F(
    SpotlightCrdManagerImplTest,
    InitiateSpotlightSessionWithCrdFailureShouldRunErrorCallback) {
  base::HistogramTester histograms;

  TestFuture<void> error_callback_future;
  EXPECT_CALL(*crd_session_, StartCrdHost)
      .WillOnce(WithArg<2>(Invoke([&](auto callback) {
        base::UmaHistogramEnumeration(
            kCrdResultUma,
            policy::ExtendedStartCrdSessionResultCode::kFailureCrdHostError);
        error_callback_future.SetValue();
      })));

  manager_->InitiateSpotlightSession(
      base::BindOnce([](const std::string& result) {
        GTEST_FAIL() << "Unexpected call to success callback";
      }),
      kUserEmail);
  ::testing::Mock::VerifyAndClearExpectations(crd_session_);

  EXPECT_TRUE(error_callback_future.Wait());
  EXPECT_EQ(
      histograms.GetBucketCount(
          kCrdResultUma,
          policy::ExtendedStartCrdSessionResultCode::kFailureCrdHostError),
      1);
}

IN_PROC_BROWSER_TEST_F(SpotlightCrdManagerImplTest,
                       OnSessionEndedShouldTerminateSession) {
  EXPECT_CALL(*crd_session_, StartCrdHost).Times(1);
  EXPECT_CALL(*crd_session_, TerminateSession()).Times(1);
  TestFuture<const std::string&> success_future;

  manager_->InitiateSpotlightSession(success_future.GetCallback(), kUserEmail);

  manager_->OnSessionEnded();
  ::testing::Mock::VerifyAndClearExpectations(crd_session_);
}

IN_PROC_BROWSER_TEST_F(SpotlightCrdManagerImplTest,
                       ShowPersistentNotificationShowsWidget) {
  manager_->ShowPersistentNotification("Teacher");

  EXPECT_TRUE(notification_bubble_controller_->IsNotificationBubbleVisible());
}

IN_PROC_BROWSER_TEST_F(SpotlightCrdManagerImplTest,
                       HidePersistentNotificationHidesWidget) {
  manager_->ShowPersistentNotification("Teacher");

  manager_->HidePersistentNotification();
  EXPECT_FALSE(notification_bubble_controller_->IsNotificationBubbleVisible());
}

IN_PROC_BROWSER_TEST_F(SpotlightCrdManagerImplTest,
                       HidesNotificationWidgetOnSessionEnd) {
  manager_->ShowPersistentNotification("Teacher");

  manager_->OnSessionEnded();
  ::testing::Mock::VerifyAndClearExpectations(crd_session_);
  EXPECT_FALSE(notification_bubble_controller_->IsNotificationBubbleVisible());
}

IN_PROC_BROWSER_TEST_F(SpotlightCrdManagerImplTest,
                       TerminatesCrdSessionOnSessionEnd) {
  TestFuture<void> session_finished_future;
  EXPECT_CALL(*crd_session_, TerminateSession)
      .WillOnce(InvokeWithoutArgs(
          [&]() { std::move(session_finished_future.GetCallback()).Run(); }));

  TestFuture<const std::string&> success_future;
  manager_->InitiateSpotlightSession(success_future.GetCallback(), kUserEmail);

  manager_->OnSessionEnded();
  ::testing::Mock::VerifyAndClearExpectations(crd_session_);
  EXPECT_TRUE(session_finished_future.Wait());
}

}  // namespace
}  // namespace ash::boca