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

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

Issue 2934513003: Changes in app.window and app.runtime to support lock screen note taking (Closed)
Patch Set: . Created 3 years, 6 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
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/lock_screen_apps/state_controller.h" 5 #include "chrome/browser/chromeos/lock_screen_apps/state_controller.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "ash/public/interfaces/tray_action.mojom.h" 12 #include "ash/public/interfaces/tray_action.mojom.h"
13 #include "base/files/file_path.h" 13 #include "base/files/file_path.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/run_loop.h"
16 #include "base/test/scoped_command_line.h" 15 #include "base/test/scoped_command_line.h"
17 #include "chrome/browser/chromeos/lock_screen_apps/app_manager.h" 16 #include "chrome/browser/chromeos/lock_screen_apps/app_manager.h"
18 #include "chrome/browser/chromeos/lock_screen_apps/state_observer.h" 17 #include "chrome/browser/chromeos/lock_screen_apps/state_observer.h"
18 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/extensions/test_extension_system.h"
20 #include "chrome/browser/ui/apps/chrome_app_delegate.h"
19 #include "chrome/common/chrome_constants.h" 21 #include "chrome/common/chrome_constants.h"
22 #include "chrome/test/base/browser_with_test_window_test.h"
20 #include "chrome/test/base/testing_browser_process.h" 23 #include "chrome/test/base/testing_browser_process.h"
21 #include "chrome/test/base/testing_profile.h" 24 #include "chrome/test/base/testing_profile.h"
22 #include "chrome/test/base/testing_profile_manager.h" 25 #include "chrome/test/base/testing_profile_manager.h"
23 #include "components/session_manager/core/session_manager.h" 26 #include "components/session_manager/core/session_manager.h"
24 #include "content/public/test/test_browser_thread_bundle.h" 27 #include "content/public/browser/web_contents.h"
28 #include "content/public/browser/web_contents_observer.h"
29 #include "content/public/test/test_utils.h"
30 #include "content/public/test/web_contents_tester.h"
31 #include "extensions/browser/app_window/app_window.h"
32 #include "extensions/browser/app_window/app_window_contents.h"
33 #include "extensions/browser/app_window/native_app_window.h"
34 #include "extensions/common/api/app_runtime.h"
35 #include "extensions/common/extension_builder.h"
36 #include "extensions/common/value_builder.h"
25 #include "testing/gtest/include/gtest/gtest.h" 37 #include "testing/gtest/include/gtest/gtest.h"
26 38
27 using ash::mojom::TrayActionState; 39 using ash::mojom::TrayActionState;
40 using extensions::DictionaryBuilder;
41 using extensions::ListBuilder;
28 42
29 namespace { 43 namespace {
30 44
45 // App IDs used for test apps.
46 const char kTestAppId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
47 const char kSecondaryTestAppId[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
48
49 scoped_refptr<extensions::Extension> CreateTestNoteTakingApp(
50 const std::string& app_id) {
51 ListBuilder action_handlers;
52 action_handlers.Append(DictionaryBuilder()
53 .Set("action", "new_note")
54 .SetBoolean("enabled_on_lock_screen", true)
55 .Build());
56 DictionaryBuilder background;
57 background.Set("scripts", ListBuilder().Append("background.js").Build());
58 return extensions::ExtensionBuilder()
59 .SetManifest(DictionaryBuilder()
60 .Set("name", "Test App")
61 .Set("version", "1.0")
62 .Set("manifest_version", 2)
63 .Set("app", DictionaryBuilder()
64 .Set("background", background.Build())
65 .Build())
66 .Set("action_handlers", action_handlers.Build())
67 .Build())
68 .SetID(app_id)
69 .Build();
70 }
71
31 class TestAppManager : public lock_screen_apps::AppManager { 72 class TestAppManager : public lock_screen_apps::AppManager {
32 public: 73 public:
33 enum class State { 74 enum class State {
34 kNotInitialized, 75 kNotInitialized,
35 kStarted, 76 kStarted,
36 kStopped, 77 kStopped,
37 }; 78 };
38 79
39 TestAppManager(Profile* expected_primary_profile, 80 TestAppManager(Profile* expected_primary_profile,
40 Profile* expected_lock_screen_profile) 81 Profile* expected_lock_screen_profile)
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 219
179 private: 220 private:
180 mojo::Binding<ash::mojom::TrayAction> binding_; 221 mojo::Binding<ash::mojom::TrayAction> binding_;
181 ash::mojom::TrayActionClientPtr client_; 222 ash::mojom::TrayActionClientPtr client_;
182 223
183 std::vector<TrayActionState> observed_states_; 224 std::vector<TrayActionState> observed_states_;
184 225
185 DISALLOW_COPY_AND_ASSIGN(TestTrayAction); 226 DISALLOW_COPY_AND_ASSIGN(TestTrayAction);
186 }; 227 };
187 228
229 // Wrapper around AppWindow used to manage the app window lifetime, and provide
230 // means to initialize/close the window,
231 class TestAppWindow : public content::WebContentsObserver {
232 public:
233 TestAppWindow(Profile* profile, extensions::AppWindow* window)
234 : web_contents_(
235 content::WebContentsTester::CreateTestWebContents(profile,
236 nullptr)),
237 window_(window) {}
238
239 ~TestAppWindow() override {
240 // Make sure the window is initialized, so |window_| does not get leaked.
241 if (!initialized_ && window_)
242 Initialize(false /* shown */);
243
244 Close();
245 }
246
247 void Initialize(bool shown) {
248 ASSERT_FALSE(initialized_);
249 ASSERT_TRUE(window_);
250 initialized_ = true;
251
252 extensions::AppWindow::CreateParams params;
253 params.hidden = !shown;
254 window_->Init(GURL(), new extensions::AppWindowContentsImpl(window_),
255 web_contents_->GetMainFrame(), params);
256 Observe(window_->web_contents());
257 }
258
259 void Close() {
260 if (!window_)
261 return;
262
263 if (!initialized_)
264 return;
265
266 content::WebContentsDestroyedWatcher destroyed_watcher(
267 window_->web_contents());
268 window_->GetBaseWindow()->Close();
269 destroyed_watcher.Wait();
270
271 EXPECT_FALSE(window_);
272 EXPECT_TRUE(closed_);
273 }
274
275 void WebContentsDestroyed() override {
276 closed_ = true;
277 window_ = nullptr;
278 }
279
280 extensions::AppWindow* window() { return window_; }
281
282 bool closed() const { return closed_; }
283
284 private:
285 std::unique_ptr<content::WebContents> web_contents_;
286 extensions::AppWindow* window_;
287 bool closed_ = false;
288 bool initialized_ = false;
289
290 DISALLOW_COPY_AND_ASSIGN(TestAppWindow);
291 };
292
188 class LockScreenAppStateNotSupportedTest : public testing::Test { 293 class LockScreenAppStateNotSupportedTest : public testing::Test {
189 public: 294 public:
190 LockScreenAppStateNotSupportedTest() = default; 295 LockScreenAppStateNotSupportedTest() = default;
191 296
192 ~LockScreenAppStateNotSupportedTest() override = default; 297 ~LockScreenAppStateNotSupportedTest() override = default;
193 298
194 void SetUp() override { 299 void SetUp() override {
195 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); 300 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>();
196 command_line_->GetProcessCommandLine()->InitFromArgv({""}); 301 command_line_->GetProcessCommandLine()->InitFromArgv({""});
197 } 302 }
198 303
199 private: 304 private:
200 std::unique_ptr<base::test::ScopedCommandLine> command_line_; 305 std::unique_ptr<base::test::ScopedCommandLine> command_line_;
201 306
202 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateNotSupportedTest); 307 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateNotSupportedTest);
203 }; 308 };
204 309
205 class LockScreenAppStateTest : public testing::Test { 310 class LockScreenAppStateTest : public BrowserWithTestWindowTest {
206 public: 311 public:
207 LockScreenAppStateTest() 312 LockScreenAppStateTest()
208 : profile_manager_(TestingBrowserProcess::GetGlobal()) {} 313 : profile_manager_(TestingBrowserProcess::GetGlobal()) {}
209 ~LockScreenAppStateTest() override = default; 314 ~LockScreenAppStateTest() override = default;
210 315
211 void SetUp() override { 316 void SetUp() override {
212 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); 317 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>();
213 command_line_->GetProcessCommandLine()->InitFromArgv( 318 command_line_->GetProcessCommandLine()->InitFromArgv(
214 {"", "--enable-lock-screen-apps"}); 319 {"", "--enable-lock-screen-apps"});
215 320
216 ASSERT_TRUE(profile_manager_.SetUp()); 321 ASSERT_TRUE(profile_manager_.SetUp());
217 322
323 BrowserWithTestWindowTest::SetUp();
324
218 session_manager_ = base::MakeUnique<session_manager::SessionManager>(); 325 session_manager_ = base::MakeUnique<session_manager::SessionManager>();
219 session_manager_->SetSessionState( 326 session_manager_->SetSessionState(
220 session_manager::SessionState::LOGIN_PRIMARY); 327 session_manager::SessionState::LOGIN_PRIMARY);
221 328
222 ASSERT_TRUE(lock_screen_apps::StateController::IsEnabled()); 329 ASSERT_TRUE(lock_screen_apps::StateController::IsEnabled());
223 330
224 state_controller_ = base::MakeUnique<lock_screen_apps::StateController>(); 331 state_controller_ = base::MakeUnique<lock_screen_apps::StateController>();
225 state_controller_->SetTrayActionPtrForTesting( 332 state_controller_->SetTrayActionPtrForTesting(
226 tray_action_.CreateInterfacePtrAndBind()); 333 tray_action_.CreateInterfacePtrAndBind());
227 state_controller_->Initialize(); 334 state_controller_->Initialize();
228 state_controller_->FlushTrayActionForTesting(); 335 state_controller_->FlushTrayActionForTesting();
229 336
230 state_controller_->AddObserver(&observer_); 337 state_controller_->AddObserver(&observer_);
231 338
232 // Create fake sign-in profile. 339 // Create fake sign-in profile.
233 TestingProfile::Builder builder; 340 TestingProfile::Builder builder;
234 builder.SetPath( 341 builder.SetPath(
235 profile_manager_.profiles_dir().AppendASCII(chrome::kInitialProfile)); 342 profile_manager_.profiles_dir().AppendASCII(chrome::kInitialProfile));
236 TestingProfile* signin_profile = builder.BuildIncognito( 343 signin_profile_ = builder.BuildIncognito(
237 profile_manager_.CreateTestingProfile(chrome::kInitialProfile)); 344 profile_manager_.CreateTestingProfile(chrome::kInitialProfile));
238 345
346 InitExtensionSystem(profile());
347 InitExtensionSystem(signin_profile());
348
239 std::unique_ptr<TestAppManager> app_manager = 349 std::unique_ptr<TestAppManager> app_manager =
240 base::MakeUnique<TestAppManager>(&profile_, 350 base::MakeUnique<TestAppManager>(&profile_,
241 signin_profile->GetOriginalProfile()); 351 signin_profile_->GetOriginalProfile());
242 app_manager_ = app_manager.get(); 352 app_manager_ = app_manager.get();
243 state_controller_->SetAppManagerForTesting(std::move(app_manager)); 353 state_controller_->SetAppManagerForTesting(std::move(app_manager));
244 } 354 }
245 355
246 void TearDown() override { 356 void TearDown() override {
357 extensions::ExtensionSystem::Get(profile())->Shutdown();
358 extensions::ExtensionSystem::Get(signin_profile())->Shutdown();
359
247 state_controller_->RemoveObserver(&observer_); 360 state_controller_->RemoveObserver(&observer_);
248 state_controller_.reset(); 361 state_controller_.reset();
249 session_manager_.reset(); 362 session_manager_.reset();
250 app_manager_ = nullptr; 363 app_manager_ = nullptr;
364 BrowserWithTestWindowTest::TearDown();
251 } 365 }
252 366
253 // Helper method to move state controller to available state. 367 void InitExtensionSystem(Profile* profile) {
368 extensions::TestExtensionSystem* extension_system =
369 static_cast<extensions::TestExtensionSystem*>(
370 extensions::ExtensionSystem::Get(profile));
371 extension_system->CreateExtensionService(
372 base::CommandLine::ForCurrentProcess(),
373 base::FilePath() /* install_directory */,
374 false /* autoupdate_enabled */);
375 }
376
377 void ExpectObservedStatesMatch(const std::vector<TrayActionState>& states,
378 const std::string& message) {
379 state_controller_->FlushTrayActionForTesting();
380 EXPECT_EQ(states, observer()->observed_states()) << message;
381 EXPECT_EQ(states, tray_action()->observed_states()) << message;
382 }
383
384 // Helper method to create and register an app window for lock screen note
385 // taking action with the state controller.
386 // Note that app window creation may fail if the app is not allowed to create
387 // the app window for the action - in that case returned |TestAppWindow| will
388 // have null |window| (rather than being null itself).
389 std::unique_ptr<TestAppWindow> CreateNoteTakingWindow(
390 Profile* profile,
391 const extensions::Extension* extension) {
392 return base::MakeUnique<TestAppWindow>(
393 profile, state_controller()->CreateAppWindowForLockScreenAction(
394 profile, extension,
395 extensions::api::app_runtime::ACTION_TYPE_NEW_NOTE,
396 base::MakeUnique<ChromeAppDelegate>(true)));
397 }
398
399 void ClearObservedStates() {
400 state_controller_->FlushTrayActionForTesting();
401 observer_.ClearObservedStates();
402 tray_action_.ClearObservedStates();
403 }
404
405 // Helper method to move state controller to the specified state.
254 // Should be called at the begining of tests, at most once. 406 // Should be called at the begining of tests, at most once.
255 bool EnableNoteTakingApp(bool enable_app_launch) { 407 bool InitializeNoteTakingApp(TrayActionState target_state,
256 app_manager_->SetInitialAppState("test_app_id", enable_app_launch); 408 bool enable_app_launch) {
409 app_ = CreateTestNoteTakingApp(kTestAppId);
410 extensions::ExtensionSystem::Get(signin_profile())
411 ->extension_service()
412 ->AddExtension(app_.get());
413
414 app_manager_->SetInitialAppState(kTestAppId, enable_app_launch);
257 state_controller_->SetPrimaryProfile(&profile_); 415 state_controller_->SetPrimaryProfile(&profile_);
416
417 if (target_state == TrayActionState::kNotAvailable)
418 return true;
419
258 session_manager_->SetSessionState(session_manager::SessionState::LOCKED); 420 session_manager_->SetSessionState(session_manager::SessionState::LOCKED);
259 if (app_manager_->state() != TestAppManager::State::kStarted) { 421 if (app_manager_->state() != TestAppManager::State::kStarted) {
260 ADD_FAILURE() << "Lock app manager Start not invoked."; 422 ADD_FAILURE() << "Lock app manager Start not invoked.";
261 return false; 423 return false;
262 } 424 }
425
426 ClearObservedStates();
427
428 if (state_controller_->GetLockScreenNoteState() !=
429 TrayActionState::kAvailable) {
430 ADD_FAILURE() << "Unable to move to available state.";
431 return false;
432 }
433 if (target_state == TrayActionState::kAvailable)
434 return true;
435
436 tray_action()->SendNewNoteRequest();
263 state_controller_->FlushTrayActionForTesting(); 437 state_controller_->FlushTrayActionForTesting();
264 438
265 observer_.ClearObservedStates(); 439 ClearObservedStates();
266 tray_action_.ClearObservedStates();
267 440
268 return state_controller_->GetLockScreenNoteState() == 441 if (state_controller_->GetLockScreenNoteState() !=
269 TrayActionState::kAvailable; 442 TrayActionState::kLaunching) {
443 ADD_FAILURE() << "Unable to move to launching state.";
444 return false;
445 }
446 if (target_state == TrayActionState::kLaunching)
447 return true;
448
449 app_window_ = CreateNoteTakingWindow(signin_profile(), app());
450 if (!app_window_->window()) {
451 ADD_FAILURE() << "Not allowed to create app window.";
452 return false;
453 }
454
455 app_window()->Initialize(true /* shown */);
456
457 ClearObservedStates();
458
459 return state_controller()->GetLockScreenNoteState() ==
460 TrayActionState::kActive;
270 } 461 }
271 462
272 TestingProfile* profile() { return &profile_; } 463 TestingProfile* profile() { return &profile_; }
464 TestingProfile* signin_profile() { return signin_profile_; }
273 465
274 session_manager::SessionManager* session_manager() { 466 session_manager::SessionManager* session_manager() {
275 return session_manager_.get(); 467 return session_manager_.get();
276 } 468 }
277 469
278 TestStateObserver* observer() { return &observer_; } 470 TestStateObserver* observer() { return &observer_; }
279 471
280 TestTrayAction* tray_action() { return &tray_action_; } 472 TestTrayAction* tray_action() { return &tray_action_; }
281 473
282 lock_screen_apps::StateController* state_controller() { 474 lock_screen_apps::StateController* state_controller() {
283 return state_controller_.get(); 475 return state_controller_.get();
284 } 476 }
285 477
286 TestAppManager* app_manager() { return app_manager_; } 478 TestAppManager* app_manager() { return app_manager_; }
287 479
480 TestAppWindow* app_window() { return app_window_.get(); }
481 const extensions::Extension* app() { return app_.get(); }
482
288 private: 483 private:
289 std::unique_ptr<base::test::ScopedCommandLine> command_line_; 484 std::unique_ptr<base::test::ScopedCommandLine> command_line_;
290 content::TestBrowserThreadBundle threads_;
291 TestingProfileManager profile_manager_; 485 TestingProfileManager profile_manager_;
292 TestingProfile profile_; 486 TestingProfile profile_;
487 TestingProfile* signin_profile_ = nullptr;
293 488
294 std::unique_ptr<session_manager::SessionManager> session_manager_; 489 std::unique_ptr<session_manager::SessionManager> session_manager_;
295 490
296 std::unique_ptr<lock_screen_apps::StateController> state_controller_; 491 std::unique_ptr<lock_screen_apps::StateController> state_controller_;
297 492
298 TestStateObserver observer_; 493 TestStateObserver observer_;
299 TestTrayAction tray_action_; 494 TestTrayAction tray_action_;
300 TestAppManager* app_manager_ = nullptr; 495 TestAppManager* app_manager_ = nullptr;
301 496
497 std::unique_ptr<TestAppWindow> app_window_;
498 scoped_refptr<const extensions::Extension> app_;
499
302 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateTest); 500 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateTest);
303 }; 501 };
304 502
305 } // namespace 503 } // namespace
306 504
307 TEST_F(LockScreenAppStateNotSupportedTest, NoInstance) { 505 TEST_F(LockScreenAppStateNotSupportedTest, NoInstance) {
308 EXPECT_FALSE(lock_screen_apps::StateController::IsEnabled()); 506 EXPECT_FALSE(lock_screen_apps::StateController::IsEnabled());
309 } 507 }
310 508
311 TEST_F(LockScreenAppStateTest, InitialState) { 509 TEST_F(LockScreenAppStateTest, InitialState) {
(...skipping 20 matching lines...) Expand all
332 EXPECT_EQ(0u, observer()->observed_states().size()); 530 EXPECT_EQ(0u, observer()->observed_states().size());
333 } 531 }
334 532
335 TEST_F(LockScreenAppStateTest, SetPrimaryProfileWhenSessionLocked) { 533 TEST_F(LockScreenAppStateTest, SetPrimaryProfileWhenSessionLocked) {
336 session_manager()->SetSessionState(session_manager::SessionState::LOCKED); 534 session_manager()->SetSessionState(session_manager::SessionState::LOCKED);
337 EXPECT_EQ(TrayActionState::kNotAvailable, 535 EXPECT_EQ(TrayActionState::kNotAvailable,
338 state_controller()->GetLockScreenNoteState()); 536 state_controller()->GetLockScreenNoteState());
339 537
340 EXPECT_EQ(TestAppManager::State::kNotInitialized, app_manager()->state()); 538 EXPECT_EQ(TestAppManager::State::kNotInitialized, app_manager()->state());
341 539
342 app_manager()->SetInitialAppState("app_id", true); 540 app_manager()->SetInitialAppState(kTestAppId, true);
343 state_controller()->SetPrimaryProfile(profile()); 541 state_controller()->SetPrimaryProfile(profile());
344 542
345 ASSERT_EQ(TestAppManager::State::kStarted, app_manager()->state()); 543 ASSERT_EQ(TestAppManager::State::kStarted, app_manager()->state());
346 544
347 EXPECT_EQ(TrayActionState::kAvailable, 545 EXPECT_EQ(TrayActionState::kAvailable,
348 state_controller()->GetLockScreenNoteState()); 546 state_controller()->GetLockScreenNoteState());
349 547
350 state_controller()->FlushTrayActionForTesting(); 548 ExpectObservedStatesMatch({TrayActionState::kAvailable}, "Available on lock");
351 ASSERT_EQ(1u, observer()->observed_states().size());
352 EXPECT_EQ(TrayActionState::kAvailable, observer()->observed_states()[0]);
353 ASSERT_EQ(1u, tray_action()->observed_states().size());
354 EXPECT_EQ(TrayActionState::kAvailable, tray_action()->observed_states()[0]);
355 } 549 }
356 550
357 TEST_F(LockScreenAppStateTest, SessionLock) { 551 TEST_F(LockScreenAppStateTest, SessionLock) {
358 app_manager()->SetInitialAppState("app_id", true); 552 app_manager()->SetInitialAppState(kTestAppId, true);
359 state_controller()->SetPrimaryProfile(profile()); 553 state_controller()->SetPrimaryProfile(profile());
360 ASSERT_EQ(TestAppManager::State::kStopped, app_manager()->state()); 554 ASSERT_EQ(TestAppManager::State::kStopped, app_manager()->state());
361 555
362 session_manager()->SetSessionState(session_manager::SessionState::LOCKED); 556 session_manager()->SetSessionState(session_manager::SessionState::LOCKED);
363 557
364 ASSERT_EQ(TestAppManager::State::kStarted, app_manager()->state()); 558 ASSERT_EQ(TestAppManager::State::kStarted, app_manager()->state());
365 559
366 EXPECT_EQ(TrayActionState::kAvailable, 560 EXPECT_EQ(TrayActionState::kAvailable,
367 state_controller()->GetLockScreenNoteState()); 561 state_controller()->GetLockScreenNoteState());
368 state_controller()->FlushTrayActionForTesting(); 562
369 ASSERT_EQ(1u, observer()->observed_states().size()); 563 ExpectObservedStatesMatch({TrayActionState::kAvailable}, "Available on lock");
370 EXPECT_EQ(TrayActionState::kAvailable, observer()->observed_states()[0]); 564 ClearObservedStates();
371 observer()->ClearObservedStates();
372 ASSERT_EQ(1u, tray_action()->observed_states().size());
373 EXPECT_EQ(TrayActionState::kAvailable, tray_action()->observed_states()[0]);
374 tray_action()->ClearObservedStates();
375 565
376 // When the session is unlocked again, app manager is stopped, and tray action 566 // When the session is unlocked again, app manager is stopped, and tray action
377 // disabled again. 567 // disabled again.
378 session_manager()->SetSessionState(session_manager::SessionState::ACTIVE); 568 session_manager()->SetSessionState(session_manager::SessionState::ACTIVE);
379 569
380 EXPECT_EQ(TestAppManager::State::kStopped, app_manager()->state()); 570 EXPECT_EQ(TestAppManager::State::kStopped, app_manager()->state());
381 571
382 EXPECT_EQ(TrayActionState::kNotAvailable, 572 EXPECT_EQ(TrayActionState::kNotAvailable,
383 state_controller()->GetLockScreenNoteState()); 573 state_controller()->GetLockScreenNoteState());
384 state_controller()->FlushTrayActionForTesting(); 574
385 ASSERT_EQ(1u, observer()->observed_states().size()); 575 ExpectObservedStatesMatch({TrayActionState::kNotAvailable},
386 EXPECT_EQ(TrayActionState::kNotAvailable, observer()->observed_states()[0]); 576 "Not available on unlock");
387 observer()->ClearObservedStates(); 577 ClearObservedStates();
388 ASSERT_EQ(1u, tray_action()->observed_states().size());
389 EXPECT_EQ(TrayActionState::kNotAvailable,
390 tray_action()->observed_states()[0]);
391 tray_action()->ClearObservedStates();
392 578
393 // Test that subsequent session lock works as expected. 579 // Test that subsequent session lock works as expected.
394 session_manager()->SetSessionState(session_manager::SessionState::LOCKED); 580 session_manager()->SetSessionState(session_manager::SessionState::LOCKED);
395 ASSERT_EQ(TestAppManager::State::kStarted, app_manager()->state()); 581 ASSERT_EQ(TestAppManager::State::kStarted, app_manager()->state());
396 582 ExpectObservedStatesMatch({TrayActionState::kAvailable},
397 EXPECT_EQ(TrayActionState::kAvailable, 583 "Available on second lock");
398 state_controller()->GetLockScreenNoteState());
399 state_controller()->FlushTrayActionForTesting();
400 ASSERT_EQ(1u, observer()->observed_states().size());
401 EXPECT_EQ(TrayActionState::kAvailable, observer()->observed_states()[0]);
402 ASSERT_EQ(1u, tray_action()->observed_states().size());
403 EXPECT_EQ(TrayActionState::kAvailable, tray_action()->observed_states()[0]);
404 } 584 }
405 585
406 TEST_F(LockScreenAppStateTest, SessionUnlockedWhileStartingAppManager) { 586 TEST_F(LockScreenAppStateTest, SessionUnlockedWhileStartingAppManager) {
407 state_controller()->SetPrimaryProfile(profile()); 587 state_controller()->SetPrimaryProfile(profile());
408 ASSERT_EQ(TestAppManager::State::kStopped, app_manager()->state()); 588 ASSERT_EQ(TestAppManager::State::kStopped, app_manager()->state());
409 589
410 session_manager()->SetSessionState(session_manager::SessionState::LOCKED); 590 session_manager()->SetSessionState(session_manager::SessionState::LOCKED);
411 591
412 ASSERT_EQ(TestAppManager::State::kStarted, app_manager()->state()); 592 ASSERT_EQ(TestAppManager::State::kStarted, app_manager()->state());
413 593
414 session_manager()->SetSessionState(session_manager::SessionState::ACTIVE); 594 session_manager()->SetSessionState(session_manager::SessionState::ACTIVE);
415 ASSERT_EQ(TestAppManager::State::kStopped, app_manager()->state()); 595 ASSERT_EQ(TestAppManager::State::kStopped, app_manager()->state());
416 596
417 EXPECT_EQ(TrayActionState::kNotAvailable, 597 EXPECT_EQ(TrayActionState::kNotAvailable,
418 state_controller()->GetLockScreenNoteState()); 598 state_controller()->GetLockScreenNoteState());
419 state_controller()->FlushTrayActionForTesting(); 599 state_controller()->FlushTrayActionForTesting();
420 EXPECT_EQ(0u, observer()->observed_states().size()); 600 EXPECT_EQ(0u, observer()->observed_states().size());
421 EXPECT_EQ(0u, tray_action()->observed_states().size()); 601 EXPECT_EQ(0u, tray_action()->observed_states().size());
422 602
423 // Test that subsequent session lock works as expected. 603 // Test that subsequent session lock works as expected.
424 session_manager()->SetSessionState(session_manager::SessionState::LOCKED); 604 session_manager()->SetSessionState(session_manager::SessionState::LOCKED);
425 605
426 ASSERT_EQ(TestAppManager::State::kStarted, app_manager()->state()); 606 ASSERT_EQ(TestAppManager::State::kStarted, app_manager()->state());
427 app_manager()->UpdateApp("app_id", true); 607 app_manager()->UpdateApp(kTestAppId, true);
428 608
429 EXPECT_EQ(TrayActionState::kAvailable, 609 EXPECT_EQ(TrayActionState::kAvailable,
430 state_controller()->GetLockScreenNoteState()); 610 state_controller()->GetLockScreenNoteState());
431 state_controller()->FlushTrayActionForTesting(); 611 ExpectObservedStatesMatch({TrayActionState::kAvailable}, "Available on lock");
432 ASSERT_EQ(1u, observer()->observed_states().size());
433 EXPECT_EQ(TrayActionState::kAvailable, observer()->observed_states()[0]);
434 ASSERT_EQ(1u, tray_action()->observed_states().size());
435 EXPECT_EQ(TrayActionState::kAvailable, tray_action()->observed_states()[0]);
436 } 612 }
437 613
438 TEST_F(LockScreenAppStateTest, AppManagerNoApp) { 614 TEST_F(LockScreenAppStateTest, AppManagerNoApp) {
439 state_controller()->SetPrimaryProfile(profile()); 615 state_controller()->SetPrimaryProfile(profile());
440 ASSERT_EQ(TestAppManager::State::kStopped, app_manager()->state()); 616 ASSERT_EQ(TestAppManager::State::kStopped, app_manager()->state());
441 617
442 session_manager()->SetSessionState(session_manager::SessionState::LOCKED); 618 session_manager()->SetSessionState(session_manager::SessionState::LOCKED);
443 619
444 EXPECT_EQ(TestAppManager::State::kStarted, app_manager()->state()); 620 EXPECT_EQ(TestAppManager::State::kStarted, app_manager()->state());
445 621
446 EXPECT_EQ(TrayActionState::kNotAvailable, 622 EXPECT_EQ(TrayActionState::kNotAvailable,
447 state_controller()->GetLockScreenNoteState()); 623 state_controller()->GetLockScreenNoteState());
448 state_controller()->FlushTrayActionForTesting(); 624 state_controller()->FlushTrayActionForTesting();
449 EXPECT_EQ(0u, observer()->observed_states().size()); 625 EXPECT_EQ(0u, observer()->observed_states().size());
450 EXPECT_EQ(0u, tray_action()->observed_states().size()); 626 EXPECT_EQ(0u, tray_action()->observed_states().size());
451 627
452 tray_action()->SendNewNoteRequest(); 628 tray_action()->SendNewNoteRequest();
453 629
454 EXPECT_EQ(TrayActionState::kNotAvailable, 630 EXPECT_EQ(TrayActionState::kNotAvailable,
455 state_controller()->GetLockScreenNoteState()); 631 state_controller()->GetLockScreenNoteState());
456 state_controller()->FlushTrayActionForTesting(); 632 state_controller()->FlushTrayActionForTesting();
457 EXPECT_EQ(0u, observer()->observed_states().size()); 633 EXPECT_EQ(0u, observer()->observed_states().size());
458 EXPECT_EQ(0u, tray_action()->observed_states().size()); 634 EXPECT_EQ(0u, tray_action()->observed_states().size());
459 635
460 // App manager should be started on next session lock. 636 // App manager should be started on next session lock.
461 session_manager()->SetSessionState(session_manager::SessionState::ACTIVE); 637 session_manager()->SetSessionState(session_manager::SessionState::ACTIVE);
462 ASSERT_EQ(TestAppManager::State::kStopped, app_manager()->state()); 638 ASSERT_EQ(TestAppManager::State::kStopped, app_manager()->state());
463 app_manager()->SetInitialAppState("app_id", false); 639 app_manager()->SetInitialAppState(kTestAppId, false);
464 640
465 session_manager()->SetSessionState(session_manager::SessionState::LOCKED); 641 session_manager()->SetSessionState(session_manager::SessionState::LOCKED);
466 642
467 EXPECT_EQ(TrayActionState::kAvailable, 643 EXPECT_EQ(TrayActionState::kAvailable,
468 state_controller()->GetLockScreenNoteState()); 644 state_controller()->GetLockScreenNoteState());
469 state_controller()->FlushTrayActionForTesting(); 645 ExpectObservedStatesMatch({TrayActionState::kAvailable}, "Available on lock");
470 ASSERT_EQ(1u, observer()->observed_states().size());
471 EXPECT_EQ(TrayActionState::kAvailable, observer()->observed_states()[0]);
472 ASSERT_EQ(1u, tray_action()->observed_states().size());
473 EXPECT_EQ(TrayActionState::kAvailable, tray_action()->observed_states()[0]);
474 } 646 }
475 647
476 TEST_F(LockScreenAppStateTest, AppAvailabilityChanges) { 648 TEST_F(LockScreenAppStateTest, AppAvailabilityChanges) {
477 state_controller()->SetPrimaryProfile(profile()); 649 state_controller()->SetPrimaryProfile(profile());
478 ASSERT_EQ(TestAppManager::State::kStopped, app_manager()->state()); 650 ASSERT_EQ(TestAppManager::State::kStopped, app_manager()->state());
479 651
480 app_manager()->SetInitialAppState("app_id", false); 652 app_manager()->SetInitialAppState(kTestAppId, false);
481 session_manager()->SetSessionState(session_manager::SessionState::LOCKED); 653 session_manager()->SetSessionState(session_manager::SessionState::LOCKED);
482 654
483 EXPECT_EQ(TestAppManager::State::kStarted, app_manager()->state()); 655 EXPECT_EQ(TestAppManager::State::kStarted, app_manager()->state());
484 656
485 EXPECT_EQ(TrayActionState::kAvailable, 657 EXPECT_EQ(TrayActionState::kAvailable,
486 state_controller()->GetLockScreenNoteState()); 658 state_controller()->GetLockScreenNoteState());
487 state_controller()->FlushTrayActionForTesting(); 659 ExpectObservedStatesMatch({TrayActionState::kAvailable}, "Available on lock");
488 ASSERT_EQ(1u, observer()->observed_states().size()); 660 ClearObservedStates();
489 EXPECT_EQ(TrayActionState::kAvailable, observer()->observed_states()[0]);
490 observer()->ClearObservedStates();
491 ASSERT_EQ(1u, tray_action()->observed_states().size());
492 EXPECT_EQ(TrayActionState::kAvailable, tray_action()->observed_states()[0]);
493 tray_action()->ClearObservedStates();
494 661
495 app_manager()->UpdateApp("", false); 662 app_manager()->UpdateApp("", false);
496 663
497 EXPECT_EQ(TrayActionState::kNotAvailable, 664 EXPECT_EQ(TrayActionState::kNotAvailable,
498 state_controller()->GetLockScreenNoteState()); 665 state_controller()->GetLockScreenNoteState());
499 state_controller()->FlushTrayActionForTesting(); 666 ExpectObservedStatesMatch({TrayActionState::kNotAvailable},
500 ASSERT_EQ(1u, observer()->observed_states().size()); 667 "Not available on app cleared");
501 EXPECT_EQ(TrayActionState::kNotAvailable, observer()->observed_states()[0]); 668 ClearObservedStates();
502 observer()->ClearObservedStates();
503 ASSERT_EQ(1u, tray_action()->observed_states().size());
504 EXPECT_EQ(TrayActionState::kNotAvailable,
505 tray_action()->observed_states()[0]);
506 tray_action()->ClearObservedStates();
507 669
508 app_manager()->UpdateApp("app_id_1", true); 670 app_manager()->UpdateApp(kSecondaryTestAppId, true);
509 671
510 EXPECT_EQ(TrayActionState::kAvailable, 672 EXPECT_EQ(TrayActionState::kAvailable,
511 state_controller()->GetLockScreenNoteState()); 673 state_controller()->GetLockScreenNoteState());
512 state_controller()->FlushTrayActionForTesting(); 674 ExpectObservedStatesMatch({TrayActionState::kAvailable},
513 ASSERT_EQ(1u, observer()->observed_states().size()); 675 "Available on other app set");
514 EXPECT_EQ(TrayActionState::kAvailable, observer()->observed_states()[0]);
515 observer()->ClearObservedStates();
516 ASSERT_EQ(1u, tray_action()->observed_states().size());
517 EXPECT_EQ(TrayActionState::kAvailable, tray_action()->observed_states()[0]);
518 tray_action()->ClearObservedStates();
519 } 676 }
520 677
521 TEST_F(LockScreenAppStateTest, MoveToBackgroundFromActive) { 678 TEST_F(LockScreenAppStateTest, MoveToBackgroundAndForeground) {
522 ASSERT_TRUE(EnableNoteTakingApp(true /* enable_app_launch */)); 679 ASSERT_TRUE(InitializeNoteTakingApp(TrayActionState::kActive,
523 680 true /* enable_app_launch */));
524 state_controller()->SetLockScreenNoteStateForTesting(
525 TrayActionState::kActive);
526 681
527 state_controller()->MoveToBackground(); 682 state_controller()->MoveToBackground();
528 state_controller()->FlushTrayActionForTesting(); 683 state_controller()->FlushTrayActionForTesting();
529 684
530 EXPECT_EQ(TrayActionState::kBackground, 685 EXPECT_EQ(TrayActionState::kBackground,
531 state_controller()->GetLockScreenNoteState()); 686 state_controller()->GetLockScreenNoteState());
532 687
533 ASSERT_EQ(1u, observer()->observed_states().size()); 688 ExpectObservedStatesMatch({TrayActionState::kBackground},
534 EXPECT_EQ(TrayActionState::kBackground, observer()->observed_states()[0]); 689 "Move to background");
535 ASSERT_EQ(1u, tray_action()->observed_states().size()); 690 ClearObservedStates();
536 EXPECT_EQ(TrayActionState::kBackground, tray_action()->observed_states()[0]);
537 }
538 691
539 TEST_F(LockScreenAppStateTest, MoveToForeground) { 692 base::RunLoop().RunUntilIdle();
540 ASSERT_TRUE(EnableNoteTakingApp(true /* enable_app_launch */)); 693 EXPECT_FALSE(app_window()->closed());
541
542 state_controller()->SetLockScreenNoteStateForTesting(
543 TrayActionState::kBackground);
544 694
545 state_controller()->MoveToForeground(); 695 state_controller()->MoveToForeground();
546 state_controller()->FlushTrayActionForTesting(); 696 state_controller()->FlushTrayActionForTesting();
547 697
548 EXPECT_EQ(TrayActionState::kActive, 698 EXPECT_EQ(TrayActionState::kActive,
549 state_controller()->GetLockScreenNoteState()); 699 state_controller()->GetLockScreenNoteState());
550 700
551 ASSERT_EQ(1u, observer()->observed_states().size()); 701 ExpectObservedStatesMatch({TrayActionState::kActive}, "Move to foreground");
552 EXPECT_EQ(TrayActionState::kActive, observer()->observed_states()[0]); 702
553 ASSERT_EQ(1u, tray_action()->observed_states().size()); 703 base::RunLoop().RunUntilIdle();
554 EXPECT_EQ(TrayActionState::kActive, tray_action()->observed_states()[0]); 704 EXPECT_FALSE(app_window()->closed());
555 } 705 }
556 706
557 TEST_F(LockScreenAppStateTest, MoveToForegroundFromNonBackgroundState) { 707 TEST_F(LockScreenAppStateTest, MoveToForegroundFromNonBackgroundState) {
558 ASSERT_TRUE(EnableNoteTakingApp(true /* enable_app_launch */)); 708 ASSERT_TRUE(InitializeNoteTakingApp(TrayActionState::kAvailable,
709 true /* enable_app_launch */));
559 710
560 state_controller()->MoveToForeground(); 711 state_controller()->MoveToForeground();
561 state_controller()->FlushTrayActionForTesting(); 712 state_controller()->FlushTrayActionForTesting();
562 713
563 EXPECT_EQ(TrayActionState::kAvailable, 714 EXPECT_EQ(TrayActionState::kAvailable,
564 state_controller()->GetLockScreenNoteState()); 715 state_controller()->GetLockScreenNoteState());
565 716
566 EXPECT_EQ(0u, observer()->observed_states().size()); 717 EXPECT_EQ(0u, observer()->observed_states().size());
567 EXPECT_EQ(0u, tray_action()->observed_states().size()); 718 EXPECT_EQ(0u, tray_action()->observed_states().size());
568 } 719 }
569 720
570 TEST_F(LockScreenAppStateTest, HandleActionWhenNotAvaiable) { 721 TEST_F(LockScreenAppStateTest, HandleActionWhenNotAvaiable) {
571 ASSERT_EQ(TrayActionState::kNotAvailable, 722 ASSERT_EQ(TrayActionState::kNotAvailable,
572 state_controller()->GetLockScreenNoteState()); 723 state_controller()->GetLockScreenNoteState());
573 724
574 tray_action()->SendNewNoteRequest(); 725 tray_action()->SendNewNoteRequest();
575 state_controller()->FlushTrayActionForTesting(); 726 state_controller()->FlushTrayActionForTesting();
576 727
577 EXPECT_EQ(0u, observer()->observed_states().size()); 728 EXPECT_EQ(0u, observer()->observed_states().size());
578 EXPECT_EQ(0u, tray_action()->observed_states().size()); 729 EXPECT_EQ(0u, tray_action()->observed_states().size());
579 } 730 }
580 731
581 TEST_F(LockScreenAppStateTest, HandleAction) { 732 TEST_F(LockScreenAppStateTest, HandleAction) {
582 ASSERT_TRUE(EnableNoteTakingApp(true /* enable_app_launch */)); 733 ASSERT_TRUE(InitializeNoteTakingApp(TrayActionState::kAvailable,
734 true /* enable_app_launch */));
583 735
584 tray_action()->SendNewNoteRequest(); 736 tray_action()->SendNewNoteRequest();
585 state_controller()->FlushTrayActionForTesting(); 737 state_controller()->FlushTrayActionForTesting();
586 738
587 EXPECT_EQ(TrayActionState::kLaunching, 739 ExpectObservedStatesMatch({TrayActionState::kLaunching},
588 state_controller()->GetLockScreenNoteState()); 740 "Launch on new note request");
589 ASSERT_EQ(1u, observer()->observed_states().size()); 741 ClearObservedStates();
590 EXPECT_EQ(TrayActionState::kLaunching, observer()->observed_states()[0]);
591 observer()->ClearObservedStates();
592 ASSERT_EQ(1u, tray_action()->observed_states().size());
593 EXPECT_EQ(TrayActionState::kLaunching, tray_action()->observed_states()[0]);
594 tray_action()->ClearObservedStates();
595
596 EXPECT_EQ(1, app_manager()->launch_count()); 742 EXPECT_EQ(1, app_manager()->launch_count());
597 743
598 tray_action()->SendNewNoteRequest(); 744 tray_action()->SendNewNoteRequest();
599 state_controller()->FlushTrayActionForTesting(); 745 state_controller()->FlushTrayActionForTesting();
600 746
601 // There should be no state change - the state_controller was already in 747 // There should be no state change - the state_controller was already in
602 // launching state when the request was received. 748 // launching state when the request was received.
603 EXPECT_EQ(0u, observer()->observed_states().size()); 749 EXPECT_EQ(0u, observer()->observed_states().size());
604 EXPECT_EQ(0u, tray_action()->observed_states().size()); 750 EXPECT_EQ(0u, tray_action()->observed_states().size());
605 EXPECT_EQ(1, app_manager()->launch_count()); 751 EXPECT_EQ(1, app_manager()->launch_count());
606 } 752 }
607 753
608 TEST_F(LockScreenAppStateTest, HandleActionWithLaunchFailure) { 754 TEST_F(LockScreenAppStateTest, HandleActionWithLaunchFailure) {
609 ASSERT_TRUE(EnableNoteTakingApp(false /* enable_app_launch */)); 755 ASSERT_TRUE(InitializeNoteTakingApp(TrayActionState::kAvailable,
756 false /* enable_app_launch */));
610 757
611 tray_action()->SendNewNoteRequest(); 758 tray_action()->SendNewNoteRequest();
612 state_controller()->FlushTrayActionForTesting(); 759 state_controller()->FlushTrayActionForTesting();
613 760
614 EXPECT_EQ(TrayActionState::kAvailable, 761 EXPECT_EQ(TrayActionState::kAvailable,
615 state_controller()->GetLockScreenNoteState()); 762 state_controller()->GetLockScreenNoteState());
616 ASSERT_EQ(2u, observer()->observed_states().size()); 763 ExpectObservedStatesMatch(
617 EXPECT_EQ(TrayActionState::kLaunching, observer()->observed_states()[0]); 764 {TrayActionState::kLaunching, TrayActionState::kAvailable},
618 EXPECT_EQ(TrayActionState::kAvailable, observer()->observed_states()[1]); 765 "Failed launch on new note request");
619 observer()->ClearObservedStates(); 766 ClearObservedStates();
620 ASSERT_EQ(2u, tray_action()->observed_states().size());
621 EXPECT_EQ(TrayActionState::kLaunching, tray_action()->observed_states()[0]);
622 EXPECT_EQ(TrayActionState::kAvailable, tray_action()->observed_states()[1]);
623 tray_action()->ClearObservedStates();
624 767
625 EXPECT_EQ(1, app_manager()->launch_count()); 768 EXPECT_EQ(1, app_manager()->launch_count());
626 769
627 tray_action()->SendNewNoteRequest(); 770 tray_action()->SendNewNoteRequest();
628 state_controller()->FlushTrayActionForTesting(); 771 state_controller()->FlushTrayActionForTesting();
629 772
630 EXPECT_EQ(TrayActionState::kAvailable, 773 EXPECT_EQ(TrayActionState::kAvailable,
631 state_controller()->GetLockScreenNoteState()); 774 state_controller()->GetLockScreenNoteState());
632 775
633 // There should be no state change - the state_controller was already in 776 ExpectObservedStatesMatch(
634 // launching state when the request was received. 777 {TrayActionState::kLaunching, TrayActionState::kAvailable},
635 EXPECT_EQ(2u, observer()->observed_states().size()); 778 "Second failed launch on new note request");
636 EXPECT_EQ(2u, tray_action()->observed_states().size());
637 EXPECT_EQ(2, app_manager()->launch_count()); 779 EXPECT_EQ(2, app_manager()->launch_count());
638 } 780 }
781
782 TEST_F(LockScreenAppStateTest, AppWindowRegistration) {
783 ASSERT_TRUE(InitializeNoteTakingApp(TrayActionState::kAvailable,
784 true /* enable_app_launch */));
785
786 std::unique_ptr<TestAppWindow> app_window =
787 CreateNoteTakingWindow(signin_profile(), app());
788 EXPECT_FALSE(app_window->window());
789
790 tray_action()->SendNewNoteRequest();
791 state_controller()->FlushTrayActionForTesting();
792
793 EXPECT_EQ(TrayActionState::kLaunching,
794 state_controller()->GetLockScreenNoteState());
795 observer()->ClearObservedStates();
796 tray_action()->ClearObservedStates();
797
798 std::unique_ptr<TestAppWindow> non_eligible_app_window =
799 CreateNoteTakingWindow(profile(), app());
800 EXPECT_FALSE(non_eligible_app_window->window());
801
802 EXPECT_FALSE(state_controller()->CreateAppWindowForLockScreenAction(
803 signin_profile(), app(), extensions::api::app_runtime::ACTION_TYPE_NONE,
804 base::MakeUnique<ChromeAppDelegate>(true)));
805
806 app_window = CreateNoteTakingWindow(signin_profile(), app());
807 ASSERT_TRUE(app_window->window());
808
809 app_window->Initialize(true /* shown */);
810 EXPECT_EQ(TrayActionState::kActive,
811 state_controller()->GetLockScreenNoteState());
812
813 // Test that second app window cannot be registered.
814 std::unique_ptr<TestAppWindow> second_app_window =
815 CreateNoteTakingWindow(signin_profile(), app());
816 EXPECT_FALSE(second_app_window->window());
817
818 // Test the app window does not get closed by itself.
819 base::RunLoop().RunUntilIdle();
820 EXPECT_FALSE(app_window->closed());
821
822 EXPECT_EQ(TrayActionState::kActive,
823 state_controller()->GetLockScreenNoteState());
824
825 // Closing the second app window, will not change the state.
826 second_app_window->Close();
827 EXPECT_EQ(TrayActionState::kActive,
828 state_controller()->GetLockScreenNoteState());
829
830 app_window->Close();
831 EXPECT_EQ(TrayActionState::kAvailable,
832 state_controller()->GetLockScreenNoteState());
833 }
834
835 TEST_F(LockScreenAppStateTest, AppWindowClosedBeforeBeingShown) {
836 ASSERT_TRUE(InitializeNoteTakingApp(TrayActionState::kLaunching,
837 true /* enable_app_launch */));
838
839 std::unique_ptr<TestAppWindow> app_window =
840 CreateNoteTakingWindow(signin_profile(), app());
841 ASSERT_TRUE(app_window->window());
842 app_window->Initialize(false /* shown */);
843
844 app_window->Close();
845 EXPECT_EQ(TrayActionState::kAvailable,
846 state_controller()->GetLockScreenNoteState());
847 }
848
849 TEST_F(LockScreenAppStateTest, AppWindowClosedOnSessionUnlock) {
850 ASSERT_TRUE(InitializeNoteTakingApp(TrayActionState::kActive,
851 true /* enable_app_launch */));
852
853 session_manager()->SetSessionState(session_manager::SessionState::ACTIVE);
854 EXPECT_EQ(TrayActionState::kNotAvailable,
855 state_controller()->GetLockScreenNoteState());
856
857 base::RunLoop().RunUntilIdle();
858 EXPECT_TRUE(app_window()->closed());
859 }
860
861 TEST_F(LockScreenAppStateTest, AppWindowClosedOnAppUnload) {
862 ASSERT_TRUE(InitializeNoteTakingApp(TrayActionState::kActive,
863 true /* enable_app_launch */));
864
865 extensions::ExtensionSystem::Get(signin_profile())
866 ->extension_service()
867 ->UnloadExtension(app()->id(),
868 extensions::UnloadedExtensionReason::UNINSTALL);
869 app_manager()->UpdateApp("", false);
870
871 EXPECT_EQ(TrayActionState::kNotAvailable,
872 state_controller()->GetLockScreenNoteState());
873
874 base::RunLoop().RunUntilIdle();
875 EXPECT_TRUE(app_window()->closed());
876 }
877
878 TEST_F(LockScreenAppStateTest, AppWindowClosedOnNoteTakingAppChange) {
879 ASSERT_TRUE(InitializeNoteTakingApp(TrayActionState::kActive,
880 true /* enable_app_launch */));
881
882 scoped_refptr<extensions::Extension> secondary_app =
883 CreateTestNoteTakingApp(kSecondaryTestAppId);
884 extensions::ExtensionSystem::Get(signin_profile())
885 ->extension_service()
886 ->AddExtension(secondary_app.get());
887
888 app_manager()->UpdateApp(secondary_app->id(), true);
889
890 EXPECT_EQ(TrayActionState::kAvailable,
891 state_controller()->GetLockScreenNoteState());
892
893 base::RunLoop().RunUntilIdle();
894 EXPECT_TRUE(app_window()->closed());
895
896 tray_action()->SendNewNoteRequest();
897 state_controller()->FlushTrayActionForTesting();
898
899 std::unique_ptr<TestAppWindow> app_window =
900 CreateNoteTakingWindow(signin_profile(), app());
901 EXPECT_FALSE(app_window->window());
902 ASSERT_EQ(TrayActionState::kLaunching,
903 state_controller()->GetLockScreenNoteState());
904
905 std::unique_ptr<TestAppWindow> secondary_app_window =
906 CreateNoteTakingWindow(signin_profile(), secondary_app.get());
907 ASSERT_TRUE(secondary_app_window->window());
908
909 secondary_app_window->Initialize(true /* shown*/);
910 EXPECT_EQ(TrayActionState::kActive,
911 state_controller()->GetLockScreenNoteState());
912
913 base::RunLoop().RunUntilIdle();
914 EXPECT_FALSE(secondary_app_window->closed());
915
916 // Uninstall the app and test the secondary app window is closed.
917 extensions::ExtensionSystem::Get(signin_profile())
918 ->extension_service()
919 ->UnloadExtension(secondary_app->id(),
920 extensions::UnloadedExtensionReason::UNINSTALL);
921 base::RunLoop().RunUntilIdle();
922 EXPECT_TRUE(secondary_app_window->closed());
923 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698