summaryrefslogtreecommitdiffstats
path: root/webkit/appcache/appcache_service_unittest.cc
blob: 2d0723b07f1f4a866445d200cf6442e79a0324d6 (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
// 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_service.h"

#include "base/message_loop.h"
#include "net/base/completion_callback.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/appcache/mock_appcache_storage.h"

namespace appcache {

class AppCacheServiceTest : public testing::Test {
 public:
  AppCacheServiceTest()
      : kOrigin("http://hello/"),
        service_(new AppCacheService(NULL)),
        delete_result_(net::OK), delete_completion_count_(0),
        ALLOW_THIS_IN_INITIALIZER_LIST(deletion_callback_(
            this, &AppCacheServiceTest::OnDeleteAppCachesComplete)) {
    // Setup to use mock storage.
    service_->storage_.reset(new MockAppCacheStorage(service_.get()));
  }

  void OnDeleteAppCachesComplete(int result) {
    delete_result_ = result;
    ++delete_completion_count_;
  }

  MockAppCacheStorage* mock_storage() {
    return static_cast<MockAppCacheStorage*>(service_->storage());
  }

  const GURL kOrigin;

  scoped_ptr<AppCacheService> service_;
  int delete_result_;
  int delete_completion_count_;
  net::CompletionCallbackImpl<AppCacheServiceTest> deletion_callback_;
};

TEST_F(AppCacheServiceTest, DeleteAppCachesForOrigin) {
  // Without giving mock storage simiulated info, should fail.
  service_->DeleteAppCachesForOrigin(kOrigin, &deletion_callback_);
  EXPECT_EQ(0, delete_completion_count_);
  MessageLoop::current()->RunAllPending();
  EXPECT_EQ(1, delete_completion_count_);
  EXPECT_EQ(net::ERR_FAILED, delete_result_);
  delete_completion_count_ = 0;

  // Should succeed given an empty info collection.
  mock_storage()->SimulateGetAllInfo(new AppCacheInfoCollection);
  service_->DeleteAppCachesForOrigin(kOrigin, &deletion_callback_);
  EXPECT_EQ(0, delete_completion_count_);
  MessageLoop::current()->RunAllPending();
  EXPECT_EQ(1, delete_completion_count_);
  EXPECT_EQ(net::OK, delete_result_);
  delete_completion_count_ = 0;

  scoped_refptr<AppCacheInfoCollection> info(new AppCacheInfoCollection);

  // Should succeed given a non-empty info collection.
  AppCacheInfo mock_manifest_1;
  AppCacheInfo mock_manifest_2;
  AppCacheInfo mock_manifest_3;
  mock_manifest_1.manifest_url = kOrigin.Resolve("manifest1");
  mock_manifest_2.manifest_url = kOrigin.Resolve("manifest2");
  mock_manifest_3.manifest_url = kOrigin.Resolve("manifest3");
  AppCacheInfoVector info_vector;
  info_vector.push_back(mock_manifest_1);
  info_vector.push_back(mock_manifest_2);
  info_vector.push_back(mock_manifest_3);
  info->infos_by_origin[kOrigin] = info_vector;
  mock_storage()->SimulateGetAllInfo(info);
  service_->DeleteAppCachesForOrigin(kOrigin, &deletion_callback_);
  EXPECT_EQ(0, delete_completion_count_);
  MessageLoop::current()->RunAllPending();
  EXPECT_EQ(1, delete_completion_count_);
  EXPECT_EQ(net::OK, delete_result_);
  delete_completion_count_ = 0;

  // Should fail if storage fails to delete.
  info->infos_by_origin[kOrigin] = info_vector;
  mock_storage()->SimulateGetAllInfo(info);
  mock_storage()->SimulateMakeGroupObsoleteFailure();
  service_->DeleteAppCachesForOrigin(kOrigin, &deletion_callback_);
  EXPECT_EQ(0, delete_completion_count_);
  MessageLoop::current()->RunAllPending();
  EXPECT_EQ(1, delete_completion_count_);
  EXPECT_EQ(net::ERR_FAILED, delete_result_);
  delete_completion_count_ = 0;

  // Should complete with abort error if the service is deleted
  // prior to a delete completion.
  service_->DeleteAppCachesForOrigin(kOrigin, &deletion_callback_);
  EXPECT_EQ(0, delete_completion_count_);
  service_.reset();  // kill it
  EXPECT_EQ(1, delete_completion_count_);
  EXPECT_EQ(net::ERR_ABORTED, delete_result_);
  delete_completion_count_ = 0;

  // Let any tasks lingering from the sudden deletion run and verify
  // no other completion calls occur.
  MessageLoop::current()->RunAllPending();
  EXPECT_EQ(0, delete_completion_count_);
}

}  // namespace appcache