File: eche_app_manager_factory_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 (268 lines) | stat: -rw-r--r-- 11,443 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
// Copyright 2022 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/eche_app/eche_app_manager_factory.h"

#include <memory>
#include <optional>

#include "ash/constants/ash_features.h"
#include "ash/system/eche/eche_tray.h"
#include "ash/system/phonehub/phone_hub_tray.h"
#include "ash/system/status_area_widget_test_helper.h"
#include "ash/system/tray/tray_bubble_wrapper.h"
#include "ash/test/test_ash_web_view_factory.h"
#include "ash/webui/eche_app_ui/apps_launch_info_provider.h"
#include "ash/webui/eche_app_ui/eche_alert_generator.h"
#include "ash/webui/eche_app_ui/system_info.h"
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_chromeos_version_info.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "chrome/browser/ash/eche_app/eche_app_notification_controller.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/test/base/chrome_ash_test_base.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "ui/gfx/image/image_unittest_util.h"

namespace ash {
namespace eche_app {

class EcheAppManagerFactoryTest : public ChromeAshTestBase {
 protected:
  EcheAppManagerFactoryTest() {
    scoped_feature_list_.InitWithFeatures(
        /*enabled_features=*/{features::kEcheSWA},
        /*disabled_features=*/{});
    profile_manager_ = std::make_unique<TestingProfileManager>(
        TestingBrowserProcess::GetGlobal());
    if (profile_manager_->SetUp()) {
      profile_ = profile_manager_->CreateTestingProfile("testing_profile");
    }
  }
  ~EcheAppManagerFactoryTest() override = default;
  EcheAppManagerFactoryTest(const EcheAppManagerFactoryTest&) = delete;
  EcheAppManagerFactoryTest& operator=(const EcheAppManagerFactoryTest&) =
      delete;

  // ChromeAshTestBase::
  void SetUp() override {
    DCHECK(profile_);
    DCHECK(test_web_view_factory_.get());
    ChromeAshTestBase::SetUp();
    connection_handler_ = std::make_unique<EcheConnectionStatusHandler>();
    apps_launch_info_provider_ =
        std::make_unique<AppsLaunchInfoProvider>(connection_handler_.get());
    apps_launch_info_provider_->SetAppLaunchInfo(
        mojom::AppStreamLaunchEntryPoint::APPS_LIST);
    eche_tray_ = StatusAreaWidgetTestHelper::GetStatusAreaWidget()->eche_tray();
    phone_hub_tray_ =
        StatusAreaWidgetTestHelper::GetStatusAreaWidget()->phone_hub_tray();
    display_service_ =
        std::make_unique<NotificationDisplayServiceTester>(GetProfile());
    eche_app_manager_factory_ = EcheAppManagerFactory::GetInstance();
  }

  void ShowAndCloseConnectionOrLaunchErrorNotifications() {
    std::u16string title = u"title";
    std::u16string message = u"message";
    EcheAppManagerFactory::ShowNotification(
        eche_app_manager_factory_->weak_ptr_factory_.GetWeakPtr(), GetProfile(),
        title, message,
        std::make_unique<LaunchAppHelper::NotificationInfo>(
            LaunchAppHelper::NotificationInfo::Category::kNative,
            LaunchAppHelper::NotificationInfo::NotificationType::kScreenLock));
    EcheAppManagerFactory::ShowNotification(
        eche_app_manager_factory_->weak_ptr_factory_.GetWeakPtr(), GetProfile(),
        title, message,
        std::make_unique<LaunchAppHelper::NotificationInfo>(
            LaunchAppHelper::NotificationInfo::Category::kWebUI,
            mojom::WebNotificationType::CONNECTION_FAILED));
    EcheAppManagerFactory::ShowNotification(
        eche_app_manager_factory_->weak_ptr_factory_.GetWeakPtr(), GetProfile(),
        title, message,
        std::make_unique<LaunchAppHelper::NotificationInfo>(
            LaunchAppHelper::NotificationInfo::Category::kWebUI,
            mojom::WebNotificationType::DEVICE_IDLE));
    EcheAppManagerFactory::ShowNotification(
        eche_app_manager_factory_->weak_ptr_factory_.GetWeakPtr(), GetProfile(),
        title, message,
        std::make_unique<LaunchAppHelper::NotificationInfo>(
            LaunchAppHelper::NotificationInfo::Category::kWebUI,
            mojom::WebNotificationType::INVALID_NOTIFICATION));
    eche_app_manager_factory_->CloseConnectionOrLaunchErrorNotifications();

    std::optional<message_center::Notification> notification =
        display_service_->GetNotification(kEcheAppScreenLockNotifierId);
    ASSERT_TRUE(notification.has_value());
    notification =
        display_service_->GetNotification(kEcheAppRetryConnectionNotifierId);
    ASSERT_FALSE(notification.has_value());
    notification =
        display_service_->GetNotification(kEcheAppInactivityNotifierId);
    ASSERT_FALSE(notification.has_value());
    notification = display_service_->GetNotification(
        kEcheAppFromWebWithoutButtonNotifierId);
    ASSERT_FALSE(notification.has_value());
  }

