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

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

Powered by Google App Engine
This is Rietveld 408576698