| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 |
| 5 #include "chrome/browser/chromeos/extensions/default_app_order.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/file_path.h" |
| 11 #include "base/file_util.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/path_service.h" |
| 14 #include "base/scoped_temp_dir.h" |
| 15 #include "base/test/scoped_path_override.h" |
| 16 #include "chrome/common/chrome_paths.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace chromeos { |
| 20 |
| 21 namespace { |
| 22 |
| 23 const FilePath::CharType kTestFile[] = |
| 24 FILE_PATH_LITERAL("test_default_app_order.json"); |
| 25 } |
| 26 |
| 27 class DefaultAppOrderTest : public testing::Test { |
| 28 public: |
| 29 DefaultAppOrderTest() {} |
| 30 virtual ~DefaultAppOrderTest() {} |
| 31 |
| 32 // testing::Test overrides: |
| 33 virtual void SetUp() OVERRIDE { |
| 34 default_app_order::Get(&built_in_default_); |
| 35 } |
| 36 virtual void TearDown() OVERRIDE { |
| 37 } |
| 38 |
| 39 bool IsBuiltInDefault(const std::vector<std::string>& apps) { |
| 40 if (apps.size() != built_in_default_.size()) |
| 41 return false; |
| 42 |
| 43 for (size_t i = 0; i < built_in_default_.size(); ++i) { |
| 44 if (built_in_default_[i] != apps[i]) |
| 45 return false; |
| 46 } |
| 47 |
| 48 return true; |
| 49 } |
| 50 |
| 51 void SetExternalFile(const FilePath& path) { |
| 52 path_override_.reset(new base::ScopedPathOverride( |
| 53 chrome::FILE_DEFAULT_APP_ORDER, path)); |
| 54 } |
| 55 |
| 56 void CreateExternalOrderFile(const std::string& content) { |
| 57 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 58 FilePath external_file = temp_dir_.path().Append(kTestFile); |
| 59 file_util::WriteFile(external_file, content.c_str(), content.size()); |
| 60 SetExternalFile(external_file); |
| 61 } |
| 62 |
| 63 private: |
| 64 std::vector<std::string> built_in_default_; |
| 65 |
| 66 ScopedTempDir temp_dir_; |
| 67 scoped_ptr<base::ScopedPathOverride> path_override_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(DefaultAppOrderTest); |
| 70 }; |
| 71 |
| 72 // Tests that the built-in default order is returned when ExternalLoader is not |
| 73 // created. |
| 74 TEST_F(DefaultAppOrderTest, BuiltInDefault) { |
| 75 std::vector<std::string> apps; |
| 76 default_app_order::Get(&apps); |
| 77 EXPECT_TRUE(IsBuiltInDefault(apps)); |
| 78 } |
| 79 |
| 80 // Tests external order file overrides built-in default. |
| 81 TEST_F(DefaultAppOrderTest, ExternalOrder) { |
| 82 const char kExternalOrder[] = "[\"app1\",\"app2\",\"app3\"]"; |
| 83 CreateExternalOrderFile(std::string(kExternalOrder)); |
| 84 |
| 85 scoped_ptr<default_app_order::ExternalLoader> loader( |
| 86 new default_app_order::ExternalLoader(false)); |
| 87 |
| 88 std::vector<std::string> apps; |
| 89 default_app_order::Get(&apps); |
| 90 EXPECT_EQ(3u, apps.size()); |
| 91 EXPECT_EQ(std::string("app1"), apps[0]); |
| 92 EXPECT_EQ(std::string("app2"), apps[1]); |
| 93 EXPECT_EQ(std::string("app3"), apps[2]); |
| 94 } |
| 95 |
| 96 // Tests none-existent order file gives built-in default. |
| 97 TEST_F(DefaultAppOrderTest, NoExternalFile) { |
| 98 SetExternalFile(FilePath(FILE_PATH_LITERAL("none_existent_file"))); |
| 99 |
| 100 scoped_ptr<default_app_order::ExternalLoader> loader( |
| 101 new default_app_order::ExternalLoader(false)); |
| 102 |
| 103 std::vector<std::string> apps; |
| 104 default_app_order::Get(&apps); |
| 105 EXPECT_TRUE(IsBuiltInDefault(apps)); |
| 106 } |
| 107 |
| 108 // Tests bad json file gives built-in default. |
| 109 TEST_F(DefaultAppOrderTest, BadExternalFile) { |
| 110 const char kExternalOrder[] = "This is not a valid json."; |
| 111 CreateExternalOrderFile(std::string(kExternalOrder)); |
| 112 |
| 113 scoped_ptr<default_app_order::ExternalLoader> loader( |
| 114 new default_app_order::ExternalLoader(false)); |
| 115 |
| 116 std::vector<std::string> apps; |
| 117 default_app_order::Get(&apps); |
| 118 EXPECT_TRUE(IsBuiltInDefault(apps)); |
| 119 } |
| 120 |
| 121 } // namespace chromeos |
| OLD | NEW |