summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/network_message_observer_unittest.cc
blob: a742696776deb7a1d236bd743f9231617600f9c0 (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
// Copyright (c) 2010 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/network_message_observer.h"

#include "base/time.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chromeos {

namespace {

CellularDataPlan CreateDataPlan(CellularDataPlanType type, int64 start_sec,
                                int64 end_sec, int64 bytes, int64 used) {
  CellularDataPlan plan;
  plan.plan_type = type;
  plan.plan_start_time = base::Time::FromDoubleT(start_sec);
  plan.plan_end_time = base::Time::FromDoubleT(end_sec);
  plan.plan_data_bytes = bytes;
  plan.data_bytes_used = used;
  return plan;
}

}  // namespace

// Test the code that checks if a data plan is an applicable backup plan.
TEST(NetworkMessageObserverTest, TestIsApplicableBackupPlan) {
  // IsApplicableBackupPlan returns true if:
  //   (unlimited OR used bytes < max bytes) AND
  //   ((start time - 1 sec) <= end time of currently active plan).

  // Current plan that ends at 100.
  CellularDataPlan plan =
      CreateDataPlan(CELLULAR_DATA_PLAN_UNLIMITED, 0, 100, 0, 0);

  // Test unlimited plans.
  CellularDataPlan time_50 =
      CreateDataPlan(CELLULAR_DATA_PLAN_UNLIMITED, 50, 500, 0, 0);
  CellularDataPlan time_100 =
      CreateDataPlan(CELLULAR_DATA_PLAN_UNLIMITED, 100, 500, 0, 0);
  CellularDataPlan time_101 =
      CreateDataPlan(CELLULAR_DATA_PLAN_UNLIMITED, 101, 500, 0, 0);
  CellularDataPlan time_102 =
      CreateDataPlan(CELLULAR_DATA_PLAN_UNLIMITED, 102, 500, 0, 0);
  EXPECT_TRUE(NetworkMessageObserver::IsApplicableBackupPlan(&plan, &time_50));
  EXPECT_TRUE(NetworkMessageObserver::IsApplicableBackupPlan(&plan, &time_100));
  EXPECT_TRUE(NetworkMessageObserver::IsApplicableBackupPlan(&plan, &time_101));
  EXPECT_FALSE(NetworkMessageObserver::IsApplicableBackupPlan(&plan,
                                                              &time_102));

  // Test data plans.
  CellularDataPlan data_0_0 =
      CreateDataPlan(CELLULAR_DATA_PLAN_METERED_PAID, 100, 500, 0, 0);
  CellularDataPlan data_100_0 =
      CreateDataPlan(CELLULAR_DATA_PLAN_METERED_PAID, 100, 500, 100, 0);
  CellularDataPlan data_100_50 =
      CreateDataPlan(CELLULAR_DATA_PLAN_METERED_PAID, 100, 500, 100, 50);
  CellularDataPlan data_100_100 =
      CreateDataPlan(CELLULAR_DATA_PLAN_METERED_PAID, 100, 500, 100, 100);
  CellularDataPlan data_100_200 =
      CreateDataPlan(CELLULAR_DATA_PLAN_METERED_PAID, 100, 500, 100, 200);
  CellularDataPlan data_100_0_gap =
      CreateDataPlan(CELLULAR_DATA_PLAN_METERED_PAID, 200, 500, 100, 0);
  EXPECT_FALSE(NetworkMessageObserver::IsApplicableBackupPlan(&plan,
                                                              &data_0_0));
  EXPECT_TRUE(NetworkMessageObserver::IsApplicableBackupPlan(&plan,
                                                             &data_100_0));
  EXPECT_TRUE(NetworkMessageObserver::IsApplicableBackupPlan(&plan,
                                                             &data_100_50));
  EXPECT_FALSE(NetworkMessageObserver::IsApplicableBackupPlan(&plan,
                                                              &data_100_100));
  EXPECT_FALSE(NetworkMessageObserver::IsApplicableBackupPlan(&plan,
                                                              &data_100_200));
  EXPECT_FALSE(NetworkMessageObserver::IsApplicableBackupPlan(&plan,
                                                              &data_100_0_gap));
}


}  // namespace chromeos