| OLD | NEW |
| 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" |
| 6 |
| 5 #include <memory> | 7 #include <memory> |
| 8 #include <utility> |
| 9 #include <vector> |
| 6 | 10 |
| 11 #include "ash/public/interfaces/tray_action.mojom.h" |
| 7 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/run_loop.h" |
| 8 #include "base/test/scoped_command_line.h" | 14 #include "base/test/scoped_command_line.h" |
| 9 #include "chrome/browser/chromeos/lock_screen_apps/state_controller.h" | |
| 10 #include "chrome/browser/chromeos/lock_screen_apps/state_observer.h" | 15 #include "chrome/browser/chromeos/lock_screen_apps/state_observer.h" |
| 11 #include "chrome/browser/chromeos/lock_screen_apps/types.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 18 |
| 19 using ash::mojom::TrayActionState; |
| 20 |
| 14 namespace { | 21 namespace { |
| 15 | 22 |
| 16 const lock_screen_apps::Action kTestAction = lock_screen_apps::Action::kNewNote; | |
| 17 | |
| 18 class TestStateObserver : public lock_screen_apps::StateObserver { | 23 class TestStateObserver : public lock_screen_apps::StateObserver { |
| 19 public: | 24 public: |
| 20 TestStateObserver() = default; | 25 TestStateObserver() = default; |
| 21 ~TestStateObserver() override = default; | 26 ~TestStateObserver() override = default; |
| 22 | 27 |
| 23 void OnLockScreenAppsStateChanged( | 28 void OnLockScreenNoteStateChanged(TrayActionState state) override { |
| 24 lock_screen_apps::Action action, | 29 observed_states_.push_back(state); |
| 25 lock_screen_apps::ActionState state) override { | |
| 26 ASSERT_EQ(kTestAction, action); | |
| 27 | |
| 28 ++state_change_count_; | |
| 29 } | 30 } |
| 30 | 31 |
| 31 int state_change_count() const { return state_change_count_; } | 32 const std::vector<TrayActionState> observed_states() const { |
| 33 return observed_states_; |
| 34 } |
| 35 |
| 36 void ClearObservedStates() { observed_states_.clear(); } |
| 32 | 37 |
| 33 private: | 38 private: |
| 34 int state_change_count_ = 0; | 39 std::vector<TrayActionState> observed_states_; |
| 35 | 40 |
| 36 DISALLOW_COPY_AND_ASSIGN(TestStateObserver); | 41 DISALLOW_COPY_AND_ASSIGN(TestStateObserver); |
| 37 }; | 42 }; |
| 38 | 43 |
| 39 class LockScreenAppStateControllerNotSupportedTest : public testing::Test { | 44 class TestTrayAction : public ash::mojom::TrayAction { |
| 40 public: | 45 public: |
| 41 LockScreenAppStateControllerNotSupportedTest() = default; | 46 TestTrayAction() : binding_(this) {} |
| 42 | 47 |
| 43 ~LockScreenAppStateControllerNotSupportedTest() override = default; | 48 ~TestTrayAction() override = default; |
| 49 |
| 50 ash::mojom::TrayActionPtr CreateInterfacePtrAndBind() { |
| 51 return binding_.CreateInterfacePtrAndBind(); |
| 52 } |
| 53 |
| 54 void SetClient(ash::mojom::TrayActionClientPtr client, |
| 55 TrayActionState state) override { |
| 56 client_ = std::move(client); |
| 57 EXPECT_EQ(TrayActionState::kNotAvailable, state); |
| 58 } |
| 59 |
| 60 void UpdateLockScreenNoteState(TrayActionState state) override { |
| 61 observed_states_.push_back(state); |
| 62 } |
| 63 |
| 64 void SendNewNoteRequest() { |
| 65 ASSERT_TRUE(client_); |
| 66 client_->RequestNewLockScreenNote(); |
| 67 } |
| 68 |
| 69 const std::vector<TrayActionState>& observed_states() const { |
| 70 return observed_states_; |
| 71 } |
| 72 |
| 73 void ClearObservedStates() { observed_states_.clear(); } |
| 74 |
| 75 private: |
| 76 mojo::Binding<ash::mojom::TrayAction> binding_; |
| 77 ash::mojom::TrayActionClientPtr client_; |
| 78 |
| 79 std::vector<TrayActionState> observed_states_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(TestTrayAction); |
| 82 }; |
| 83 |
| 84 class LockScreenAppStateNotSupportedTest : public testing::Test { |
| 85 public: |
| 86 LockScreenAppStateNotSupportedTest() = default; |
| 87 |
| 88 ~LockScreenAppStateNotSupportedTest() override = default; |
| 44 | 89 |
| 45 void SetUp() override { | 90 void SetUp() override { |
| 46 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); | 91 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); |
| 47 command_line_->GetProcessCommandLine()->InitFromArgv({""}); | 92 command_line_->GetProcessCommandLine()->InitFromArgv({""}); |
| 48 } | 93 } |
| 49 | 94 |
| 50 private: | 95 private: |
| 51 std::unique_ptr<base::test::ScopedCommandLine> command_line_; | 96 std::unique_ptr<base::test::ScopedCommandLine> command_line_; |
| 52 | 97 |
| 53 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateControllerNotSupportedTest); | 98 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateNotSupportedTest); |
| 54 }; | 99 }; |
| 55 | 100 |
| 56 class LockScreenAppStateControllerTest : public testing::Test { | 101 class LockScreenAppStateTest : public testing::Test { |
| 57 public: | 102 public: |
| 58 LockScreenAppStateControllerTest() = default; | 103 LockScreenAppStateTest() = default; |
| 59 ~LockScreenAppStateControllerTest() override = default; | 104 ~LockScreenAppStateTest() override = default; |
| 60 | 105 |
| 61 void SetUp() override { | 106 void SetUp() override { |
| 62 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); | 107 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); |
| 63 command_line_->GetProcessCommandLine()->InitFromArgv( | 108 command_line_->GetProcessCommandLine()->InitFromArgv( |
| 64 {"", "--enable-lock-screen-apps"}); | 109 {"", "--enable-lock-screen-apps"}); |
| 65 | 110 |
| 66 state_controller_ = lock_screen_apps::StateController::Get(); | 111 ASSERT_TRUE(lock_screen_apps::StateController::IsEnabled()); |
| 67 ASSERT_TRUE(state_controller_); | 112 |
| 113 state_controller_ = base::MakeUnique<lock_screen_apps::StateController>(); |
| 114 state_controller_->SetTrayActionPtrForTesting( |
| 115 tray_action_.CreateInterfacePtrAndBind()); |
| 116 state_controller_->Initialize(); |
| 117 state_controller_->FlushTrayActionForTesting(); |
| 68 | 118 |
| 69 state_controller_->AddObserver(&observer_); | 119 state_controller_->AddObserver(&observer_); |
| 70 } | 120 } |
| 71 | 121 |
| 72 void TearDown() override { state_controller_->RemoveObserver(&observer_); } | 122 void TearDown() override { |
| 123 state_controller_->RemoveObserver(&observer_); |
| 124 state_controller_.reset(); |
| 125 } |
| 73 | 126 |
| 74 const TestStateObserver& observer() const { return observer_; } | 127 TestStateObserver* observer() { return &observer_; } |
| 128 |
| 129 TestTrayAction* tray_action() { return &tray_action_; } |
| 75 | 130 |
| 76 lock_screen_apps::StateController* state_controller() { | 131 lock_screen_apps::StateController* state_controller() { |
| 77 return state_controller_; | 132 return state_controller_.get(); |
| 78 } | 133 } |
| 79 | 134 |
| 80 private: | 135 private: |
| 81 std::unique_ptr<base::test::ScopedCommandLine> command_line_; | 136 std::unique_ptr<base::test::ScopedCommandLine> command_line_; |
| 137 content::TestBrowserThreadBundle threads_; |
| 82 | 138 |
| 83 lock_screen_apps::StateController* state_controller_; | 139 std::unique_ptr<lock_screen_apps::StateController> state_controller_; |
| 84 TestStateObserver observer_; | 140 TestStateObserver observer_; |
| 141 TestTrayAction tray_action_; |
| 85 | 142 |
| 86 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateControllerTest); | 143 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateTest); |
| 87 }; | 144 }; |
| 88 | 145 |
| 89 } // namespace | 146 } // namespace |
| 90 | 147 |
| 91 TEST_F(LockScreenAppStateControllerNotSupportedTest, NoControllerInstance) { | 148 TEST_F(LockScreenAppStateNotSupportedTest, NoInstance) { |
| 92 EXPECT_FALSE(lock_screen_apps::StateController::Get()); | 149 EXPECT_FALSE(lock_screen_apps::StateController::IsEnabled()); |
| 93 } | 150 } |
| 94 | 151 |
| 95 TEST_F(LockScreenAppStateControllerTest, InitialState) { | 152 TEST_F(LockScreenAppStateTest, InitialState) { |
| 96 EXPECT_EQ(lock_screen_apps::ActionState::kNotSupported, | 153 EXPECT_EQ(TrayActionState::kNotAvailable, |
| 97 state_controller()->GetActionState(kTestAction)); | 154 state_controller()->GetLockScreenNoteState()); |
| 98 | |
| 99 ASSERT_EQ(0, observer().state_change_count()); | |
| 100 | 155 |
| 101 state_controller()->MoveToBackground(); | 156 state_controller()->MoveToBackground(); |
| 102 | 157 |
| 103 EXPECT_EQ(lock_screen_apps::ActionState::kNotSupported, | 158 EXPECT_EQ(TrayActionState::kNotAvailable, |
| 104 state_controller()->GetActionState(kTestAction)); | 159 state_controller()->GetLockScreenNoteState()); |
| 105 | 160 |
| 106 ASSERT_EQ(0, observer().state_change_count()); | 161 EXPECT_EQ(0u, observer()->observed_states().size()); |
| 162 EXPECT_EQ(0u, tray_action()->observed_states().size()); |
| 107 } | 163 } |
| 108 | 164 |
| 109 TEST_F(LockScreenAppStateControllerTest, HandleActionWhenNotAvaiable) { | 165 TEST_F(LockScreenAppStateTest, MoveToBackgroundFromActive) { |
| 110 ASSERT_EQ(lock_screen_apps::ActionState::kNotSupported, | 166 state_controller()->SetLockScreenNoteStateForTesting( |
| 111 state_controller()->GetActionState(kTestAction)); | 167 TrayActionState::kActive); |
| 112 EXPECT_FALSE( | 168 |
| 113 state_controller()->HandleAction(lock_screen_apps::Action::kNewNote)); | 169 state_controller()->MoveToBackground(); |
| 170 state_controller()->FlushTrayActionForTesting(); |
| 171 |
| 172 EXPECT_EQ(TrayActionState::kBackground, |
| 173 state_controller()->GetLockScreenNoteState()); |
| 174 |
| 175 ASSERT_EQ(1u, observer()->observed_states().size()); |
| 176 EXPECT_EQ(TrayActionState::kBackground, observer()->observed_states()[0]); |
| 177 ASSERT_EQ(1u, tray_action()->observed_states().size()); |
| 178 EXPECT_EQ(TrayActionState::kBackground, tray_action()->observed_states()[0]); |
| 114 } | 179 } |
| 180 |
| 181 TEST_F(LockScreenAppStateTest, HandleActionWhenNotAvaiable) { |
| 182 ASSERT_EQ(TrayActionState::kNotAvailable, |
| 183 state_controller()->GetLockScreenNoteState()); |
| 184 |
| 185 tray_action()->SendNewNoteRequest(); |
| 186 state_controller()->FlushTrayActionForTesting(); |
| 187 |
| 188 EXPECT_EQ(0u, observer()->observed_states().size()); |
| 189 EXPECT_EQ(0u, tray_action()->observed_states().size()); |
| 190 } |
| 191 |
| 192 TEST_F(LockScreenAppStateTest, HandleAction) { |
| 193 state_controller()->SetLockScreenNoteStateForTesting( |
| 194 TrayActionState::kAvailable); |
| 195 |
| 196 tray_action()->SendNewNoteRequest(); |
| 197 state_controller()->FlushTrayActionForTesting(); |
| 198 |
| 199 EXPECT_EQ(TrayActionState::kActive, |
| 200 state_controller()->GetLockScreenNoteState()); |
| 201 ASSERT_EQ(1u, observer()->observed_states().size()); |
| 202 EXPECT_EQ(TrayActionState::kActive, observer()->observed_states()[0]); |
| 203 observer()->ClearObservedStates(); |
| 204 ASSERT_EQ(1u, tray_action()->observed_states().size()); |
| 205 EXPECT_EQ(TrayActionState::kActive, tray_action()->observed_states()[0]); |
| 206 tray_action()->ClearObservedStates(); |
| 207 |
| 208 tray_action()->SendNewNoteRequest(); |
| 209 state_controller()->FlushTrayActionForTesting(); |
| 210 |
| 211 // There should be no state change - the state_controller was already in |
| 212 // active state when the request was received. |
| 213 EXPECT_EQ(0u, observer()->observed_states().size()); |
| 214 EXPECT_EQ(0u, tray_action()->observed_states().size()); |
| 215 } |
| OLD | NEW |