Home | History | Annotate | Download | only in chromeos
      1 // Copyright (c) 2010 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/network_message_observer.h"
      6 
      7 #include "base/time.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 
     10 namespace chromeos {
     11 
     12 namespace {
     13 
     14 CellularDataPlan CreateDataPlan(CellularDataPlanType type, int64 start_sec,
     15                                 int64 end_sec, int64 bytes, int64 used) {
     16   CellularDataPlan plan;
     17   plan.plan_type = type;
     18   plan.plan_start_time = base::Time::FromDoubleT(start_sec);
     19   plan.plan_end_time = base::Time::FromDoubleT(end_sec);
     20   plan.plan_data_bytes = bytes;
     21   plan.data_bytes_used = used;
     22   return plan;
     23 }
     24 
     25 }  // namespace
     26 
     27 // Test the code that checks if a data plan is an applicable backup plan.
     28 TEST(NetworkMessageObserverTest, TestIsApplicableBackupPlan) {
     29   // IsApplicableBackupPlan returns true if:
     30   //   (unlimited OR used bytes < max bytes) AND
     31   //   ((start time - 1 sec) <= end time of currently active plan).
     32 
     33   // Current plan that ends at 100.
     34   CellularDataPlan plan =
     35       CreateDataPlan(CELLULAR_DATA_PLAN_UNLIMITED, 0, 100, 0, 0);
     36 
     37   // Test unlimited plans.
     38   CellularDataPlan time_50 =
     39       CreateDataPlan(CELLULAR_DATA_PLAN_UNLIMITED, 50, 500, 0, 0);
     40   CellularDataPlan time_100 =
     41       CreateDataPlan(CELLULAR_DATA_PLAN_UNLIMITED, 100, 500, 0, 0);
     42   CellularDataPlan time_101 =
     43       CreateDataPlan(CELLULAR_DATA_PLAN_UNLIMITED, 101, 500, 0, 0);
     44   CellularDataPlan time_102 =
     45       CreateDataPlan(CELLULAR_DATA_PLAN_UNLIMITED, 102, 500, 0, 0);
     46   EXPECT_TRUE(NetworkMessageObserver::IsApplicableBackupPlan(&plan, &time_50));
     47   EXPECT_TRUE(NetworkMessageObserver::IsApplicableBackupPlan(&plan, &time_100));
     48   EXPECT_TRUE(NetworkMessageObserver::IsApplicableBackupPlan(&plan, &time_101));
     49   EXPECT_FALSE(NetworkMessageObserver::IsApplicableBackupPlan(&plan,
     50                                                               &time_102));
     51 
     52   // Test data plans.
     53   CellularDataPlan data_0_0 =
     54       CreateDataPlan(CELLULAR_DATA_PLAN_METERED_PAID, 100, 500, 0, 0);
     55   CellularDataPlan data_100_0 =
     56       CreateDataPlan(CELLULAR_DATA_PLAN_METERED_PAID, 100, 500, 100, 0);
     57   CellularDataPlan data_100_50 =
     58       CreateDataPlan(CELLULAR_DATA_PLAN_METERED_PAID, 100, 500, 100, 50);
     59   CellularDataPlan data_100_100 =
     60       CreateDataPlan(CELLULAR_DATA_PLAN_METERED_PAID, 100, 500, 100, 100);
     61   CellularDataPlan data_100_200 =
     62       CreateDataPlan(CELLULAR_DATA_PLAN_METERED_PAID, 100, 500, 100, 200);
     63   CellularDataPlan data_100_0_gap =
     64       CreateDataPlan(CELLULAR_DATA_PLAN_METERED_PAID, 200, 500, 100, 0);
     65   EXPECT_FALSE(NetworkMessageObserver::IsApplicableBackupPlan(&plan,
     66                                                               &data_0_0));
     67   EXPECT_TRUE(NetworkMessageObserver::IsApplicableBackupPlan(&plan,
     68                                                              &data_100_0));
     69   EXPECT_TRUE(NetworkMessageObserver::IsApplicableBackupPlan(&plan,
     70                                                              &data_100_50));
     71   EXPECT_FALSE(NetworkMessageObserver::IsApplicableBackupPlan(&plan,
     72                                                               &data_100_100));
     73   EXPECT_FALSE(NetworkMessageObserver::IsApplicableBackupPlan(&plan,
     74                                                               &data_100_200));
     75   EXPECT_FALSE(NetworkMessageObserver::IsApplicableBackupPlan(&plan,
     76                                                               &data_100_0_gap));
     77 }
     78 
     79 
     80 }  // namespace chromeos
     81