summaryrefslogtreecommitdiffstats
path: root/webkit/appcache/appcache_test_helper.cc
blob: c23c2a6220470e31a2d9cfbe80b63c6e29270135 (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
// Copyright (c) 2011 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 "webkit/appcache/appcache_test_helper.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/message_loop.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/appcache/appcache.h"
#include "webkit/appcache/appcache_entry.h"
#include "webkit/appcache/appcache_group.h"
#include "webkit/appcache/appcache_service.h"

namespace appcache {

AppCacheTestHelper::AppCacheTestHelper()
    : group_id_(0),
      appcache_id_(0),
      response_id_(0),
      origins_(NULL) {}

AppCacheTestHelper::~AppCacheTestHelper() {}

void AppCacheTestHelper::OnGroupAndNewestCacheStored(
    AppCacheGroup* /*group*/,
    AppCache* /*newest_cache*/,
    bool success,
    bool /*would_exceed_quota*/) {
  ASSERT_TRUE(success);
  MessageLoop::current()->Quit();
}

void AppCacheTestHelper::AddGroupAndCache(AppCacheService* appcache_service,
                                          const GURL& manifest_url) {
  AppCacheGroup* appcache_group =
      new AppCacheGroup(appcache_service->storage(),
                        manifest_url,
                        ++group_id_);
  AppCache* appcache = new AppCache(appcache_service->storage(),
                                    ++appcache_id_);
  AppCacheEntry entry(AppCacheEntry::MANIFEST,
                      ++response_id_);
  appcache->AddEntry(manifest_url, entry);
  appcache->set_complete(true);
  appcache_group->AddCache(appcache);
  appcache_service->storage()->StoreGroupAndNewestCache(appcache_group,
                                                        appcache,
                                                        this);
  // OnGroupAndNewestCacheStored will quit the message loop.
  MessageLoop::current()->Run();
}

void AppCacheTestHelper::GetOriginsWithCaches(AppCacheService* appcache_service,
                                              std::set<GURL>* origins) {
  appcache_info_ = new AppCacheInfoCollection;
  origins_ = origins;
  appcache_service->GetAllAppCacheInfo(
      appcache_info_, base::Bind(&AppCacheTestHelper::OnGotAppCacheInfo,
                                 base::Unretained(this)));

  // OnGotAppCacheInfo will quit the message loop.
  MessageLoop::current()->Run();
}

void AppCacheTestHelper::OnGotAppCacheInfo(int rv) {
  typedef std::map<GURL, AppCacheInfoVector> InfoByOrigin;

  origins_->clear();
  for (InfoByOrigin::const_iterator origin =
           appcache_info_->infos_by_origin.begin();
       origin != appcache_info_->infos_by_origin.end(); ++origin) {
    origins_->insert(origin->first);
  }
  MessageLoop::current()->Quit();
}

}  // namespace appcache