Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(568)

Side by Side Diff: chrome/browser/chromeos/lock_screen_apps/app_manager_impl_unittest.cc

Issue 2902293002: Introduce lock screen app manager (Closed)
Patch Set: rebase Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/chromeos/lock_screen_apps/app_manager_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/lock_screen_apps/app_manager_impl.h"
6
7 #include <memory>
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/json/json_file_value_serializer.h"
15 #include "base/memory/ptr_util.h"
16 #include "base/test/scoped_command_line.h"
17 #include "base/values.h"
18 #include "chrome/browser/chromeos/arc/arc_session_manager.h"
19 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
20 #include "chrome/browser/chromeos/note_taking_helper.h"
21 #include "chrome/browser/chromeos/settings/cros_settings.h"
22 #include "chrome/browser/chromeos/settings/device_settings_service.h"
23 #include "chrome/browser/extensions/extension_service.h"
24 #include "chrome/browser/extensions/test_extension_system.h"
25 #include "chrome/common/chrome_constants.h"
26 #include "chrome/common/pref_names.h"
27 #include "chrome/test/base/testing_browser_process.h"
28 #include "chrome/test/base/testing_profile.h"
29 #include "chrome/test/base/testing_profile_manager.h"
30 #include "components/arc/arc_service_manager.h"
31 #include "components/arc/arc_session.h"
32 #include "content/public/test/test_browser_thread_bundle.h"
33 #include "extensions/browser/event_router.h"
34 #include "extensions/browser/event_router_factory.h"
35 #include "extensions/browser/extension_prefs.h"
36 #include "extensions/browser/extension_registry.h"
37 #include "extensions/common/api/app_runtime.h"
38 #include "extensions/common/extension.h"
39 #include "extensions/common/extension_builder.h"
40 #include "extensions/common/value_builder.h"
41 #include "testing/gtest/include/gtest/gtest.h"
42
43 using extensions::DictionaryBuilder;
44 using extensions::ListBuilder;
45
46 namespace lock_screen_apps {
47
48 namespace {
49
50 std::unique_ptr<arc::ArcSession> ArcSessionFactory() {
51 ADD_FAILURE() << "Attempt to create arc session.";
52 return nullptr;
53 }
54
55 class TestEventRouter : public extensions::EventRouter {
56 public:
57 explicit TestEventRouter(content::BrowserContext* context)
58 : extensions::EventRouter(context,
59 extensions::ExtensionPrefs::Get(context)),
60 context_(context) {}
61 ~TestEventRouter() override = default;
62
63 bool ExtensionHasEventListener(const std::string& extension_id,
64 const std::string& event_name) const override {
65 return event_name == extensions::api::app_runtime::OnLaunched::kEventName;
66 }
67
68 void BroadcastEvent(std::unique_ptr<extensions::Event> event) override {}
69
70 void DispatchEventToExtension(
71 const std::string& extension_id,
72 std::unique_ptr<extensions::Event> event) override {
73 if (event->event_name !=
74 extensions::api::app_runtime::OnLaunched::kEventName) {
75 return;
76 }
77 ASSERT_TRUE(event->event_args);
78 const base::Value* arg_value = nullptr;
79 ASSERT_TRUE(event->event_args->Get(0, &arg_value));
80 ASSERT_TRUE(arg_value);
81 if (event->restrict_to_browser_context)
82 EXPECT_EQ(context_, event->restrict_to_browser_context);
83
84 std::unique_ptr<extensions::api::app_runtime::LaunchData> launch_data =
85 extensions::api::app_runtime::LaunchData::FromValue(*arg_value);
86 ASSERT_TRUE(launch_data);
87 ASSERT_TRUE(launch_data->action_data);
88 EXPECT_EQ(extensions::api::app_runtime::ACTION_TYPE_NEW_NOTE,
89 launch_data->action_data->action_type);
90 ASSERT_TRUE(launch_data->action_data->is_lock_screen_action);
91 EXPECT_TRUE(*launch_data->action_data->is_lock_screen_action);
92
93 launched_apps_.push_back(extension_id);
94 }
95
96 const std::vector<std::string>& launched_apps() const {
97 return launched_apps_;
98 }
99
100 void ClearLaunchedApps() { launched_apps_.clear(); }
101
102 private:
103 std::vector<std::string> launched_apps_;
104 content::BrowserContext* context_;
105
106 DISALLOW_COPY_AND_ASSIGN(TestEventRouter);
107 };
108
109 std::unique_ptr<KeyedService> TestEventRouterFactoryFunction(
110 content::BrowserContext* profile) {
111 return base::MakeUnique<TestEventRouter>(profile);
112 }
113
114 enum class TestAppLocation { kUnpacked, kInternal };
115
116 class LockScreenAppManagerImplTest
117 : public testing::TestWithParam<TestAppLocation> {
118 public:
119 LockScreenAppManagerImplTest()
120 : profile_manager_(TestingBrowserProcess::GetGlobal()) {}
121
122 ~LockScreenAppManagerImplTest() override = default;
123
124 void SetUp() override {
125 // Initialize command line so chromeos::NoteTakingHelper thinks note taking
126 // on lock screen is enabled.
127 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>();
128 command_line_->GetProcessCommandLine()->InitFromArgv(
129 {"", "--enable-lock-screen-apps", "--force-enable-stylus-tools"});
130
131 ASSERT_TRUE(profile_manager_.SetUp());
132
133 profile_ = profile_manager_.CreateTestingProfile("primary_profile");
134
135 lock_screen_profile_ =
136 profile_manager_.CreateTestingProfile(chrome::kInitialProfile);
137
138 InitExtensionSystem(profile());
139 InitExtensionSystem(lock_screen_profile()->GetOriginalProfile());
140
141 // Initialize arc session manager - NoteTakingHelper expects it to be set.
142 arc_session_manager_ = base::MakeUnique<arc::ArcSessionManager>(
143 base::MakeUnique<arc::ArcSessionRunner>(
144 base::Bind(&ArcSessionFactory)));
145
146 chromeos::NoteTakingHelper::Initialize();
147
148 ResetAppManager();
149 }
150
151 void TearDown() override {
152 extensions::ExtensionSystem::Get(profile())->Shutdown();
153 extensions::ExtensionSystem::Get(lock_screen_profile())->Shutdown();
154 chromeos::NoteTakingHelper::Shutdown();
155 }
156
157 void InitExtensionSystem(Profile* profile) {
158 extensions::TestExtensionSystem* extension_system =
159 static_cast<extensions::TestExtensionSystem*>(
160 extensions::ExtensionSystem::Get(profile));
161 extension_system->CreateExtensionService(
162 base::CommandLine::ForCurrentProcess(),
163 profile->GetPath().Append("Extensions") /* install_directory */,
164 false /* autoupdate_enabled */);
165 }
166
167 base::FilePath GetTestAppSourcePath(TestAppLocation location,
168 Profile* profile,
169 const std::string& id,
170 const std::string& version) {
171 switch (location) {
172 case TestAppLocation::kUnpacked:
173 return profile->GetPath().Append("Downloads").Append("app");
174 case TestAppLocation::kInternal:
175 return extensions::ExtensionSystem::Get(profile)
176 ->extension_service()
177 ->install_directory()
178 .Append(id)
179 .Append(version);
180 }
181 return base::FilePath();
182 }
183
184 base::FilePath GetLockScreenAppPath(const std::string& id,
185 const std::string& version) {
186 return GetLockScreenAppPathWithOriginalProfile(profile(), id, version);
187 }
188
189 base::FilePath GetLockScreenAppPathWithOriginalProfile(
190 Profile* original_profile,
191 const std::string& id,
192 const std::string& version) {
193 return GetLockScreenAppPathWithOriginalLocation(
194 GetParam(), original_profile, id, version);
195 }
196
197 base::FilePath GetLockScreenAppPathWithOriginalLocation(
198 TestAppLocation location,
199 Profile* original_profile,
200 const std::string& id,
201 const std::string& version) {
202 switch (location) {
203 case TestAppLocation::kUnpacked:
204 return original_profile->GetPath().Append("Downloads").Append("app");
205 case TestAppLocation::kInternal:
206 return extensions::ExtensionSystem::Get(lock_screen_profile())
207 ->extension_service()
208 ->install_directory()
209 .Append(id)
210 .Append(version + "_0");
211 }
212 return base::FilePath();
213 }
214
215 extensions::Manifest::Location GetAppLocation(TestAppLocation location) {
216 switch (location) {
217 case TestAppLocation::kUnpacked:
218 return extensions::Manifest::UNPACKED;
219 case TestAppLocation::kInternal:
220 return extensions::Manifest::INTERNAL;
221 }
222
223 return extensions::Manifest::UNPACKED;
224 }
225
226 scoped_refptr<const extensions::Extension> CreateTestApp(
227 const std::string& id,
228 const std::string& version,
229 bool supports_lock_screen) {
230 return CreateTestAppInProfile(profile(), id, version, supports_lock_screen);
231 }
232
233 scoped_refptr<const extensions::Extension> CreateTestAppInProfile(
234 Profile* profile,
235 const std::string& id,
236 const std::string& version,
237 bool supports_lock_screen) {
238 return CreateTestAppWithLocation(GetParam(), profile, id, version,
239 supports_lock_screen);
240 }
241
242 scoped_refptr<const extensions::Extension> CreateTestAppWithLocation(
243 TestAppLocation location,
244 Profile* profile,
245 const std::string& id,
246 const std::string& version,
247 bool supports_lock_screen) {
248 std::unique_ptr<base::DictionaryValue> background =
249 DictionaryBuilder()
250 .Set("scripts", ListBuilder().Append("background.js").Build())
251 .Build();
252 std::unique_ptr<base::ListValue> action_handlers =
253 ListBuilder()
254 .Append(
255 DictionaryBuilder()
256 .Set("action", "new_note")
257 .SetBoolean("enabled_on_lock_screen", supports_lock_screen)
258 .Build())
259 .Build();
260
261 DictionaryBuilder manifest_builder;
262 manifest_builder.Set("name", "Note taking app")
263 .Set("version", version)
264 .Set("manifest_version", 2)
265 .Set("app", DictionaryBuilder()
266 .Set("background", std::move(background))
267 .Build())
268 .Set("permissions", ListBuilder().Append("lockScreen").Build())
269 .Set("action_handlers", std::move(action_handlers));
270
271 base::FilePath extension_path =
272 GetTestAppSourcePath(location, profile, id, version);
273
274 scoped_refptr<const extensions::Extension> extension =
275 extensions::ExtensionBuilder()
276 .SetManifest(manifest_builder.Build())
277 .SetID(id)
278 .SetPath(extension_path)
279 .SetLocation(GetAppLocation(location))
280 .Build();
281
282 // Create the app path with required files - app manager *will* attempt to
283 // load the app from the disk, so extension directory has to be present for
284 // the load to succeed.
285 base::File::Error error;
286 if (!base::CreateDirectoryAndGetError(extension_path, &error)) {
287 ADD_FAILURE() << "Failed to create path " << extension_path.value() << " "
288 << error;
289 return nullptr;
290 }
291
292 JSONFileValueSerializer manifest_writer(
293 extension_path.Append("manifest.json"));
294 if (!manifest_writer.Serialize(*extension->manifest()->value())) {
295 ADD_FAILURE() << "Failed to create manifest file";
296 return nullptr;
297 }
298
299 if (base::WriteFile(extension_path.Append("background.js"), "{}", 2) != 2) {
300 ADD_FAILURE() << "Failed to write background script file";
301 return nullptr;
302 }
303
304 return extension;
305 }
306
307 TestingProfile* CreateSecondaryProfile() {
308 TestingProfile* profile =
309 profile_manager_.CreateTestingProfile("secondary_profile");
310 InitExtensionSystem(profile);
311 return profile;
312 }
313
314 scoped_refptr<const extensions::Extension> AddTestAppWithLockScreenSupport(
315 Profile* profile,
316 const std::string& app_id,
317 const std::string& version,
318 bool enable_on_lock_screen) {
319 scoped_refptr<const extensions::Extension> app = CreateTestAppInProfile(
320 profile, app_id, version, true /* supports_lock_screen*/);
321 extensions::ExtensionSystem::Get(profile)
322 ->extension_service()
323 ->AddExtension(app.get());
324
325 chromeos::NoteTakingHelper::Get()->SetPreferredApp(profile, app_id);
326 profile->GetPrefs()->SetBoolean(prefs::kNoteTakingAppEnabledOnLockScreen,
327 enable_on_lock_screen);
328 return app;
329 }
330
331 void InitializeAndStartAppManager(Profile* profile) {
332 app_manager()->Initialize(profile, lock_screen_profile());
333 app_manager()->Start(
334 base::Bind(&LockScreenAppManagerImplTest::OnNoteTakingChanged,
335 base::Unretained(this)));
336 }
337
338 TestingProfile* profile() { return profile_; }
339 TestingProfile* lock_screen_profile() { return lock_screen_profile_; }
340
341 AppManager* app_manager() { return app_manager_.get(); }
342
343 void ResetAppManager() { app_manager_ = base::MakeUnique<AppManagerImpl>(); }
344
345 int note_taking_changed_count() const { return note_taking_changed_count_; }
346
347 void ResetNoteTakingChangedCount() { note_taking_changed_count_ = 0; }
348
349 // Waits for a round trip between file task runner used by the profile's
350 // extension service and the main thread - used to ensure that all pending
351 // file runner task finish,
352 void RunExtensionServiceTaskRunner(Profile* profile) {
353 base::RunLoop run_loop;
354 extensions::ExtensionSystem::Get(profile)
355 ->extension_service()
356 ->GetFileTaskRunner()
357 ->PostTaskAndReply(FROM_HERE, base::Bind(&base::DoNothing),
358 run_loop.QuitClosure());
359 run_loop.Run();
360 }
361
362 bool IsInstallAsync() { return GetParam() != TestAppLocation::kUnpacked; }
363
364 int NoteTakingChangedCountOnStart() { return IsInstallAsync() ? 1 : 0; }
365
366 private:
367 void OnNoteTakingChanged() { ++note_taking_changed_count_; }
368
369 std::unique_ptr<base::test::ScopedCommandLine> command_line_;
370 content::TestBrowserThreadBundle threads_;
371
372 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
373 chromeos::ScopedTestCrosSettings test_cros_settings_;
374 chromeos::ScopedTestUserManager user_manager_;
375
376 TestingProfileManager profile_manager_;
377 TestingProfile* profile_ = nullptr;
378 TestingProfile* lock_screen_profile_ = nullptr;
379
380 std::unique_ptr<arc::ArcServiceManager> arc_service_manager_;
381 std::unique_ptr<arc::ArcSessionManager> arc_session_manager_;
382
383 std::unique_ptr<AppManager> app_manager_;
384
385 int note_taking_changed_count_ = 0;
386
387 DISALLOW_COPY_AND_ASSIGN(LockScreenAppManagerImplTest);
388 };
389
390 } // namespace
391
392 INSTANTIATE_TEST_CASE_P(Unpacked,
393 LockScreenAppManagerImplTest,
394 ::testing::Values(TestAppLocation::kUnpacked));
395 INSTANTIATE_TEST_CASE_P(Internal,
396 LockScreenAppManagerImplTest,
397 ::testing::Values(TestAppLocation::kInternal));
398
399 TEST_P(LockScreenAppManagerImplTest, StartAddsAppToTarget) {
400 scoped_refptr<const extensions::Extension> note_taking_app =
401 AddTestAppWithLockScreenSupport(
402 profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
403 true /* enable_on_lock_screen */);
404
405 InitializeAndStartAppManager(profile());
406
407 EXPECT_EQ(0, note_taking_changed_count());
408 EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
409
410 RunExtensionServiceTaskRunner(lock_screen_profile());
411
412 EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
413 ResetNoteTakingChangedCount();
414
415 EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
416 EXPECT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
417 app_manager()->GetNoteTakingAppId());
418
419 EXPECT_TRUE(base::PathExists(note_taking_app->path()));
420
421 const extensions::Extension* lock_app =
422 extensions::ExtensionRegistry::Get(lock_screen_profile())
423 ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
424 extensions::ExtensionRegistry::ENABLED);
425 ASSERT_TRUE(lock_app);
426
427 EXPECT_TRUE(base::PathExists(lock_app->path()));
428 EXPECT_EQ(GetLockScreenAppPath(note_taking_app->id(),
429 note_taking_app->VersionString()),
430 lock_app->path());
431
432 app_manager()->Stop();
433
434 EXPECT_EQ(0, note_taking_changed_count());
435 EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
436 EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
437
438 lock_app =
439 extensions::ExtensionRegistry::Get(lock_screen_profile())
440 ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
441 extensions::ExtensionRegistry::EVERYTHING);
442 EXPECT_FALSE(lock_app);
443
444 RunExtensionServiceTaskRunner(lock_screen_profile());
445 RunExtensionServiceTaskRunner(profile());
446
447 EXPECT_TRUE(base::PathExists(note_taking_app->path()));
448 }
449
450 TEST_P(LockScreenAppManagerImplTest, StartWhenLockScreenNotesNotEnabled) {
451 scoped_refptr<const extensions::Extension> note_taking_app =
452 AddTestAppWithLockScreenSupport(
453 profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
454 false /* enable_on_lock_screen */);
455
456 InitializeAndStartAppManager(profile());
457 RunExtensionServiceTaskRunner(lock_screen_profile());
458
459 EXPECT_EQ(0, note_taking_changed_count());
460 EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
461 EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
462
463 const extensions::Extension* lock_app =
464 extensions::ExtensionRegistry::Get(lock_screen_profile())
465 ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
466 extensions::ExtensionRegistry::ENABLED);
467 EXPECT_FALSE(lock_app);
468
469 app_manager()->Stop();
470 EXPECT_EQ(0, note_taking_changed_count());
471 EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
472 EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
473
474 lock_app =
475 extensions::ExtensionRegistry::Get(lock_screen_profile())
476 ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
477 extensions::ExtensionRegistry::EVERYTHING);
478 EXPECT_FALSE(lock_app);
479
480 RunExtensionServiceTaskRunner(lock_screen_profile());
481 RunExtensionServiceTaskRunner(profile());
482
483 EXPECT_TRUE(base::PathExists(note_taking_app->path()));
484 }
485
486 TEST_P(LockScreenAppManagerImplTest, LockScreenNoteTakingDisabledWhileStarted) {
487 scoped_refptr<const extensions::Extension> note_taking_app =
488 AddTestAppWithLockScreenSupport(
489 profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
490 true /* enable_on_lock_screen */);
491
492 InitializeAndStartAppManager(profile());
493
494 EXPECT_EQ(0, note_taking_changed_count());
495 EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
496
497 RunExtensionServiceTaskRunner(lock_screen_profile());
498
499 EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
500 ResetNoteTakingChangedCount();
501
502 EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
503 EXPECT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
504 app_manager()->GetNoteTakingAppId());
505
506 const extensions::Extension* lock_app =
507 extensions::ExtensionRegistry::Get(lock_screen_profile())
508 ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
509 extensions::ExtensionRegistry::ENABLED);
510 ASSERT_TRUE(lock_app);
511
512 EXPECT_TRUE(base::PathExists(lock_app->path()));
513 EXPECT_EQ(GetLockScreenAppPath(note_taking_app->id(),
514 note_taking_app->VersionString()),
515 lock_app->path());
516 EXPECT_TRUE(base::PathExists(note_taking_app->path()));
517
518 profile()->GetPrefs()->SetBoolean(prefs::kNoteTakingAppEnabledOnLockScreen,
519 false);
520
521 EXPECT_EQ(1, note_taking_changed_count());
522 ResetNoteTakingChangedCount();
523
524 EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
525 EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
526 lock_app =
527 extensions::ExtensionRegistry::Get(lock_screen_profile())
528 ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
529 extensions::ExtensionRegistry::EVERYTHING);
530 EXPECT_FALSE(lock_app);
531
532 app_manager()->Stop();
533
534 EXPECT_EQ(0, note_taking_changed_count());
535 EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
536 EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
537
538 RunExtensionServiceTaskRunner(lock_screen_profile());
539 RunExtensionServiceTaskRunner(profile());
540
541 EXPECT_TRUE(base::PathExists(note_taking_app->path()));
542 }
543
544 TEST_P(LockScreenAppManagerImplTest, LockScreenNoteTakingEnabledWhileStarted) {
545 scoped_refptr<const extensions::Extension> note_taking_app =
546 AddTestAppWithLockScreenSupport(
547 profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
548 false /* enable_on_lock_screen */);
549
550 InitializeAndStartAppManager(profile());
551 RunExtensionServiceTaskRunner(lock_screen_profile());
552
553 EXPECT_EQ(0, note_taking_changed_count());
554 EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
555 EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
556
557 const extensions::Extension* lock_app =
558 extensions::ExtensionRegistry::Get(lock_screen_profile())
559 ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
560 extensions::ExtensionRegistry::EVERYTHING);
561 EXPECT_FALSE(lock_app);
562
563 profile()->GetPrefs()->SetBoolean(prefs::kNoteTakingAppEnabledOnLockScreen,
564 true);
565
566 EXPECT_EQ(1, note_taking_changed_count());
567 ResetNoteTakingChangedCount();
568 EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
569
570 RunExtensionServiceTaskRunner(lock_screen_profile());
571
572 EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
573 ResetNoteTakingChangedCount();
574
575 EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
576 EXPECT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
577 app_manager()->GetNoteTakingAppId());
578
579 lock_app =
580 extensions::ExtensionRegistry::Get(lock_screen_profile())
581 ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
582 extensions::ExtensionRegistry::ENABLED);
583 ASSERT_TRUE(lock_app);
584
585 EXPECT_TRUE(base::PathExists(lock_app->path()));
586 EXPECT_EQ(GetLockScreenAppPath(note_taking_app->id(),
587 note_taking_app->VersionString()),
588 lock_app->path());
589 EXPECT_TRUE(base::PathExists(note_taking_app->path()));
590
591 app_manager()->Stop();
592
593 EXPECT_EQ(0, note_taking_changed_count());
594 EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
595 EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
596
597 RunExtensionServiceTaskRunner(lock_screen_profile());
598 RunExtensionServiceTaskRunner(profile());
599
600 EXPECT_TRUE(base::PathExists(note_taking_app->path()));
601 }
602
603 TEST_P(LockScreenAppManagerImplTest, LockScreenNoteTakingChangedWhileStarted) {
604 scoped_refptr<const extensions::Extension> dev_note_taking_app =
605 AddTestAppWithLockScreenSupport(
606 profile(), chromeos::NoteTakingHelper::kDevKeepExtensionId, "1.0",
607 false /* enable_on_lock_screen */);
608
609 scoped_refptr<const extensions::Extension> prod_note_taking_app =
610 AddTestAppWithLockScreenSupport(
611 profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
612 true /* enable_on_lock_screen */);
613
614 InitializeAndStartAppManager(profile());
615
616 EXPECT_EQ(0, note_taking_changed_count());
617 EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
618
619 RunExtensionServiceTaskRunner(lock_screen_profile());
620
621 EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
622 ResetNoteTakingChangedCount();
623
624 EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
625 EXPECT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
626 app_manager()->GetNoteTakingAppId());
627
628 const extensions::Extension* lock_app =
629 extensions::ExtensionRegistry::Get(lock_screen_profile())
630 ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
631 extensions::ExtensionRegistry::ENABLED);
632 ASSERT_TRUE(lock_app);
633
634 EXPECT_TRUE(base::PathExists(lock_app->path()));
635 EXPECT_EQ(GetLockScreenAppPath(prod_note_taking_app->id(),
636 prod_note_taking_app->VersionString()),
637 lock_app->path());
638 EXPECT_TRUE(base::PathExists(prod_note_taking_app->path()));
639
640 chromeos::NoteTakingHelper::Get()->SetPreferredApp(
641 profile(), chromeos::NoteTakingHelper::kDevKeepExtensionId);
642
643 EXPECT_EQ(1, note_taking_changed_count());
644 ResetNoteTakingChangedCount();
645 EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
646
647 RunExtensionServiceTaskRunner(lock_screen_profile());
648
649 EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
650 ResetNoteTakingChangedCount();
651
652 EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
653 EXPECT_EQ(chromeos::NoteTakingHelper::kDevKeepExtensionId,
654 app_manager()->GetNoteTakingAppId());
655
656 // Verify prod app was unloaded from signin profile.
657 lock_app =
658 extensions::ExtensionRegistry::Get(lock_screen_profile())
659 ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
660 extensions::ExtensionRegistry::EVERYTHING);
661 EXPECT_FALSE(lock_app);
662
663 lock_app =
664 extensions::ExtensionRegistry::Get(lock_screen_profile())
665 ->GetExtensionById(chromeos::NoteTakingHelper::kDevKeepExtensionId,
666 extensions::ExtensionRegistry::ENABLED);
667
668 ASSERT_TRUE(lock_app);
669
670 EXPECT_TRUE(base::PathExists(lock_app->path()));
671 EXPECT_EQ(GetLockScreenAppPath(dev_note_taking_app->id(),
672 dev_note_taking_app->VersionString()),
673 lock_app->path());
674
675 app_manager()->Stop();
676 EXPECT_EQ(0, note_taking_changed_count());
677 EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
678 EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
679
680 RunExtensionServiceTaskRunner(lock_screen_profile());
681 RunExtensionServiceTaskRunner(profile());
682
683 EXPECT_TRUE(base::PathExists(dev_note_taking_app->path()));
684 EXPECT_TRUE(base::PathExists(prod_note_taking_app->path()));
685 }
686
687 TEST_P(LockScreenAppManagerImplTest, LockScreenNoteTakingReloadedWhileStarted) {
688 scoped_refptr<const extensions::Extension> note_taking_app =
689 AddTestAppWithLockScreenSupport(
690 profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
691 true /* enable_on_lock_screen */);
692
693 InitializeAndStartAppManager(profile());
694 RunExtensionServiceTaskRunner(lock_screen_profile());
695
696 EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
697 ResetNoteTakingChangedCount();
698
699 EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
700 EXPECT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
701 app_manager()->GetNoteTakingAppId());
702
703 const extensions::Extension* lock_app =
704 extensions::ExtensionRegistry::Get(lock_screen_profile())
705 ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
706 extensions::ExtensionRegistry::ENABLED);
707 ASSERT_TRUE(lock_app);
708 EXPECT_EQ("1.0", lock_app->VersionString());
709
710 EXPECT_TRUE(base::PathExists(lock_app->path()));
711 EXPECT_EQ(GetLockScreenAppPath(note_taking_app->id(),
712 note_taking_app->VersionString()),
713 lock_app->path());
714 EXPECT_TRUE(base::PathExists(note_taking_app->path()));
715
716 extensions::ExtensionSystem::Get(profile())
717 ->extension_service()
718 ->UnloadExtension(chromeos::NoteTakingHelper::kProdKeepExtensionId,
719 extensions::UnloadedExtensionReason::UPDATE);
720
721 EXPECT_EQ(1, note_taking_changed_count());
722 ResetNoteTakingChangedCount();
723
724 EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
725 EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
726
727 // Verify prod app was unloaded from signin profile.
728 lock_app =
729 extensions::ExtensionRegistry::Get(lock_screen_profile())
730 ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
731 extensions::ExtensionRegistry::EVERYTHING);
732 EXPECT_FALSE(lock_app);
733
734 // Add the app again.
735 note_taking_app = CreateTestApp(
736 chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.1", true);
737 extensions::ExtensionSystem::Get(profile())
738 ->extension_service()
739 ->AddExtension(note_taking_app.get());
740
741 EXPECT_EQ(1, note_taking_changed_count());
742 ResetNoteTakingChangedCount();
743 EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
744
745 RunExtensionServiceTaskRunner(lock_screen_profile());
746
747 EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
748 ResetNoteTakingChangedCount();
749 EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
750 EXPECT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
751 app_manager()->GetNoteTakingAppId());
752
753 lock_app =
754 extensions::ExtensionRegistry::Get(lock_screen_profile())
755 ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
756 extensions::ExtensionRegistry::ENABLED);
757
758 ASSERT_TRUE(lock_app);
759 EXPECT_EQ("1.1", lock_app->VersionString());
760
761 EXPECT_TRUE(base::PathExists(lock_app->path()));
762 EXPECT_EQ(GetLockScreenAppPath(note_taking_app->id(),
763 note_taking_app->VersionString()),
764 lock_app->path());
765
766 app_manager()->Stop();
767 EXPECT_EQ(0, note_taking_changed_count());
768 EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
769 EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
770
771 RunExtensionServiceTaskRunner(lock_screen_profile());
772 RunExtensionServiceTaskRunner(profile());
773
774 EXPECT_TRUE(base::PathExists(note_taking_app->path()));
775 }
776
777 TEST_P(LockScreenAppManagerImplTest,
778 NoteTakingAppChangeToUnpackedWhileActivating) {
779 scoped_refptr<const extensions::Extension> initial_note_taking_app =
780 AddTestAppWithLockScreenSupport(
781 profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.1",
782 true /* enable_on_lock_screen */);
783
784 scoped_refptr<const extensions::Extension> final_note_taking_app =
785 CreateTestAppWithLocation(TestAppLocation::kUnpacked, profile(),
786 chromeos::NoteTakingHelper::kDevKeepExtensionId,
787 "1.1", true /* enable_on_lock_screen */);
788 extensions::ExtensionSystem::Get(profile())
789 ->extension_service()
790 ->AddExtension(final_note_taking_app.get());
791
792 InitializeAndStartAppManager(profile());
793
794 EXPECT_EQ(0, note_taking_changed_count());
795 EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
796
797 chromeos::NoteTakingHelper::Get()->SetPreferredApp(
798 profile(), chromeos::NoteTakingHelper::kDevKeepExtensionId);
799
800 EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
801 EXPECT_EQ(chromeos::NoteTakingHelper::kDevKeepExtensionId,
802 app_manager()->GetNoteTakingAppId());
803 EXPECT_EQ(1, note_taking_changed_count());
804 ResetNoteTakingChangedCount();
805
806 RunExtensionServiceTaskRunner(lock_screen_profile());
807
808 EXPECT_EQ(0, note_taking_changed_count());
809
810 EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
811 EXPECT_EQ(chromeos::NoteTakingHelper::kDevKeepExtensionId,
812 app_manager()->GetNoteTakingAppId());
813
814 const extensions::Extension* lock_app =
815 extensions::ExtensionRegistry::Get(lock_screen_profile())
816 ->GetExtensionById(chromeos::NoteTakingHelper::kDevKeepExtensionId,
817 extensions::ExtensionRegistry::ENABLED);
818 ASSERT_TRUE(lock_app);
819 EXPECT_EQ("1.1", lock_app->VersionString());
820
821 EXPECT_TRUE(base::PathExists(lock_app->path()));
822 EXPECT_EQ(
823 GetLockScreenAppPathWithOriginalLocation(
824 TestAppLocation::kUnpacked, profile(), final_note_taking_app->id(),
825 final_note_taking_app->VersionString()),
826 lock_app->path());
827
828 app_manager()->Stop();
829
830 RunExtensionServiceTaskRunner(lock_screen_profile());
831 RunExtensionServiceTaskRunner(profile());
832
833 EXPECT_TRUE(base::PathExists(initial_note_taking_app->path()));
834 EXPECT_TRUE(base::PathExists(final_note_taking_app->path()));
835 }
836
837 TEST_P(LockScreenAppManagerImplTest,
838 NoteTakingAppChangeToInternalWhileActivating) {
839 scoped_refptr<const extensions::Extension> initial_note_taking_app =
840 AddTestAppWithLockScreenSupport(
841 profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.1",
842 true /* enable_on_lock_screen */);
843
844 scoped_refptr<const extensions::Extension> final_note_taking_app =
845 CreateTestAppWithLocation(TestAppLocation::kInternal, profile(),
846 chromeos::NoteTakingHelper::kDevKeepExtensionId,
847 "1.1", true /* enable_on_lock_screen */);
848 extensions::ExtensionSystem::Get(profile())
849 ->extension_service()
850 ->AddExtension(final_note_taking_app.get());
851
852 InitializeAndStartAppManager(profile());
853
854 EXPECT_EQ(0, note_taking_changed_count());
855 EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
856
857 chromeos::NoteTakingHelper::Get()->SetPreferredApp(
858 profile(), chromeos::NoteTakingHelper::kDevKeepExtensionId);
859
860 EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
861 EXPECT_EQ(1, note_taking_changed_count());
862 ResetNoteTakingChangedCount();
863
864 RunExtensionServiceTaskRunner(lock_screen_profile());
865
866 EXPECT_EQ(1, note_taking_changed_count());
867 ResetNoteTakingChangedCount();
868
869 EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
870 EXPECT_EQ(chromeos::NoteTakingHelper::kDevKeepExtensionId,
871 app_manager()->GetNoteTakingAppId());
872
873 const extensions::Extension* lock_app =
874 extensions::ExtensionRegistry::Get(lock_screen_profile())
875 ->GetExtensionById(chromeos::NoteTakingHelper::kDevKeepExtensionId,
876 extensions::ExtensionRegistry::ENABLED);
877 ASSERT_TRUE(lock_app);
878 EXPECT_EQ("1.1", lock_app->VersionString());
879
880 EXPECT_TRUE(base::PathExists(lock_app->path()));
881 EXPECT_EQ(
882 GetLockScreenAppPathWithOriginalLocation(
883 TestAppLocation::kInternal, profile(), final_note_taking_app->id(),
884 final_note_taking_app->VersionString()),
885 lock_app->path());
886
887 app_manager()->Stop();
888
889 RunExtensionServiceTaskRunner(lock_screen_profile());
890 RunExtensionServiceTaskRunner(profile());
891
892 EXPECT_TRUE(base::PathExists(initial_note_taking_app->path()));
893 EXPECT_TRUE(base::PathExists(final_note_taking_app->path()));
894 }
895
896 TEST_P(LockScreenAppManagerImplTest, ShutdownWhenStarted) {
897 scoped_refptr<const extensions::Extension> note_taking_app =
898 AddTestAppWithLockScreenSupport(
899 profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.1",
900 true /* enable_on_lock_screen */);
901
902 InitializeAndStartAppManager(profile());
903 RunExtensionServiceTaskRunner(lock_screen_profile());
904
905 const extensions::Extension* lock_app =
906 extensions::ExtensionRegistry::Get(lock_screen_profile())
907 ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
908 extensions::ExtensionRegistry::ENABLED);
909 EXPECT_TRUE(lock_app);
910 }
911
912 TEST_P(LockScreenAppManagerImplTest, LaunchAppWhenEnabled) {
913 TestEventRouter* event_router = static_cast<TestEventRouter*>(
914 extensions::EventRouterFactory::GetInstance()->SetTestingFactoryAndUse(
915 lock_screen_profile()->GetOriginalProfile(),
916 &TestEventRouterFactoryFunction));
917 ASSERT_TRUE(event_router);
918
919 scoped_refptr<const extensions::Extension> note_taking_app =
920 AddTestAppWithLockScreenSupport(
921 profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
922 true /* enable_on_lock_screen */);
923
924 InitializeAndStartAppManager(profile());
925 RunExtensionServiceTaskRunner(lock_screen_profile());
926
927 ASSERT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
928 app_manager()->GetNoteTakingAppId());
929
930 EXPECT_TRUE(app_manager()->LaunchNoteTaking());
931
932 ASSERT_EQ(1u, event_router->launched_apps().size());
933 EXPECT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
934 event_router->launched_apps()[0]);
935 event_router->ClearLaunchedApps();
936
937 app_manager()->Stop();
938
939 EXPECT_FALSE(app_manager()->LaunchNoteTaking());
940 EXPECT_TRUE(event_router->launched_apps().empty());
941 }
942
943 TEST_P(LockScreenAppManagerImplTest, LaunchAppWhenNoLockScreenApp) {
944 TestEventRouter* event_router = static_cast<TestEventRouter*>(
945 extensions::EventRouterFactory::GetInstance()->SetTestingFactoryAndUse(
946 lock_screen_profile()->GetOriginalProfile(),
947 &TestEventRouterFactoryFunction));
948 ASSERT_TRUE(event_router);
949
950 scoped_refptr<const extensions::Extension> note_taking_app =
951 AddTestAppWithLockScreenSupport(
952 profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
953 false /* enable_on_lock_screen */);
954
955 InitializeAndStartAppManager(profile());
956 RunExtensionServiceTaskRunner(lock_screen_profile());
957
958 EXPECT_FALSE(app_manager()->LaunchNoteTaking());
959 EXPECT_TRUE(event_router->launched_apps().empty());
960
961 app_manager()->Stop();
962 EXPECT_FALSE(app_manager()->LaunchNoteTaking());
963 EXPECT_TRUE(event_router->launched_apps().empty());
964 }
965
966 } // namespace lock_screen_apps
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/lock_screen_apps/app_manager_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698