Welcome to mirror list, hosted at ThFree Co, Russian Federation.

storage_http_tests.cpp « storage_integration_tests « storage - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5b780d25f6a5f1deb8267fbcdc8be27cc0dd18bf (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "testing/testing.hpp"

#include "storage/storage_integration_tests/test_defines.hpp"

#include "storage/storage.hpp"

#include "platform/local_country_file_utils.hpp"
#include "platform/mwm_version.hpp"
#include "platform/platform.hpp"
#include "platform/platform_tests_support/scoped_dir.hpp"
#include "platform/platform_tests_support/writable_dir_changer.hpp"

#include "base/file_name_utils.hpp"
#include "base/scope_guard.hpp"
#include "base/string_utils.hpp"
#include "base/thread.hpp"

#include <string>

using namespace platform;
using namespace std;
using namespace storage;

namespace
{
string const kCountryId = "Angola";
string const kDisputedCountryId1 = "Jerusalem";
string const kDisputedCountryId2 = "Crimea";
string const kDisputedCountryId3 = "Campo de Hielo Sur";
string const kUndisputedCountryId = "Argentina_Buenos Aires_North";

void Update(CountryId const &, LocalFilePtr const localCountryFile)
{
  TEST_EQUAL(localCountryFile->GetCountryName(), kCountryId, ());
}

void UpdateWithoutChecks(CountryId const &, LocalFilePtr const /* localCountryFile */) {}

string const GetMwmFullPath(string const & countryId, string const & version)
{
  return base::JoinPath(GetPlatform().WritableDir(), version, countryId + DATA_FILE_EXTENSION);
}

string const GetDownloadingFullPath(string const & countryId, string const & version)
{
  return base::JoinPath(
      GetPlatform().WritableDir(), version,
      kCountryId + DATA_FILE_EXTENSION READY_FILE_EXTENSION DOWNLOADING_FILE_EXTENSION);
}

string const GetResumeFullPath(string const & countryId, string const & version)
{
  return base::JoinPath(
      GetPlatform().WritableDir(), version,
      kCountryId + DATA_FILE_EXTENSION READY_FILE_EXTENSION RESUME_FILE_EXTENSION);
}

void InitStorage(Storage & storage, Storage::UpdateCallback const & didDownload,
                 Storage::ProgressFunction const & progress)
{
  auto const changeCountryFunction = [&](CountryId const & /* countryId */) {
    if (!storage.IsDownloadInProgress())
    {
      // End wait for downloading complete.
      testing::StopEventLoop();
    }
  };

  storage.Init(didDownload, [](CountryId const &, LocalFilePtr const) { return false; });
  storage.RegisterAllLocalMaps(false /* enableDiffs */);
  storage.Subscribe(changeCountryFunction, progress);
  storage.SetDownloadingServersForTesting({kTestWebServer});
}

class StorageHttpTest
{
public:
  StorageHttpTest()
    : m_writableDirChanger(kMapTestDir),
      m_version(strings::to_string(m_storage.GetCurrentDataVersion())),
      m_cleanupVersionDir(m_version)
  {
  }

protected:
  WritableDirChanger const m_writableDirChanger;
  Storage m_storage;
  string const m_version;
  tests_support::ScopedDir const m_cleanupVersionDir;
};
}  // namespace

UNIT_CLASS_TEST(StorageHttpTest, StorageDownloadNodeAndDeleteNode)
{
  auto const progressFunction = [this](CountryId const & countryId,
                                       LocalAndRemoteSize const & mapSize) {
    NodeAttrs nodeAttrs;
    m_storage.GetNodeAttrs(countryId, nodeAttrs);

    TEST_EQUAL(static_cast<int64_t>(mapSize.first),
               nodeAttrs.m_downloadingProgress.first, (countryId));
    TEST_EQUAL(static_cast<int64_t>(mapSize.second),
               nodeAttrs.m_downloadingProgress.second, (countryId));
    TEST_EQUAL(countryId, kCountryId, (countryId));
  };

  InitStorage(m_storage, Update, progressFunction);

  string const mwmFullPath = GetMwmFullPath(kCountryId, m_version);
  string const downloadingFullPath = GetDownloadingFullPath(kCountryId, m_version);
  string const resumeFullPath = GetResumeFullPath(kCountryId, m_version);

  // Downloading to an empty directory.
  m_storage.DownloadNode(kCountryId);
  // Wait for downloading complete.
  testing::RunEventLoop();

  Platform & platform = GetPlatform();
  TEST(platform.IsFileExistsByFullPath(mwmFullPath), ());
  TEST(!platform.IsFileExistsByFullPath(downloadingFullPath), ());
  TEST(!platform.IsFileExistsByFullPath(resumeFullPath), ());

  // Downloading to directory with Angola.mwm.
  m_storage.DownloadNode(kCountryId);

  TEST(platform.IsFileExistsByFullPath(mwmFullPath), ());
  TEST(!platform.IsFileExistsByFullPath(downloadingFullPath), ());
  TEST(!platform.IsFileExistsByFullPath(resumeFullPath), ());

  m_storage.DeleteNode(kCountryId);
  TEST(!platform.IsFileExistsByFullPath(mwmFullPath), ());
}

UNIT_CLASS_TEST(StorageHttpTest, StorageDownloadAndDeleteDisputedNode)
{
  auto const progressFunction = [this](CountryId const & countryId,
                                       LocalAndRemoteSize const & mapSize) {
    NodeAttrs nodeAttrs;
    m_storage.GetNodeAttrs(countryId, nodeAttrs);

    TEST_EQUAL(static_cast<int64_t>(mapSize.first),
               nodeAttrs.m_downloadingProgress.first, (countryId));
    TEST_EQUAL(static_cast<int64_t>(mapSize.second),
               nodeAttrs.m_downloadingProgress.second, (countryId));
  };

  InitStorage(m_storage, UpdateWithoutChecks, progressFunction);

  string const mwmFullPath1 = GetMwmFullPath(kDisputedCountryId1, m_version);
  string const mwmFullPath2 = GetMwmFullPath(kDisputedCountryId2, m_version);
  string const mwmFullPath3 = GetMwmFullPath(kDisputedCountryId3, m_version);
  string const mwmFullPathUndisputed = GetMwmFullPath(kUndisputedCountryId, m_version);

  // Downloading to an empty directory.
  m_storage.DownloadNode(kDisputedCountryId1);
  m_storage.DownloadNode(kDisputedCountryId2);
  m_storage.DownloadNode(kDisputedCountryId3);
  m_storage.DownloadNode(kUndisputedCountryId);
  // Wait for downloading complete.
  testing::RunEventLoop();

  Platform & platform = GetPlatform();
  TEST(platform.IsFileExistsByFullPath(mwmFullPath1), ());
  TEST(platform.IsFileExistsByFullPath(mwmFullPath2), ());
  TEST(platform.IsFileExistsByFullPath(mwmFullPath3), ());
  TEST(platform.IsFileExistsByFullPath(mwmFullPathUndisputed), ());

  CountriesVec downloadedChildren;
  CountriesVec availChildren;
  m_storage.GetChildrenInGroups(m_storage.GetRootId(), downloadedChildren, availChildren);

  CountriesVec const expectedDownloadedChildren = {"Argentina", kDisputedCountryId2,
                                                   kDisputedCountryId1};
  TEST_EQUAL(downloadedChildren, expectedDownloadedChildren, ());
  TEST_EQUAL(availChildren.size(), 223, ());

  m_storage.DeleteNode(kDisputedCountryId1);
  m_storage.DeleteNode(kDisputedCountryId2);
  m_storage.DeleteNode(kDisputedCountryId3);
  m_storage.DeleteNode(kUndisputedCountryId);
  TEST(!platform.IsFileExistsByFullPath(mwmFullPath1), ());
  TEST(!platform.IsFileExistsByFullPath(mwmFullPath2), ());
  TEST(!platform.IsFileExistsByFullPath(mwmFullPath3), ());
  TEST(!platform.IsFileExistsByFullPath(mwmFullPathUndisputed), ());
}