- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / extensions / default_app_order_unittest.cc
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_util.h"
11 #include "base/files/file_path.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/path_service.h"
15 #include "base/test/scoped_path_override.h"
16 #include "chromeos/chromeos_paths.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace chromeos {
20
21 namespace {
22
23 const base::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 base::FilePath& path) {
52     path_override_.reset(new base::ScopedPathOverride(
53         chromeos::FILE_DEFAULT_APP_ORDER, path));
54   }
55
56   void CreateExternalOrderFile(const std::string& content) {
57     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
58     base::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   base::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   base::ScopedTempDir scoped_tmp_dir;
99   ASSERT_TRUE(scoped_tmp_dir.CreateUniqueTempDir());
100
101   base::FilePath none_existent_file =
102       scoped_tmp_dir.path().AppendASCII("none_existent_file");
103   ASSERT_FALSE(base::PathExists(none_existent_file));
104   SetExternalFile(none_existent_file);
105
106   scoped_ptr<default_app_order::ExternalLoader> loader(
107       new default_app_order::ExternalLoader(false));
108
109   std::vector<std::string> apps;
110   default_app_order::Get(&apps);
111   EXPECT_TRUE(IsBuiltInDefault(apps));
112 }
113
114 // Tests bad json file gives built-in default.
115 TEST_F(DefaultAppOrderTest, BadExternalFile) {
116   const char kExternalOrder[] = "This is not a valid json.";
117   CreateExternalOrderFile(std::string(kExternalOrder));
118
119   scoped_ptr<default_app_order::ExternalLoader> loader(
120       new default_app_order::ExternalLoader(false));
121
122   std::vector<std::string> apps;
123   default_app_order::Get(&apps);
124   EXPECT_TRUE(IsBuiltInDefault(apps));
125 }
126
127 }  // namespace chromeos