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