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/bind.h"
13 #include "base/callback_helpers.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/json/json_file_value_serializer.h"
17 #include "base/run_loop.h"
18 #include "base/test/scoped_command_line.h"
19 #include "base/test/simple_test_tick_clock.h"
20 #include "base/values.h"
21 #include "chrome/browser/chromeos/arc/session/arc_session_manager.h"
22 #include "chrome/browser/chromeos/arc/test/test_arc_session_manager.h"
23 #include "chrome/browser/chromeos/lock_screen_apps/fake_lock_screen_profile_creator.h"
24 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
25 #include "chrome/browser/chromeos/note_taking_helper.h"
26 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
27 #include "chrome/browser/extensions/extension_service.h"
28 #include "chrome/browser/extensions/test_extension_system.h"
29 #include "chrome/common/chrome_constants.h"
30 #include "chrome/common/pref_names.h"
31 #include "chrome/test/base/testing_browser_process.h"
32 #include "chrome/test/base/testing_profile.h"
33 #include "chrome/test/base/testing_profile_manager.h"
34 #include "chromeos/dbus/dbus_thread_manager.h"
35 #include "components/arc/arc_service_manager.h"
36 #include "components/arc/session/arc_session.h"
37 #include "content/public/test/browser_task_environment.h"
38 #include "extensions/browser/disable_reason.h"
39 #include "extensions/browser/extension_file_task_runner.h"
40 #include "extensions/browser/extension_prefs.h"
41 #include "extensions/browser/extension_registry.h"
42 #include "extensions/browser/test_event_router.h"
43 #include "extensions/common/api/app_runtime.h"
44 #include "extensions/common/extension.h"
45 #include "extensions/common/extension_builder.h"
46 #include "extensions/common/value_builder.h"
47 #include "testing/gtest/include/gtest/gtest.h"
48 
49 using extensions::DictionaryBuilder;
50 using extensions::ListBuilder;
51 
52 namespace lock_screen_apps {
53 
54 namespace {
55 
56 constexpr int kMaxLockScreenAppReloadsCount = 3;
57 
ArcSessionFactory()58 std::unique_ptr<arc::ArcSession> ArcSessionFactory() {
59   ADD_FAILURE() << "Attempt to create arc session.";
60   return nullptr;
61 }
62 
63 class LockScreenEventRouter : public extensions::TestEventRouter {
64  public:
LockScreenEventRouter(content::BrowserContext * context)65   explicit LockScreenEventRouter(content::BrowserContext* context)
66       : extensions::TestEventRouter(context) {}
67   ~LockScreenEventRouter() override = default;
68 
69   // extensions::EventRouter:
ExtensionHasEventListener(const std::string & extension_id,const std::string & event_name) const70   bool ExtensionHasEventListener(const std::string& extension_id,
71                                  const std::string& event_name) const override {
72     return event_name == extensions::api::app_runtime::OnLaunched::kEventName;
73   }
74 
75  private:
76   DISALLOW_COPY_AND_ASSIGN(LockScreenEventRouter);
77 };
78 
79 class LockScreenEventObserver
80     : public extensions::TestEventRouter::EventObserver {
81  public:
LockScreenEventObserver(content::BrowserContext * context)82   explicit LockScreenEventObserver(content::BrowserContext* context)
83       : context_(context) {}
84   ~LockScreenEventObserver() override = default;
85 
86   // extensions::TestEventRouter::EventObserver:
OnDispatchEventToExtension(const std::string & extension_id,const extensions::Event & event)87   void OnDispatchEventToExtension(const std::string& extension_id,
88                                   const extensions::Event& event) override {
89     if (event.event_name !=
90         extensions::api::app_runtime::OnLaunched::kEventName) {
91       return;
92     }
93     ASSERT_TRUE(event.event_args);
94     const base::Value* arg_value = nullptr;
95     ASSERT_TRUE(event.event_args->Get(0, &arg_value));
96     ASSERT_TRUE(arg_value);
97     if (event.restrict_to_browser_context)
98       EXPECT_EQ(context_, event.restrict_to_browser_context);
99 
100     std::unique_ptr<extensions::api::app_runtime::LaunchData> launch_data =
101         extensions::api::app_runtime::LaunchData::FromValue(*arg_value);
102     ASSERT_TRUE(launch_data);
103     ASSERT_TRUE(launch_data->action_data);
104     EXPECT_EQ(extensions::api::app_runtime::ACTION_TYPE_NEW_NOTE,
105               launch_data->action_data->action_type);
106 
107     ASSERT_TRUE(launch_data->action_data->is_lock_screen_action);
108     EXPECT_TRUE(*launch_data->action_data->is_lock_screen_action);
109 
110     ASSERT_TRUE(launch_data->action_data->restore_last_action_state);
111     EXPECT_EQ(expect_restore_action_state_,
112               *launch_data->action_data->restore_last_action_state);
113 
114     launched_apps_.push_back(extension_id);
115   }
116 
launched_apps() const117   const std::vector<std::string>& launched_apps() const {
118     return launched_apps_;
119   }
120 
ClearLaunchedApps()121   void ClearLaunchedApps() { launched_apps_.clear(); }
122 
set_expect_restore_action_state(bool expect_restore_action_state)123   void set_expect_restore_action_state(bool expect_restore_action_state) {
124     expect_restore_action_state_ = expect_restore_action_state;
125   }
126 
127  private:
128   std::vector<std::string> launched_apps_;
129   content::BrowserContext* context_;
130   bool expect_restore_action_state_ = true;
131 
132   DISALLOW_COPY_AND_ASSIGN(LockScreenEventObserver);
133 };
134 
135 enum class TestAppLocation { kUnpacked, kInternal };
136 
137 class LockScreenAppManagerImplTest
138     : public testing::TestWithParam<TestAppLocation> {
139  public:
LockScreenAppManagerImplTest()140   LockScreenAppManagerImplTest()
141       : profile_manager_(TestingBrowserProcess::GetGlobal()) {}
142 
143   ~LockScreenAppManagerImplTest() override = default;
144 
SetUp()145   void SetUp() override {
146     // Need to initialize DBusThreadManager before ArcSessionManager's
147     // constructor calls DBusThreadManager::Get().
148     chromeos::DBusThreadManager::Initialize();
149     // Initialize command line so chromeos::NoteTakingHelper thinks note taking
150     // on lock screen is enabled.
151     command_line_ = std::make_unique<base::test::ScopedCommandLine>();
152     command_line_->GetProcessCommandLine()->InitFromArgv(
153         {"", "--enable-lock-screen-apps", "--force-enable-stylus-tools"});
154 
155     ASSERT_TRUE(profile_manager_.SetUp());
156 
157     profile_ = profile_manager_.CreateTestingProfile("primary_profile");
158 
159     InitExtensionSystem(profile());
160 
161     // Initialize arc session manager - NoteTakingHelper expects it to be set.
162     arc_session_manager_ = arc::CreateTestArcSessionManager(
163         std::make_unique<arc::ArcSessionRunner>(
164             base::BindRepeating(&ArcSessionFactory)));
165 
166     chromeos::NoteTakingHelper::Initialize();
167     chromeos::NoteTakingHelper::Get()->SetProfileWithEnabledLockScreenApps(
168         profile());
169 
170     lock_screen_profile_creator_ =
171         std::make_unique<lock_screen_apps::FakeLockScreenProfileCreator>(
172             &profile_manager_);
173     lock_screen_profile_creator_->Initialize();
174 
175     ResetAppManager();
176   }
177 
TearDown()178   void TearDown() override {
179     // App manager has to be destroyed before NoteTakingHelper is shutdown - it
180     // removes itself from the NoteTakingHelper observer list during its
181     // destruction.
182     app_manager_.reset();
183 
184     lock_screen_profile_creator_.reset();
185     chromeos::NoteTakingHelper::Shutdown();
186     arc_session_manager_.reset();
187     extensions::ExtensionSystem::Get(profile())->Shutdown();
188     chromeos::DBusThreadManager::Shutdown();
189   }
190 
InitExtensionSystem(Profile * profile)191   void InitExtensionSystem(Profile* profile) {
192     extensions::TestExtensionSystem* extension_system =
193         static_cast<extensions::TestExtensionSystem*>(
194             extensions::ExtensionSystem::Get(profile));
195     extension_system->CreateExtensionService(
196         base::CommandLine::ForCurrentProcess(),
197         profile->GetPath().Append("Extensions") /* install_directory */,
198         false /* autoupdate_enabled */);
199   }
200 
SetUpTestEventRouter()201   void SetUpTestEventRouter() {
202     LockScreenEventRouter* event_router =
203         extensions::CreateAndUseTestEventRouter<LockScreenEventRouter>(
204             LockScreenProfile()->GetOriginalProfile());
205     event_observer_ = std::make_unique<LockScreenEventObserver>(
206         LockScreenProfile()->GetOriginalProfile());
207     event_router->AddEventObserver(event_observer_.get());
208   }
209 
GetTestAppSourcePath(TestAppLocation location,Profile * profile,const std::string & id,const std::string & version)210   base::FilePath GetTestAppSourcePath(TestAppLocation location,
211                                       Profile* profile,
212                                       const std::string& id,
213                                       const std::string& version) {
214     switch (location) {
215       case TestAppLocation::kUnpacked:
216         return profile->GetPath().Append("Downloads").Append("app");
217       case TestAppLocation::kInternal:
218         return extensions::ExtensionSystem::Get(profile)
219             ->extension_service()
220             ->install_directory()
221             .Append(id)
222             .Append(version);
223     }
224     return base::FilePath();
225   }
226 
GetLockScreenAppPath(const std::string & id,const std::string & version)227   base::FilePath GetLockScreenAppPath(const std::string& id,
228                                       const std::string& version) {
229     return GetLockScreenAppPathWithOriginalProfile(profile(), id, version);
230   }
231 
GetLockScreenAppPathWithOriginalProfile(Profile * original_profile,const std::string & id,const std::string & version)232   base::FilePath GetLockScreenAppPathWithOriginalProfile(
233       Profile* original_profile,
234       const std::string& id,
235       const std::string& version) {
236     return GetLockScreenAppPathWithOriginalLocation(
237         GetParam(), original_profile, id, version);
238   }
239 
GetLockScreenAppPathWithOriginalLocation(TestAppLocation location,Profile * original_profile,const std::string & id,const std::string & version)240   base::FilePath GetLockScreenAppPathWithOriginalLocation(
241       TestAppLocation location,
242       Profile* original_profile,
243       const std::string& id,
244       const std::string& version) {
245     switch (location) {
246       case TestAppLocation::kUnpacked:
247         return original_profile->GetPath().Append("Downloads").Append("app");
248       case TestAppLocation::kInternal:
249         return extensions::ExtensionSystem::Get(LockScreenProfile())
250             ->extension_service()
251             ->install_directory()
252             .Append(id)
253             .Append(version + "_0");
254     }
255     return base::FilePath();
256   }
257 
GetAppLocation(TestAppLocation location)258   extensions::Manifest::Location GetAppLocation(TestAppLocation location) {
259     switch (location) {
260       case TestAppLocation::kUnpacked:
261         return extensions::Manifest::UNPACKED;
262       case TestAppLocation::kInternal:
263         return extensions::Manifest::INTERNAL;
264     }
265 
266     return extensions::Manifest::UNPACKED;
267   }
268 
CreateTestApp(const std::string & id,const std::string & version,bool supports_lock_screen)269   scoped_refptr<const extensions::Extension> CreateTestApp(
270       const std::string& id,
271       const std::string& version,
272       bool supports_lock_screen) {
273     return CreateTestAppInProfile(profile(), id, version, supports_lock_screen);
274   }
275 
CreateTestAppInProfile(Profile * profile,const std::string & id,const std::string & version,bool supports_lock_screen)276   scoped_refptr<const extensions::Extension> CreateTestAppInProfile(
277       Profile* profile,
278       const std::string& id,
279       const std::string& version,
280       bool supports_lock_screen) {
281     return CreateTestAppWithLocation(GetParam(), profile, id, version,
282                                      supports_lock_screen);
283   }
284 
CreateTestAppWithLocation(TestAppLocation location,Profile * profile,const std::string & id,const std::string & version,bool supports_lock_screen)285   scoped_refptr<const extensions::Extension> CreateTestAppWithLocation(
286       TestAppLocation location,
287       Profile* profile,
288       const std::string& id,
289       const std::string& version,
290       bool supports_lock_screen) {
291     std::unique_ptr<base::DictionaryValue> background =
292         DictionaryBuilder()
293             .Set("scripts", ListBuilder().Append("background.js").Build())
294             .Build();
295     std::unique_ptr<base::ListValue> action_handlers =
296         ListBuilder()
297             .Append(DictionaryBuilder()
298                         .Set("action", "new_note")
299                         .Set("enabled_on_lock_screen", supports_lock_screen)
300                         .Build())
301             .Build();
302 
303     DictionaryBuilder manifest_builder;
304     manifest_builder.Set("name", "Note taking app")
305         .Set("version", version)
306         .Set("manifest_version", 2)
307         .Set("app", DictionaryBuilder()
308                         .Set("background", std::move(background))
309                         .Build())
310         .Set("permissions", ListBuilder().Append("lockScreen").Build())
311         .Set("action_handlers", std::move(action_handlers));
312 
313     base::FilePath extension_path =
314         GetTestAppSourcePath(location, profile, id, version);
315 
316     scoped_refptr<const extensions::Extension> extension =
317         extensions::ExtensionBuilder()
318             .SetManifest(manifest_builder.Build())
319             .SetID(id)
320             .SetPath(extension_path)
321             .SetLocation(GetAppLocation(location))
322             .Build();
323 
324     // Create the app path with required files - app manager *will* attempt to
325     // load the app from the disk, so extension directory has to be present for
326     // the load to succeed.
327     base::File::Error error;
328     if (!base::CreateDirectoryAndGetError(extension_path, &error)) {
329       ADD_FAILURE() << "Failed to create path " << extension_path.value() << " "
330                     << error;
331       return nullptr;
332     }
333 
334     JSONFileValueSerializer manifest_writer(
335         extension_path.Append("manifest.json"));
336     if (!manifest_writer.Serialize(*extension->manifest()->value())) {
337       ADD_FAILURE() << "Failed to create manifest file";
338       return nullptr;
339     }
340 
341     if (!base::WriteFile(extension_path.Append("background.js"), "{}")) {
342       ADD_FAILURE() << "Failed to write background script file";
343       return nullptr;
344     }
345 
346     return extension;
347   }
348 
CreateSecondaryProfile()349   TestingProfile* CreateSecondaryProfile() {
350     TestingProfile* profile =
351         profile_manager_.CreateTestingProfile("secondary_profile");
352     InitExtensionSystem(profile);
353     return profile;
354   }
355 
AddTestAppWithLockScreenSupport(Profile * profile,const std::string & app_id,const std::string & version,bool enable_on_lock_screen)356   scoped_refptr<const extensions::Extension> AddTestAppWithLockScreenSupport(
357       Profile* profile,
358       const std::string& app_id,
359       const std::string& version,
360       bool enable_on_lock_screen) {
361     scoped_refptr<const extensions::Extension> app = CreateTestAppInProfile(
362         profile, app_id, version, true /* supports_lock_screen*/);
363     extensions::ExtensionSystem::Get(profile)
364         ->extension_service()
365         ->AddExtension(app.get());
366 
367     chromeos::NoteTakingHelper::Get()->SetPreferredApp(profile, app_id);
368     chromeos::NoteTakingHelper::Get()->SetPreferredAppEnabledOnLockScreen(
369         profile, enable_on_lock_screen);
370     return app;
371   }
372 
373   // Initializes and starts app manager.
374   // |create_lock_screen_profile| - whether the lock screen profile should be
375   //     created (using the lock screen profile creator passed to the app
376   //     manager) before starting the app manager. If this is not set, the test
377   //     should create the lock screen profile itself (and should not use
378   //     |LockScreenProfile| before that).
InitializeAndStartAppManager(Profile * profile,bool create_lock_screen_profile)379   void InitializeAndStartAppManager(Profile* profile,
380                                     bool create_lock_screen_profile) {
381     app_manager()->Initialize(profile, lock_screen_profile_creator_.get());
382     if (create_lock_screen_profile)
383       CreateLockScreenProfile();
384     app_manager()->Start(
385         base::Bind(&LockScreenAppManagerImplTest::OnNoteTakingChanged,
386                    base::Unretained(this)));
387   }
388 
RestartLockScreenAppManager()389   void RestartLockScreenAppManager() {
390     app_manager()->Stop();
391     app_manager()->Start(
392         base::Bind(&LockScreenAppManagerImplTest::OnNoteTakingChanged,
393                    base::Unretained(this)));
394   }
395 
CreateLockScreenProfile()396   void CreateLockScreenProfile() {
397     lock_screen_profile_creator_->CreateProfile();
398     if (needs_lock_screen_event_router_)
399       SetUpTestEventRouter();
400   }
401 
set_needs_lock_screen_event_router()402   void set_needs_lock_screen_event_router() {
403     needs_lock_screen_event_router_ = true;
404   }
405 
profile()406   TestingProfile* profile() { return profile_; }
407 
LockScreenProfile()408   Profile* LockScreenProfile() {
409     return lock_screen_profile_creator_->lock_screen_profile();
410   }
411 
app_manager()412   AppManager* app_manager() { return app_manager_.get(); }
413 
ResetAppManager()414   void ResetAppManager() {
415     app_manager_ = std::make_unique<AppManagerImpl>(&tick_clock_);
416   }
417 
note_taking_changed_count() const418   int note_taking_changed_count() const { return note_taking_changed_count_; }
419 
ResetNoteTakingChangedCount()420   void ResetNoteTakingChangedCount() { note_taking_changed_count_ = 0; }
421 
422   // Waits for a round trip between file task runner used by the profile's
423   // extension service and the main thread - used to ensure that all pending
424   // file runner task finish,
RunExtensionServiceTaskRunner(Profile * profile)425   void RunExtensionServiceTaskRunner(Profile* profile) {
426     base::RunLoop run_loop;
427     extensions::GetExtensionFileTaskRunner()->PostTaskAndReply(
428         FROM_HERE, base::DoNothing(), run_loop.QuitClosure());
429     run_loop.Run();
430   }
431 
IsInstallAsync()432   bool IsInstallAsync() { return GetParam() != TestAppLocation::kUnpacked; }
433 
NoteTakingChangedCountOnStart()434   int NoteTakingChangedCountOnStart() { return IsInstallAsync() ? 1 : 0; }
435 
event_observer()436   LockScreenEventObserver* event_observer() { return event_observer_.get(); }
437 
lock_screen_profile_creator()438   FakeLockScreenProfileCreator* lock_screen_profile_creator() {
439     return lock_screen_profile_creator_.get();
440   }
441 
442  protected:
443   base::SimpleTestTickClock tick_clock_;
444   std::unique_ptr<lock_screen_apps::FakeLockScreenProfileCreator>
445       lock_screen_profile_creator_;
446 
447  private:
OnNoteTakingChanged()448   void OnNoteTakingChanged() { ++note_taking_changed_count_; }
449 
450   std::unique_ptr<base::test::ScopedCommandLine> command_line_;
451   content::BrowserTaskEnvironment task_environment_;
452 
453   chromeos::ScopedCrosSettingsTestHelper cros_settings_test_helper_;
454   chromeos::ScopedTestUserManager user_manager_;
455 
456   TestingProfileManager profile_manager_;
457   TestingProfile* profile_ = nullptr;
458 
459   std::unique_ptr<LockScreenEventObserver> event_observer_;
460 
461   std::unique_ptr<arc::ArcServiceManager> arc_service_manager_;
462   std::unique_ptr<arc::ArcSessionManager> arc_session_manager_;
463 
464   std::unique_ptr<AppManager> app_manager_;
465 
466   bool needs_lock_screen_event_router_ = false;
467   int note_taking_changed_count_ = 0;
468 
469   DISALLOW_COPY_AND_ASSIGN(LockScreenAppManagerImplTest);
470 };
471 
472 }  // namespace
473 
474 INSTANTIATE_TEST_SUITE_P(Unpacked,
475                          LockScreenAppManagerImplTest,
476                          ::testing::Values(TestAppLocation::kUnpacked));
477 INSTANTIATE_TEST_SUITE_P(Internal,
478                          LockScreenAppManagerImplTest,
479                          ::testing::Values(TestAppLocation::kInternal));
480 
TEST_P(LockScreenAppManagerImplTest,StartAddsAppToTarget)481 TEST_P(LockScreenAppManagerImplTest, StartAddsAppToTarget) {
482   scoped_refptr<const extensions::Extension> note_taking_app =
483       AddTestAppWithLockScreenSupport(
484           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
485           true /* enable_on_lock_screen */);
486 
487   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
488 
489   EXPECT_EQ(0, note_taking_changed_count());
490   EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
491 
492   RunExtensionServiceTaskRunner(LockScreenProfile());
493 
494   EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
495   ResetNoteTakingChangedCount();
496 
497   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
498   EXPECT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
499             app_manager()->GetNoteTakingAppId());
500 
501   EXPECT_TRUE(base::PathExists(note_taking_app->path()));
502 
503   const extensions::Extension* lock_app =
504       extensions::ExtensionRegistry::Get(LockScreenProfile())
505           ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
506                              extensions::ExtensionRegistry::ENABLED);
507   ASSERT_TRUE(lock_app);
508 
509   EXPECT_TRUE(base::PathExists(lock_app->path()));
510   EXPECT_EQ(GetLockScreenAppPath(note_taking_app->id(),
511                                  note_taking_app->VersionString()),
512             lock_app->path());
513 
514   app_manager()->Stop();
515 
516   EXPECT_EQ(0, note_taking_changed_count());
517   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
518   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
519 
520   lock_app =
521       extensions::ExtensionRegistry::Get(LockScreenProfile())
522           ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
523                              extensions::ExtensionRegistry::EVERYTHING);
524   EXPECT_FALSE(lock_app);
525 
526   RunExtensionServiceTaskRunner(LockScreenProfile());
527   RunExtensionServiceTaskRunner(profile());
528 
529   EXPECT_TRUE(base::PathExists(note_taking_app->path()));
530 }
531 
TEST_P(LockScreenAppManagerImplTest,StartWhenLockScreenNotesNotEnabled)532 TEST_P(LockScreenAppManagerImplTest, StartWhenLockScreenNotesNotEnabled) {
533   scoped_refptr<const extensions::Extension> note_taking_app =
534       AddTestAppWithLockScreenSupport(
535           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
536           false /* enable_on_lock_screen */);
537 
538   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
539   RunExtensionServiceTaskRunner(LockScreenProfile());
540 
541   EXPECT_EQ(0, note_taking_changed_count());
542   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
543   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
544 
545   const extensions::Extension* lock_app =
546       extensions::ExtensionRegistry::Get(LockScreenProfile())
547           ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
548                              extensions::ExtensionRegistry::ENABLED);
549   EXPECT_FALSE(lock_app);
550 
551   app_manager()->Stop();
552   EXPECT_EQ(0, note_taking_changed_count());
553   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
554   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
555 
556   lock_app =
557       extensions::ExtensionRegistry::Get(LockScreenProfile())
558           ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
559                              extensions::ExtensionRegistry::EVERYTHING);
560   EXPECT_FALSE(lock_app);
561 
562   RunExtensionServiceTaskRunner(LockScreenProfile());
563   RunExtensionServiceTaskRunner(profile());
564 
565   EXPECT_TRUE(base::PathExists(note_taking_app->path()));
566 }
567 
TEST_P(LockScreenAppManagerImplTest,LockScreenNoteTakingDisabledWhileStarted)568 TEST_P(LockScreenAppManagerImplTest, LockScreenNoteTakingDisabledWhileStarted) {
569   scoped_refptr<const extensions::Extension> note_taking_app =
570       AddTestAppWithLockScreenSupport(
571           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
572           true /* enable_on_lock_screen */);
573 
574   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
575 
576   EXPECT_EQ(0, note_taking_changed_count());
577   EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
578 
579   RunExtensionServiceTaskRunner(LockScreenProfile());
580 
581   EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
582   ResetNoteTakingChangedCount();
583 
584   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
585   EXPECT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
586             app_manager()->GetNoteTakingAppId());
587 
588   const extensions::Extension* lock_app =
589       extensions::ExtensionRegistry::Get(LockScreenProfile())
590           ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
591                              extensions::ExtensionRegistry::ENABLED);
592   ASSERT_TRUE(lock_app);
593 
594   EXPECT_TRUE(base::PathExists(lock_app->path()));
595   EXPECT_EQ(GetLockScreenAppPath(note_taking_app->id(),
596                                  note_taking_app->VersionString()),
597             lock_app->path());
598   EXPECT_TRUE(base::PathExists(note_taking_app->path()));
599 
600   chromeos::NoteTakingHelper::Get()->SetPreferredAppEnabledOnLockScreen(
601       profile(), false);
602 
603   EXPECT_EQ(1, note_taking_changed_count());
604   ResetNoteTakingChangedCount();
605 
606   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
607   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
608   lock_app =
609       extensions::ExtensionRegistry::Get(LockScreenProfile())
610           ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
611                              extensions::ExtensionRegistry::EVERYTHING);
612   EXPECT_FALSE(lock_app);
613 
614   app_manager()->Stop();
615 
616   EXPECT_EQ(0, note_taking_changed_count());
617   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
618   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
619 
620   RunExtensionServiceTaskRunner(LockScreenProfile());
621   RunExtensionServiceTaskRunner(profile());
622 
623   EXPECT_TRUE(base::PathExists(note_taking_app->path()));
624 }
625 
TEST_P(LockScreenAppManagerImplTest,LockScreenNoteTakingEnabledWhileStarted)626 TEST_P(LockScreenAppManagerImplTest, LockScreenNoteTakingEnabledWhileStarted) {
627   scoped_refptr<const extensions::Extension> note_taking_app =
628       AddTestAppWithLockScreenSupport(
629           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
630           false /* enable_on_lock_screen */);
631 
632   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
633   RunExtensionServiceTaskRunner(LockScreenProfile());
634 
635   EXPECT_EQ(0, note_taking_changed_count());
636   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
637   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
638 
639   const extensions::Extension* lock_app =
640       extensions::ExtensionRegistry::Get(LockScreenProfile())
641           ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
642                              extensions::ExtensionRegistry::EVERYTHING);
643   EXPECT_FALSE(lock_app);
644 
645   chromeos::NoteTakingHelper::Get()->SetPreferredAppEnabledOnLockScreen(
646       profile(), true);
647 
648   EXPECT_EQ(1, note_taking_changed_count());
649   ResetNoteTakingChangedCount();
650   EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
651 
652   RunExtensionServiceTaskRunner(LockScreenProfile());
653 
654   EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
655   ResetNoteTakingChangedCount();
656 
657   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
658   EXPECT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
659             app_manager()->GetNoteTakingAppId());
660 
661   lock_app =
662       extensions::ExtensionRegistry::Get(LockScreenProfile())
663           ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
664                              extensions::ExtensionRegistry::ENABLED);
665   ASSERT_TRUE(lock_app);
666 
667   EXPECT_TRUE(base::PathExists(lock_app->path()));
668   EXPECT_EQ(GetLockScreenAppPath(note_taking_app->id(),
669                                  note_taking_app->VersionString()),
670             lock_app->path());
671   EXPECT_TRUE(base::PathExists(note_taking_app->path()));
672 
673   app_manager()->Stop();
674 
675   EXPECT_EQ(0, note_taking_changed_count());
676   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
677   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
678 
679   RunExtensionServiceTaskRunner(LockScreenProfile());
680   RunExtensionServiceTaskRunner(profile());
681 
682   EXPECT_TRUE(base::PathExists(note_taking_app->path()));
683 }
684 
TEST_P(LockScreenAppManagerImplTest,LockScreenNoteTakingChangedWhileStarted)685 TEST_P(LockScreenAppManagerImplTest, LockScreenNoteTakingChangedWhileStarted) {
686   scoped_refptr<const extensions::Extension> dev_note_taking_app =
687       AddTestAppWithLockScreenSupport(
688           profile(), chromeos::NoteTakingHelper::kDevKeepExtensionId, "1.0",
689           true /* enable_on_lock_screen */);
690 
691   scoped_refptr<const extensions::Extension> prod_note_taking_app =
692       AddTestAppWithLockScreenSupport(
693           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
694           true /* enable_on_lock_screen */);
695 
696   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
697 
698   EXPECT_EQ(0, note_taking_changed_count());
699   EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
700 
701   RunExtensionServiceTaskRunner(LockScreenProfile());
702 
703   EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
704   ResetNoteTakingChangedCount();
705 
706   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
707   EXPECT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
708             app_manager()->GetNoteTakingAppId());
709 
710   const extensions::Extension* lock_app =
711       extensions::ExtensionRegistry::Get(LockScreenProfile())
712           ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
713                              extensions::ExtensionRegistry::ENABLED);
714   ASSERT_TRUE(lock_app);
715 
716   EXPECT_TRUE(base::PathExists(lock_app->path()));
717   EXPECT_EQ(GetLockScreenAppPath(prod_note_taking_app->id(),
718                                  prod_note_taking_app->VersionString()),
719             lock_app->path());
720   EXPECT_TRUE(base::PathExists(prod_note_taking_app->path()));
721 
722   chromeos::NoteTakingHelper::Get()->SetPreferredApp(
723       profile(), chromeos::NoteTakingHelper::kDevKeepExtensionId);
724 
725   EXPECT_EQ(1, note_taking_changed_count());
726   ResetNoteTakingChangedCount();
727   EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
728 
729   RunExtensionServiceTaskRunner(LockScreenProfile());
730 
731   EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
732   ResetNoteTakingChangedCount();
733 
734   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
735   EXPECT_EQ(chromeos::NoteTakingHelper::kDevKeepExtensionId,
736             app_manager()->GetNoteTakingAppId());
737 
738   // Verify prod app was unloaded from signin profile.
739   lock_app =
740       extensions::ExtensionRegistry::Get(LockScreenProfile())
741           ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
742                              extensions::ExtensionRegistry::EVERYTHING);
743   EXPECT_FALSE(lock_app);
744 
745   lock_app =
746       extensions::ExtensionRegistry::Get(LockScreenProfile())
747           ->GetExtensionById(chromeos::NoteTakingHelper::kDevKeepExtensionId,
748                              extensions::ExtensionRegistry::ENABLED);
749 
750   ASSERT_TRUE(lock_app);
751 
752   EXPECT_TRUE(base::PathExists(lock_app->path()));
753   EXPECT_EQ(GetLockScreenAppPath(dev_note_taking_app->id(),
754                                  dev_note_taking_app->VersionString()),
755             lock_app->path());
756 
757   app_manager()->Stop();
758   EXPECT_EQ(0, note_taking_changed_count());
759   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
760   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
761 
762   RunExtensionServiceTaskRunner(LockScreenProfile());
763   RunExtensionServiceTaskRunner(profile());
764 
765   EXPECT_TRUE(base::PathExists(dev_note_taking_app->path()));
766   EXPECT_TRUE(base::PathExists(prod_note_taking_app->path()));
767 }
768 
TEST_P(LockScreenAppManagerImplTest,NoteTakingChangedToLockScreenSupported)769 TEST_P(LockScreenAppManagerImplTest, NoteTakingChangedToLockScreenSupported) {
770   scoped_refptr<const extensions::Extension> dev_note_taking_app =
771       AddTestAppWithLockScreenSupport(
772           profile(), chromeos::NoteTakingHelper::kDevKeepExtensionId, "1.0",
773           true /* enable_on_lock_screen */);
774 
775   scoped_refptr<const extensions::Extension> prod_note_taking_app =
776       CreateTestAppInProfile(profile(),
777                              chromeos::NoteTakingHelper::kProdKeepExtensionId,
778                              "1.0", false /* supports_lock_screen */);
779   extensions::ExtensionSystem::Get(profile())
780       ->extension_service()
781       ->AddExtension(prod_note_taking_app.get());
782   chromeos::NoteTakingHelper::Get()->SetPreferredApp(
783       profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId);
784 
785   // Initialize app manager - the note taking should be disabled initially
786   // because the preferred app (prod) is not enabled on lock screen.
787   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
788   RunExtensionServiceTaskRunner(LockScreenProfile());
789   EXPECT_EQ(0, note_taking_changed_count());
790   EXPECT_EQ(false, app_manager()->IsNoteTakingAppAvailable());
791 
792   // Setting dev app, which is enabled on lock screen, as preferred will enable
793   // lock screen note taking,
794   chromeos::NoteTakingHelper::Get()->SetPreferredApp(
795       profile(), chromeos::NoteTakingHelper::kDevKeepExtensionId);
796 
797   EXPECT_EQ(1, note_taking_changed_count());
798   ResetNoteTakingChangedCount();
799   // If test app is installed asynchronously. the app won't be enabled on
800   // lock screen until extension service task runner tasks are run.
801   EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
802   RunExtensionServiceTaskRunner(LockScreenProfile());
803 
804   EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
805   ResetNoteTakingChangedCount();
806   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
807   EXPECT_EQ(chromeos::NoteTakingHelper::kDevKeepExtensionId,
808             app_manager()->GetNoteTakingAppId());
809 
810   // Verify the dev app copy is installed in the lock screen app profile.
811   const extensions::Extension* lock_app =
812       extensions::ExtensionRegistry::Get(LockScreenProfile())
813           ->GetExtensionById(chromeos::NoteTakingHelper::kDevKeepExtensionId,
814                              extensions::ExtensionRegistry::ENABLED);
815   ASSERT_TRUE(lock_app);
816   EXPECT_TRUE(base::PathExists(lock_app->path()));
817   EXPECT_EQ(GetLockScreenAppPath(dev_note_taking_app->id(),
818                                  dev_note_taking_app->VersionString()),
819             lock_app->path());
820 
821   // Verify the prod app was not coppied to the lock screen profile (for
822   // unpacked apps, the lock screen extension path is the same as the original
823   // app path, so it's expected to still exist).
824   EXPECT_EQ(
825       GetParam() == TestAppLocation::kUnpacked,
826       base::PathExists(GetLockScreenAppPath(
827           prod_note_taking_app->id(), prod_note_taking_app->VersionString())));
828 
829   app_manager()->Stop();
830 
831   // Stopping app manager will disable lock screen note taking.
832   EXPECT_EQ(0, note_taking_changed_count());
833   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
834   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
835 
836   RunExtensionServiceTaskRunner(LockScreenProfile());
837   RunExtensionServiceTaskRunner(profile());
838 
839   // Make sure original app paths are not deleted.
840   EXPECT_TRUE(base::PathExists(dev_note_taking_app->path()));
841   EXPECT_TRUE(base::PathExists(prod_note_taking_app->path()));
842 }
843 
TEST_P(LockScreenAppManagerImplTest,LockScreenNoteTakingReloadedWhileStarted)844 TEST_P(LockScreenAppManagerImplTest, LockScreenNoteTakingReloadedWhileStarted) {
845   scoped_refptr<const extensions::Extension> note_taking_app =
846       AddTestAppWithLockScreenSupport(
847           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
848           true /* enable_on_lock_screen */);
849 
850   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
851   RunExtensionServiceTaskRunner(LockScreenProfile());
852 
853   EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
854   ResetNoteTakingChangedCount();
855 
856   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
857   EXPECT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
858             app_manager()->GetNoteTakingAppId());
859 
860   const extensions::Extension* lock_app =
861       extensions::ExtensionRegistry::Get(LockScreenProfile())
862           ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
863                              extensions::ExtensionRegistry::ENABLED);
864   ASSERT_TRUE(lock_app);
865   EXPECT_EQ("1.0", lock_app->VersionString());
866 
867   EXPECT_TRUE(base::PathExists(lock_app->path()));
868   EXPECT_EQ(GetLockScreenAppPath(note_taking_app->id(),
869                                  note_taking_app->VersionString()),
870             lock_app->path());
871   EXPECT_TRUE(base::PathExists(note_taking_app->path()));
872 
873   extensions::ExtensionSystem::Get(profile())
874       ->extension_service()
875       ->UnloadExtension(chromeos::NoteTakingHelper::kProdKeepExtensionId,
876                         extensions::UnloadedExtensionReason::UPDATE);
877 
878   EXPECT_EQ(1, note_taking_changed_count());
879   ResetNoteTakingChangedCount();
880 
881   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
882   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
883 
884   // Verify prod app was unloaded from signin profile.
885   lock_app =
886       extensions::ExtensionRegistry::Get(LockScreenProfile())
887           ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
888                              extensions::ExtensionRegistry::EVERYTHING);
889   EXPECT_FALSE(lock_app);
890 
891   // Add the app again.
892   note_taking_app = CreateTestApp(
893       chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.1", true);
894   extensions::ExtensionSystem::Get(profile())
895       ->extension_service()
896       ->AddExtension(note_taking_app.get());
897 
898   EXPECT_EQ(1, note_taking_changed_count());
899   ResetNoteTakingChangedCount();
900   EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
901 
902   RunExtensionServiceTaskRunner(LockScreenProfile());
903 
904   EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
905   ResetNoteTakingChangedCount();
906   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
907   EXPECT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
908             app_manager()->GetNoteTakingAppId());
909 
910   lock_app =
911       extensions::ExtensionRegistry::Get(LockScreenProfile())
912           ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
913                              extensions::ExtensionRegistry::ENABLED);
914 
915   ASSERT_TRUE(lock_app);
916   EXPECT_EQ("1.1", lock_app->VersionString());
917 
918   EXPECT_TRUE(base::PathExists(lock_app->path()));
919   EXPECT_EQ(GetLockScreenAppPath(note_taking_app->id(),
920                                  note_taking_app->VersionString()),
921             lock_app->path());
922 
923   app_manager()->Stop();
924   EXPECT_EQ(0, note_taking_changed_count());
925   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
926   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
927 
928   RunExtensionServiceTaskRunner(LockScreenProfile());
929   RunExtensionServiceTaskRunner(profile());
930 
931   EXPECT_TRUE(base::PathExists(note_taking_app->path()));
932 }
933 
TEST_P(LockScreenAppManagerImplTest,NoteTakingAppChangeToUnpackedWhileActivating)934 TEST_P(LockScreenAppManagerImplTest,
935        NoteTakingAppChangeToUnpackedWhileActivating) {
936   scoped_refptr<const extensions::Extension> initial_note_taking_app =
937       AddTestAppWithLockScreenSupport(
938           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.1",
939           true /* enable_on_lock_screen */);
940 
941   scoped_refptr<const extensions::Extension> final_note_taking_app =
942       CreateTestAppWithLocation(TestAppLocation::kUnpacked, profile(),
943                                 chromeos::NoteTakingHelper::kDevKeepExtensionId,
944                                 "1.1", true /* enable_on_lock_screen */);
945   extensions::ExtensionSystem::Get(profile())
946       ->extension_service()
947       ->AddExtension(final_note_taking_app.get());
948 
949   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
950 
951   EXPECT_EQ(0, note_taking_changed_count());
952   EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
953 
954   chromeos::NoteTakingHelper::Get()->SetPreferredApp(
955       profile(), chromeos::NoteTakingHelper::kDevKeepExtensionId);
956 
957   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
958   EXPECT_EQ(chromeos::NoteTakingHelper::kDevKeepExtensionId,
959             app_manager()->GetNoteTakingAppId());
960   EXPECT_EQ(1, note_taking_changed_count());
961   ResetNoteTakingChangedCount();
962 
963   RunExtensionServiceTaskRunner(LockScreenProfile());
964 
965   EXPECT_EQ(0, note_taking_changed_count());
966 
967   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
968   EXPECT_EQ(chromeos::NoteTakingHelper::kDevKeepExtensionId,
969             app_manager()->GetNoteTakingAppId());
970 
971   const extensions::Extension* lock_app =
972       extensions::ExtensionRegistry::Get(LockScreenProfile())
973           ->GetExtensionById(chromeos::NoteTakingHelper::kDevKeepExtensionId,
974                              extensions::ExtensionRegistry::ENABLED);
975   ASSERT_TRUE(lock_app);
976   EXPECT_EQ("1.1", lock_app->VersionString());
977 
978   EXPECT_TRUE(base::PathExists(lock_app->path()));
979   EXPECT_EQ(
980       GetLockScreenAppPathWithOriginalLocation(
981           TestAppLocation::kUnpacked, profile(), final_note_taking_app->id(),
982           final_note_taking_app->VersionString()),
983       lock_app->path());
984 
985   app_manager()->Stop();
986 
987   RunExtensionServiceTaskRunner(LockScreenProfile());
988   RunExtensionServiceTaskRunner(profile());
989 
990   EXPECT_TRUE(base::PathExists(initial_note_taking_app->path()));
991   EXPECT_TRUE(base::PathExists(final_note_taking_app->path()));
992 }
993 
TEST_P(LockScreenAppManagerImplTest,NoteTakingAppChangeToInternalWhileActivating)994 TEST_P(LockScreenAppManagerImplTest,
995        NoteTakingAppChangeToInternalWhileActivating) {
996   scoped_refptr<const extensions::Extension> initial_note_taking_app =
997       AddTestAppWithLockScreenSupport(
998           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.1",
999           true /* enable_on_lock_screen */);
1000 
1001   scoped_refptr<const extensions::Extension> final_note_taking_app =
1002       CreateTestAppWithLocation(TestAppLocation::kInternal, profile(),
1003                                 chromeos::NoteTakingHelper::kDevKeepExtensionId,
1004                                 "1.1", true /* enable_on_lock_screen */);
1005   extensions::ExtensionSystem::Get(profile())
1006       ->extension_service()
1007       ->AddExtension(final_note_taking_app.get());
1008 
1009   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
1010 
1011   EXPECT_EQ(0, note_taking_changed_count());
1012   EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
1013 
1014   chromeos::NoteTakingHelper::Get()->SetPreferredApp(
1015       profile(), chromeos::NoteTakingHelper::kDevKeepExtensionId);
1016 
1017   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
1018   EXPECT_EQ(1, note_taking_changed_count());
1019   ResetNoteTakingChangedCount();
1020 
1021   RunExtensionServiceTaskRunner(LockScreenProfile());
1022 
1023   EXPECT_EQ(1, note_taking_changed_count());
1024   ResetNoteTakingChangedCount();
1025 
1026   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
1027   EXPECT_EQ(chromeos::NoteTakingHelper::kDevKeepExtensionId,
1028             app_manager()->GetNoteTakingAppId());
1029 
1030   const extensions::Extension* lock_app =
1031       extensions::ExtensionRegistry::Get(LockScreenProfile())
1032           ->GetExtensionById(chromeos::NoteTakingHelper::kDevKeepExtensionId,
1033                              extensions::ExtensionRegistry::ENABLED);
1034   ASSERT_TRUE(lock_app);
1035   EXPECT_EQ("1.1", lock_app->VersionString());
1036 
1037   EXPECT_TRUE(base::PathExists(lock_app->path()));
1038   EXPECT_EQ(
1039       GetLockScreenAppPathWithOriginalLocation(
1040           TestAppLocation::kInternal, profile(), final_note_taking_app->id(),
1041           final_note_taking_app->VersionString()),
1042       lock_app->path());
1043 
1044   app_manager()->Stop();
1045 
1046   RunExtensionServiceTaskRunner(LockScreenProfile());
1047   RunExtensionServiceTaskRunner(profile());
1048 
1049   EXPECT_TRUE(base::PathExists(initial_note_taking_app->path()));
1050   EXPECT_TRUE(base::PathExists(final_note_taking_app->path()));
1051 }
1052 
TEST_P(LockScreenAppManagerImplTest,ShutdownWhenStarted)1053 TEST_P(LockScreenAppManagerImplTest, ShutdownWhenStarted) {
1054   scoped_refptr<const extensions::Extension> note_taking_app =
1055       AddTestAppWithLockScreenSupport(
1056           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.1",
1057           true /* enable_on_lock_screen */);
1058 
1059   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
1060   RunExtensionServiceTaskRunner(LockScreenProfile());
1061 
1062   const extensions::Extension* lock_app =
1063       extensions::ExtensionRegistry::Get(LockScreenProfile())
1064           ->GetExtensionById(chromeos::NoteTakingHelper::kProdKeepExtensionId,
1065                              extensions::ExtensionRegistry::ENABLED);
1066   EXPECT_TRUE(lock_app);
1067 }
1068 
TEST_P(LockScreenAppManagerImplTest,LaunchAppWhenEnabled)1069 TEST_P(LockScreenAppManagerImplTest, LaunchAppWhenEnabled) {
1070   set_needs_lock_screen_event_router();
1071 
1072   scoped_refptr<const extensions::Extension> note_taking_app =
1073       AddTestAppWithLockScreenSupport(
1074           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
1075           true /* enable_on_lock_screen */);
1076 
1077   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
1078   RunExtensionServiceTaskRunner(LockScreenProfile());
1079 
1080   ASSERT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
1081             app_manager()->GetNoteTakingAppId());
1082 
1083   EXPECT_TRUE(app_manager()->LaunchNoteTaking());
1084 
1085   ASSERT_EQ(1u, event_observer()->launched_apps().size());
1086   EXPECT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
1087             event_observer()->launched_apps()[0]);
1088   event_observer()->ClearLaunchedApps();
1089 
1090   app_manager()->Stop();
1091 
1092   EXPECT_FALSE(app_manager()->LaunchNoteTaking());
1093   EXPECT_TRUE(event_observer()->launched_apps().empty());
1094 }
1095 
TEST_P(LockScreenAppManagerImplTest,LaunchAppWithFalseRestoreLastActionState)1096 TEST_P(LockScreenAppManagerImplTest, LaunchAppWithFalseRestoreLastActionState) {
1097   set_needs_lock_screen_event_router();
1098 
1099   profile()->GetPrefs()->SetBoolean(prefs::kRestoreLastLockScreenNote, false);
1100 
1101   scoped_refptr<const extensions::Extension> note_taking_app =
1102       AddTestAppWithLockScreenSupport(
1103           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
1104           true /* enable_on_lock_screen */);
1105 
1106   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
1107   RunExtensionServiceTaskRunner(LockScreenProfile());
1108 
1109   ASSERT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
1110             app_manager()->GetNoteTakingAppId());
1111 
1112   event_observer()->set_expect_restore_action_state(false);
1113   EXPECT_TRUE(app_manager()->LaunchNoteTaking());
1114 
1115   ASSERT_EQ(1u, event_observer()->launched_apps().size());
1116   EXPECT_EQ(chromeos::NoteTakingHelper::kProdKeepExtensionId,
1117             event_observer()->launched_apps()[0]);
1118   event_observer()->ClearLaunchedApps();
1119 
1120   app_manager()->Stop();
1121 
1122   EXPECT_FALSE(app_manager()->LaunchNoteTaking());
1123   EXPECT_TRUE(event_observer()->launched_apps().empty());
1124 }
1125 
TEST_P(LockScreenAppManagerImplTest,LaunchAppWhenNoLockScreenApp)1126 TEST_P(LockScreenAppManagerImplTest, LaunchAppWhenNoLockScreenApp) {
1127   set_needs_lock_screen_event_router();
1128 
1129   scoped_refptr<const extensions::Extension> note_taking_app =
1130       AddTestAppWithLockScreenSupport(
1131           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
1132           false /* enable_on_lock_screen */);
1133 
1134   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
1135   RunExtensionServiceTaskRunner(LockScreenProfile());
1136 
1137   EXPECT_FALSE(app_manager()->LaunchNoteTaking());
1138   EXPECT_TRUE(event_observer()->launched_apps().empty());
1139 
1140   app_manager()->Stop();
1141   EXPECT_FALSE(app_manager()->LaunchNoteTaking());
1142   EXPECT_TRUE(event_observer()->launched_apps().empty());
1143 }
1144 
TEST_P(LockScreenAppManagerImplTest,InitializedAfterLockScreenProfileCreated)1145 TEST_P(LockScreenAppManagerImplTest, InitializedAfterLockScreenProfileCreated) {
1146   scoped_refptr<const extensions::Extension> note_taking_app =
1147       AddTestAppWithLockScreenSupport(
1148           profile(), chromeos::NoteTakingHelper::kDevKeepExtensionId, "1.0",
1149           true /* enable_on_lock_screen */);
1150 
1151   CreateLockScreenProfile();
1152 
1153   InitializeAndStartAppManager(profile(), false /*create_lock_screen_profile*/);
1154 
1155   EXPECT_EQ(0, note_taking_changed_count());
1156   EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
1157 
1158   RunExtensionServiceTaskRunner(LockScreenProfile());
1159 
1160   EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
1161   ResetNoteTakingChangedCount();
1162 
1163   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
1164   EXPECT_EQ(chromeos::NoteTakingHelper::kDevKeepExtensionId,
1165             app_manager()->GetNoteTakingAppId());
1166 
1167   const extensions::Extension* lock_app =
1168       extensions::ExtensionRegistry::Get(LockScreenProfile())
1169           ->GetExtensionById(chromeos::NoteTakingHelper::kDevKeepExtensionId,
1170                              extensions::ExtensionRegistry::ENABLED);
1171   ASSERT_TRUE(lock_app);
1172 
1173   EXPECT_TRUE(base::PathExists(lock_app->path()));
1174   EXPECT_EQ(GetLockScreenAppPath(note_taking_app->id(),
1175                                  note_taking_app->VersionString()),
1176             lock_app->path());
1177   EXPECT_TRUE(base::PathExists(note_taking_app->path()));
1178 
1179   app_manager()->Stop();
1180 }
1181 
TEST_P(LockScreenAppManagerImplTest,StartedBeforeLockScreenProfileCreated)1182 TEST_P(LockScreenAppManagerImplTest, StartedBeforeLockScreenProfileCreated) {
1183   scoped_refptr<const extensions::Extension> note_taking_app =
1184       AddTestAppWithLockScreenSupport(
1185           profile(), chromeos::NoteTakingHelper::kDevKeepExtensionId, "1.0",
1186           true /* enable_on_lock_screen */);
1187 
1188   InitializeAndStartAppManager(profile(), false /*create_lock_screen_profile*/);
1189 
1190   EXPECT_EQ(0, note_taking_changed_count());
1191   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
1192   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
1193 
1194   CreateLockScreenProfile();
1195 
1196   EXPECT_EQ(1, note_taking_changed_count());
1197   ResetNoteTakingChangedCount();
1198   EXPECT_EQ(!IsInstallAsync(), app_manager()->IsNoteTakingAppAvailable());
1199 
1200   RunExtensionServiceTaskRunner(LockScreenProfile());
1201 
1202   EXPECT_EQ(NoteTakingChangedCountOnStart(), note_taking_changed_count());
1203   ResetNoteTakingChangedCount();
1204 
1205   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
1206   EXPECT_EQ(chromeos::NoteTakingHelper::kDevKeepExtensionId,
1207             app_manager()->GetNoteTakingAppId());
1208 
1209   const extensions::Extension* lock_app =
1210       extensions::ExtensionRegistry::Get(LockScreenProfile())
1211           ->GetExtensionById(chromeos::NoteTakingHelper::kDevKeepExtensionId,
1212                              extensions::ExtensionRegistry::ENABLED);
1213   ASSERT_TRUE(lock_app);
1214 
1215   EXPECT_TRUE(base::PathExists(lock_app->path()));
1216   EXPECT_EQ(GetLockScreenAppPath(note_taking_app->id(),
1217                                  note_taking_app->VersionString()),
1218             lock_app->path());
1219   EXPECT_TRUE(base::PathExists(note_taking_app->path()));
1220 
1221   app_manager()->Stop();
1222 }
1223 
TEST_P(LockScreenAppManagerImplTest,LockScreenProfileCreatedNoSupportedApp)1224 TEST_P(LockScreenAppManagerImplTest, LockScreenProfileCreatedNoSupportedApp) {
1225   scoped_refptr<const extensions::Extension> note_taking_app =
1226       AddTestAppWithLockScreenSupport(
1227           profile(), chromeos::NoteTakingHelper::kDevKeepExtensionId, "1.0",
1228           false /* enable_on_lock_screen */);
1229 
1230   InitializeAndStartAppManager(profile(), false /*create_lock_screen_profile*/);
1231 
1232   EXPECT_EQ(0, note_taking_changed_count());
1233   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
1234   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
1235 
1236   CreateLockScreenProfile();
1237   RunExtensionServiceTaskRunner(LockScreenProfile());
1238 
1239   EXPECT_EQ(0, note_taking_changed_count());
1240   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
1241   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
1242 
1243   app_manager()->Stop();
1244 }
1245 
TEST_P(LockScreenAppManagerImplTest,LockScreenProfileCreationFailure)1246 TEST_P(LockScreenAppManagerImplTest, LockScreenProfileCreationFailure) {
1247   scoped_refptr<const extensions::Extension> note_taking_app =
1248       AddTestAppWithLockScreenSupport(
1249           profile(), chromeos::NoteTakingHelper::kDevKeepExtensionId, "1.0",
1250           true /* enable_on_lock_screen */);
1251 
1252   InitializeAndStartAppManager(profile(), false /*create_lock_screen_profile*/);
1253 
1254   EXPECT_EQ(0, note_taking_changed_count());
1255   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
1256   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
1257 
1258   lock_screen_profile_creator()->SetProfileCreationFailed();
1259 
1260   EXPECT_EQ(0, note_taking_changed_count());
1261   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
1262   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
1263 }
1264 
TEST_P(LockScreenAppManagerImplTest,LockScreenProfileCreationFailedBeforeInitialization)1265 TEST_P(LockScreenAppManagerImplTest,
1266        LockScreenProfileCreationFailedBeforeInitialization) {
1267   lock_screen_profile_creator()->SetProfileCreationFailed();
1268 
1269   scoped_refptr<const extensions::Extension> note_taking_app =
1270       AddTestAppWithLockScreenSupport(
1271           profile(), chromeos::NoteTakingHelper::kDevKeepExtensionId, "1.0",
1272           true /* enable_on_lock_screen */);
1273 
1274   InitializeAndStartAppManager(profile(), false /*create_lock_screen_profile*/);
1275 
1276   EXPECT_EQ(0, note_taking_changed_count());
1277   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
1278   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
1279 
1280   EXPECT_EQ(0, note_taking_changed_count());
1281   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
1282   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
1283 }
1284 
TEST_P(LockScreenAppManagerImplTest,ReloadLockScreenAppAfterAppCrash)1285 TEST_P(LockScreenAppManagerImplTest, ReloadLockScreenAppAfterAppCrash) {
1286   set_needs_lock_screen_event_router();
1287 
1288   scoped_refptr<const extensions::Extension> note_taking_app =
1289       AddTestAppWithLockScreenSupport(
1290           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
1291           true /* enable_on_lock_screen */);
1292 
1293   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
1294   RunExtensionServiceTaskRunner(LockScreenProfile());
1295   ResetNoteTakingChangedCount();
1296 
1297   // Simulate lock screen note app crash.
1298   extensions::ExtensionSystem::Get(LockScreenProfile())
1299       ->extension_service()
1300       ->TerminateExtension(note_taking_app->id());
1301 
1302   // Even though the app was terminated, the observers should not see any state
1303   // change - the app should be reloaded when launch is requested next time.
1304   EXPECT_EQ(0, note_taking_changed_count());
1305   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
1306   EXPECT_EQ(note_taking_app->id(), app_manager()->GetNoteTakingAppId());
1307 
1308   // App launch should be successful - this action should reload the
1309   // terminated app.
1310   EXPECT_TRUE(app_manager()->LaunchNoteTaking());
1311 
1312   // Verify the lock screen note app is enabled.
1313   const extensions::Extension* lock_app =
1314       extensions::ExtensionRegistry::Get(LockScreenProfile())
1315           ->GetExtensionById(note_taking_app->id(),
1316                              extensions::ExtensionRegistry::ENABLED);
1317   ASSERT_TRUE(lock_app);
1318   EXPECT_EQ("1.0", lock_app->VersionString());
1319 
1320   // Verify the lock screen app was sent launch event.
1321   ASSERT_EQ(1u, event_observer()->launched_apps().size());
1322   EXPECT_EQ(lock_app->id(), event_observer()->launched_apps()[0]);
1323   event_observer()->ClearLaunchedApps();
1324 }
1325 
TEST_P(LockScreenAppManagerImplTest,AppReloadFailure)1326 TEST_P(LockScreenAppManagerImplTest, AppReloadFailure) {
1327   set_needs_lock_screen_event_router();
1328 
1329   scoped_refptr<const extensions::Extension> note_taking_app =
1330       AddTestAppWithLockScreenSupport(
1331           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
1332           true /* enable_on_lock_screen */);
1333 
1334   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
1335   RunExtensionServiceTaskRunner(LockScreenProfile());
1336   ResetNoteTakingChangedCount();
1337 
1338   // Simulate lock screen note app crash.
1339   extensions::ExtensionSystem::Get(LockScreenProfile())
1340       ->extension_service()
1341       ->TerminateExtension(note_taking_app->id());
1342 
1343   // Even though the app was terminated, the observers should not see any state
1344   // change - the app should be reloaded when launch is requested next time.
1345   EXPECT_EQ(0, note_taking_changed_count());
1346   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
1347   EXPECT_EQ(note_taking_app->id(), app_manager()->GetNoteTakingAppId());
1348 
1349   // Disable the note taking app in the lock screen app profile - this should
1350   // prevent app reload.
1351   extensions::ExtensionSystem::Get(LockScreenProfile())
1352       ->extension_service()
1353       ->DisableExtension(note_taking_app->id(),
1354                          extensions::disable_reason::DISABLE_USER_ACTION);
1355 
1356   // App launch should fail - given that the app got disabled, it should not
1357   // be reloadable anymore.
1358   EXPECT_FALSE(app_manager()->LaunchNoteTaking());
1359 
1360   // Make sure that note taking is not reported as available any longer.
1361   EXPECT_EQ(1, note_taking_changed_count());
1362   ResetNoteTakingChangedCount();
1363   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
1364 }
1365 
TEST_P(LockScreenAppManagerImplTest,LockScreenAppGetsUninstalled)1366 TEST_P(LockScreenAppManagerImplTest, LockScreenAppGetsUninstalled) {
1367   set_needs_lock_screen_event_router();
1368 
1369   scoped_refptr<const extensions::Extension> note_taking_app =
1370       AddTestAppWithLockScreenSupport(
1371           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
1372           true /* enable_on_lock_screen */);
1373 
1374   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
1375   RunExtensionServiceTaskRunner(LockScreenProfile());
1376   ResetNoteTakingChangedCount();
1377 
1378   // Disable the note taking app in the lock screen app profile.
1379   extensions::ExtensionSystem::Get(LockScreenProfile())
1380       ->extension_service()
1381       ->UninstallExtension(note_taking_app->id(),
1382                            extensions::UNINSTALL_REASON_FOR_TESTING, nullptr);
1383 
1384   // Note taking should be reported to be unavailable if the app was uninstalled
1385   // from the lock screen profile.
1386   EXPECT_EQ(1, note_taking_changed_count());
1387   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
1388 }
1389 
TEST_P(LockScreenAppManagerImplTest,TerminatedAppGetsUninstalled)1390 TEST_P(LockScreenAppManagerImplTest, TerminatedAppGetsUninstalled) {
1391   set_needs_lock_screen_event_router();
1392 
1393   scoped_refptr<const extensions::Extension> note_taking_app =
1394       AddTestAppWithLockScreenSupport(
1395           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
1396           true /* enable_on_lock_screen */);
1397 
1398   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
1399   RunExtensionServiceTaskRunner(LockScreenProfile());
1400   ResetNoteTakingChangedCount();
1401 
1402   // Simulate lock screen note app crash.
1403   extensions::ExtensionSystem::Get(LockScreenProfile())
1404       ->extension_service()
1405       ->TerminateExtension(note_taking_app->id());
1406 
1407   // Even though the app was terminated, the observers should not see any state
1408   // change - the app should be reloaded when launch is requested next time.
1409   EXPECT_EQ(0, note_taking_changed_count());
1410   EXPECT_EQ(note_taking_app->id(), app_manager()->GetNoteTakingAppId());
1411 
1412   // Disable the note taking app in the lock screen app profile - this should
1413   // prevent app reload.
1414   extensions::ExtensionSystem::Get(LockScreenProfile())
1415       ->extension_service()
1416       ->UninstallExtension(note_taking_app->id(),
1417                            extensions::UNINSTALL_REASON_FOR_TESTING, nullptr);
1418 
1419   // Note taking should be reported to be unavailable if the app was uninstalled
1420   // from the lock screen profile.
1421   EXPECT_EQ(1, note_taking_changed_count());
1422   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
1423 }
1424 
TEST_P(LockScreenAppManagerImplTest,DoNotReloadLockScreenAppWhenDisabled)1425 TEST_P(LockScreenAppManagerImplTest, DoNotReloadLockScreenAppWhenDisabled) {
1426   set_needs_lock_screen_event_router();
1427 
1428   scoped_refptr<const extensions::Extension> note_taking_app =
1429       AddTestAppWithLockScreenSupport(
1430           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
1431           true /* enable_on_lock_screen */);
1432 
1433   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
1434   RunExtensionServiceTaskRunner(LockScreenProfile());
1435   ResetNoteTakingChangedCount();
1436 
1437   // Disable the lock screen app..
1438   extensions::ExtensionSystem::Get(LockScreenProfile())
1439       ->extension_service()
1440       ->DisableExtension(note_taking_app->id(),
1441                          extensions::disable_reason::DISABLE_USER_ACTION);
1442 
1443   EXPECT_EQ(1, note_taking_changed_count());
1444   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
1445   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
1446   EXPECT_FALSE(app_manager()->LaunchNoteTaking());
1447   EXPECT_FALSE(
1448       extensions::ExtensionRegistry::Get(LockScreenProfile())
1449           ->GetExtensionById(note_taking_app->id(),
1450                              extensions::ExtensionRegistry::EVERYTHING));
1451 
1452   app_manager()->Stop();
1453 }
1454 
TEST_P(LockScreenAppManagerImplTest,RestartingAppManagerAfterLockScreenAppDisabled)1455 TEST_P(LockScreenAppManagerImplTest,
1456        RestartingAppManagerAfterLockScreenAppDisabled) {
1457   set_needs_lock_screen_event_router();
1458 
1459   scoped_refptr<const extensions::Extension> note_taking_app =
1460       AddTestAppWithLockScreenSupport(
1461           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
1462           true /* enable_on_lock_screen */);
1463 
1464   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
1465   RunExtensionServiceTaskRunner(LockScreenProfile());
1466   ResetNoteTakingChangedCount();
1467 
1468   // Disable the lock screen app..
1469   extensions::ExtensionSystem::Get(LockScreenProfile())
1470       ->extension_service()
1471       ->DisableExtension(note_taking_app->id(),
1472                          extensions::disable_reason::DISABLE_USER_ACTION);
1473 
1474   EXPECT_EQ(1, note_taking_changed_count());
1475   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
1476 
1477   // Restarting the app manager should enable lock screen app again.
1478   RestartLockScreenAppManager();
1479   RunExtensionServiceTaskRunner(LockScreenProfile());
1480 
1481   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
1482   EXPECT_EQ(note_taking_app->id(), app_manager()->GetNoteTakingAppId());
1483   EXPECT_TRUE(app_manager()->LaunchNoteTaking());
1484 
1485   // Verify the lock screen app was sent launch event.
1486   ASSERT_EQ(1u, event_observer()->launched_apps().size());
1487   EXPECT_EQ(note_taking_app->id(), event_observer()->launched_apps()[0]);
1488 }
1489 
TEST_P(LockScreenAppManagerImplTest,AppNotReloadedAfterRepeatedCrashes)1490 TEST_P(LockScreenAppManagerImplTest, AppNotReloadedAfterRepeatedCrashes) {
1491   set_needs_lock_screen_event_router();
1492 
1493   scoped_refptr<const extensions::Extension> note_taking_app =
1494       AddTestAppWithLockScreenSupport(
1495           profile(), chromeos::NoteTakingHelper::kProdKeepExtensionId, "1.0",
1496           true /* enable_on_lock_screen */);
1497 
1498   InitializeAndStartAppManager(profile(), true /*create_lock_screen_profile*/);
1499   RunExtensionServiceTaskRunner(LockScreenProfile());
1500   ResetNoteTakingChangedCount();
1501 
1502   // Simulate lock screen note app crash and launch few times.
1503   for (int i = 0; i < kMaxLockScreenAppReloadsCount; ++i) {
1504     extensions::ExtensionSystem::Get(LockScreenProfile())
1505         ->extension_service()
1506         ->TerminateExtension(note_taking_app->id());
1507     EXPECT_TRUE(app_manager()->LaunchNoteTaking());
1508   }
1509 
1510   // If app is reloaded too many times, lock screen app should eventually
1511   // become unavailable.
1512   extensions::ExtensionSystem::Get(LockScreenProfile())
1513       ->extension_service()
1514       ->TerminateExtension(note_taking_app->id());
1515 
1516   EXPECT_EQ(1, note_taking_changed_count());
1517   EXPECT_FALSE(app_manager()->IsNoteTakingAppAvailable());
1518   EXPECT_TRUE(app_manager()->GetNoteTakingAppId().empty());
1519   EXPECT_FALSE(app_manager()->LaunchNoteTaking());
1520   EXPECT_FALSE(extensions::ExtensionRegistry::Get(LockScreenProfile())
1521                    ->GetExtensionById(note_taking_app->id(),
1522                                       extensions::ExtensionRegistry::ENABLED));
1523   event_observer()->ClearLaunchedApps();
1524 
1525   // Restarting the app manager should enable lock screen app again.
1526   RestartLockScreenAppManager();
1527   RunExtensionServiceTaskRunner(LockScreenProfile());
1528 
1529   EXPECT_TRUE(app_manager()->IsNoteTakingAppAvailable());
1530   EXPECT_EQ(note_taking_app->id(), app_manager()->GetNoteTakingAppId());
1531   EXPECT_TRUE(app_manager()->LaunchNoteTaking());
1532 
1533   // Verify the lock screen app was sent launch event.
1534   ASSERT_EQ(1u, event_observer()->launched_apps().size());
1535   EXPECT_EQ(note_taking_app->id(), event_observer()->launched_apps()[0]);
1536 }
1537 
1538 }  // namespace lock_screen_apps
1539