File: crostini_test_helper.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (180 lines) | stat: -rw-r--r-- 6,790 bytes parent folder | download | duplicates (6)
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
// Copyright 2018 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/crostini/crostini_test_helper.h"

#include "base/feature_list.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/ash/crostini/crostini_manager.h"
#include "chrome/browser/ash/crostini/crostini_pref_names.h"
#include "chrome/browser/ash/guest_os/guest_id.h"
#include "chrome/browser/ash/guest_os/guest_os_registry_service.h"
#include "chrome/browser/ash/guest_os/guest_os_registry_service_factory.h"
#include "chrome/browser/ash/guest_os/public/guest_os_service.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_features.h"
#include "chromeos/ash/components/dbus/cicerone/cicerone_client.h"
#include "components/prefs/pref_service.h"
#include "components/user_manager/scoped_user_manager.h"
#include "google_apis/gaia/gaia_id.h"
#include "third_party/skia/include/core/SkColor.h"

using vm_tools::apps::App;
using vm_tools::apps::ApplicationList;

namespace crostini {

constexpr SkColor kTestContainerBadgeColor = SK_ColorBLUE;

CrostiniTestHelper::CrostiniTestHelper(Profile* profile, bool enable_crostini)
    : fake_user_manager_(std::make_unique<ash::FakeChromeUserManager>()),
      profile_(profile) {
  scoped_feature_list_.InitAndEnableFeature(features::kCrostini);

  ash::ProfileHelper::SetAlwaysReturnPrimaryUserForTesting(true);
  auto account =
      AccountId::FromUserEmailGaiaId("test@example.com", GaiaId("12345"));
  fake_user_manager_->AddUserWithAffiliationAndTypeAndProfile(
      account, false, user_manager::UserType::kRegular, profile);
  fake_user_manager_->LoginUser(account);

  if (enable_crostini) {
    EnableCrostini(profile);
  }

  current_apps_.set_vm_name(kCrostiniDefaultVmName);
  current_apps_.set_container_name(kCrostiniDefaultContainerName);

  guest_os::AddContainerToPrefs(profile_, DefaultContainerId(), {});
  SetContainerBadgeColor(profile_, DefaultContainerId(),
                         kTestContainerBadgeColor);
}

CrostiniTestHelper::~CrostiniTestHelper() {
  ash::ProfileHelper::SetAlwaysReturnPrimaryUserForTesting(false);
  DisableCrostini(profile_);
}

void CrostiniTestHelper::SetupDummyApps() {
  // This updates the registry for us.
  AddApp(BasicApp("dummy1"));
  AddApp(BasicApp("dummy2"));
}

App CrostiniTestHelper::GetApp(int i) {
  return current_apps_.apps(i);
}

void CrostiniTestHelper::AddApp(const vm_tools::apps::App& app) {
  for (int i = 0; i < current_apps_.apps_size(); ++i) {
    if (current_apps_.apps(i).desktop_file_id() == app.desktop_file_id()) {
      *current_apps_.mutable_apps(i) = app;
      UpdateRegistry();
      return;
    }
  }
  *current_apps_.add_apps() = app;
  UpdateRegistry();
}

void CrostiniTestHelper::RemoveApp(int i) {
  auto* apps = current_apps_.mutable_apps();
  apps->erase(apps->begin() + i);
  UpdateRegistry();
}

void CrostiniTestHelper::ReInitializeAppServiceIntegration() {
  // Some Crostini-related tests add apps to the registry, which queues
  // (asynchronous) icon loading requests, which depends on the Cicerone D-Bus
  // client. These requests are merely queued, not executed, so without further
  // action, D-Bus can be ignored.
  //
  // It is simpler if those RunUntilIdle calls are unconditional, so we require
  // Cicerone to be initialized by this point, whether or not the App Service
  // is enabled. Note that we can't initialize it ourselves here because once it
  // has been initialized it must be shutdown, but it can't be shutdown until
  // after the profile (and all keyed services) have been destroyed, which we
  // can't manage because we don't own the profile.
  CHECK(ash::CiceroneClient::Get())
      << "CiceroneClient must be initialized before calling "
         "ReInitializeAppServiceIntegration";

  // The App Service is originally initialized when the Profile is created,
  // but this class' constructor takes the Profile* as an argument, which
  // means that the fake user (created during that constructor) is
  // necessarily configured after the App Service's initialization.
  //
  // Without further action, in tests (but not in production which looks at
  // real users, not fakes), the App Service serves no Crostini apps, as at
  // the time it looked, the profile/user doesn't have Crostini enabled.
  //
  // We therefore manually have the App Service re-examine whether Crostini
  // is enabled for this profile.
  auto* proxy = apps::AppServiceProxyFactory::GetForProfile(profile_);
  proxy->ReInitializeCrostiniForTesting();
}

void CrostiniTestHelper::UpdateAppKeywords(
    vm_tools::apps::App& app,
    const std::map<std::string, std::set<std::string>>& keywords) {
  for (const auto& keywords_entry : keywords) {
    auto* strings_with_locale = app.mutable_keywords()->add_values();
    strings_with_locale->set_locale(keywords_entry.first);
    for (const auto& curr_keyword : keywords_entry.second) {
      strings_with_locale->add_value(curr_keyword);
    }
  }
}

// static
void CrostiniTestHelper::EnableCrostini(Profile* profile) {
  profile->GetPrefs()->SetBoolean(crostini::prefs::kCrostiniEnabled, true);
}
// static
void CrostiniTestHelper::DisableCrostini(Profile* profile) {
  profile->GetPrefs()->SetBoolean(crostini::prefs::kCrostiniEnabled, false);
}

// static
std::string CrostiniTestHelper::GenerateAppId(
    const std::string& desktop_file_id,
    const std::string& vm_name,
    const std::string& container_name) {
  return guest_os::GuestOsRegistryService::GenerateAppId(
      desktop_file_id, vm_name, container_name);
}

// static
App CrostiniTestHelper::BasicApp(const std::string& desktop_file_id,
                                 const std::string& name,
                                 bool no_display) {
  App app;
  app.set_desktop_file_id(desktop_file_id);
  App::LocaleString::Entry* entry = app.mutable_name()->add_values();
  entry->set_locale(std::string());
  entry->set_value(name.empty() ? desktop_file_id : name);
  app.set_no_display(no_display);
  return app;
}

// static
ApplicationList CrostiniTestHelper::BasicAppList(
    const std::string& desktop_file_id,
    const std::string& vm_name,
    const std::string& container_name) {
  ApplicationList app_list;
  app_list.set_vm_name(vm_name);
  app_list.set_container_name(container_name);
  *app_list.add_apps() = BasicApp(desktop_file_id);
  return app_list;
}

void CrostiniTestHelper::UpdateRegistry() {
  guest_os::GuestOsRegistryServiceFactory::GetForProfile(profile_)
      ->UpdateApplicationList(current_apps_);
}

}  // namespace crostini