  TestingProfile* GetProfile() { return profile_; }
  AppsLaunchInfoProvider* GetAppsLaunchInfoProvider() {
    return apps_launch_info_provider_.get();
  }
  EcheTray* eche_tray() { return eche_tray_; }
  PhoneHubTray* phone_hub_tray() { return phone_hub_tray_; }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
  std::unique_ptr<TestingProfileManager> profile_manager_;
  raw_ptr<TestingProfile> profile_;
  std::unique_ptr<EcheConnectionStatusHandler> connection_handler_;
  std::unique_ptr<AppsLaunchInfoProvider> apps_launch_info_provider_;
  raw_ptr<EcheTray, DanglingUntriaged> eche_tray_ = nullptr;
  raw_ptr<PhoneHubTray, DanglingUntriaged> phone_hub_tray_ = nullptr;
  raw_ptr<EcheAppManagerFactory> eche_app_manager_factory_ = nullptr;
  // Calling the factory constructor is enough to set it up.
  std::unique_ptr<TestAshWebViewFactory> test_web_view_factory_ =
      std::make_unique<TestAshWebViewFactory>();
  std::unique_ptr<NotificationDisplayServiceTester> display_service_;
};

class EcheAppManagerFactoryWithBackgroundTest : public ChromeAshTestBase {
 protected:
  EcheAppManagerFactoryWithBackgroundTest() {
    scoped_feature_list_.InitWithFeatures(
        /*enabled_features=*/{features::kEcheSWA},
        /*disabled_features=*/{});
    profile_manager_ = std::make_unique<TestingProfileManager>(
        TestingBrowserProcess::GetGlobal());
    if (profile_manager_->SetUp()) {
      profile_ = profile_manager_->CreateTestingProfile("testing_profile");
    }
  }
  ~EcheAppManagerFactoryWithBackgroundTest() override = default;
  EcheAppManagerFactoryWithBackgroundTest(
      const EcheAppManagerFactoryWithBackgroundTest&) = delete;
  EcheAppManagerFactoryWithBackgroundTest& operator=(
      const EcheAppManagerFactoryWithBackgroundTest&) = delete;

  // AshTestBase::Test:
  void SetUp() override {
    DCHECK(profile_);
    DCHECK(test_web_view_factory_.get());
    ChromeAshTestBase::SetUp();
    connection_handler_ = std::make_unique<EcheConnectionStatusHandler>();
    apps_launch_info_provider_ =
        std::make_unique<AppsLaunchInfoProvider>(connection_handler_.get());
    apps_launch_info_provider_->SetAppLaunchInfo(
        mojom::AppStreamLaunchEntryPoint::APPS_LIST);
    eche_tray_ = StatusAreaWidgetTestHelper::GetStatusAreaWidget()->eche_tray();
  }

  TestingProfile* GetProfile() { return profile_; }
  AppsLaunchInfoProvider* GetAppsLaunchInfoProvider() {
    return apps_launch_info_provider_.get();
  }

