summaryrefslogtreecommitdiffstats
path: root/android_webview/native/aw_media_url_interceptor_unittest.cc
blob: 0f0ee0dd4bffb0e55b0c5318e9c5e6db580a43ac (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
// Copyright 2014 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 <string>

#include "android_webview/native/aw_media_url_interceptor.h"
#include "base/memory/scoped_ptr.h"

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

using testing::Test;

namespace android_webview {

namespace {

// Sentinel value to check whether the fields have been set.
const int UNSET_VALUE = -1;

class AwMediaUrlInterceptorTest : public Test {
 public:
   AwMediaUrlInterceptorTest()
       : fd_(UNSET_VALUE), offset_(UNSET_VALUE), size_(UNSET_VALUE),
         url_interceptor_(new AwMediaUrlInterceptor()){
   }
 protected:
   int fd_;
   int64_t offset_;
   int64_t size_;
   scoped_ptr<AwMediaUrlInterceptor> url_interceptor_;
};

}  // namespace

TEST_F(AwMediaUrlInterceptorTest, TestInterceptValidAssetUrl) {
  // This asset file exists in the android_webview_unittests-debug.apk.
  // See gyp rule android_webview_unittests_apk.
  const std::string valid_asset_url(
      "file:///android_asset/asset_file.ogg");

  ASSERT_TRUE(url_interceptor_->Intercept(
      valid_asset_url, &fd_, &offset_, &size_));
  EXPECT_NE(UNSET_VALUE, fd_);
  EXPECT_NE(UNSET_VALUE, offset_);
  EXPECT_NE(UNSET_VALUE, size_);
}

TEST_F(AwMediaUrlInterceptorTest, TestInterceptInvalidAssetUrl) {
  // This asset file does not exist in the android_webview_unittests-debug.apk.
  // See gyp rule android_webview_unittests_apk.
  const std::string invalid_asset_url(
      "file:///android_asset/file_does_not_exist.ogg");

  ASSERT_FALSE(url_interceptor_->Intercept(
      invalid_asset_url, &fd_, &offset_, &size_));
  EXPECT_EQ(UNSET_VALUE, fd_);
  EXPECT_EQ(UNSET_VALUE, offset_);
  EXPECT_EQ(UNSET_VALUE, size_);
}

TEST_F(AwMediaUrlInterceptorTest, TestInterceptNonAssetUrl) {
  // This url does not refer to an asset in the apk.
  const std::string non_asset_url("file:///sdcard/file.txt");

  ASSERT_FALSE(url_interceptor_->Intercept(
      non_asset_url, &fd_, &offset_, &size_));
  EXPECT_EQ(UNSET_VALUE, fd_);
  EXPECT_EQ(UNSET_VALUE, offset_);
  EXPECT_EQ(UNSET_VALUE, size_);
}

}  // namespace android_webview