| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "ui/base/material_design/material_design_controller.h" | 10 #include "ui/base/material_design/material_design_controller.h" |
| 11 #include "ui/base/test/material_design_controller_test_api.h" | 11 #include "ui/base/test/material_design_controller_test_api.h" |
| 12 #include "ui/base/ui_base_switches.h" | 12 #include "ui/base/ui_base_switches.h" |
| 13 | 13 |
| 14 namespace ui { | 14 namespace ui { |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Test fixture for the MaterialDesignController class. | 17 // Test fixture for the MaterialDesignController class. |
| 18 class MaterialDesignControllerTest : public testing::Test { | 18 class MaterialDesignControllerTest : public testing::Test { |
| 19 public: | 19 public: |
| 20 MaterialDesignControllerTest(); | 20 MaterialDesignControllerTest(); |
| 21 ~MaterialDesignControllerTest() override; | 21 ~MaterialDesignControllerTest() override; |
| 22 | 22 |
| 23 protected: |
| 23 // testing::Test: | 24 // testing::Test: |
| 24 void SetUp() override; | 25 void SetUp() override; |
| 26 void TearDown() override; |
| 27 void SetCommandLineSwitch(const std::string& value_string); |
| 25 | 28 |
| 26 private: | 29 private: |
| 27 DISALLOW_COPY_AND_ASSIGN(MaterialDesignControllerTest); | 30 DISALLOW_COPY_AND_ASSIGN(MaterialDesignControllerTest); |
| 28 }; | 31 }; |
| 29 | 32 |
| 30 MaterialDesignControllerTest::MaterialDesignControllerTest() { | 33 MaterialDesignControllerTest::MaterialDesignControllerTest() { |
| 31 } | 34 } |
| 32 | 35 |
| 33 MaterialDesignControllerTest::~MaterialDesignControllerTest() { | 36 MaterialDesignControllerTest::~MaterialDesignControllerTest() { |
| 34 } | 37 } |
| 35 | 38 |
| 36 void MaterialDesignControllerTest::SetUp() { | 39 void MaterialDesignControllerTest::SetUp() { |
| 37 testing::Test::SetUp(); | 40 testing::Test::SetUp(); |
| 41 MaterialDesignController::Initialize(); |
| 42 } |
| 38 | 43 |
| 39 // Ensure other tests aren't polluted by a Mode set in these tests. The Mode | 44 void MaterialDesignControllerTest::TearDown() { |
| 40 // has to be cleared in SetUp and not in TearDown because when the test suite | |
| 41 // starts up it triggers a call to ResourceBundle::LoadCommonResources() | |
| 42 // which calls MaterialDesignController::IsModeMaterial(), thereby | |
| 43 // initializing the mode and potentially causing the first test to fail. | |
| 44 test::MaterialDesignControllerTestAPI::UninitializeMode(); | 45 test::MaterialDesignControllerTestAPI::UninitializeMode(); |
| 46 testing::Test::TearDown(); |
| 45 } | 47 } |
| 46 | 48 |
| 49 void MaterialDesignControllerTest::SetCommandLineSwitch( |
| 50 const std::string& value_string) { |
| 51 #if defined(ENABLE_TOPCHROME_MD) |
| 52 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 53 switches::kTopChromeMD, value_string); |
| 54 #endif // defined(ENABLE_TOPCHROME_MD) |
| 55 } |
| 56 |
| 57 class MaterialDesignControllerTestMaterial : |
| 58 public MaterialDesignControllerTest { |
| 59 public: |
| 60 MaterialDesignControllerTestMaterial() { |
| 61 SetCommandLineSwitch("material"); |
| 62 } |
| 63 |
| 64 private: |
| 65 DISALLOW_COPY_AND_ASSIGN(MaterialDesignControllerTestMaterial); |
| 66 }; |
| 67 |
| 68 class MaterialDesignControllerTestHybrid : public MaterialDesignControllerTest { |
| 69 public: |
| 70 MaterialDesignControllerTestHybrid() { |
| 71 SetCommandLineSwitch("material-hybrid"); |
| 72 } |
| 73 |
| 74 private: |
| 75 DISALLOW_COPY_AND_ASSIGN(MaterialDesignControllerTestHybrid); |
| 76 }; |
| 77 |
| 78 class MaterialDesignControllerTestDefault : |
| 79 public MaterialDesignControllerTest { |
| 80 public: |
| 81 MaterialDesignControllerTestDefault() { |
| 82 SetCommandLineSwitch(std::string()); |
| 83 } |
| 84 |
| 85 private: |
| 86 DISALLOW_COPY_AND_ASSIGN(MaterialDesignControllerTestDefault); |
| 87 }; |
| 88 |
| 89 class MaterialDesignControllerTestInvalid : |
| 90 public MaterialDesignControllerTest { |
| 91 public: |
| 92 MaterialDesignControllerTestInvalid() { |
| 93 const std::string kInvalidValue = "1nvalid-valu3"; |
| 94 SetCommandLineSwitch(kInvalidValue); |
| 95 } |
| 96 |
| 97 private: |
| 98 DISALLOW_COPY_AND_ASSIGN(MaterialDesignControllerTestInvalid); |
| 99 }; |
| 100 |
| 47 #if !defined(ENABLE_TOPCHROME_MD) | 101 #if !defined(ENABLE_TOPCHROME_MD) |
| 48 | 102 |
| 49 // Verify the Mode maps to Mode::NON_MATERIAL when the compile time flag is not | 103 // Verify the Mode maps to Mode::NON_MATERIAL when the compile time flag is not |
| 50 // defined. | 104 // defined. |
| 51 TEST_F(MaterialDesignControllerTest, | 105 TEST_F(MaterialDesignControllerTest, |
| 52 NonMaterialModeWhenCompileTimeFlagDisabled) { | 106 NonMaterialModeWhenCompileTimeFlagDisabled) { |
| 53 EXPECT_EQ(MaterialDesignController::Mode::NON_MATERIAL, | 107 EXPECT_EQ(MaterialDesignController::Mode::NON_MATERIAL, |
| 54 MaterialDesignController::GetMode()); | 108 MaterialDesignController::GetMode()); |
| 55 } | 109 } |
| 56 | 110 |
| 57 #else | 111 #else |
| 58 | 112 |
| 59 // Verify command line value "material" maps to Mode::MATERIAL when the compile | 113 // Verify command line value "material" maps to Mode::MATERIAL when the compile |
| 60 // time flag is defined. | 114 // time flag is defined. |
| 61 TEST_F(MaterialDesignControllerTest, | 115 TEST_F(MaterialDesignControllerTestMaterial, |
| 62 EnabledCommandLineValueMapsToMaterialModeWhenCompileTimeFlagEnabled) { | 116 EnabledCommandLineValueMapsToMaterialModeWhenCompileTimeFlagEnabled) { |
| 63 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 64 switches::kTopChromeMD, "material"); | |
| 65 EXPECT_EQ(MaterialDesignController::Mode::MATERIAL_NORMAL, | 117 EXPECT_EQ(MaterialDesignController::Mode::MATERIAL_NORMAL, |
| 66 MaterialDesignController::GetMode()); | 118 MaterialDesignController::GetMode()); |
| 67 } | 119 } |
| 68 | 120 |
| 69 // Verify command line value "material-hybrid" maps to Mode::MATERIAL_HYBRID | 121 // Verify command line value "material-hybrid" maps to Mode::MATERIAL_HYBRID |
| 70 // when the compile time flag is defined. | 122 // when the compile time flag is defined. |
| 71 TEST_F( | 123 TEST_F( |
| 72 MaterialDesignControllerTest, | 124 MaterialDesignControllerTestHybrid, |
| 73 EnabledHybridCommandLineValueMapsToMaterialHybridModeWhenCompileTimeFlagEnab
led) { | 125 EnabledHybridCommandLineValueMapsToMaterialHybridModeWhenCompileTimeFlagEnab
led) { |
| 74 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 75 switches::kTopChromeMD, "material-hybrid"); | |
| 76 EXPECT_EQ(MaterialDesignController::Mode::MATERIAL_HYBRID, | 126 EXPECT_EQ(MaterialDesignController::Mode::MATERIAL_HYBRID, |
| 77 MaterialDesignController::GetMode()); | 127 MaterialDesignController::GetMode()); |
| 78 } | 128 } |
| 79 | 129 |
| 80 // Verify command line value "" maps to the default mode when the compile time | 130 // Verify command line value "" maps to the default mode when the compile time |
| 81 // flag is defined. | 131 // flag is defined. |
| 82 TEST_F( | 132 TEST_F( |
| 83 MaterialDesignControllerTest, | 133 MaterialDesignControllerTestDefault, |
| 84 DisabledCommandLineValueMapsToNonMaterialModeWhenCompileTimeFlagEnabled) { | 134 DisabledCommandLineValueMapsToNonMaterialModeWhenCompileTimeFlagEnabled) { |
| 85 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 86 switches::kTopChromeMD, ""); | |
| 87 EXPECT_EQ(MaterialDesignController::DefaultMode(), | 135 EXPECT_EQ(MaterialDesignController::DefaultMode(), |
| 88 MaterialDesignController::GetMode()); | 136 MaterialDesignController::GetMode()); |
| 89 } | 137 } |
| 90 | 138 |
| 91 // Verify the current mode is reported as the default mode when no command line | 139 // Verify the current mode is reported as the default mode when no command line |
| 92 // flag is defined. | 140 // flag is defined. |
| 93 TEST_F(MaterialDesignControllerTest, | 141 TEST_F(MaterialDesignControllerTest, |
| 94 NoCommandLineValueMapsToNonMaterialModeWhenCompileTimeFlagEnabled) { | 142 NoCommandLineValueMapsToNonMaterialModeWhenCompileTimeFlagEnabled) { |
| 95 ASSERT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch( | 143 ASSERT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 96 switches::kTopChromeMD)); | 144 switches::kTopChromeMD)); |
| 97 EXPECT_EQ(MaterialDesignController::DefaultMode(), | 145 EXPECT_EQ(MaterialDesignController::DefaultMode(), |
| 98 MaterialDesignController::GetMode()); | 146 MaterialDesignController::GetMode()); |
| 99 } | 147 } |
| 100 | 148 |
| 101 // Verify an invalid command line value uses the default mode. | 149 // Verify an invalid command line value uses the default mode. |
| 102 TEST_F(MaterialDesignControllerTest, InvalidCommandLineValue) { | 150 TEST_F(MaterialDesignControllerTestInvalid, InvalidCommandLineValue) { |
| 103 const std::string kInvalidValue = "1nvalid-valu3"; | |
| 104 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 105 switches::kTopChromeMD, kInvalidValue); | |
| 106 EXPECT_EQ(MaterialDesignController::DefaultMode(), | 151 EXPECT_EQ(MaterialDesignController::DefaultMode(), |
| 107 MaterialDesignController::GetMode()); | 152 MaterialDesignController::GetMode()); |
| 108 } | 153 } |
| 109 | 154 |
| 110 // Verify that MaterialDesignController::IsModeMaterial() will initialize the | 155 // Verify that MaterialDesignController::IsModeMaterial() will be true when |
| 111 // mode if it hasn't been initialized yet. | 156 // initialized with command line flag "material". |
| 112 TEST_F(MaterialDesignControllerTest, IsModeMaterialInitializesMode) { | 157 TEST_F(MaterialDesignControllerTestMaterial, IsModeMaterialInitializesMode) { |
| 113 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 114 switches::kTopChromeMD, "material"); | |
| 115 EXPECT_TRUE(MaterialDesignController::IsModeMaterial()); | 158 EXPECT_TRUE(MaterialDesignController::IsModeMaterial()); |
| 116 } | 159 } |
| 117 | 160 |
| 118 #endif // !defined(ENABLE_TOPCHROME_MD) | 161 #endif // !defined(ENABLE_TOPCHROME_MD) |
| 119 | 162 |
| 120 } // namespace | 163 } // namespace |
| 121 } // namespace ui | 164 } // namespace ui |
| OLD | NEW |