unified_system_info_view_unittest.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/system/unified/unified_system_info_view.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/public/cpp/login_types.h"
  7. #include "ash/session/session_controller_impl.h"
  8. #include "ash/session/test_session_controller_client.h"
  9. #include "ash/shell.h"
  10. #include "ash/system/model/enterprise_domain_model.h"
  11. #include "ash/system/model/system_tray_model.h"
  12. #include "ash/system/unified/unified_system_tray_controller.h"
  13. #include "ash/system/unified/unified_system_tray_model.h"
  14. #include "ash/test/ash_test_base.h"
  15. #include "ash/test_shell_delegate.h"
  16. #include "base/memory/scoped_refptr.h"
  17. #include "base/test/scoped_feature_list.h"
  18. #include "components/version_info/channel.h"
  19. namespace ash {
  20. // `UnifiedSystemInfoView` contains a set of "baseline" UI elements that are
  21. // always visible, but some elements are visible only under certain conditions.
  22. // To verify that these "conditional" UI elements are visible or not-visible
  23. // only when expected, each `UnifiedSystemInfoViewTest` test case is executed
  24. // with every possible combination of the following flags, passed as a
  25. // parameter.
  26. enum class TestFlags : uint8_t {
  27. // No conditional UI flags are set.
  28. kNone = 0b00000000,
  29. // Enterprise/management status display is enabled.
  30. kManagedDeviceUi = 0b00000001,
  31. // Release track UI is visible if two conditions are met: (1) the feature that
  32. // guards its display is enabled (kReleaseTrackUi) and (2) the release track
  33. // itself is a value other than "stable" (kReleaseTrackNotStable). Each
  34. // combination of one, none, or both of these conditions is a valid scenario.
  35. kReleaseTrackUi = 0b00000010,
  36. kReleaseTrackNotStable = 0b00000100,
  37. };
  38. TestFlags operator&(TestFlags a, TestFlags b) {
  39. return static_cast<TestFlags>(static_cast<uint8_t>(a) &
  40. static_cast<uint8_t>(b));
  41. }
  42. TestFlags operator|(TestFlags a, TestFlags b) {
  43. return static_cast<TestFlags>(static_cast<uint8_t>(a) |
  44. static_cast<uint8_t>(b));
  45. }
  46. class UnifiedSystemInfoViewTest
  47. : public AshTestBase,
  48. public testing::WithParamInterface<TestFlags> {
  49. public:
  50. UnifiedSystemInfoViewTest() = default;
  51. UnifiedSystemInfoViewTest(const UnifiedSystemInfoViewTest&) = delete;
  52. UnifiedSystemInfoViewTest& operator=(const UnifiedSystemInfoViewTest&) =
  53. delete;
  54. ~UnifiedSystemInfoViewTest() override = default;
  55. void SetUp() override {
  56. // Provide our own `TestShellDelegate`, with a non-stable channel set if
  57. // the passed-in parameter dictates.
  58. std::unique_ptr<TestShellDelegate> shell_delegate =
  59. std::make_unique<TestShellDelegate>();
  60. if (IsReleaseTrackNotStable())
  61. shell_delegate->set_channel(version_info::Channel::BETA);
  62. AshTestBase::SetUp(std::move(shell_delegate));
  63. // Enable/disable of the two features we care about is conditional on the
  64. // passed-in parameter.
  65. scoped_feature_list_ = std::make_unique<base::test::ScopedFeatureList>();
  66. std::vector<base::Feature> enabled_features, disabled_features;
  67. if (IsManagedDeviceUIRedesignEnabled())
  68. enabled_features.push_back(features::kManagedDeviceUIRedesign);
  69. else
  70. disabled_features.push_back(features::kManagedDeviceUIRedesign);
  71. if (IsReleaseTrackUiEnabled())
  72. enabled_features.push_back(features::kReleaseTrackUi);
  73. else
  74. disabled_features.push_back(features::kReleaseTrackUi);
  75. scoped_feature_list_->InitWithFeatures(enabled_features, disabled_features);
  76. // Instantiate members.
  77. model_ = base::MakeRefCounted<UnifiedSystemTrayModel>(nullptr);
  78. controller_ = std::make_unique<UnifiedSystemTrayController>(model_.get());
  79. info_view_ = std::make_unique<UnifiedSystemInfoView>(controller_.get());
  80. }
  81. bool IsManagedDeviceUIRedesignEnabled() const {
  82. return (GetParam() & TestFlags::kManagedDeviceUi) != TestFlags::kNone;
  83. }
  84. bool IsReleaseTrackUiEnabled() const {
  85. return (GetParam() & TestFlags::kReleaseTrackUi) != TestFlags::kNone;
  86. }
  87. bool IsReleaseTrackNotStable() const {
  88. return (GetParam() & TestFlags::kReleaseTrackNotStable) != TestFlags::kNone;
  89. }
  90. void TearDown() override {
  91. info_view_.reset();
  92. controller_.reset();
  93. model_.reset();
  94. scoped_feature_list_.reset();
  95. AshTestBase::TearDown();
  96. }
  97. protected:
  98. UnifiedSystemInfoView* info_view() { return info_view_.get(); }
  99. EnterpriseDomainModel* enterprise_domain() {
  100. return Shell::Get()->system_tray_model()->enterprise_domain();
  101. }
  102. private:
  103. scoped_refptr<UnifiedSystemTrayModel> model_;
  104. std::unique_ptr<UnifiedSystemTrayController> controller_;
  105. std::unique_ptr<UnifiedSystemInfoView> info_view_;
  106. std::unique_ptr<base::test::ScopedFeatureList> scoped_feature_list_;
  107. };
  108. // Execute each test case with every possible combination of `TestFlags`.
  109. INSTANTIATE_TEST_SUITE_P(
  110. All,
  111. UnifiedSystemInfoViewTest,
  112. testing::Values(TestFlags::kNone,
  113. TestFlags::kManagedDeviceUi,
  114. TestFlags::kReleaseTrackUi,
  115. TestFlags::kManagedDeviceUi | TestFlags::kReleaseTrackUi,
  116. TestFlags::kReleaseTrackNotStable,
  117. TestFlags::kManagedDeviceUi |
  118. TestFlags::kReleaseTrackNotStable,
  119. TestFlags::kReleaseTrackUi |
  120. TestFlags::kReleaseTrackNotStable,
  121. TestFlags::kManagedDeviceUi | TestFlags::kReleaseTrackUi |
  122. TestFlags::kReleaseTrackNotStable));
  123. TEST_P(UnifiedSystemInfoViewTest, EnterpriseManagedVisible) {
  124. // By default, EnterpriseManagedView is not shown.
  125. EXPECT_FALSE(info_view()->IsEnterpriseManagedVisibleForTesting());
  126. // Simulate enterprise information becoming available.
  127. enterprise_domain()->SetDeviceEnterpriseInfo(
  128. DeviceEnterpriseInfo{"example.com", /*active_directory_managed=*/false,
  129. ManagementDeviceMode::kChromeEnterprise});
  130. // EnterpriseManagedView should be shown.
  131. EXPECT_TRUE(info_view()->IsEnterpriseManagedVisibleForTesting());
  132. // If the release track UI is enabled AND the release track is non-stable, the
  133. // ChannelIndicatorQuickSettingsView is shown.
  134. EXPECT_EQ(IsReleaseTrackUiEnabled() && IsReleaseTrackNotStable(),
  135. info_view()->IsChannelIndicatorQuickSettingsVisibleForTesting());
  136. }
  137. TEST_P(UnifiedSystemInfoViewTest, EnterpriseManagedVisibleForActiveDirectory) {
  138. // Active directory information becoming available.
  139. const std::string empty_domain;
  140. enterprise_domain()->SetDeviceEnterpriseInfo(
  141. DeviceEnterpriseInfo{empty_domain, /*active_directory_managed=*/true,
  142. ManagementDeviceMode::kChromeEnterprise});
  143. // EnterpriseManagedView should be shown.
  144. EXPECT_TRUE(info_view()->IsEnterpriseManagedVisibleForTesting());
  145. // If the release track UI is enabled AND the release track is non-stable, the
  146. // ChannelIndicatorQuickSettingsView is shown.
  147. EXPECT_EQ(IsReleaseTrackUiEnabled() && IsReleaseTrackNotStable(),
  148. info_view()->IsChannelIndicatorQuickSettingsVisibleForTesting());
  149. }
  150. TEST_P(UnifiedSystemInfoViewTest, EnterpriseUserManagedVisible) {
  151. // By default, EnterpriseManagedView is not shown.
  152. EXPECT_FALSE(info_view()->IsEnterpriseManagedVisibleForTesting());
  153. // Simulate enterprise information becoming available.
  154. enterprise_domain()->SetEnterpriseAccountDomainInfo("example.com");
  155. // EnterpriseManagedView should be shown if the feature is enabled.
  156. EXPECT_EQ(IsManagedDeviceUIRedesignEnabled(),
  157. info_view()->IsEnterpriseManagedVisibleForTesting());
  158. // If the release track UI is enabled AND the release track is non-stable, the
  159. // ChannelIndicatorQuickSettingsView is shown.
  160. EXPECT_EQ(IsReleaseTrackUiEnabled() && IsReleaseTrackNotStable(),
  161. info_view()->IsChannelIndicatorQuickSettingsVisibleForTesting());
  162. }
  163. using UnifiedSystemInfoViewNoSessionTest = NoSessionAshTestBase;
  164. TEST_F(UnifiedSystemInfoViewNoSessionTest, ChildVisible) {
  165. base::test::ScopedFeatureList scoped_feature_list;
  166. scoped_feature_list.InitAndDisableFeature(features::kManagedDeviceUIRedesign);
  167. auto model = base::MakeRefCounted<UnifiedSystemTrayModel>(nullptr);
  168. auto controller = std::make_unique<UnifiedSystemTrayController>(model.get());
  169. SessionControllerImpl* session = Shell::Get()->session_controller();
  170. ASSERT_FALSE(session->IsActiveUserSessionStarted());
  171. // Before login the supervised user view is invisible.
  172. {
  173. auto info_view = std::make_unique<UnifiedSystemInfoView>(controller.get());
  174. EXPECT_FALSE(info_view->IsSupervisedVisibleForTesting());
  175. }
  176. // Simulate a supervised user logging in.
  177. TestSessionControllerClient* client = GetSessionControllerClient();
  178. client->Reset();
  179. client->AddUserSession("child@test.com", user_manager::USER_TYPE_CHILD);
  180. client->SetSessionState(session_manager::SessionState::ACTIVE);
  181. UserSession user_session = *session->GetUserSession(0);
  182. user_session.custodian_email = "parent@test.com";
  183. session->UpdateUserSession(std::move(user_session));
  184. // Now the supervised user view is visible.
  185. {
  186. auto info_view = std::make_unique<UnifiedSystemInfoView>(controller.get());
  187. EXPECT_TRUE(info_view->IsSupervisedVisibleForTesting());
  188. }
  189. }
  190. } // namespace ash