Home | History | Annotate | Download | only in extensions
      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     base::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       "{ \"oem_apps_folder\": true,\"localized_content\": {"
     84       "    \"default\": {\"name\": \"OEM name\"}}}]";
     85   CreateExternalOrderFile(std::string(kExternalOrder));
     86 
     87   scoped_ptr<default_app_order::ExternalLoader> loader(
     88       new default_app_order::ExternalLoader(false));
     89 
     90   std::vector<std::string> apps;
     91   default_app_order::Get(&apps);
     92   EXPECT_EQ(3u, apps.size());
     93   EXPECT_EQ(std::string("app1"), apps[0]);
     94   EXPECT_EQ(std::string("app2"), apps[1]);
     95   EXPECT_EQ(std::string("app3"), apps[2]);
     96   EXPECT_EQ(std::string("OEM name"), default_app_order::GetOemAppsFolderName());
     97 }
     98 
     99 // Tests none-existent order file gives built-in default.
    100 TEST_F(DefaultAppOrderTest, NoExternalFile) {
    101   base::ScopedTempDir scoped_tmp_dir;
    102   ASSERT_TRUE(scoped_tmp_dir.CreateUniqueTempDir());
    103 
    104   base::FilePath none_existent_file =
    105       scoped_tmp_dir.path().AppendASCII("none_existent_file");
    106   ASSERT_FALSE(base::PathExists(none_existent_file));
    107   SetExternalFile(none_existent_file);
    108 
    109   scoped_ptr<default_app_order::ExternalLoader> loader(
    110       new default_app_order::ExternalLoader(false));
    111 
    112   std::vector<std::string> apps;
    113   default_app_order::Get(&apps);
    114   EXPECT_TRUE(IsBuiltInDefault(apps));
    115 }
    116 
    117 // Tests bad json file gives built-in default.
    118 TEST_F(DefaultAppOrderTest, BadExternalFile) {
    119   const char kExternalOrder[] = "This is not a valid json.";
    120   CreateExternalOrderFile(std::string(kExternalOrder));
    121 
    122   scoped_ptr<default_app_order::ExternalLoader> loader(
    123       new default_app_order::ExternalLoader(false));
    124 
    125   std::vector<std::string> apps;
    126   default_app_order::Get(&apps);
    127   EXPECT_TRUE(IsBuiltInDefault(apps));
    128 }
    129 
    130 }  // namespace chromeos
    131