| 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 <memory> |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/test/scoped_command_line.h" |
| 5 #include "chrome/browser/chromeos/lock_screen_apps/state_controller.h" | 9 #include "chrome/browser/chromeos/lock_screen_apps/state_controller.h" |
| 6 | |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "ash/public/interfaces/tray_action.mojom.h" | |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "base/test/scoped_command_line.h" | |
| 15 #include "chrome/browser/chromeos/lock_screen_apps/state_observer.h" | 10 #include "chrome/browser/chromeos/lock_screen_apps/state_observer.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | 11 #include "chrome/browser/chromeos/lock_screen_apps/types.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 13 |
| 19 using ash::mojom::TrayActionState; | 14 namespace { |
| 20 | 15 |
| 21 namespace { | 16 const lock_screen_apps::Action kTestAction = lock_screen_apps::Action::kNewNote; |
| 22 | 17 |
| 23 class TestStateObserver : public lock_screen_apps::StateObserver { | 18 class TestStateObserver : public lock_screen_apps::StateObserver { |
| 24 public: | 19 public: |
| 25 TestStateObserver() = default; | 20 TestStateObserver() = default; |
| 26 ~TestStateObserver() override = default; | 21 ~TestStateObserver() override = default; |
| 27 | 22 |
| 28 void OnLockScreenNoteStateChanged(TrayActionState state) override { | 23 void OnLockScreenAppsStateChanged( |
| 29 observed_states_.push_back(state); | 24 lock_screen_apps::Action action, |
| 25 lock_screen_apps::ActionState state) override { |
| 26 ASSERT_EQ(kTestAction, action); |
| 27 |
| 28 ++state_change_count_; |
| 30 } | 29 } |
| 31 | 30 |
| 32 const std::vector<TrayActionState> observed_states() const { | 31 int state_change_count() const { return state_change_count_; } |
| 33 return observed_states_; | |
| 34 } | |
| 35 | |
| 36 void ClearObservedStates() { observed_states_.clear(); } | |
| 37 | 32 |
| 38 private: | 33 private: |
| 39 std::vector<TrayActionState> observed_states_; | 34 int state_change_count_ = 0; |
| 40 | 35 |
| 41 DISALLOW_COPY_AND_ASSIGN(TestStateObserver); | 36 DISALLOW_COPY_AND_ASSIGN(TestStateObserver); |
| 42 }; | 37 }; |
| 43 | 38 |
| 44 class TestTrayAction : public ash::mojom::TrayAction { | 39 class LockScreenAppStateControllerNotSupportedTest : public testing::Test { |
| 45 public: | 40 public: |
| 46 TestTrayAction() : binding_(this) {} | 41 LockScreenAppStateControllerNotSupportedTest() = default; |
| 47 | 42 |
| 48 ~TestTrayAction() override = default; | 43 ~LockScreenAppStateControllerNotSupportedTest() 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; | |
| 89 | 44 |
| 90 void SetUp() override { | 45 void SetUp() override { |
| 91 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); | 46 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); |
| 92 command_line_->GetProcessCommandLine()->InitFromArgv({""}); | 47 command_line_->GetProcessCommandLine()->InitFromArgv({""}); |
| 93 } | 48 } |
| 94 | 49 |
| 95 private: | 50 private: |
| 96 std::unique_ptr<base::test::ScopedCommandLine> command_line_; | 51 std::unique_ptr<base::test::ScopedCommandLine> command_line_; |
| 97 | 52 |
| 98 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateNotSupportedTest); | 53 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateControllerNotSupportedTest); |
| 99 }; | 54 }; |
| 100 | 55 |
| 101 class LockScreenAppStateTest : public testing::Test { | 56 class LockScreenAppStateControllerTest : public testing::Test { |
| 102 public: | 57 public: |
| 103 LockScreenAppStateTest() = default; | 58 LockScreenAppStateControllerTest() = default; |
| 104 ~LockScreenAppStateTest() override = default; | 59 ~LockScreenAppStateControllerTest() override = default; |
| 105 | 60 |
| 106 void SetUp() override { | 61 void SetUp() override { |
| 107 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); | 62 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); |
| 108 command_line_->GetProcessCommandLine()->InitFromArgv( | 63 command_line_->GetProcessCommandLine()->InitFromArgv( |
| 109 {"", "--enable-lock-screen-apps"}); | 64 {"", "--enable-lock-screen-apps"}); |
| 110 | 65 |
| 111 ASSERT_TRUE(lock_screen_apps::StateController::IsEnabled()); | 66 state_controller_ = lock_screen_apps::StateController::Get(); |
| 112 | 67 ASSERT_TRUE(state_controller_); |
| 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(); | |
| 118 | 68 |
| 119 state_controller_->AddObserver(&observer_); | 69 state_controller_->AddObserver(&observer_); |
| 120 } | 70 } |
| 121 | 71 |
| 122 void TearDown() override { | 72 void TearDown() override { state_controller_->RemoveObserver(&observer_); } |
| 123 state_controller_->RemoveObserver(&observer_); | |
| 124 state_controller_.reset(); | |
| 125 } | |
| 126 | 73 |
| 127 TestStateObserver* observer() { return &observer_; } | 74 const TestStateObserver& observer() const { return observer_; } |
| 128 | |
| 129 TestTrayAction* tray_action() { return &tray_action_; } | |
| 130 | 75 |
| 131 lock_screen_apps::StateController* state_controller() { | 76 lock_screen_apps::StateController* state_controller() { |
| 132 return state_controller_.get(); | 77 return state_controller_; |
| 133 } | 78 } |
| 134 | 79 |
| 135 private: | 80 private: |
| 136 std::unique_ptr<base::test::ScopedCommandLine> command_line_; | 81 std::unique_ptr<base::test::ScopedCommandLine> command_line_; |
| 137 content::TestBrowserThreadBundle threads_; | |
| 138 | 82 |
| 139 std::unique_ptr<lock_screen_apps::StateController> state_controller_; | 83 lock_screen_apps::StateController* state_controller_; |
| 140 TestStateObserver observer_; | 84 TestStateObserver observer_; |
| 141 TestTrayAction tray_action_; | |
| 142 | 85 |
| 143 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateTest); | 86 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateControllerTest); |
| 144 }; | 87 }; |
| 145 | 88 |
| 146 } // namespace | 89 } // namespace |
| 147 | 90 |
| 148 TEST_F(LockScreenAppStateNotSupportedTest, NoInstance) { | 91 TEST_F(LockScreenAppStateControllerNotSupportedTest, NoControllerInstance) { |
| 149 EXPECT_FALSE(lock_screen_apps::StateController::IsEnabled()); | 92 EXPECT_FALSE(lock_screen_apps::StateController::Get()); |
| 150 } | 93 } |
| 151 | 94 |
| 152 TEST_F(LockScreenAppStateTest, InitialState) { | 95 TEST_F(LockScreenAppStateControllerTest, InitialState) { |
| 153 EXPECT_EQ(TrayActionState::kNotAvailable, | 96 EXPECT_EQ(lock_screen_apps::ActionState::kNotSupported, |
| 154 state_controller()->GetLockScreenNoteState()); | 97 state_controller()->GetActionState(kTestAction)); |
| 98 |
| 99 ASSERT_EQ(0, observer().state_change_count()); |
| 155 | 100 |
| 156 state_controller()->MoveToBackground(); | 101 state_controller()->MoveToBackground(); |
| 157 | 102 |
| 158 EXPECT_EQ(TrayActionState::kNotAvailable, | 103 EXPECT_EQ(lock_screen_apps::ActionState::kNotSupported, |
| 159 state_controller()->GetLockScreenNoteState()); | 104 state_controller()->GetActionState(kTestAction)); |
| 160 | 105 |
| 161 EXPECT_EQ(0u, observer()->observed_states().size()); | 106 ASSERT_EQ(0, observer().state_change_count()); |
| 162 EXPECT_EQ(0u, tray_action()->observed_states().size()); | |
| 163 } | 107 } |
| 164 | 108 |
| 165 TEST_F(LockScreenAppStateTest, MoveToBackgroundFromActive) { | 109 TEST_F(LockScreenAppStateControllerTest, HandleActionWhenNotAvaiable) { |
| 166 state_controller()->SetLockScreenNoteStateForTesting( | 110 ASSERT_EQ(lock_screen_apps::ActionState::kNotSupported, |
| 167 TrayActionState::kActive); | 111 state_controller()->GetActionState(kTestAction)); |
| 168 | 112 EXPECT_FALSE( |
| 169 state_controller()->MoveToBackground(); | 113 state_controller()->HandleAction(lock_screen_apps::Action::kNewNote)); |
| 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]); | |
| 179 } | 114 } |
| 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 |