summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/extensions/default_app_order_unittest.cc
blob: 7f92499451f901acd0e57bd1a0e40238d87dce0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/extensions/default_app_order.h"

#include <string>
#include <vector>

#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/test/scoped_path_override.h"
#include "chromeos/chromeos_paths.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chromeos {

namespace {

const base::FilePath::CharType kTestFile[] =
    FILE_PATH_LITERAL("test_default_app_order.json");
}

class DefaultAppOrderTest : public testing::Test {
 public:
  DefaultAppOrderTest() {}
  virtual ~DefaultAppOrderTest() {}

  // testing::Test overrides:
  virtual void SetUp() OVERRIDE {
    default_app_order::Get(&built_in_default_);
  }
  virtual void TearDown() OVERRIDE {
  }

  bool IsBuiltInDefault(const std::vector<std::string>& apps) {
    if (apps.size() != built_in_default_.size())
      return false;

    for (size_t i = 0; i < built_in_default_.size(); ++i) {
      if (built_in_default_[i] != apps[i])
        return false;
    }

    return true;
  }

  void SetExternalFile(const base::FilePath& path) {
    path_override_.reset(new base::ScopedPathOverride(
        chromeos::FILE_DEFAULT_APP_ORDER, path));
  }

  void CreateExternalOrderFile(const std::string& content) {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    base::FilePath external_file = temp_dir_.path().Append(kTestFile);
    file_util::WriteFile(external_file, content.c_str(), content.size());
    SetExternalFile(external_file);
  }

 private:
  std::vector<std::string> built_in_default_;

  base::ScopedTempDir temp_dir_;
  scoped_ptr<base::ScopedPathOverride> path_override_;

  DISALLOW_COPY_AND_ASSIGN(DefaultAppOrderTest);
};

// Tests that the built-in default order is returned when ExternalLoader is not
// created.
TEST_F(DefaultAppOrderTest, BuiltInDefault) {
  std::vector<std::string> apps;
  default_app_order::Get(&apps);
  EXPECT_TRUE(IsBuiltInDefault(apps));
}

// Tests external order file overrides built-in default.
TEST_F(DefaultAppOrderTest, ExternalOrder) {
  const char kExternalOrder[] = "[\"app1\",\"app2\",\"app3\"]";
  CreateExternalOrderFile(std::string(kExternalOrder));

  scoped_ptr<default_app_order::ExternalLoader> loader(
      new default_app_order::ExternalLoader(false));

  std::vector<std::string> apps;
  default_app_order::Get(&apps);
  EXPECT_EQ(3u, apps.size());
  EXPECT_EQ(std::string("app1"), apps[0]);
  EXPECT_EQ(std::string("app2"), apps[1]);
  EXPECT_EQ(std::string("app3"), apps[2]);
}

// Tests none-existent order file gives built-in default.
TEST_F(DefaultAppOrderTest, NoExternalFile) {
  base::ScopedTempDir scoped_tmp_dir;
  ASSERT_TRUE(scoped_tmp_dir.CreateUniqueTempDir());

  base::FilePath none_existent_file =
      scoped_tmp_dir.path().AppendASCII("none_existent_file");
  ASSERT_FALSE(file_util::PathExists(none_existent_file));
  SetExternalFile(none_existent_file);

  scoped_ptr<default_app_order::ExternalLoader> loader(
      new default_app_order::ExternalLoader(false));

  std::vector<std::string> apps;
  default_app_order::Get(&apps);
  EXPECT_TRUE(IsBuiltInDefault(apps));
}

// Tests bad json file gives built-in default.
TEST_F(DefaultAppOrderTest, BadExternalFile) {
  const char kExternalOrder[] = "This is not a valid json.";
  CreateExternalOrderFile(std::string(kExternalOrder));

  scoped_ptr<default_app_order::ExternalLoader> loader(
      new default_app_order::ExternalLoader(false));

  std::vector<std::string> apps;
  default_app_order::Get(&apps);
  EXPECT_TRUE(IsBuiltInDefault(apps));
}

}  // namespace chromeos