  EcheTray* eche_tray() { return eche_tray_; }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
  std::unique_ptr<TestingProfileManager> profile_manager_;
  raw_ptr<TestingProfile> profile_;
  std::unique_ptr<EcheConnectionStatusHandler> connection_handler_;
  std::unique_ptr<AppsLaunchInfoProvider> apps_launch_info_provider_;
  raw_ptr<EcheTray, DanglingUntriaged> eche_tray_ = nullptr;
  // Calling the factory constructor is enough to set it up.
  std::unique_ptr<TestAshWebViewFactory> test_web_view_factory_ =
      std::make_unique<TestAshWebViewFactory>();
};

TEST_F(EcheAppManagerFactoryTest, LaunchEcheApp) {
  const int64_t user_id = 1;
  const char16_t visible_name_1[] = u"Fake App 1";
  const char package_name_1[] = "com.fakeapp1";
  const char16_t phone_name[] = u"your phone";

  EcheAppManagerFactory::LaunchEcheApp(
      GetProfile(), /*notification_id=*/std::nullopt, package_name_1,
      visible_name_1, user_id, gfx::Image(), phone_name,
      GetAppsLaunchInfoProvider());
  // Wait for Eche Tray to load Eche Web to complete
  base::RunLoop().RunUntilIdle();
  // Eche icon should be visible after launch.
  EXPECT_TRUE(phone_hub_tray()->eche_icon_view()->GetVisible());

  // Launch different application should not recreate widget
  views::Widget* widget = eche_tray()->GetBubbleWidget();
  const char16_t visible_name_2[] = u"Fake App 2";
  const char package_name_2[] = "com.fakeapp2";
  EcheAppManagerFactory::LaunchEcheApp(
      GetProfile(), /*notification_id=*/std::nullopt, package_name_2,
      visible_name_2, user_id, gfx::Image(), phone_name,
      GetAppsLaunchInfoProvider());
  // Wait for Eche Tray to load Eche Web to complete
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(widget, eche_tray()->GetBubbleWidget());
}

TEST_F(EcheAppManagerFactoryTest, LaunchedAppInfo) {
  const int64_t user_id = 1;
  const std::u16string visible_name = u"Fake App";
  const std::string package_name = "com.fakeapp";
  const gfx::Image icon = gfx::test::CreateImage(100, 100);
  const std::u16string phone_name = u"your phone";

  EcheAppManagerFactory::LaunchEcheApp(
      GetProfile(), /*notification_id=*/std::nullopt, package_name,
      visible_name, user_id, icon, phone_name, GetAppsLaunchInfoProvider());

  std::unique_ptr<LaunchedAppInfo> launched_app_info =
      EcheAppManagerFactory::GetInstance()->GetLastLaunchedAppInfo();

  EXPECT_EQ(launched_app_info->user_id(), user_id);
  EXPECT_EQ(launched_app_info->visible_name(), visible_name);
  EXPECT_EQ(launched_app_info->package_name(), package_name);
  EXPECT_EQ(launched_app_info->icon(), icon);
}

TEST_F(EcheAppManagerFactoryTest, CloseConnectionOrLaunchErrorNotifications) {
  ShowAndCloseConnectionOrLaunchErrorNotifications();
}

TEST_F(EcheAppManagerFactoryWithBackgroundTest, LaunchEcheApp) {
  const int64_t user_id = 1;
  const char16_t visible_name[] = u"Fake App";
  const char package_name[] = "com.fakeapp";
  const char16_t phone_name[] = u"your phone";

  EcheAppManagerFactory::LaunchEcheApp(
      GetProfile(), /*notification_id=*/std::nullopt, package_name,
      visible_name, user_id, gfx::Image(), phone_name,
      GetAppsLaunchInfoProvider());
  // Wait for Eche Tray to load Eche Web to complete
  base::RunLoop().RunUntilIdle();
  // Eche tray should be visible when streaming is active, not ative when
  // launch.
  EXPECT_FALSE(eche_tray()->is_active());
}

TEST_F(EcheAppManagerFactoryTest, GetSystemInfo) {
  const char kLsbRelease[] =
      "CHROMEOS_RELEASE_NAME=Non Chrome OS\n"
      "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
  const base::Time lsb_release_time(
      base::Time::FromSecondsSinceUnixEpoch(12345.6));
  base::test::ScopedChromeOSVersionInfo version(kLsbRelease, lsb_release_time);
  std::unique_ptr<SystemInfo> system_info =
      EcheAppManagerFactory::GetInstance()->GetSystemInfo(GetProfile());

  EXPECT_EQ("1.2.3", system_info->GetOsVersion());
  EXPECT_EQ("Chrome device", system_info->GetDeviceType());
}

}  // namespace eche_app
}  // namespace ash