File: office_web_app_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; 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 (146 lines) | stat: -rw-r--r-- 6,093 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
// 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/chromeos/office_web_app/office_web_app.h"

#include "ash/constants/web_app_id_constants.h"
#include "base/test/run_until.h"
#include "base/test/test_future.h"
#include "chrome/browser/web_applications/test/fake_web_contents_manager.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/test/web_app_test.h"
#include "chrome/browser/web_applications/test/web_app_test_observers.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/test/base/testing_profile.h"
#include "components/webapps/browser/install_result_code.h"
#include "components/webapps/browser/installable/installable_metrics.h"

namespace chromeos {
namespace {

// This URL will result in the unexpected app ID.
constexpr char kUnexpectedWebAppUrl[] = "https://www.microsoft365.com/?auth=1";

// Generated as: web_app::GenerateAppId(/*manifest_id=*/std::nullopt, GURL(
//     "https://www.microsoft365.com/?auth=1"))
constexpr char kUnexpectedMicrosoft365AppId[] =
    "ioolobdpeeccbgnljglmgfmbgboajhdf";

using base::test::TestFuture;

// Test class to check that the Office (Microsoft365) web app can be installed
// online and offline.
class OfficeWebAppUnitTest : public WebAppTest {
 protected:
  OfficeWebAppUnitTest() = default;
  ~OfficeWebAppUnitTest() override = default;

  void SetUp() override {
    WebAppTest::SetUp();
    web_app::test::AwaitStartWebAppProviderAndSubsystems(profile());
  }

  // Make `LoadUrl` to return `kUrlLoaded` for the specified URL.
  void SetupUrlLoadSuccess(const GURL& url) {
    auto& web_contents_manager = static_cast<web_app::FakeWebContentsManager&>(
        web_app::WebAppProvider::GetForTest(profile())->web_contents_manager());
    auto& fake_page_state = web_contents_manager.GetOrCreatePageState(url);
    fake_page_state.url_load_result =
        webapps::WebAppUrlLoaderResult::kUrlLoaded;
  }

  webapps::AppId SetupInstalledMicrosoft365(
      const GURL& install_url,
      const GURL& start_url,
      webapps::WebappInstallSource install_source) {
    auto install_info =
        std::make_unique<web_app::WebAppInstallInfo>(start_url, start_url);
    install_info->install_url = install_url;
    install_info->title = u"Microsoft 365";
    return web_app::test::InstallWebApp(
        profile(), std::move(install_info),
        /*overwrite_existing_manifest_fields=*/false, install_source);
  }

  std::unique_ptr<TestingProfile> profile_;
};

TEST_F(OfficeWebAppUnitTest, InstallMicrosoft365WhenOffline) {
  TestFuture<webapps::InstallResultCode> future;
  InstallMicrosoft365(profile(), future.GetCallback());
  EXPECT_EQ(future.Get(),
            webapps::InstallResultCode::kSuccessOfflineOnlyInstall);
}

TEST_F(OfficeWebAppUnitTest, InstallMicrosoft365WhenOnline) {
  // Set the behaviour of `LoadUrl` to return `kUrlLoaded` for the Microsoft365
  // install URL (set the system to be online).
  SetupUrlLoadSuccess(GURL(kMicrosoft365WebAppUrl));

  TestFuture<webapps::InstallResultCode> future;
  InstallMicrosoft365(profile(), future.GetCallback());
  EXPECT_EQ(future.Get(), webapps::InstallResultCode::kSuccessNewInstall);
}

TEST_F(OfficeWebAppUnitTest, ReplaceUnexpectedMicrosoft365App) {
  auto* provider = web_app::WebAppProvider::GetForTest(profile());
  auto& registrar = provider->registrar_unsafe();

  // Install the app with the unexpected ID (via office setup).
  EXPECT_EQ(SetupInstalledMicrosoft365(
                /*install_url=*/GURL(kMicrosoft365WebAppUrl),
                /*start_url=*/GURL(kUnexpectedWebAppUrl),
                webapps::WebappInstallSource::MICROSOFT_365_SETUP),
            kUnexpectedMicrosoft365AppId);
  EXPECT_TRUE(registrar.IsInRegistrar(kUnexpectedMicrosoft365AppId));

  // Set the behaviour of `LoadUrl` to return `kUrlLoaded` for the Microsoft365
  // install URL (set the system to be online).
  SetupUrlLoadSuccess(GURL(kMicrosoft365WebAppUrl));

  // Install the app.
  web_app::WebAppTestUninstallObserver uninstall_observer(profile());
  uninstall_observer.BeginListening({kUnexpectedMicrosoft365AppId});
  TestFuture<webapps::InstallResultCode> future;
  InstallMicrosoft365(profile(), future.GetCallback());

  EXPECT_EQ(future.Get(), webapps::InstallResultCode::kSuccessNewInstall);
  EXPECT_TRUE(registrar.IsInRegistrar(ash::kMicrosoft365AppId));
  uninstall_observer.Wait();
}

TEST_F(OfficeWebAppUnitTest, DoNotReplaceManuallyInstalledMicrosoft365App) {
  auto* provider = web_app::WebAppProvider::GetForTest(profile());
  auto& registrar = provider->registrar_unsafe();

  // Install the app with the unexpected ID (manually).
  EXPECT_EQ(SetupInstalledMicrosoft365(
                /*install_url=*/GURL(kMicrosoft365WebAppUrl),
                /*start_url=*/GURL(kUnexpectedWebAppUrl),
                webapps::WebappInstallSource::MENU_BROWSER_TAB),
            kUnexpectedMicrosoft365AppId);
  EXPECT_TRUE(registrar.IsInRegistrar(kUnexpectedMicrosoft365AppId));

  // Set the behaviour of `LoadUrl` to return `kUrlLoaded` for the Microsoft365
  // install URL (set the system to be online).
  SetupUrlLoadSuccess(GURL(kMicrosoft365WebAppUrl));

  // Install the app.
  TestFuture<webapps::InstallResultCode> future;
  InstallMicrosoft365(profile(), future.GetCallback());
  // Makes sure the uninstall task is NOT run (waiting for the future only waits
  // for install to complete).
  // NOTE: we have to use RunUntilIdle as we are expecting an async uninstall
  // task to NOT happen, so there is no condition to wait on.
  task_environment()->RunUntilIdle();

  EXPECT_EQ(future.Get(), webapps::InstallResultCode::kSuccessNewInstall);
  EXPECT_TRUE(registrar.IsInRegistrar(ash::kMicrosoft365AppId));
  // Manually installed app is not replaced.
  EXPECT_TRUE(registrar.IsInRegistrar(kUnexpectedMicrosoft365AppId));
}

}  // namespace
}  // namespace chromeos